From 6e2eb0543d943ec69c84202cbc9838ded6164f59 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 --- 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 dd81bac35f..712f9c6bd6 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -95,23 +95,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 77f9e6e41b..2f246a46c0 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -921,6 +921,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 = { @@ -1020,6 +1044,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 = {