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

Commit

Permalink
Fix bug when update the list of available days and months
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmdev committed Dec 9, 2016
1 parent 1a0c9f1 commit d956165
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngComboDatePicker",
"version": "1.3.0",
"version": "1.3.1",
"authors": [
"jfmdev <[email protected]>"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-combo-date-picker",
"version": "1.3.0",
"version": "1.3.1",
"description": "An Angular directive to select dates using combo boxes",
"main": "source/ngComboDatePicker.js",
"scripts": {
Expand Down
38 changes: 26 additions & 12 deletions source/ngComboDatePicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* ngComboDatePicker v1.3.0
* ngComboDatePicker v1.3.1
* http://github.com/jfmdev/ngComboDatePicker
* «Copyright 2015 Jose F. Maldonado»
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -49,6 +49,11 @@ angular.module("ngComboDatePicker", [])
return res;
};

// Function to parse an string returning either a number or 'null' (instead of NaN).
function parseIntStrict(num) {
return (num !== null && num !== '' && parseInt(num) != NaN)? parseInt(num) : null;
};

// Function to parse a JSON object.
function parseJsonPlus(jsonObj) {
var res = null;
Expand Down Expand Up @@ -153,10 +158,13 @@ angular.module("ngComboDatePicker", [])
}

// Update list of months.
$scope.updateMonthList = function() {
$scope.updateMonthList = function(year) {
// Parse parameter.
year = parseIntStrict(year);

// Some months can not be choosed if the year matchs with the year of the minimum or maximum dates.
var start = $scope.ngModel != null && $scope.ngModel.getFullYear() == $scope.minDate.getFullYear()? $scope.minDate.getMonth() : 0;
var end = $scope.ngModel != null && $scope.ngModel.getFullYear() == $scope.maxDate.getFullYear()? $scope.maxDate.getMonth() : 11;
var start = year !== null && year == $scope.minDate.getFullYear()? $scope.minDate.getMonth() : 0;
var end = year !== null && year == $scope.maxDate.getFullYear()? $scope.maxDate.getMonth() : 11;

// Generate list.
$scope.months = [];
Expand All @@ -167,16 +175,22 @@ angular.module("ngComboDatePicker", [])
};

// Initialize list of days.
$scope.updateDateList = function() {
$scope.updateDateList = function(month, year) {
// Parse parameters.
month = parseIntStrict(month);
year = parseIntStrict(year);

// Start date is 1, unless the selected month and year matchs the minimum date.
var start = 1;
if($scope.ngModel != null && $scope.ngModel.getMonth() == $scope.minDate.getMonth() && $scope.ngModel.getFullYear() == $scope.minDate.getFullYear()) {
if(month !== null && month == $scope.minDate.getMonth() &&
year !== null && year == $scope.minDate.getFullYear()) {
start = $scope.minDate.getDate();
}

// End date is 30 or 31 (28 or 29 in February), unless the selected month and year matchs the maximum date.
var end = $scope.ngModel != null? maxDate($scope.ngModel.getMonth()+1, $scope.ngModel.getFullYear()) : maxDate(null, null);
if($scope.ngModel != null && $scope.ngModel.getMonth() == $scope.maxDate.getMonth() && $scope.ngModel.getFullYear() == $scope.maxDate.getFullYear()) {
var end = maxDate(month !== null? (month+1) : null, year);
if(month !== null && month == $scope.maxDate.getMonth() &&
year !== null && year == $scope.maxDate.getFullYear()) {
end = $scope.maxDate.getDate();
}

Expand Down Expand Up @@ -223,8 +237,8 @@ angular.module("ngComboDatePicker", [])
}

// Hide or show days and months according to the min and max dates.
scope.updateMonthList();
scope.updateDateList();
scope.updateMonthList(res.year);
scope.updateDateList(res.month, res.year);
return res;
});

Expand Down Expand Up @@ -280,8 +294,8 @@ angular.module("ngComboDatePicker", [])
}

// Hide or show days and months according to the min and max dates.
scope.updateMonthList();
scope.updateDateList();
scope.updateMonthList(viewValue.year);
scope.updateDateList(viewValue.month, viewValue.year);

return res;
});
Expand Down
4 changes: 2 additions & 2 deletions source/ngComboDatePicker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d956165

Please sign in to comment.