forked from Gillardo/bootstrap-ui-datetime-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime-picker.js
350 lines (305 loc) · 15.7 KB
/
datetime-picker.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* Add parents() to jqLite.
*/
if (!angular.element.prototype.parents) {
angular.element.prototype.parents = function() {
var res = [], l = this.length, el;
for (var i = 0; i < l; i++) {
el = this[i];
while (el.parentNode && el.parentNode.nodeType == el.ELEMENT_NODE) {
el = el.parentNode;
res.push(el);
}
}
return angular.element(res);
};
}
angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bootstrap.position'])
.directive('datetimePicker', ['$compile', '$parse', '$document', '$timeout', '$position', 'dateFilter', 'dateParser', 'datepickerPopupConfig',
function ($compile, $parse, $document, $timeout, $position, dateFilter, dateParser, datepickerPopupConfig) {
return {
restrict: 'A',
require: 'ngModel',
scope: {
isOpen: '=?',
enableDate: '=?',
enableTime: '=?',
todayText: '@',
nowText: '@',
dateText: '@',
timeText: '@',
clearText: '@',
closeText: '@',
dateDisabled: '&'
},
link: function (scope, element, attrs, ngModel) {
var dateFormat = 'dd MMM yyyy HH:mm:ss', currentDate,
closeOnDateSelection = angular.isDefined(attrs.closeOnDateSelection) ? scope.$parent.$eval(attrs.closeOnDateSelection) : datepickerPopupConfig.closeOnDateSelection,
appendToBody = angular.isDefined(attrs.datepickerAppendToBody) ? scope.$parent.$eval(attrs.datepickerAppendToBody) : datepickerPopupConfig.appendToBody;
scope.showButtonBar = angular.isDefined(attrs.showButtonBar) ? scope.$parent.$eval(attrs.showButtonBar) : datepickerPopupConfig.showButtonBar;
// determine which pickers should be available. Defaults to date and time
scope.enableDate = !(scope.enableDate == false);
scope.enableTime = !(scope.enableTime == false);
// default picker view
scope.showPicker = scope.enableDate ? 'date' : 'time';
// default text
scope.todayText = scope.todayText || 'Today';
scope.nowText = scope.nowText || 'Now';
scope.clearText = scope.clearText || 'Clear';
scope.closeText = scope.closeText || 'Close';
scope.dateText = scope.dateText || 'Date';
scope.timeText = scope.timeText || 'Time';
scope.getText = function (key) {
return scope[key + 'Text'] || datepickerPopupConfig[key + 'Text'];
};
attrs.$observe('datetimePicker', function (value) {
dateFormat = value || datepickerPopupConfig.datepickerPopup;
ngModel.$render();
});
// popup element used to display calendar
var popupEl = angular.element('' +
'<div date-picker-wrap ng-show="showPicker == \'date\'">' +
'<div datepicker></div>' +
'</div>' +
'<div time-picker-wrap ng-show="showPicker == \'time\'">' +
'<div timepicker style="margin:0 auto"></div>' +
'</div>');
// get attributes from directive
popupEl.attr({
'ng-model': 'date',
'ng-change': 'dateSelection()'
});
function cameltoDash(string) {
return string.replace(/([A-Z])/g, function ($1) { return '-' + $1.toLowerCase(); });
}
// datepicker element
var datepickerEl = angular.element(popupEl.children()[0]);
if (attrs.datepickerOptions) {
angular.forEach(scope.$parent.$eval(attrs.datepickerOptions), function (value, option) {
datepickerEl.attr(cameltoDash(option), value);
});
}
// timepicker element
var timepickerEl = angular.element(popupEl.children()[1]);
if (attrs.timepickerOptions) {
angular.forEach(scope.$parent.$eval(attrs.timepickerOptions), function (value, option) {
timepickerEl.attr(cameltoDash(option), value);
});
}
// set datepickerMode to day by default as need to create watch
// this gets round issue#5 where by the highlight is not shown
if (!attrs['datepickerMode']) attrs['datepickerMode'] = 'day';
scope.watchData = {};
angular.forEach(['minDate', 'maxDate', 'datepickerMode'], function (key) {
if (attrs[key]) {
var getAttribute = $parse(attrs[key]);
scope.$parent.$watch(getAttribute, function (value) {
scope.watchData[key] = value;
});
datepickerEl.attr(cameltoDash(key), 'watchData.' + key);
// Propagate changes from datepicker to outside
if (key === 'datepickerMode') {
var setAttribute = getAttribute.assign;
scope.$watch('watchData.' + key, function (value, oldvalue) {
if (value !== oldvalue) {
setAttribute(scope.$parent, value);
}
});
}
}
});
if (attrs.dateDisabled) {
datepickerEl.attr('date-disabled', 'dateDisabled({ date: date, mode: mode })');
}
function isDateDisabled(dt) {
return attrs.dateDisabled && angular.isDefined(dt) && scope.dateDisabled({ date: dt, mode: scope.watchData['datepickerMode']});
}
function parseDate(viewValue) {
if (!viewValue) {
ngModel.$setValidity('date', true);
return null;
} else if (angular.isDate(viewValue) && !isNaN(viewValue)) {
ngModel.$setValidity('date', true);
return viewValue;
} else if (angular.isString(viewValue)) {
var date = dateParser.parse(viewValue, dateFormat);
if (isNaN(date)) {
ngModel.$setValidity('date', false);
return undefined;
} else {
ngModel.$setValidity('date', true);
return date;
}
} else {
ngModel.$setValidity('date', false);
return undefined;
}
}
ngModel.$parsers.unshift(parseDate);
// Inner change
scope.dateSelection = function (dt) {
// check which picker is being shown, if its date, all is fine and this is the date
// we will use, if its the timePicker but enableDate = true, we need to merge
// the values, else timePicker will reset the date
if (scope.enableDate && scope.enableTime && scope.showPicker === 'time') {
if (currentDate && currentDate !== null && (scope.date !== null || dt || dt != null)) {
// dt will not be undefined if the now or today button is pressed
if (dt && dt != null) {
currentDate.setHours(dt.getHours());
currentDate.setMinutes(dt.getMinutes());
currentDate.setSeconds(dt.getSeconds());
currentDate.setMilliseconds(dt.getMilliseconds());
dt = new Date(currentDate);
} else {
currentDate.setHours(scope.date.getHours());
currentDate.setMinutes(scope.date.getMinutes());
currentDate.setSeconds(scope.date.getSeconds());
currentDate.setMilliseconds(scope.date.getMilliseconds());
}
}
}
if (angular.isDefined(dt)) {
scope.date = dt;
}
// store currentDate
currentDate = scope.date;
ngModel.$setViewValue(scope.date);
ngModel.$render();
if (closeOnDateSelection) {
// do not close when using timePicker
if (scope.showPicker != 'time') {
// if time is enabled, swap to timePicker
if (scope.enableTime) {
scope.showPicker = 'time';
} else {
scope.isOpen = false;
element[0].focus();
}
}
}
};
element.bind('input change keyup', function () {
scope.$apply(function () {
scope.date = ngModel.$modelValue;
});
});
// Outer change
ngModel.$render = function () {
var date = ngModel.$viewValue ? parseDate(ngModel.$viewValue) : null;
var display = date ? dateFilter(date, dateFormat) : '';
element.val(display);
scope.date = date;
};
var documentClickBind = function (event) {
if (scope.isOpen && event.target !== element[0]) {
scope.$apply(function () {
scope.isOpen = false;
});
}
};
var keydown = function (evt, noApply) {
scope.keydown(evt);
};
element.bind('keydown', keydown);
scope.keydown = function (evt) {
if (evt.which === 27) {
evt.preventDefault();
evt.stopPropagation();
scope.close();
} else if (evt.which === 40 && !scope.isOpen) {
scope.isOpen = true;
}
};
var reposition = function() {
scope.position = appendToBody ? $position.offset(element) : $position.position(element);
scope.position.top = scope.position.top + element.prop('offsetHeight');
if (appendToBody) {
scope.position.right = $document.width() - scope.position.left - element.parent().width();
}
};
var repositionScrollHandler = function() {
console.log('reposition');
scope.$apply(reposition);
};
scope.$watch('isOpen', function (value) {
if (value) {
scope.$broadcast('datepicker.focus');
reposition();
if (appendToBody) {
element.parents().on('scroll', repositionScrollHandler);
}
$document.bind('mousedown', documentClickBind);
} else {
$document.unbind('mousedown', documentClickBind);
if (appendToBody) {
element.parents().off('scroll', repositionScrollHandler);
}
}
});
scope.isTodayDisabled = function() {
return isDateDisabled(new Date());
};
scope.select = function (date) {
if (date === 'today' || date == 'now') {
var now = new Date();
if (angular.isDate(ngModel.$modelValue)) {
date = new Date(ngModel.$modelValue);
date.setFullYear(now.getFullYear(), now.getMonth(), now.getDate());
date.setHours(now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
} else {
date = now;
}
}
scope.dateSelection(date);
};
scope.close = function () {
scope.isOpen = false;
element[0].focus();
};
scope.changePicker = function (e) {
scope.showPicker = e;
};
var $popup = $compile(popupEl)(scope);
// Prevent jQuery cache memory leak (template is now redundant after linking)
popupEl.remove();
if (appendToBody) {
$document.find('body').append($popup);
} else {
element.after($popup);
}
scope.$on('$destroy', function () {
$popup.remove();
element.unbind('keydown', keydown);
$document.unbind('mousedown', documentClickBind);
});
}
};
}])
.directive('datePickerWrap', function () {
return {
restrict: 'EA',
replace: true,
transclude: true,
templateUrl: 'template/datetime-picker.html',
link: function (scope, element, attrs) {
element.bind('mousedown', function (event) {
event.preventDefault();
event.stopPropagation();
});
}
};
})
.directive('timePickerWrap', function () {
return {
restrict: 'EA',
replace: true,
transclude: true,
templateUrl: 'template/datetime-picker.html',
link: function (scope, element, attrs) {
element.bind('mousedown', function (event) {
event.preventDefault();
event.stopPropagation();
});
}
};
});