This repository was archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathdate.js
More file actions
243 lines (196 loc) · 8.04 KB
/
date.js
File metadata and controls
243 lines (196 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import jQuery from 'jquery';
import angular from 'angular';
import _datePicker from 'jquery-ui/datepicker'; // sets up jQuery with the datepicker plugin
function addDateMinutes(date, minutes) {
date = new Date(date.getTime());
date.setMinutes(date.getMinutes() + minutes);
return date;
}
function convertTimezoneToLocal(date, timezone, reverse) {
if (!date) {
return date;
}
reverse = reverse ? -1 : 1;
var dateTimezoneOffset = date.getTimezoneOffset();
var timezoneOffset = timezoneToOffset(timezone, dateTimezoneOffset);
return addDateMinutes(date, reverse * (timezoneOffset - dateTimezoneOffset));
}
//https://github.com/angular/angular.js/blob/622c42169699ec07fc6daaa19fe6d224e5d2f70e/src/Angular.js#L1207
function timezoneToOffset(timezone, fallback) {
timezone = timezone.replace(/:/g, '');
var requestedTimezoneOffset = Date.parse('Jan 01, 1970 00:00:00 ' + timezone) / 60000;
return isNaN(requestedTimezoneOffset) ? fallback : requestedTimezoneOffset;
}
export default angular.module('ui.date', [])
.constant('uiDateConfig', {})
.constant('uiDateFormatConfig', '')
.factory('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig) {
return {
stringToDate: stringToDate,
dateToString: dateToString,
};
function doTZ(date,timezone,reverse) {
return timezone ? convertTimezoneToLocal(date, timezone, reverse) : date;
}
function dateToString(uiDateFormat, value) {
var dateFormat = uiDateFormat || uiDateFormatConfig;
if (value) {
if (dateFormat) {
try {
return jQuery.datepicker.formatDate(dateFormat, value);
} catch (formatException) {
return undefined;
}
}
if (value.toISOString) {
return value.toISOString();
}
}
return null;
}
function stringToDate(dateFormat, valueToParse, timezone) {
dateFormat = dateFormat || uiDateFormatConfig;
if (angular.isDate(valueToParse) && !isNaN(valueToParse)) {
return doTZ(valueToParse, timezone);
}
if (angular.isString(valueToParse)) {
if (dateFormat) {
return doTZ(jQuery.datepicker.parseDate(dateFormat, valueToParse), timezone);
}
var isoDate = new Date(valueToParse);
return isNaN(isoDate.getTime()) ? null : doTZ(isoDate, timezone);
}
if (angular.isNumber(valueToParse)) {
// presumably timestamp to date object
return doTZ(new Date(valueToParse), timezone);
}
return null;
}
}])
.directive('uiDate', ['uiDateConfig', 'uiDateConverter', function uiDateDirective(uiDateConfig, uiDateConverter) {
return {
require: '?ngModel',
link: function link(scope, element, attrs, controller) {
var $element = jQuery(element);
var getOptions = function() {
return angular.extend({}, uiDateConfig, scope.$eval(attrs.uiDate));
};
var initDateWidget = function() {
var showing = false;
var opts = getOptions();
var timezone = controller ? controller.$options.getOption('timezone') : null;
function setVal(forcedUpdate) {
var keys = ['Hours', 'Minutes', 'Seconds', 'Milliseconds'];
var isDate = angular.isDate(controller.$modelValue);
var preserve = {};
if (!forcedUpdate && isDate && controller.$modelValue.toDateString() === $element.datepicker('getDate').toDateString()) {
return;
}
if (isDate && !timezone) {
angular.forEach(keys, function(key) {
preserve[key] = controller.$modelValue['get' + key]();
});
}
var newViewValue = $element.datepicker('getDate');
if (isDate && !timezone) {
angular.forEach(keys, (key) => {
newViewValue['set' + key](preserve[key]);
});
}
controller.$setViewValue(newViewValue);
}
// If we have a controller (i.e. ngModelController) then wire it up
if (controller) {
// Set the view value in a $apply block when users selects
// (calling directive user's function too if provided)
var _onSelect = opts.onSelect || angular.noop;
opts.onSelect = function(value, picker) {
scope.$apply(function() {
showing = true;
setVal();
$element.blur();
_onSelect(value, picker, $element);
});
};
var _beforeShow = opts.beforeShow || angular.noop;
opts.beforeShow = function(input, picker) {
showing = true;
_beforeShow(input, picker, $element);
};
var _onClose = opts.onClose || angular.noop;
opts.onClose = function(value, picker) {
showing = false;
_onClose(value, picker, $element);
};
element.on('focus', function(focusEvent) {
if (attrs.readonly) {
focusEvent.stopImmediatePropagation();
}
});
$element.off('blur.datepicker').on('blur.datepicker', function() {
if (!showing) {
scope.$apply(function() {
$element.datepicker('setDate', $element.datepicker('getDate'));
setVal();
});
}
});
controller.$validators.uiDateValidator = function uiDateValidator(modelValue, viewValue) {
return viewValue === null
|| viewValue === ''
|| angular.isDate(uiDateConverter.stringToDate(attrs.uiDateFormat, viewValue));
};
controller.$parsers.push(function uiDateParser(valueToParse) {
return uiDateConverter.stringToDate(attrs.uiDateFormat, valueToParse, timezone);
});
// Update the date picker when the model changes
controller.$render = function() {
// Force a render to override whatever is in the input text box
if (angular.isDate(controller.$modelValue) === false && angular.isString(controller.$modelValue)) {
controller.$modelValue = uiDateConverter.stringToDate(attrs.uiDateFormat, controller.$modelValue, timezone);
}
controller.$modelValue = convertTimezoneToLocal(controller.$modelValue, timezone, true);
$element.datepicker('setDate', controller.$modelValue);
};
}
// Check if the $element already has a datepicker.
//
if ($element.data('datepicker')) {
// Updates the datepicker options
$element.datepicker('option', opts);
$element.datepicker('refresh');
} else {
// Creates the new datepicker widget
$element.datepicker(opts);
// Cleanup on destroy, prevent memory leaking
$element.on('$destroy', function() {
$element.datepicker('hide');
$element.datepicker('destroy');
});
}
if (controller) {
controller.$render();
// Update the model with the value from the datepicker after parsed
setVal(true);
}
};
// Watch for changes to the directives options
scope.$watch(getOptions, initDateWidget, true);
},
};
}])
.directive('uiDateFormat', ['uiDateConverter', function(uiDateConverter) {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var dateFormat = attrs.uiDateFormat;
// Use the datepicker with the attribute value as the dateFormat string to convert to and from a string
modelCtrl.$formatters.unshift(function(value) {
return uiDateConverter.stringToDate(dateFormat, value);
});
modelCtrl.$parsers.push(function(value) {
return uiDateConverter.dateToString(dateFormat, value);
});
},
};
}]);