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

Commit

Permalink
fix(datepicker): allow using min/max mode/date with datepickerOptions
Browse files Browse the repository at this point in the history
Closes #5334
Fixes #5315
  • Loading branch information
Foxandxss authored and wesleycho committed Jan 24, 2016
1 parent d5a48ea commit 97af6a9
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,14 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
}
}

if (attrs.datepickerOptions) {
var options = scope.$parent.$eval(attrs.datepickerOptions);
if (options && options.initDate) {
scope.initDate = dateParser.fromTimezone(options.initDate, ngModelOptions.timezone);
datepickerEl.attr('init-date', 'initDate');
delete options.initDate;
}
angular.forEach(options, function(value, option) {
datepickerEl.attr(cameltoDash(option), value);
if (scope.datepickerOptions) {
angular.forEach(scope.datepickerOptions, function(value, option) {
// Ignore this options, will be managed later
if (['minDate', 'maxDate', 'minMode', 'maxMode', 'initDate', 'datepickerMode'].indexOf(option) === -1) {
datepickerEl.attr(cameltoDash(option), value);
} else {
datepickerEl.attr(cameltoDash(option), 'datepickerOptions.' + option);
}
});
}

Expand Down Expand Up @@ -984,6 +983,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
require: ['ngModel', 'uibDatepickerPopup'],
controller: 'UibDatepickerPopupController',
scope: {
datepickerOptions: '=?',
isOpen: '=?',
currentText: '@',
clearText: '@',
Expand Down
104 changes: 104 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,110 @@ describe('datepicker', function() {
expect(getTitle()).toBe('November 1980');
});
});

describe('min-date', function() {
it('should be able to specify a min-date through options', function() {
$rootScope.opts = {
minDate: new Date('September 12, 2010'),
shortcutPropagation: 'dog'
};

var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);

var buttons = getAllOptionsEl();
angular.forEach(buttons, function(button, index) {
expect(angular.element(button).prop('disabled')).toBe(index < 14);
});

$rootScope.opts.minDate = new Date('September 13, 2010');
$rootScope.$digest();
buttons = getAllOptionsEl();
angular.forEach(buttons, function(button, index) {
expect(angular.element(button).prop('disabled')).toBe(index < 15);
});
});
});

describe('max-date', function() {
it('should be able to specify a max-date through options', function() {
$rootScope.opts = {
maxDate: new Date('September 25, 2010')
};

var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);

var buttons = getAllOptionsEl();
angular.forEach(buttons, function(button, index) {
expect(angular.element(button).prop('disabled')).toBe(index > 27);
});

$rootScope.opts.maxDate = new Date('September 15, 2010');
$rootScope.$digest();
buttons = getAllOptionsEl();
angular.forEach(buttons, function(button, index) {
expect(angular.element(button).prop('disabled')).toBe(index > 17);
});
});
});

describe('min-mode', function() {
it('should be able to specify min-mode through options', function() {
$rootScope.opts = {
minMode: 'month'
};

var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);

expect(getTitle()).toBe('2010');
});
});

describe('max-mode', function() {
it('should be able to specify max-mode through options', function() {
$rootScope.opts = {
maxMode: 'month'
};

var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);

expect(getTitle()).toBe('September 2010');
clickTitleButton();
assignElements(wrapElement);
expect(getTitle()).toBe('2010');
clickTitleButton();
assignElements(wrapElement);
expect(getTitle()).toBe('2010');
});
});

describe('datepicker-mode', function() {
beforeEach(inject(function() {
$rootScope.date = new Date('August 11, 2013');
$rootScope.opts = {
datepickerMode: 'month'
};
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
}));

it('shows the correct title', function() {
expect(getTitle()).toBe('2013');
});

it('updates binding', function() {
clickTitleButton();
expect($rootScope.opts.datepickerMode).toBe('year');
});
});
});

describe('attribute `init-date`', function() {
Expand Down

0 comments on commit 97af6a9

Please sign in to comment.