Guest User

Untitled

a guest
Nov 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. <input type="image" src="images/calendar-icon.jpg" ng-model="selectedWeek" weekpicker format="yy-mm-dd" style="margin-left:10px;" />
  2.  
  3. myApp.directive('weekpicker', function () {
  4. function link(scope, element, attrs, ngModelCtrl, $rootScope) {
  5. var frmt = "mm/dd/yy";
  6. if (attrs.format != undefined) {
  7. frmt = attrs.format;
  8. } else if (attrs.placeholder != undefined) {
  9. frmt = attrs.placeholder;
  10. }
  11.  
  12. scope.getSunday = function(fromDate) {
  13. var curr = new Date(fromDate); // get current date
  14. var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
  15. var firstday = new Date(curr.setDate(first)).toUTCString();
  16. return firstday;
  17. };
  18. scope.getWeekDaysRange = function (inputDateString) {
  19. return scope.getDates(new Date(inputDateString), (new Date(inputDateString)).addDays(6));
  20. };
  21. scope.getDates = function(startDate, stopDate) {
  22. var dateArray = new Array();
  23. var currentDate = startDate;
  24. while (currentDate <= stopDate) {
  25. var curdate = new Date(currentDate);
  26. var dateElements = {
  27. day : cal_days_labels[curdate.getDay()],
  28. date : curdate.getDate(),
  29. month : cal_months_labels[curdate.getMonth()],
  30. year : curdate.getFullYear(),
  31. datestamp : curdate.getFullYear()+''+scope.padWithZero(curdate.getMonth()+1)+''+scope.padWithZero(curdate.getDate())
  32. };
  33. dateArray.push(dateElements);
  34. currentDate = currentDate.addDays(1);
  35. }
  36. return dateArray;
  37. };
  38.  
  39. scope.padWithZero = function(number) {
  40. if(number>-10 && number<10) {
  41. number = '0'+number;
  42. }
  43. return number;
  44. };
  45.  
  46.  
  47. $(element).datepicker({
  48. showOtherMonths: true,
  49. selectOtherMonths: true,
  50. changeMonth: true,
  51. changeYear: true,
  52. showWeek: true,
  53. beforeShow: function(dateText, inst) {
  54. },
  55. onSelect : function(dateText, inst) {
  56. current = {day: inst.currentDay, month : inst.currentMonth, year : inst.currentYear};
  57.  
  58. var selectedDateString = new Date(current.year+'-' + (1+current.month) + '-' + current.day);
  59. var weekBoundryDates = selectedDateString.getWeekBoundryDaysFromToday();
  60.  
  61. var weekBoundries = weekString(weekBoundryDates);
  62. scope.$apply(function() {
  63. scope.selectedWeek = weekBoundries;
  64. });
  65. scope.$digest();
  66. scope.$emit("weekselected", inst);
  67. },
  68. onClose: function(dateText, inst) {
  69. }
  70. });
  71. scope.$watch(function(scope){
  72. return scope.selectedWeek;
  73. }, function(newVal, oldVal){
  74. });
  75. };
  76.  
  77. return {
  78. restrict: 'A',
  79. require: 'ngModel',
  80. link: link
  81. };
  82. });
Add Comment
Please, Sign In to add comment