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

Commit 97af6a9

Browse files
Foxandxsswesleycho
authored andcommitted
fix(datepicker): allow using min/max mode/date with datepickerOptions
Closes #5334 Fixes #5315
1 parent d5a48ea commit 97af6a9

File tree

2 files changed

+113
-9
lines changed

2 files changed

+113
-9
lines changed

src/datepicker/datepicker.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,14 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
659659
}
660660
}
661661

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

@@ -984,6 +983,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
984983
require: ['ngModel', 'uibDatepickerPopup'],
985984
controller: 'UibDatepickerPopupController',
986985
scope: {
986+
datepickerOptions: '=?',
987987
isOpen: '=?',
988988
currentText: '@',
989989
clearText: '@',

src/datepicker/test/datepicker.spec.js

+104
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,110 @@ describe('datepicker', function() {
18491849
expect(getTitle()).toBe('November 1980');
18501850
});
18511851
});
1852+
1853+
describe('min-date', function() {
1854+
it('should be able to specify a min-date through options', function() {
1855+
$rootScope.opts = {
1856+
minDate: new Date('September 12, 2010'),
1857+
shortcutPropagation: 'dog'
1858+
};
1859+
1860+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
1861+
$rootScope.$digest();
1862+
assignElements(wrapElement);
1863+
1864+
var buttons = getAllOptionsEl();
1865+
angular.forEach(buttons, function(button, index) {
1866+
expect(angular.element(button).prop('disabled')).toBe(index < 14);
1867+
});
1868+
1869+
$rootScope.opts.minDate = new Date('September 13, 2010');
1870+
$rootScope.$digest();
1871+
buttons = getAllOptionsEl();
1872+
angular.forEach(buttons, function(button, index) {
1873+
expect(angular.element(button).prop('disabled')).toBe(index < 15);
1874+
});
1875+
});
1876+
});
1877+
1878+
describe('max-date', function() {
1879+
it('should be able to specify a max-date through options', function() {
1880+
$rootScope.opts = {
1881+
maxDate: new Date('September 25, 2010')
1882+
};
1883+
1884+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
1885+
$rootScope.$digest();
1886+
assignElements(wrapElement);
1887+
1888+
var buttons = getAllOptionsEl();
1889+
angular.forEach(buttons, function(button, index) {
1890+
expect(angular.element(button).prop('disabled')).toBe(index > 27);
1891+
});
1892+
1893+
$rootScope.opts.maxDate = new Date('September 15, 2010');
1894+
$rootScope.$digest();
1895+
buttons = getAllOptionsEl();
1896+
angular.forEach(buttons, function(button, index) {
1897+
expect(angular.element(button).prop('disabled')).toBe(index > 17);
1898+
});
1899+
});
1900+
});
1901+
1902+
describe('min-mode', function() {
1903+
it('should be able to specify min-mode through options', function() {
1904+
$rootScope.opts = {
1905+
minMode: 'month'
1906+
};
1907+
1908+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
1909+
$rootScope.$digest();
1910+
assignElements(wrapElement);
1911+
1912+
expect(getTitle()).toBe('2010');
1913+
});
1914+
});
1915+
1916+
describe('max-mode', function() {
1917+
it('should be able to specify max-mode through options', function() {
1918+
$rootScope.opts = {
1919+
maxMode: 'month'
1920+
};
1921+
1922+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
1923+
$rootScope.$digest();
1924+
assignElements(wrapElement);
1925+
1926+
expect(getTitle()).toBe('September 2010');
1927+
clickTitleButton();
1928+
assignElements(wrapElement);
1929+
expect(getTitle()).toBe('2010');
1930+
clickTitleButton();
1931+
assignElements(wrapElement);
1932+
expect(getTitle()).toBe('2010');
1933+
});
1934+
});
1935+
1936+
describe('datepicker-mode', function() {
1937+
beforeEach(inject(function() {
1938+
$rootScope.date = new Date('August 11, 2013');
1939+
$rootScope.opts = {
1940+
datepickerMode: 'month'
1941+
};
1942+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
1943+
$rootScope.$digest();
1944+
assignElements(wrapElement);
1945+
}));
1946+
1947+
it('shows the correct title', function() {
1948+
expect(getTitle()).toBe('2013');
1949+
});
1950+
1951+
it('updates binding', function() {
1952+
clickTitleButton();
1953+
expect($rootScope.opts.datepickerMode).toBe('year');
1954+
});
1955+
});
18521956
});
18531957

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

0 commit comments

Comments
 (0)