Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit f9d98ed

Browse files
committed
Adjusting the computation of previous/next month days and improving computation of the week days order
1 parent a4a83d8 commit f9d98ed

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/js/angular-datepicker.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,12 @@
293293
, nextMonthDays = []
294294
, howManyNextDays
295295
, howManyPreviousDays
296-
, monthAlias;
296+
, monthAlias
297+
, dateWeekEndDay;
297298

298299
$scope.days = [];
300+
$scope.dateWeekStartDay = $scope.validateWeekDay($scope.dateWeekStartDay);
301+
dateWeekEndDay = ($scope.dateWeekStartDay + 6) % 7;
299302

300303
for (i = 1; i <= limitDate; i += 1) {
301304

@@ -310,6 +313,12 @@
310313
} else {
311314

312315
howManyPreviousDays = firstDayMonthNumber - $scope.dateWeekStartDay;
316+
317+
if (firstDayMonthNumber < $scope.dateWeekStartDay) {
318+
319+
howManyPreviousDays += 7;
320+
}
321+
313322
//get previous month
314323
if (Number(month) === 1) {
315324

@@ -328,11 +337,16 @@
328337
}
329338

330339
//get next month days if last day in month is not last day in week
331-
if (lastDayMonthNumber === $scope.dateWeekEndDay) {
340+
if (lastDayMonthNumber === dateWeekEndDay) {
332341
//no need for it
333342
$scope.nextMonthDays = [];
334343
} else {
335344
howManyNextDays = 6 - lastDayMonthNumber + $scope.dateWeekStartDay;
345+
346+
if (lastDayMonthNumber < $scope.dateWeekStartDay) {
347+
348+
howManyNextDays -= 7;
349+
}
336350
//get previous month
337351

338352
//return next month days
@@ -683,6 +697,16 @@
683697
return true;
684698
};
685699

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+
686710
// respect previously configured interpolation symbols.
687711
htmlTemplate = htmlTemplate.replace(/{{/g, $interpolate.startSymbol()).replace(/}}/g, $interpolate.endSymbol());
688712
$scope.dateMonthTitle = $scope.dateMonthTitle || 'Select month';
@@ -692,11 +716,7 @@
692716
$scope.month = $filter('date')(date, 'MMMM');//december-November like
693717
$scope.monthNumber = Number($filter('date')(date, 'MM')); // 01-12 like
694718
$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);
700720

701721
if ($scope.dateMaxLimit) {
702722

@@ -707,20 +727,16 @@
707727
}
708728
$scope.months = datetime.MONTH;
709729

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);
716734
}
717-
$scope.daysInString.map(function mappingFunc(el) {
735+
$scope.daysInString = $scope.daysInString.map(function mappingFunc(el) {
718736

719737
return $filter('date')(new Date(new Date('06/08/2014').valueOf() + A_DAY_IN_MILLISECONDS * el), 'EEE');
720738
});
721739

722-
$scope.dateWeekEndDay = $scope.daysInString[7];
723-
724740
//create the calendar holder and append where needed
725741
if ($scope.datepickerAppendTo &&
726742
$scope.datepickerAppendTo.indexOf('.') !== -1) {

0 commit comments

Comments
 (0)