Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. /*global Vue*/
  2.  
  3. Vue.component('v-weekpicker', {
  4. template: '<div id="weekpicker"><input :class="cssClasses" :name="name" v-model="displayValue"/></div>',
  5. props: ['more_classes', 'name', 'attributes', 'value'],
  6. computed: {
  7. cssClasses: function(){
  8. return "form-control " + this.more_classes;
  9. },
  10. displayValue: function(){
  11. if(this.value === undefined){
  12. return this.format(moment());
  13. }else{
  14. return this.format2(this.value);
  15. }
  16. },
  17. },
  18. methods: {
  19. format: function(date){
  20. var d = moment(date).add(1, 'd');
  21. var week_from = moment(date).add(1, 'd').startOf('week').add(-1, 'd');
  22. var week_til = moment(date).add(1, 'd').startOf('week').add(5, 'd');
  23. return d.format("YYYY-[W]W")+ " (" + week_from.format("D/M") + "-" + week_til.format("D/M") + ")";
  24. },
  25. format2: function(date){
  26. var year = Number(date.split("-W")[0]);
  27. var week = Number(date.split("-W")[1].split(" ")[0]);
  28. var d = moment().day("Monday").year(year).week(week);
  29.  
  30. return this.format(d);
  31. }
  32. },
  33. mounted: function(){
  34. if(this.attributes !== undefined){
  35. for (var i = this.attributes.length - 1; i >= 0; i--) {
  36. $(this.$el).find('input').attr(this.attributes[i].name, this.attributes[i].value);
  37. }
  38. }
  39.  
  40. $(this.$el).find("input").datepicker({
  41. format: {
  42. toDisplay: function(date) {
  43. var d = moment(date).add(1, 'd');
  44. var week_from = moment(date).add(1, 'd').startOf('week').add(-1, 'd');
  45. var week_til = moment(date).add(1, 'd').startOf('week').add(5, 'd');
  46. return d.format("YYYY-[W]W")+ " (" + week_from.format("D/M") + "-" + week_til.format("D/M") + ")";
  47. },
  48.  
  49. toValue: function(date) {
  50. var year = Number(date.split("-W")[0]);
  51. var week = Number(date.split("-W")[1].split(" ")[0]);
  52. var d = moment().day("Monday").year(year).week(week).startOf('day');
  53. return d.toDate();
  54. }
  55. },
  56. container: '#weekpicker',
  57. autoclose: true,
  58. weekStart: 6
  59. });
  60.  
  61. $(this.$el).find("input").on('show', function() {
  62. $('#weekpicker').find('.datepicker table tr').each(function() {
  63. if ($(this).find("td.day.active").length > 0) {
  64. $(this).addClass('active');
  65. } else {
  66. $(this).removeClass('active');
  67. }
  68. });
  69. });
  70. $(this.$el).find("input").on('keyup', function() {
  71. $('#weekpicker').find('.datepicker table tr').each(function() {
  72. if ($(this).find("td.day.active").length > 0) {
  73. $(this).addClass('active');
  74. } else {
  75. $(this).removeClass('active');
  76. }
  77. });
  78. });
  79. $(this.$el).find("input").on('change', function() {
  80. this.value = $(this.$el).find("input").val();
  81. this.$emit('input', this.value);
  82. }.bind(this));
  83. },
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement