Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

datepicker dynamic min-mode and max-mode #3843

Closed
wants to merge 14 commits into from
22 changes: 18 additions & 4 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst

// Configuration attributes
angular.forEach(['formatDay', 'formatMonth', 'formatYear', 'formatDayHeader', 'formatDayTitle', 'formatMonthTitle',
'minMode', 'maxMode', 'showWeeks', 'startingDay', 'yearRange', 'shortcutPropagation'], function( key, index ) {
self[key] = angular.isDefined($attrs[key]) ? (index < 8 ? $interpolate($attrs[key])($scope.$parent) : $scope.$parent.$eval($attrs[key])) : datepickerConfig[key];
'showWeeks', 'startingDay', 'yearRange', 'shortcutPropagation'], function( key, index ) {
self[key] = angular.isDefined($attrs[key]) ? (index < 6 ? $interpolate($attrs[key])($scope.$parent) : $scope.$parent.$eval($attrs[key])) : datepickerConfig[key];
});

// Watchable date attributes
Expand All @@ -43,8 +43,22 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
}
});

angular.forEach(['minMode', 'maxMode'], function( key ) {
if ( $attrs[key] ) {
$scope.$parent.$watch($parse($attrs[key]), function(value) {
self[key] = angular.isDefined(value) ? value : $attrs[key];
$scope[key] = self[key];
if ((key == 'minMode' && self.modes.indexOf( $scope.datepickerMode ) < self.modes.indexOf( self[key] )) || (key == 'maxMode' && self.modes.indexOf( $scope.datepickerMode ) > self.modes.indexOf( self[key] ))) {
$scope.datepickerMode = self[key];
}
});
} else {
self[key] = datepickerConfig[key] || null;
$scope[key] = self[key];
}
});

$scope.datepickerMode = $scope.datepickerMode || datepickerConfig.datepickerMode;
$scope.maxMode = self.maxMode;
$scope.uniqueId = 'datepicker-' + $scope.$id + '-' + Math.floor(Math.random() * 10000);

if(angular.isDefined($attrs.initDate)) {
Expand Down Expand Up @@ -546,7 +560,7 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
}

scope.watchData = {};
angular.forEach(['minDate', 'maxDate', 'datepickerMode', 'initDate', 'shortcutPropagation'], function( key ) {
angular.forEach(['minMode', 'maxMode', 'minDate', 'maxDate', 'datepickerMode', 'initDate', 'shortcutPropagation'], function( key ) {
if ( attrs[key] ) {
var getAttribute = $parse(attrs[key]);
scope.$parent.$watch(getAttribute, function(value){
Expand Down
37 changes: 35 additions & 2 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,8 @@ describe('datepicker directive', function () {
beforeEach(inject(function() {
$rootScope.date = new Date('August 11, 2013');
$rootScope.mode = 'month';
element = $compile('<datepicker ng-model="date" min-mode="month" datepicker-mode="mode"></datepicker>')($rootScope);
$rootScope.minMode = 'month';
element = $compile('<datepicker ng-model="date" min-mode="minMode" datepicker-mode="mode"></datepicker>')($rootScope);
$rootScope.$digest();
}));

Expand All @@ -2064,13 +2065,25 @@ describe('datepicker directive', function () {
expect(getTitle()).toBe('2013');
clickTitleButton();
expect(getTitle()).toBe('2001 - 2020');
$rootScope.minMode = 'year';
$rootScope.$digest();
clickOption( 5 );
expect(getTitle()).toBe('2001 - 2020');
});

it('updates current mode if necessary', function() {
expect(getTitle()).toBe('2013');
$rootScope.minMode = 'year';
$rootScope.$digest();
expect(getTitle()).toBe('2001 - 2020');
});
});

describe('`max-mode`', function () {
beforeEach(inject(function() {
$rootScope.date = new Date('August 11, 2013');
element = $compile('<datepicker ng-model="date" max-mode="month"></datepicker>')($rootScope);
$rootScope.maxMode = 'month';
element = $compile('<datepicker ng-model="date" max-mode="maxMode"></datepicker>')($rootScope);
$rootScope.$digest();
}));

Expand All @@ -2080,6 +2093,12 @@ describe('datepicker directive', function () {
expect(getTitle()).toBe('2013');
clickTitleButton();
expect(getTitle()).toBe('2013');
clickOption( 10 );
expect(getTitle()).toBe('November 2013');
$rootScope.maxMode = 'day';
$rootScope.$digest();
clickTitleButton();
expect(getTitle()).toBe('November 2013');
});

it('disables the title button at it', function() {
Expand All @@ -2088,6 +2107,20 @@ describe('datepicker directive', function () {
expect(getTitleButton().prop('disabled')).toBe(true);
clickTitleButton();
expect(getTitleButton().prop('disabled')).toBe(true);
clickOption( 10 );
expect(getTitleButton().prop('disabled')).toBe(false);
$rootScope.maxMode = 'day';
$rootScope.$digest();
expect(getTitleButton().prop('disabled')).toBe(true);
});

it('updates current mode if necessary', function() {
expect(getTitle()).toBe('August 2013');
clickTitleButton();
expect(getTitle()).toBe('2013');
$rootScope.maxMode = 'day';
$rootScope.$digest();
expect(getTitle()).toBe('August 2013');
});
});

Expand Down