Skip to content

Commit e17be8b

Browse files
committed
feat(datepicker): ng-model-options: timezone wip
for angular-ui#4837
1 parent 3658502 commit e17be8b

File tree

3 files changed

+261
-21
lines changed

3 files changed

+261
-21
lines changed

Diff for: src/datepicker/datepicker.js

+79-21
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
1717
yearRange: 20,
1818
minDate: null,
1919
maxDate: null,
20-
shortcutPropagation: false
20+
shortcutPropagation: false,
21+
ngModelOptions: {}
2122
})
2223

23-
.controller('UibDatepickerController', ['$scope', '$attrs', '$parse', '$interpolate', '$log', 'dateFilter', 'uibDatepickerConfig', '$datepickerSuppressError', function($scope, $attrs, $parse, $interpolate, $log, dateFilter, datepickerConfig, $datepickerSuppressError) {
24+
.controller('UibDatepickerController', ['$scope', '$attrs', '$parse', '$interpolate', '$log', 'dateFilter', 'uibDatepickerConfig', '$datepickerSuppressError', 'uibDateUtils',
25+
function($scope, $attrs, $parse, $interpolate, $log, dateFilter, datepickerConfig, $datepickerSuppressError, dateUtils) {
2426
var self = this,
25-
ngModelCtrl = { $setViewValue: angular.noop }; // nullModelCtrl;
27+
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl;
28+
ngModelOptions = {};
2629

2730
// Modes chain
2831
this.modes = ['day', 'month', 'year'];
@@ -41,11 +44,11 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
4144
angular.forEach(['minDate', 'maxDate'], function(key) {
4245
if ($attrs[key]) {
4346
$scope.$parent.$watch($attrs[key], function(value) {
44-
self[key] = value ? new Date(value) : null;
47+
self[key] = value ? dateUtils.fromTimezone(new Date(value), ngModelOptions.timezone) : null;
4548
self.refreshView();
4649
});
4750
} else {
48-
self[key] = datepickerConfig[key] ? new Date(datepickerConfig[key]) : null;
51+
self[key] = datepickerConfig[key] ? dateUtils.fromTimezone(new Date(datepickerConfig[key]), ngModelOptions.timezone) : null;
4952
}
5053
});
5154

@@ -70,7 +73,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
7073
this.activeDate = $scope.$parent.$eval($attrs.initDate) || new Date();
7174
$scope.$parent.$watch($attrs.initDate, function(initDate) {
7275
if (initDate && (ngModelCtrl.$isEmpty(ngModelCtrl.$modelValue) || ngModelCtrl.$invalid)) {
73-
self.activeDate = initDate;
76+
self.activeDate = dateUtils.fromTimezone(initDate, ngModelOptions.timezone);
7477
self.refreshView();
7578
}
7679
});
@@ -96,6 +99,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
9699

97100
this.init = function(ngModelCtrl_) {
98101
ngModelCtrl = ngModelCtrl_;
102+
ngModelOptions = ngModelCtrl_.$options || datepickerConfig.ngModelOptions;
99103

100104
ngModelCtrl.$render = function() {
101105
self.render();
@@ -108,7 +112,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
108112
isValid = !isNaN(date);
109113

110114
if (isValid) {
111-
this.activeDate = date;
115+
this.activeDate = dateUtils.fromTimezone(date, ngModelOptions.timezone);
112116
} else if (!$datepickerSuppressError) {
113117
$log.error('Datepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.');
114118
}
@@ -121,13 +125,15 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
121125
this._refreshView();
122126

123127
var date = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : null;
128+
date = dateUtils.fromTimezone(date, ngModelOptions.timezone);
124129
ngModelCtrl.$setValidity('dateDisabled', !date ||
125130
this.element && !this.isDisabled(date));
126131
}
127132
};
128133

129134
this.createDateObject = function(date, format) {
130135
var model = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : null;
136+
model = dateUtils.fromTimezone(model, ngModelOptions.timezone);
131137
return {
132138
date: date,
133139
label: dateFilter(date, format),
@@ -160,8 +166,9 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
160166

161167
$scope.select = function(date) {
162168
if ($scope.datepickerMode === self.minMode) {
163-
var dt = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : new Date(0, 0, 0, 0, 0, 0, 0);
169+
var dt = ngModelCtrl.$viewValue ? dateUtils.fromTimezone(new Date(ngModelCtrl.$viewValue), ngModelOptions.timezone) : new Date(0, 0, 0, 0, 0, 0, 0);
164170
dt.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
171+
dt = dateUtils.toTimezone(dt, ngModelOptions.timezone);
165172
ngModelCtrl.$setViewValue(dt);
166173
ngModelCtrl.$render();
167174
} else {
@@ -465,7 +472,9 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
465472
controller: 'UibDatepickerController',
466473
controllerAs: 'datepicker',
467474
link: function(scope, element, attrs, ctrls) {
468-
var datepickerCtrl = ctrls[0], ngModelCtrl = ctrls[1];
475+
var datepickerCtrl = ctrls[0],
476+
ngModelCtrl = ctrls[1];
477+
469478

470479
datepickerCtrl.init(ngModelCtrl);
471480
}
@@ -543,19 +552,20 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
543552
altInputFormats: []
544553
})
545554

546-
.controller('UibDatepickerPopupController', ['$scope', '$element', '$attrs', '$compile', '$parse', '$document', '$rootScope', '$uibPosition', 'dateFilter', 'uibDateParser', 'uibDatepickerPopupConfig', '$timeout',
547-
function(scope, element, attrs, $compile, $parse, $document, $rootScope, $position, dateFilter, dateParser, datepickerPopupConfig, $timeout) {
555+
.controller('UibDatepickerPopupController', ['$scope', '$element', '$attrs', '$compile', '$parse', '$document', '$rootScope', '$uibPosition', 'dateFilter', 'uibDateParser', 'uibDatepickerPopupConfig', '$timeout', 'uibDateUtils', 'uibDatepickerConfig',
556+
function(scope, element, attrs, $compile, $parse, $document, $rootScope, $position, dateFilter, dateParser, datepickerPopupConfig, $timeout, dateUtils, datepickerConfig) {
548557
var self = this;
549558
var cache = {},
550559
isHtml5DateInput = false;
551560
var dateFormat, closeOnDateSelection, appendToBody, onOpenFocus,
552561
datepickerPopupTemplateUrl, datepickerTemplateUrl, popupEl, datepickerEl,
553-
ngModel, $popup, altInputFormats;
562+
ngModel, ngModelOptions, $popup, altInputFormats;
554563

555564
scope.watchData = {};
556565

557566
this.init = function(_ngModel_) {
558567
ngModel = _ngModel_;
568+
ngModelOptions = _ngModel_.$options || datepickerConfig.ngModelOptions;
559569
closeOnDateSelection = angular.isDefined(attrs.closeOnDateSelection) ? scope.$parent.$eval(attrs.closeOnDateSelection) : datepickerPopupConfig.closeOnDateSelection;
560570
appendToBody = angular.isDefined(attrs.datepickerAppendToBody) ? scope.$parent.$eval(attrs.datepickerAppendToBody) : datepickerPopupConfig.appendToBody;
561571
onOpenFocus = angular.isDefined(attrs.onOpenFocus) ? scope.$parent.$eval(attrs.onOpenFocus) : datepickerPopupConfig.onOpenFocus;
@@ -568,6 +578,8 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
568578
if (datepickerPopupConfig.html5Types[attrs.type]) {
569579
dateFormat = datepickerPopupConfig.html5Types[attrs.type];
570580
isHtml5DateInput = true;
581+
ngModelOptions.timezoneHtml5 = ngModelOptions.timezone;
582+
ngModelOptions.timezone = null;
571583
} else {
572584
dateFormat = attrs.uibDatepickerPopup || datepickerPopupConfig.datepickerPopup;
573585
attrs.$observe('uibDatepickerPopup', function(value, oldValue) {
@@ -595,8 +607,10 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
595607

596608
// popup element used to display calendar
597609
popupEl = angular.element('<div uib-datepicker-popup-wrap><div uib-datepicker></div></div>');
610+
scope.ngModelOptions = ngModelOptions;
598611
popupEl.attr({
599612
'ng-model': 'date',
613+
'ng-model-options': 'ngModelOptions',
600614
'ng-change': 'dateSelection(date)',
601615
'template-url': datepickerPopupTemplateUrl
602616
});
@@ -615,7 +629,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
615629
if (attrs.datepickerOptions) {
616630
var options = scope.$parent.$eval(attrs.datepickerOptions);
617631
if (options && options.initDate) {
618-
scope.initDate = options.initDate;
632+
scope.initDate = dateUtils.fromTimezone(options.initDate, ngModelOptions.timezone);
619633
datepickerEl.attr('init-date', 'initDate');
620634
delete options.initDate;
621635
}
@@ -628,9 +642,12 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
628642
if (attrs[key]) {
629643
var getAttribute = $parse(attrs[key]);
630644
scope.$parent.$watch(getAttribute, function(value) {
631-
scope.watchData[key] = value;
632645
if (key === 'minDate' || key === 'maxDate') {
633-
cache[key] = new Date(value);
646+
cache[key] = dateUtils.fromTimezone(new Date(value), ngModelOptions.timezone);
647+
}
648+
scope.watchData[key] = cache[key] || value;
649+
if (key === 'initDate') {
650+
scope.watchData[key] = dateUtils.fromTimezone(new Date(value), ngModelOptions.timezone);
634651
}
635652
});
636653
datepickerEl.attr(cameltoDash(key), 'watchData.' + key);
@@ -667,8 +684,13 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
667684
ngModel.$validators.date = validator;
668685
ngModel.$parsers.unshift(parseDate);
669686
ngModel.$formatters.push(function(value) {
670-
scope.date = value;
671-
return ngModel.$isEmpty(value) ? value : dateFilter(value, dateFormat);
687+
if (ngModel.$isEmpty(value)) {
688+
scope.date = value;
689+
return value;
690+
}
691+
var dt = dateUtils.fromTimezone(value, ngModelOptions.timezone);
692+
scope.date = dt;
693+
return dateFilter(dt, dateFormat);
672694
});
673695
} else {
674696
ngModel.$formatters.push(function(value) {
@@ -830,7 +852,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
830852
return undefined;
831853
}
832854

833-
return date;
855+
return dateUtils.toTimezone(date, ngModelOptions.timezone);
834856
}
835857

836858
return undefined;
@@ -899,7 +921,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
899921

900922
.directive('uibDatepickerPopup', function() {
901923
return {
902-
require: ['ngModel', 'uibDatepickerPopup'],
924+
require: ['uibDatepickerPopup', 'ngModel'],
903925
controller: 'UibDatepickerPopupController',
904926
scope: {
905927
isOpen: '=?',
@@ -910,8 +932,8 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
910932
customClass: '&'
911933
},
912934
link: function(scope, element, attrs, ctrls) {
913-
var ngModel = ctrls[0],
914-
ctrl = ctrls[1];
935+
var ctrl = ctrls[0],
936+
ngModel = ctrls[1];
915937

916938
ctrl.init(ngModel);
917939
}
@@ -926,4 +948,40 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
926948
return attrs.templateUrl || 'uib/template/datepicker/popup.html';
927949
}
928950
};
951+
})
952+
953+
.service('uibDateUtils', function() {
954+
return {
955+
toTimezone: toTimezone,
956+
fromTimezone: fromTimezone,
957+
timezoneToOffset: timezoneToOffset,
958+
addDateMinutes: addDateMinutes,
959+
convertTimezoneToLocal: convertTimezoneToLocal
960+
};
961+
962+
function toTimezone(date, timezone) {
963+
return date && timezone ? convertTimezoneToLocal(date, timezone) : date;
964+
}
965+
966+
function fromTimezone(date, timezone) {
967+
return date && timezone ? convertTimezoneToLocal(date, timezone, true) : date;
968+
}
969+
970+
//https://github.com/angular/angular.js/blob/4daafd3dbe6a80d578f5a31df1bb99c77559543e/src/Angular.js#L1207
971+
function timezoneToOffset(timezone, fallback) {
972+
var requestedTimezoneOffset = Date.parse('Jan 01, 1970 00:00:00 ' + timezone) / 60000;
973+
return isNaN(requestedTimezoneOffset) ? fallback : requestedTimezoneOffset;
974+
}
975+
976+
function addDateMinutes(date, minutes) {
977+
date = new Date(date.getTime());
978+
date.setMinutes(date.getMinutes() + minutes);
979+
return date;
980+
}
981+
982+
function convertTimezoneToLocal(date, timezone, reverse) {
983+
reverse = reverse ? -1 : 1;
984+
var timezoneOffset = timezoneToOffset(timezone, date.getTimezoneOffset());
985+
return addDateMinutes(date, reverse * (timezoneOffset - date.getTimezoneOffset()));
986+
}
929987
});

Diff for: src/datepicker/docs/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ The datepicker has 3 modes:
9595
_(Default: `20`)_ -
9696
Number of years displayed in year selection.
9797

98+
* `ng-model-options`
99+
_(Default: {})_ -
100+
Timezone support. [More on ngModelOptions](https://docs.angularjs.org/api/ng/directive/ngModelOptions).
101+
98102
### uib-datepicker-popup settings ###
99103

100104
Options for the uib-datepicker must be passed as JSON using the `datepicker-options` attribute. This list is only for popup settings.

0 commit comments

Comments
 (0)