From 45165ba58b34c43d746ccec02c30050997606463 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Tue, 22 Mar 2016 07:33:01 -0700 Subject: [PATCH] feat(datepicker): watch for changes when falsy - When initial value is falsy, continue to watch for changes to update Closes #5672 Closes #5677 --- src/datepicker/datepicker.js | 26 +++++++------- src/datepicker/test/datepicker.spec.js | 48 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index 143955926e..d4a133beb6 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -94,23 +94,21 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst break; case 'maxDate': case 'minDate': - if ($scope.datepickerOptions[key]) { - $scope.$watch('datepickerOptions.' + key, function(value) { - if (value) { - if (angular.isDate(value)) { - self[key] = dateParser.fromTimezone(new Date(value), ngModelOptions.timezone); - } else { - self[key] = new Date(dateFilter(value, 'medium')); - } + $scope.$watch('datepickerOptions.' + key, function(value) { + if (value) { + if (angular.isDate(value)) { + self[key] = dateParser.fromTimezone(new Date(value), ngModelOptions.timezone); } else { - self[key] = null; + self[key] = new Date(dateFilter(value, 'medium')); } + } else { + self[key] = datepickerConfig[key] ? + dateParser.fromTimezone(new Date(datepickerConfig[key]), ngModelOptions.timezone) : + null; + } - self.refreshView(); - }); - } else { - self[key] = datepickerConfig[key] ? dateParser.fromTimezone(new Date(datepickerConfig[key]), ngModelOptions.timezone) : null; - } + self.refreshView(); + }); break; case 'maxMode': diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index 87c605e4da..cbfa53a937 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -920,6 +920,30 @@ describe('datepicker', function() { }); }); + describe('minDate with no initial value', function() { + beforeEach(function() { + $rootScope.options = {}; + $rootScope.date = new Date('September 10, 2010'); + element = $compile('')($rootScope); + $rootScope.$digest(); + }); + + it('should toggle appropriately', function() { + var buttons = getAllOptionsEl(); + angular.forEach(buttons, function(button, index) { + expect(angular.element(button).prop('disabled')).toBe(false); + }); + + $rootScope.options.minDate = new Date('September 12, 2010'); + $rootScope.$digest(); + + refreshedButtons = getAllOptionsEl(); + angular.forEach(refreshedButtons, function(button, index) { + expect(angular.element(button).prop('disabled')).toBe(index < 14); + }); + }); + }); + describe('minDate', function() { beforeEach(function() { $rootScope.options = { @@ -1019,6 +1043,30 @@ describe('datepicker', function() { }); }); + describe('maxDate with no initial value', function() { + beforeEach(function() { + $rootScope.options = {}; + $rootScope.date = new Date('September 10, 2010'); + element = $compile('')($rootScope); + $rootScope.$digest(); + }); + + it('should toggle appropriately', function() { + var buttons = getAllOptionsEl(); + angular.forEach(buttons, function(button, index) { + expect(angular.element(button).prop('disabled')).toBe(false); + }); + + $rootScope.options.maxDate = new Date('September 25, 2010'); + $rootScope.$digest(); + + refreshedButtons = getAllOptionsEl(); + angular.forEach(refreshedButtons, function(button, index) { + expect(angular.element(button).prop('disabled')).toBe(index > 27); + }); + }); + }); + describe('maxDate', function() { beforeEach(function() { $rootScope.options = {