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

Commit 26d3103

Browse files
daviouswesleycho
authored andcommitted
feat(datepicker): pass through attrs in popup
- Passes through all datepicker config attributes from the popup to the datepicker Closes #4863 Fixes #3338
1 parent 0917623 commit 26d3103

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

src/datepicker/datepicker.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
2727
// Modes chain
2828
this.modes = ['day', 'month', 'year'];
2929

30-
// Configuration attributes
31-
angular.forEach(['formatDay', 'formatMonth', 'formatYear', 'formatDayHeader', 'formatDayTitle', 'formatMonthTitle',
32-
'showWeeks', 'startingDay', 'yearRange', 'shortcutPropagation'], function(key, index) {
33-
self[key] = angular.isDefined($attrs[key]) ?
34-
index < 6 ? $interpolate($attrs[key])($scope.$parent) :
35-
$scope.$parent.$eval($attrs[key]) :
36-
datepickerConfig[key];
30+
// Interpolated configuration attributes
31+
angular.forEach(['formatDay', 'formatMonth', 'formatYear', 'formatDayHeader', 'formatDayTitle', 'formatMonthTitle'], function(key) {
32+
self[key] = angular.isDefined($attrs[key]) ? $interpolate($attrs[key])($scope.$parent) : datepickerConfig[key];
33+
});
34+
35+
// Evaled configuration attributes
36+
angular.forEach(['showWeeks', 'startingDay', 'yearRange', 'shortcutPropagation'], function(key) {
37+
self[key] = angular.isDefined($attrs[key]) ? $scope.$parent.$eval($attrs[key]) : datepickerConfig[key];
3738
});
3839

3940
// Watchable date attributes
@@ -650,9 +651,11 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
650651
datepickerEl.attr('date-disabled', 'dateDisabled({ date: date, mode: mode })');
651652
}
652653

653-
if (attrs.showWeeks) {
654-
datepickerEl.attr('show-weeks', attrs.showWeeks);
655-
}
654+
angular.forEach(['formatDay', 'formatMonth', 'formatYear', 'formatDayHeader', 'formatDayTitle', 'formatMonthTitle', 'showWeeks', 'startingDay', 'yearRange'], function(key) {
655+
if (angular.isDefined(attrs[key])) {
656+
datepickerEl.attr(cameltoDash(key), attrs[key]);
657+
}
658+
});
656659

657660
if (attrs.customClass) {
658661
datepickerEl.attr('custom-class', 'customClass({ date: date, mode: mode })');

src/datepicker/test/datepicker.spec.js

+63
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,69 @@ describe('datepicker directive', function() {
23042304
expect(focused).toBe(true);
23052305
});
23062306
});
2307+
2308+
describe('pass through attributes', function() {
2309+
var wrapElement;
2310+
describe('formatting', function() {
2311+
beforeEach(function() {
2312+
$rootScope.dayTitle = 'MMMM, yy';
2313+
wrapElement = $compile('<div><input uib-datepicker-popup ng-model="date"' +
2314+
'is-open="true"' +
2315+
'format-day="d"' +
2316+
'format-day-header="EEEE"' +
2317+
'format-day-title="{{dayTitle}}"' +
2318+
'format-month="MMM"' +
2319+
'format-month-title="yy"' +
2320+
'format-year="yy"' +
2321+
'year-range="10"></input></div>')($rootScope);
2322+
$rootScope.$digest();
2323+
assignElements(wrapElement);
2324+
});
2325+
2326+
it('changes the title format in `day` mode', function() {
2327+
expect(getTitle()).toBe('September, 10');
2328+
});
2329+
2330+
it('changes the title & months format in `month` mode', function() {
2331+
clickTitleButton();
2332+
assignElements(wrapElement);
2333+
expect(getTitle()).toBe('10');
2334+
expect(getOptions()).toEqual([
2335+
['Jan', 'Feb', 'Mar'],
2336+
['Apr', 'May', 'Jun'],
2337+
['Jul', 'Aug', 'Sep'],
2338+
['Oct', 'Nov', 'Dec']
2339+
]);
2340+
});
2341+
2342+
it('changes the title, year format & range in `year` mode', function() {
2343+
clickTitleButton();
2344+
assignElements(wrapElement);
2345+
clickTitleButton();
2346+
assignElements(wrapElement);
2347+
expect(getTitle()).toBe('01 - 10');
2348+
expect(getOptions()).toEqual([
2349+
['01', '02', '03', '04', '05'],
2350+
['06', '07', '08', '09', '10']
2351+
]);
2352+
});
2353+
2354+
it('shows day labels', function() {
2355+
expect(getLabels(true)).toEqual(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']);
2356+
});
2357+
2358+
it('changes the day format', function() {
2359+
expect(getOptions(true)).toEqual([
2360+
['29', '30', '31', '1', '2', '3', '4'],
2361+
['5', '6', '7', '8', '9', '10', '11'],
2362+
['12', '13', '14', '15', '16', '17', '18'],
2363+
['19', '20', '21', '22', '23', '24', '25'],
2364+
['26', '27', '28', '29', '30', '1', '2'],
2365+
['3', '4', '5', '6', '7', '8', '9']
2366+
]);
2367+
});
2368+
});
2369+
});
23072370
});
23082371

23092372
describe('with empty initial state', function() {

0 commit comments

Comments
 (0)