From 1f39227c6f748b4e0f6928b4ce6eb857b75a1173 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Thu, 13 Aug 2015 00:22:25 -0700 Subject: [PATCH] feat(datepicker): disable today button if invalid - Disable today button if current date is before `min-date` or after `max-date` --- src/datepicker/datepicker.js | 19 ++++++++++++++++++- src/datepicker/test/datepicker.spec.js | 20 ++++++++++++++++++++ template/datepicker/popup.html | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index 799c652941..8ff29c6aff 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -513,7 +513,8 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar appendToBody = angular.isDefined(attrs.datepickerAppendToBody) ? scope.$parent.$eval(attrs.datepickerAppendToBody) : datepickerPopupConfig.appendToBody, onOpenFocus = angular.isDefined(attrs.onOpenFocus) ? scope.$parent.$eval(attrs.onOpenFocus) : datepickerPopupConfig.onOpenFocus, datepickerPopupTemplateUrl = angular.isDefined(attrs.datepickerPopupTemplateUrl) ? attrs.datepickerPopupTemplateUrl : datepickerPopupConfig.datepickerPopupTemplateUrl, - datepickerTemplateUrl = angular.isDefined(attrs.datepickerTemplateUrl) ? attrs.datepickerTemplateUrl : datepickerPopupConfig.datepickerTemplateUrl; + datepickerTemplateUrl = angular.isDefined(attrs.datepickerTemplateUrl) ? attrs.datepickerTemplateUrl : datepickerPopupConfig.datepickerTemplateUrl, + cache = {}; scope.showButtonBar = angular.isDefined(attrs.showButtonBar) ? scope.$parent.$eval(attrs.showButtonBar) : datepickerPopupConfig.showButtonBar; @@ -521,6 +522,19 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar return scope[key + 'Text'] || datepickerPopupConfig[key + 'Text']; }; + scope.isDisabled = function(date) { + if (date === 'today') { + date = new Date(); + } + + return !((scope.watchData.minDate && scope.compare(date, cache.minDate) < 0) || + (scope.watchData.maxDate && scope.compare(date, cache.maxDate) > 0)); + }; + + scope.compare = function(date1, date2) { + return (new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()) - new Date(date2.getFullYear(), date2.getMonth(), date2.getDate())); + }; + var isHtml5DateInput = false; if (datepickerPopupConfig.html5Types[attrs.type]) { dateFormat = datepickerPopupConfig.html5Types[attrs.type]; @@ -591,6 +605,9 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar var getAttribute = $parse(attrs[key]); scope.$parent.$watch(getAttribute, function(value) { scope.watchData[key] = value; + if (key === 'minDate' || key === 'maxDate') { + cache[key] = new Date(value); + } }); datepickerEl.attr(cameltoDash(key), 'watchData.' + key); diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index ac33776936..8ea3a774aa 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -1906,6 +1906,26 @@ describe('datepicker directive', function() { expect(buttons.eq(2).text()).toBe('CloseME'); }); + it('should disable today button if before min date', function() { + $rootScope.minDate = new Date().setDate(new Date().getDate() + 1); + var wrapElement = $compile('
')($rootScope); + $rootScope.$digest(); + assignElements(wrapElement); + assignButtonBar(); + + expect(buttons.eq(0).prop('disabled')).toBe(true); + }); + + it('should disable today button if after max date', function() { + $rootScope.maxDate = new Date().setDate(new Date().getDate() - 2); + var wrapElement = $compile('
')($rootScope); + $rootScope.$digest(); + assignElements(wrapElement); + assignButtonBar(); + + expect(buttons.eq(0).prop('disabled')).toBe(true); + }); + it('should remove bar', function() { $rootScope.showBar = false; var wrapElement = $compile('
')($rootScope); diff --git a/template/datepicker/popup.html b/template/datepicker/popup.html index 13b29a155f..2c1e6da5c2 100644 --- a/template/datepicker/popup.html +++ b/template/datepicker/popup.html @@ -2,7 +2,7 @@
  • - +