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

Commit 23b91d9

Browse files
committed
feat(datepicker): add ngModelOptions support
- Adds support for ngModelOptions in the `datepicker-options` object BREAKING CHANGE: This modifies the current behavior around the datepicker & popup's ngModelOptions, which may affect custom components that are built around both Closes #5933 Fixes #5825
1 parent 0530352 commit 23b91d9

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

src/datepicker/datepicker.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
156156

157157
this.init = function(ngModelCtrl_) {
158158
ngModelCtrl = ngModelCtrl_;
159-
ngModelOptions = ngModelCtrl_.$options || datepickerConfig.ngModelOptions;
159+
ngModelOptions = ngModelCtrl_.$options ||
160+
$scope.datepickerOptions.ngModelOptions ||
161+
datepickerConfig.ngModelOptions;
160162
if ($scope.datepickerOptions.initDate) {
161163
self.activeDate = dateParser.fromTimezone($scope.datepickerOptions.initDate, ngModelOptions.timezone) || new Date();
162164
$scope.$watch('datepickerOptions.initDate', function(initDate) {

src/datepicker/docs/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ Apart from the previous settings, to configure the uib-datepicker you need to cr
104104
_(Default: `day`)_ -
105105
Sets a lower limit for mode.
106106

107+
* `ngModelOptions`
108+
<small class="badge">C</small>
109+
_(Default: `null`)_ -
110+
Sets `ngModelOptions` for datepicker. This can be overridden through attribute usage
111+
107112
* `shortcutPropagation`
108113
<small class="badge">C</small>
109114
_(Default: `false`)_ -

src/datepicker/test/datepicker.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,23 @@ describe('datepicker', function() {
953953
});
954954

955955
describe('attribute `datepicker-options`', function() {
956+
describe('ngModelOptions', function() {
957+
beforeEach(inject(function() {
958+
$rootScope.date = new Date('2005-11-07T10:00:00.000Z');
959+
$rootScope.options = {
960+
ngModelOptions: {
961+
timezone: '+600'
962+
}
963+
};
964+
element = $compile('<uib-datepicker ng-model="date" datepicker-options="options"></uib-datepicker>')($rootScope);
965+
$rootScope.$digest();
966+
}));
967+
968+
it('supports ngModelOptions from options object and sets date to appropriate date', function() {
969+
expectSelectedElement(8);
970+
});
971+
});
972+
956973
describe('startingDay', function() {
957974
beforeEach(function() {
958975
$rootScope.options = {

src/datepickerPopup/popup.js

+6-20
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
2828
isHtml5DateInput = false;
2929
var dateFormat, closeOnDateSelection, appendToBody, onOpenFocus,
3030
datepickerPopupTemplateUrl, datepickerTemplateUrl, popupEl, datepickerEl, scrollParentEl,
31-
ngModel, ngModelOptions, $popup, altInputFormats, watchListeners = [],
32-
timezone;
31+
ngModel, ngModelOptions, $popup, altInputFormats, watchListeners = [];
3332

3433
this.init = function(_ngModel_) {
3534
ngModel = _ngModel_;
@@ -85,19 +84,6 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
8584

8685
// popup element used to display calendar
8786
popupEl = angular.element('<div uib-datepicker-popup-wrap><div uib-datepicker></div></div>');
88-
if (ngModelOptions) {
89-
timezone = ngModelOptions.timezone;
90-
$scope.ngModelOptions = angular.copy(ngModelOptions);
91-
$scope.ngModelOptions.timezone = null;
92-
if ($scope.ngModelOptions.updateOnDefault === true) {
93-
$scope.ngModelOptions.updateOn = $scope.ngModelOptions.updateOn ?
94-
$scope.ngModelOptions.updateOn + ' default' : 'default';
95-
}
96-
97-
popupEl.attr('ng-model-options', 'ngModelOptions');
98-
} else {
99-
timezone = null;
100-
}
10187

10288
popupEl.attr({
10389
'ng-model': 'date',
@@ -137,13 +123,13 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
137123
value = new Date(value);
138124
}
139125

140-
$scope.date = dateParser.fromTimezone(value, timezone);
126+
$scope.date = value;
141127

142128
return dateParser.filter($scope.date, dateFormat);
143129
});
144130
} else {
145131
ngModel.$formatters.push(function(value) {
146-
$scope.date = dateParser.fromTimezone(value, timezone);
132+
$scope.date = value;
147133
return value;
148134
});
149135
}
@@ -195,15 +181,15 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
195181

196182
$scope.isDisabled = function(date) {
197183
if (date === 'today') {
198-
date = dateParser.fromTimezone(new Date(), timezone);
184+
date = new Date();
199185
}
200186

201187
var dates = {};
202188
angular.forEach(['minDate', 'maxDate'], function(key) {
203189
if (!$scope.datepickerOptions[key]) {
204190
dates[key] = null;
205191
} else if (angular.isDate($scope.datepickerOptions[key])) {
206-
dates[key] = dateParser.fromTimezone(new Date($scope.datepickerOptions[key]), timezone);
192+
dates[key] = new Date($scope.datepickerOptions[key]);
207193
} else {
208194
if ($datepickerPopupLiteralWarning) {
209195
$log.warn('Literal date support has been deprecated, please switch to date object usage');
@@ -344,7 +330,7 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
344330
if (angular.isString(viewValue)) {
345331
var date = parseDateString(viewValue);
346332
if (!isNaN(date)) {
347-
return dateParser.toTimezone(date, timezone);
333+
return date;
348334
}
349335
}
350336

src/datepickerPopup/test/popup.spec.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ describe('datepicker popup', function() {
175175
$sniffer = _$sniffer_;
176176

177177
$rootScope.date = new Date('September 30, 2010 15:30:00');
178-
$rootScope.modelOptions = {allowInvalid: true};
179-
element = $compile('<div><input ng-model="date" ng-model-options="modelOptions" uib-datepicker-popup></div>')($rootScope);
178+
$rootScope.ngModelOptions = {
179+
allowInvalid: true
180+
};
181+
element = $compile('<div><input ng-model="date" ng-model-options="ngModelOptions" uib-datepicker-popup></div>')($rootScope);
180182
inputEl = element.find('input');
181183
$rootScope.$digest();
182184
}));
@@ -557,8 +559,13 @@ describe('datepicker popup', function() {
557559
$timeout = _$timeout_;
558560
$rootScope.isopen = true;
559561
$rootScope.date = new Date('2010-09-30T10:00:00.000Z');
562+
$rootScope.options = {
563+
ngModelOptions: {
564+
updateOn: 'default'
565+
}
566+
};
560567
wrapElement = $compile('<div><input ng-model="date" ' +
561-
'ng-model-options="{ updateOn: \'default\' }" ' +
568+
'datepicker-options="options" ' +
562569
'uib-datepicker-popup is-open="isopen"><div>')($rootScope);
563570
$rootScope.$digest();
564571
assignElements(wrapElement);
@@ -1598,9 +1605,13 @@ describe('datepicker popup', function() {
15981605

15991606
beforeEach(function() {
16001607
$rootScope.date = new Date('2010-09-30T10:00:00.000Z');
1601-
$rootScope.ngModelOptions = { timezone: '+600' };
1608+
$rootScope.options = {
1609+
ngModelOptions: {
1610+
timezone: '+600'
1611+
}
1612+
};
16021613
$rootScope.isopen = true;
1603-
var wrapper = $compile('<div><input ng-model="date" uib-datepicker-popup="MM/dd/yyyy" ng-model-options="ngModelOptions" is-open="isopen"></div>')($rootScope);
1614+
var wrapper = $compile('<div><input ng-model="date" uib-datepicker-popup="MM/dd/yyyy" datepicker-options="options" is-open="isopen"></div>')($rootScope);
16041615
$rootScope.$digest();
16051616
assignElements(wrapper);
16061617
});
@@ -1631,9 +1642,13 @@ describe('datepicker popup', function() {
16311642

16321643
beforeEach(function() {
16331644
$rootScope.date = new Date('2010-09-30T10:00:00.000Z');
1634-
$rootScope.ngModelOptions = { timezone: '+600' };
1645+
$rootScope.options = {
1646+
ngModelOptions: {
1647+
timezone: '+600'
1648+
}
1649+
};
16351650
$rootScope.isopen = true;
1636-
var wrapper = $compile('<div><input type="date" ng-model="date" uib-datepicker-popup ng-model-options="ngModelOptions" is-open="isopen"></div>')($rootScope);
1651+
var wrapper = $compile('<div><input type="date" ng-model="date" uib-datepicker-popup datepicker-options="options" is-open="isopen"></div>')($rootScope);
16371652
$rootScope.$digest();
16381653
assignElements(wrapper);
16391654
});

0 commit comments

Comments
 (0)