|
293 | 293 | , nextMonthDays = [] |
294 | 294 | , howManyNextDays |
295 | 295 | , howManyPreviousDays |
296 | | - , monthAlias; |
| 296 | + , monthAlias |
| 297 | + , dateWeekEndDay; |
297 | 298 |
|
298 | 299 | $scope.days = []; |
| 300 | + $scope.dateWeekStartDay = $scope.validateWeekDay($scope.dateWeekStartDay); |
| 301 | + dateWeekEndDay = ($scope.dateWeekStartDay + 6) % 7; |
299 | 302 |
|
300 | 303 | for (i = 1; i <= limitDate; i += 1) { |
301 | 304 |
|
|
310 | 313 | } else { |
311 | 314 |
|
312 | 315 | howManyPreviousDays = firstDayMonthNumber - $scope.dateWeekStartDay; |
| 316 | + |
| 317 | + if (firstDayMonthNumber < $scope.dateWeekStartDay) { |
| 318 | + |
| 319 | + howManyPreviousDays += 7; |
| 320 | + } |
| 321 | + |
313 | 322 | //get previous month |
314 | 323 | if (Number(month) === 1) { |
315 | 324 |
|
|
328 | 337 | } |
329 | 338 |
|
330 | 339 | //get next month days if last day in month is not last day in week |
331 | | - if (lastDayMonthNumber === $scope.dateWeekEndDay) { |
| 340 | + if (lastDayMonthNumber === dateWeekEndDay) { |
332 | 341 | //no need for it |
333 | 342 | $scope.nextMonthDays = []; |
334 | 343 | } else { |
335 | 344 | howManyNextDays = 6 - lastDayMonthNumber + $scope.dateWeekStartDay; |
| 345 | + |
| 346 | + if (lastDayMonthNumber < $scope.dateWeekStartDay) { |
| 347 | + |
| 348 | + howManyNextDays -= 7; |
| 349 | + } |
336 | 350 | //get previous month |
337 | 351 |
|
338 | 352 | //return next month days |
|
683 | 697 | return true; |
684 | 698 | }; |
685 | 699 |
|
| 700 | + $scope.validateWeekDay = function isValidWeekDay(weekDay) { |
| 701 | + var validWeekDay = Number(weekDay, 10); |
| 702 | + // making sure that the given option is valid |
| 703 | + if (!validWeekDay || validWeekDay < 0 || validWeekDay > 6) { |
| 704 | + |
| 705 | + validWeekDay = 0; |
| 706 | + } |
| 707 | + return validWeekDay; |
| 708 | + }; |
| 709 | + |
686 | 710 | // respect previously configured interpolation symbols. |
687 | 711 | htmlTemplate = htmlTemplate.replace(/{{/g, $interpolate.startSymbol()).replace(/}}/g, $interpolate.endSymbol()); |
688 | 712 | $scope.dateMonthTitle = $scope.dateMonthTitle || 'Select month'; |
|
692 | 716 | $scope.month = $filter('date')(date, 'MMMM');//december-November like |
693 | 717 | $scope.monthNumber = Number($filter('date')(date, 'MM')); // 01-12 like |
694 | 718 | $scope.day = Number($filter('date')(date, 'dd')); //01-31 like |
695 | | - $scope.dateWeekStartDay = parseInt($scope.dateWeekStartDay, 10); |
696 | | - // making sure that the given option is valid |
697 | | - if (!Number.isInteger($scope.dateWeekStartDay) || $scope.dateWeekStartDay < 0 || $scope.dateWeekStartDay > 6) { |
698 | | - $scope.dateWeekStartDay = 0; |
699 | | - } |
| 719 | + $scope.dateWeekStartDay = $scope.validateWeekDay($scope.dateWeekStartDay); |
700 | 720 |
|
701 | 721 | if ($scope.dateMaxLimit) { |
702 | 722 |
|
|
707 | 727 | } |
708 | 728 | $scope.months = datetime.MONTH; |
709 | 729 |
|
710 | | - $scope.daysInString = ['0', '1', '2', '3', '4', '5', '6']; |
711 | | - if ($scope.dateWeekStartDay > 0) { |
712 | | - // shifting the first day of the week according to the given option |
713 | | - for (n = 0; n < $scope.dateWeekStartDay; n += 1) { |
714 | | - $scope.daysInString.push($scope.daysInString.shift()); |
715 | | - } |
| 730 | + $scope.daysInString = []; |
| 731 | + for (n = $scope.dateWeekStartDay; n <= $scope.dateWeekStartDay + 6; n += 1) { |
| 732 | + |
| 733 | + $scope.daysInString.push(n % 7); |
716 | 734 | } |
717 | | - $scope.daysInString.map(function mappingFunc(el) { |
| 735 | + $scope.daysInString = $scope.daysInString.map(function mappingFunc(el) { |
718 | 736 |
|
719 | 737 | return $filter('date')(new Date(new Date('06/08/2014').valueOf() + A_DAY_IN_MILLISECONDS * el), 'EEE'); |
720 | 738 | }); |
721 | 739 |
|
722 | | - $scope.dateWeekEndDay = $scope.daysInString[7]; |
723 | | - |
724 | 740 | //create the calendar holder and append where needed |
725 | 741 | if ($scope.datepickerAppendTo && |
726 | 742 | $scope.datepickerAppendTo.indexOf('.') !== -1) { |
|
0 commit comments