diff --git a/bower.json b/bower.json index c0fb8f4..89ec495 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "ngComboDatePicker", - "version": "1.3.0", + "version": "1.3.1", "authors": [ "jfmdev " ], diff --git a/package.json b/package.json index 471a359..ad27371 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/source/ngComboDatePicker.js b/source/ngComboDatePicker.js index 10bc76a..e7a6922 100644 --- a/source/ngComboDatePicker.js +++ b/source/ngComboDatePicker.js @@ -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/. @@ -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; @@ -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 = []; @@ -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(); } @@ -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; }); @@ -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; }); diff --git a/source/ngComboDatePicker.min.js b/source/ngComboDatePicker.min.js index 8ecdda0..ef1effe 100644 --- a/source/ngComboDatePicker.min.js +++ b/source/ngComboDatePicker.min.js @@ -1,7 +1,7 @@ /* - * 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/. */ -angular.module("ngComboDatePicker",[]).directive("ngComboDatePicker",function(){function maxDate(a,b){var c=31;return null!=a&&(4!=a&&6!=a&&9!=a&&11!=a||(c=30),null!=b&&2==a&&(c=b%4==0&&b%100!=0?29:28)),c}function adjustTimezone(a,b){var c=isNaN(b)?(new Date).getTimezoneOffset():60*parseFloat(b);return new Date(a.getTime()+60*c*1e3)}function parseDate(a,b){var c=null;return void 0!==a&&null!==a&&(a instanceof Date?c=a:"number"!=typeof a&&"string"!=typeof a||(c=new Date(isNaN(a)?a:parseInt(a,10)),c=adjustTimezone(c,b))),c}function parseJsonPlus(a){var b=null;if(null!=a){try{b=JSON.parse(a)}catch(a){}if(null==b)try{b=JSON.parse(a.replace(/'/g,'"'))}catch(a){}}return b}return{restrict:"AEC",scope:{ngModel:"=",ngDate:"@",ngMinDate:"@",ngMaxDate:"@",ngMonths:"@",ngOrder:"@",ngAttrsDate:"@",ngAttrsMonth:"@",ngAttrsYear:"@",ngYearOrder:"@",ngTimezone:"@",ngPlaceholder:"@",ngRequired:"@"},require:"ngModel",controller:["$scope",function(a){a.ngModel=parseDate(a.ngModel,a.ngTimezone),a.ngAttrsDate=parseJsonPlus(a.ngAttrsDate),a.ngAttrsMonth=parseJsonPlus(a.ngAttrsMonth),a.ngAttrsYear=parseJsonPlus(a.ngAttrsYear);var b=parseDate(a.ngDate,a.ngTimezone);if(null!=b&&(a.ngModel=b),"string"!=typeof a.ngOrder?a.ngOrder="dmy":a.ngOrder=a.ngOrder.toLowerCase(),a.minDate=parseDate(a.ngMinDate,a.ngTimezone),null==a.ngMinDate){var c=new Date;a.minDate=new Date(c.getFullYear()-100,c.getMonth(),c.getDate(),c.getHours(),c.getMinutes(),c.getSeconds(),c.getMilliseconds())}if(a.maxDate=parseDate(a.ngMaxDate,a.ngTimezone),null==a.maxDate&&(a.maxDate=new Date),a.ngModela.maxDate&&(a.ngModel=a.maxDate),a.placeHolders=null,void 0!==a.ngPlaceholder&&null!==a.ngPlaceholder&&("string"==typeof a.ngPlaceholder||Array.isArray(a.ngPlaceholder))){var d="string"==typeof a.ngPlaceholder?a.ngPlaceholder.split(","):a.ngPlaceholder;if(3==d.length){a.placeHolders=[];for(var e=0;ed?d:b.date,e,f,g,h)}return a.placeHolders&&(""!=a.year&&(a.placeHolders[0].disabled=!0),""!=a.month&&(a.placeHolders[1].disabled=!0),""!=a.date&&(a.placeHolders[2].disabled=!0)),a.updateMonthList(),a.updateDateList(),c})},template:function(element,attrs){for(var strAttrs=["","",""],attrNames=["ngAttrsDate","ngAttrsMonth","ngAttrsYear"],i=0;i<3;i++)try{if(attrs&&attrs[attrNames[i]]){eval("var attrsAux= "+attrs[attrNames[i]]);for(var key in attrsAux){var value=attrsAux[key];"boolean"==typeof value?value&&(strAttrs[i]+=key+" "):("string"==typeof value&&value.indexOf('"')>0&&(value=value.replace(/"/g,""")),strAttrs[i]+=key+'="'+value+'" ')}}}catch(a){console.log(a)}var html='';return html}}}); +angular.module("ngComboDatePicker",[]).directive("ngComboDatePicker",function(){function maxDate(a,b){var c=31;return null!=a&&(4!=a&&6!=a&&9!=a&&11!=a||(c=30),null!=b&&2==a&&(c=b%4==0&&b%100!=0?29:28)),c}function adjustTimezone(a,b){var c=isNaN(b)?(new Date).getTimezoneOffset():60*parseFloat(b);return new Date(a.getTime()+60*c*1e3)}function parseDate(a,b){var c=null;return void 0!==a&&null!==a&&(a instanceof Date?c=a:"number"!=typeof a&&"string"!=typeof a||(c=new Date(isNaN(a)?a:parseInt(a,10)),c=adjustTimezone(c,b))),c}function parseIntStrict(a){return null!==a&&""!==a&&NaN!=parseInt(a)?parseInt(a):null}function parseJsonPlus(a){var b=null;if(null!=a){try{b=JSON.parse(a)}catch(a){}if(null==b)try{b=JSON.parse(a.replace(/'/g,'"'))}catch(a){}}return b}return{restrict:"AEC",scope:{ngModel:"=",ngDate:"@",ngMinDate:"@",ngMaxDate:"@",ngMonths:"@",ngOrder:"@",ngAttrsDate:"@",ngAttrsMonth:"@",ngAttrsYear:"@",ngYearOrder:"@",ngTimezone:"@",ngPlaceholder:"@",ngRequired:"@"},require:"ngModel",controller:["$scope",function(a){a.ngModel=parseDate(a.ngModel,a.ngTimezone),a.ngAttrsDate=parseJsonPlus(a.ngAttrsDate),a.ngAttrsMonth=parseJsonPlus(a.ngAttrsMonth),a.ngAttrsYear=parseJsonPlus(a.ngAttrsYear);var b=parseDate(a.ngDate,a.ngTimezone);if(null!=b&&(a.ngModel=b),"string"!=typeof a.ngOrder?a.ngOrder="dmy":a.ngOrder=a.ngOrder.toLowerCase(),a.minDate=parseDate(a.ngMinDate,a.ngTimezone),null==a.ngMinDate){var c=new Date;a.minDate=new Date(c.getFullYear()-100,c.getMonth(),c.getDate(),c.getHours(),c.getMinutes(),c.getSeconds(),c.getMilliseconds())}if(a.maxDate=parseDate(a.ngMaxDate,a.ngTimezone),null==a.maxDate&&(a.maxDate=new Date),a.ngModela.maxDate&&(a.ngModel=a.maxDate),a.placeHolders=null,void 0!==a.ngPlaceholder&&null!==a.ngPlaceholder&&("string"==typeof a.ngPlaceholder||Array.isArray(a.ngPlaceholder))){var d="string"==typeof a.ngPlaceholder?a.ngPlaceholder.split(","):a.ngPlaceholder;if(3==d.length){a.placeHolders=[];for(var e=0;ed?d:b.date,e,f,g,h)}return a.placeHolders&&(""!=a.year&&(a.placeHolders[0].disabled=!0),""!=a.month&&(a.placeHolders[1].disabled=!0),""!=a.date&&(a.placeHolders[2].disabled=!0)),a.updateMonthList(b.year),a.updateDateList(b.month,b.year),c})},template:function(element,attrs){for(var strAttrs=["","",""],attrNames=["ngAttrsDate","ngAttrsMonth","ngAttrsYear"],i=0;i<3;i++)try{if(attrs&&attrs[attrNames[i]]){eval("var attrsAux= "+attrs[attrNames[i]]);for(var key in attrsAux){var value=attrsAux[key];"boolean"==typeof value?value&&(strAttrs[i]+=key+" "):("string"==typeof value&&value.indexOf('"')>0&&(value=value.replace(/"/g,""")),strAttrs[i]+=key+'="'+value+'" ')}}}catch(a){console.log(a)}var html='';return html}}});