forked from mgcrea/angular-strap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimepicker.js
More file actions
474 lines (473 loc) · 20.1 KB
/
timepicker.js
File metadata and controls
474 lines (473 loc) · 20.1 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
* angular-strap
* @version v2.3.8 - 2016-03-31
* @link http://mgcrea.github.io/angular-strap
* @author Olivier Louvignes <olivier@mg-crea.com> (https://github.com/mgcrea)
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
'use strict';
angular.module('mgcrea.ngStrap.timepicker', [ 'mgcrea.ngStrap.helpers.dateParser', 'mgcrea.ngStrap.helpers.dateFormatter', 'mgcrea.ngStrap.tooltip' ]).provider('$timepicker', function() {
var defaults = this.defaults = {
animation: 'am-fade',
prefixClass: 'timepicker',
placement: 'bottom-left',
templateUrl: 'timepicker/timepicker.tpl.html',
trigger: 'focus',
container: false,
keyboard: true,
html: false,
delay: 0,
useNative: true,
timeType: 'date',
timeFormat: 'shortTime',
timezone: null,
modelTimeFormat: null,
autoclose: false,
minTime: -Infinity,
maxTime: +Infinity,
length: 5,
hourStep: 1,
minuteStep: 5,
secondStep: 5,
roundDisplay: false,
iconUp: 'glyphicon glyphicon-chevron-up',
iconDown: 'glyphicon glyphicon-chevron-down',
arrowBehavior: 'pager'
};
this.$get = [ '$window', '$document', '$rootScope', '$sce', '$dateFormatter', '$tooltip', '$timeout', function($window, $document, $rootScope, $sce, $dateFormatter, $tooltip, $timeout) {
var isNative = /(ip[ao]d|iphone|android)/gi.test($window.navigator.userAgent);
var isTouch = 'createTouch' in $window.document && isNative;
if (!defaults.lang) {
defaults.lang = $dateFormatter.getDefaultLocale();
}
function timepickerFactory(element, controller, config) {
var $timepicker = $tooltip(element, angular.extend({}, defaults, config));
var parentScope = config.scope;
var options = $timepicker.$options;
var scope = $timepicker.$scope;
var lang = options.lang;
var formatDate = function(date, format, timezone) {
return $dateFormatter.formatDate(date, format, lang, timezone);
};
function floorMinutes(time) {
var coeff = 1e3 * 60 * options.minuteStep;
return new Date(Math.floor(time.getTime() / coeff) * coeff);
}
var selectedIndex = 0;
var defaultDate = options.roundDisplay ? floorMinutes(new Date()) : new Date();
var startDate = controller.$dateValue || defaultDate;
var viewDate = {
hour: startDate.getHours(),
meridian: startDate.getHours() < 12,
minute: startDate.getMinutes(),
second: startDate.getSeconds(),
millisecond: startDate.getMilliseconds()
};
var format = $dateFormatter.getDatetimeFormat(options.timeFormat, lang);
var hoursFormat = $dateFormatter.hoursFormat(format);
var timeSeparator = $dateFormatter.timeSeparator(format);
var minutesFormat = $dateFormatter.minutesFormat(format);
var secondsFormat = $dateFormatter.secondsFormat(format);
var showSeconds = $dateFormatter.showSeconds(format);
var showAM = $dateFormatter.showAM(format);
scope.$iconUp = options.iconUp;
scope.$iconDown = options.iconDown;
scope.$select = function(date, index) {
$timepicker.select(date, index);
};
scope.$moveIndex = function(value, index) {
$timepicker.$moveIndex(value, index);
};
scope.$switchMeridian = function(date) {
$timepicker.switchMeridian(date);
};
$timepicker.update = function(date) {
if (angular.isDate(date) && !isNaN(date.getTime())) {
$timepicker.$date = date;
angular.extend(viewDate, {
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds(),
millisecond: date.getMilliseconds()
});
$timepicker.$build();
} else if (!$timepicker.$isBuilt) {
$timepicker.$build();
}
};
$timepicker.select = function(date, index, keep) {
if (!controller.$dateValue || isNaN(controller.$dateValue.getTime())) controller.$dateValue = new Date(1970, 0, 1);
if (!angular.isDate(date)) date = new Date(date);
if (index === 0) controller.$dateValue.setHours(date.getHours()); else if (index === 1) controller.$dateValue.setMinutes(date.getMinutes()); else if (index === 2) controller.$dateValue.setSeconds(date.getSeconds());
controller.$setViewValue(angular.copy(controller.$dateValue));
controller.$render();
if (options.autoclose && !keep) {
$timeout(function() {
$timepicker.hide(true);
});
}
};
$timepicker.switchMeridian = function(date) {
if (!controller.$dateValue || isNaN(controller.$dateValue.getTime())) {
return;
}
var hours = (date || controller.$dateValue).getHours();
controller.$dateValue.setHours(hours < 12 ? hours + 12 : hours - 12);
controller.$setViewValue(angular.copy(controller.$dateValue));
controller.$render();
};
$timepicker.$build = function() {
var i;
var midIndex = scope.midIndex = parseInt(options.length / 2, 10);
var hours = [];
var hour;
for (i = 0; i < options.length; i++) {
hour = new Date(1970, 0, 1, viewDate.hour - (midIndex - i) * options.hourStep);
hours.push({
date: hour,
label: formatDate(hour, hoursFormat),
selected: $timepicker.$date && $timepicker.$isSelected(hour, 0),
disabled: $timepicker.$isDisabled(hour, 0)
});
}
var minutes = [];
var minute;
for (i = 0; i < options.length; i++) {
minute = new Date(1970, 0, 1, 0, viewDate.minute - (midIndex - i) * options.minuteStep);
minutes.push({
date: minute,
label: formatDate(minute, minutesFormat),
selected: $timepicker.$date && $timepicker.$isSelected(minute, 1),
disabled: $timepicker.$isDisabled(minute, 1)
});
}
var seconds = [];
var second;
for (i = 0; i < options.length; i++) {
second = new Date(1970, 0, 1, 0, 0, viewDate.second - (midIndex - i) * options.secondStep);
seconds.push({
date: second,
label: formatDate(second, secondsFormat),
selected: $timepicker.$date && $timepicker.$isSelected(second, 2),
disabled: $timepicker.$isDisabled(second, 2)
});
}
var rows = [];
for (i = 0; i < options.length; i++) {
if (showSeconds) {
rows.push([ hours[i], minutes[i], seconds[i] ]);
} else {
rows.push([ hours[i], minutes[i] ]);
}
}
scope.rows = rows;
scope.showSeconds = showSeconds;
scope.showAM = showAM;
scope.isAM = ($timepicker.$date || hours[midIndex].date).getHours() < 12;
scope.timeSeparator = timeSeparator;
$timepicker.$isBuilt = true;
};
$timepicker.$isSelected = function(date, index) {
if (!$timepicker.$date) return false; else if (index === 0) {
return date.getHours() === $timepicker.$date.getHours();
} else if (index === 1) {
return date.getMinutes() === $timepicker.$date.getMinutes();
} else if (index === 2) {
return date.getSeconds() === $timepicker.$date.getSeconds();
}
};
$timepicker.$isDisabled = function(date, index) {
var selectedTime;
if (index === 0) {
selectedTime = date.getTime() + viewDate.minute * 6e4 + viewDate.second * 1e3;
} else if (index === 1) {
selectedTime = date.getTime() + viewDate.hour * 36e5 + viewDate.second * 1e3;
} else if (index === 2) {
selectedTime = date.getTime() + viewDate.hour * 36e5 + viewDate.minute * 6e4;
}
return selectedTime < options.minTime * 1 || selectedTime > options.maxTime * 1;
};
scope.$arrowAction = function(value, index) {
if (options.arrowBehavior === 'picker') {
$timepicker.$setTimeByStep(value, index);
} else {
$timepicker.$moveIndex(value, index);
}
};
$timepicker.$setTimeByStep = function(value, index) {
var newDate = new Date($timepicker.$date || startDate);
var hours = newDate.getHours();
var minutes = newDate.getMinutes();
var seconds = newDate.getSeconds();
if (index === 0) {
newDate.setHours(hours - parseInt(options.hourStep, 10) * value);
} else if (index === 1) {
newDate.setMinutes(minutes - parseInt(options.minuteStep, 10) * value);
} else if (index === 2) {
newDate.setSeconds(seconds - parseInt(options.secondStep, 10) * value);
}
$timepicker.select(newDate, index, true);
};
$timepicker.$moveIndex = function(value, index) {
var targetDate;
if (index === 0) {
targetDate = new Date(1970, 0, 1, viewDate.hour + value * options.length, viewDate.minute, viewDate.second);
angular.extend(viewDate, {
hour: targetDate.getHours()
});
} else if (index === 1) {
targetDate = new Date(1970, 0, 1, viewDate.hour, viewDate.minute + value * options.length * options.minuteStep, viewDate.second);
angular.extend(viewDate, {
minute: targetDate.getMinutes()
});
} else if (index === 2) {
targetDate = new Date(1970, 0, 1, viewDate.hour, viewDate.minute, viewDate.second + value * options.length * options.secondStep);
angular.extend(viewDate, {
second: targetDate.getSeconds()
});
}
$timepicker.$build();
};
$timepicker.$onMouseDown = function(evt) {
if (evt.target.nodeName.toLowerCase() !== 'input') evt.preventDefault();
evt.stopPropagation();
if (isTouch) {
var targetEl = angular.element(evt.target);
if (targetEl[0].nodeName.toLowerCase() !== 'button') {
targetEl = targetEl.parent();
}
targetEl.triggerHandler('click');
}
};
$timepicker.$onKeyDown = function(evt) {
if (!/(38|37|39|40|13)/.test(evt.keyCode) || evt.shiftKey || evt.altKey) return;
evt.preventDefault();
evt.stopPropagation();
if (evt.keyCode === 13) {
$timepicker.hide(true);
return;
}
var newDate = new Date($timepicker.$date);
var hours = newDate.getHours();
var hoursLength = formatDate(newDate, hoursFormat).length;
var minutes = newDate.getMinutes();
var minutesLength = formatDate(newDate, minutesFormat).length;
var seconds = newDate.getSeconds();
var secondsLength = formatDate(newDate, secondsFormat).length;
var sepLength = 1;
var lateralMove = /(37|39)/.test(evt.keyCode);
var count = 2 + showSeconds * 1 + showAM * 1;
if (lateralMove) {
if (evt.keyCode === 37) selectedIndex = selectedIndex < 1 ? count - 1 : selectedIndex - 1; else if (evt.keyCode === 39) selectedIndex = selectedIndex < count - 1 ? selectedIndex + 1 : 0;
}
var selectRange = [ 0, hoursLength ];
var incr = 0;
if (evt.keyCode === 38) incr = -1;
if (evt.keyCode === 40) incr = +1;
var isSeconds = selectedIndex === 2 && showSeconds;
var isMeridian = selectedIndex === 2 && !showSeconds || selectedIndex === 3 && showSeconds;
if (selectedIndex === 0) {
newDate.setHours(hours + incr * parseInt(options.hourStep, 10));
hoursLength = formatDate(newDate, hoursFormat).length;
selectRange = [ 0, hoursLength ];
} else if (selectedIndex === 1) {
newDate.setMinutes(minutes + incr * parseInt(options.minuteStep, 10));
minutesLength = formatDate(newDate, minutesFormat).length;
selectRange = [ hoursLength + sepLength, minutesLength ];
} else if (isSeconds) {
newDate.setSeconds(seconds + incr * parseInt(options.secondStep, 10));
secondsLength = formatDate(newDate, secondsFormat).length;
selectRange = [ hoursLength + sepLength + minutesLength + sepLength, secondsLength ];
} else if (isMeridian) {
if (!lateralMove) $timepicker.switchMeridian();
selectRange = [ hoursLength + sepLength + minutesLength + sepLength + (secondsLength + sepLength) * showSeconds, 2 ];
}
$timepicker.select(newDate, selectedIndex, true);
createSelection(selectRange[0], selectRange[1]);
parentScope.$digest();
};
function createSelection(start, length) {
var end = start + length;
if (element[0].createTextRange) {
var selRange = element[0].createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end);
selRange.select();
} else if (element[0].setSelectionRange) {
element[0].setSelectionRange(start, end);
} else if (angular.isUndefined(element[0].selectionStart)) {
element[0].selectionStart = start;
element[0].selectionEnd = end;
}
}
function focusElement() {
element[0].focus();
}
var _init = $timepicker.init;
$timepicker.init = function() {
if (isNative && options.useNative) {
element.prop('type', 'time');
element.css('-webkit-appearance', 'textfield');
return;
} else if (isTouch) {
element.prop('type', 'text');
element.attr('readonly', 'true');
element.on('click', focusElement);
}
_init();
};
var _destroy = $timepicker.destroy;
$timepicker.destroy = function() {
if (isNative && options.useNative) {
element.off('click', focusElement);
}
_destroy();
};
var _show = $timepicker.show;
$timepicker.show = function() {
if (!isTouch && element.attr('readonly') || element.attr('disabled')) return;
_show();
$timeout(function() {
if ($timepicker.$element) $timepicker.$element.on(isTouch ? 'touchstart' : 'mousedown', $timepicker.$onMouseDown);
if (options.keyboard) {
if (element) element.on('keydown', $timepicker.$onKeyDown);
}
}, 0, false);
};
var _hide = $timepicker.hide;
$timepicker.hide = function(blur) {
if (!$timepicker.$isShown) return;
if ($timepicker.$element) $timepicker.$element.off(isTouch ? 'touchstart' : 'mousedown', $timepicker.$onMouseDown);
if (options.keyboard) {
if (element) element.off('keydown', $timepicker.$onKeyDown);
}
_hide(blur);
};
return $timepicker;
}
timepickerFactory.defaults = defaults;
return timepickerFactory;
} ];
}).directive('bsTimepicker', [ '$window', '$parse', '$q', '$dateFormatter', '$dateParser', '$timepicker', function($window, $parse, $q, $dateFormatter, $dateParser, $timepicker) {
var defaults = $timepicker.defaults;
var isNative = /(ip[ao]d|iphone|android)/gi.test($window.navigator.userAgent);
return {
restrict: 'EAC',
require: 'ngModel',
link: function postLink(scope, element, attr, controller) {
var options = {
scope: scope
};
angular.forEach([ 'template', 'templateUrl', 'controller', 'controllerAs', 'placement', 'container', 'delay', 'trigger', 'keyboard', 'html', 'animation', 'autoclose', 'timeType', 'timeFormat', 'timezone', 'modelTimeFormat', 'useNative', 'hourStep', 'minuteStep', 'secondStep', 'length', 'arrowBehavior', 'iconUp', 'iconDown', 'roundDisplay', 'id', 'prefixClass', 'prefixEvent' ], function(key) {
if (angular.isDefined(attr[key])) options[key] = attr[key];
});
var falseValueRegExp = /^(false|0|)$/i;
angular.forEach([ 'html', 'container', 'autoclose', 'useNative', 'roundDisplay' ], function(key) {
if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) {
options[key] = false;
}
});
if (isNative && (options.useNative || defaults.useNative)) options.timeFormat = 'HH:mm';
var timepicker = $timepicker(element, controller, options);
options = timepicker.$options;
var lang = options.lang;
var formatDate = function(date, format, timezone) {
return $dateFormatter.formatDate(date, format, lang, timezone);
};
if (attr.bsShow) {
scope.$watch(attr.bsShow, function(newValue, oldValue) {
if (!timepicker || !angular.isDefined(newValue)) return;
if (angular.isString(newValue)) newValue = !!newValue.match(/true|,?(timepicker),?/i);
if (newValue === true) {
timepicker.show();
} else {
timepicker.hide();
}
});
}
var dateParser = $dateParser({
format: options.timeFormat,
lang: lang
});
angular.forEach([ 'minTime', 'maxTime' ], function(key) {
if (angular.isDefined(attr[key])) {
attr.$observe(key, function(newValue) {
timepicker.$options[key] = dateParser.getTimeForAttribute(key, newValue);
if (!isNaN(timepicker.$options[key])) timepicker.$build();
validateAgainstMinMaxTime(controller.$dateValue);
});
}
});
scope.$watch(attr.ngModel, function(newValue, oldValue) {
timepicker.update(controller.$dateValue);
}, true);
function validateAgainstMinMaxTime(parsedTime) {
if (!angular.isDate(parsedTime)) return;
var isMinValid = isNaN(options.minTime) || new Date(parsedTime.getTime()).setFullYear(1970, 0, 1) >= options.minTime;
var isMaxValid = isNaN(options.maxTime) || new Date(parsedTime.getTime()).setFullYear(1970, 0, 1) <= options.maxTime;
var isValid = isMinValid && isMaxValid;
controller.$setValidity('date', isValid);
controller.$setValidity('min', isMinValid);
controller.$setValidity('max', isMaxValid);
if (!isValid) {
return;
}
controller.$dateValue = parsedTime;
}
controller.$parsers.unshift(function(viewValue) {
var date;
if (!viewValue) {
controller.$setValidity('date', true);
return null;
}
var parsedTime = angular.isDate(viewValue) ? viewValue : dateParser.parse(viewValue, controller.$dateValue);
if (!parsedTime || isNaN(parsedTime.getTime())) {
controller.$setValidity('date', false);
return undefined;
}
validateAgainstMinMaxTime(parsedTime);
if (options.timeType === 'string') {
date = dateParser.timezoneOffsetAdjust(parsedTime, options.timezone, true);
return formatDate(date, options.modelTimeFormat || options.timeFormat);
}
date = dateParser.timezoneOffsetAdjust(controller.$dateValue, options.timezone, true);
if (options.timeType === 'number') {
return date.getTime();
} else if (options.timeType === 'unix') {
return date.getTime() / 1e3;
} else if (options.timeType === 'iso') {
return date.toISOString();
}
return new Date(date);
});
controller.$formatters.push(function(modelValue) {
var date;
if (angular.isUndefined(modelValue) || modelValue === null) {
date = NaN;
} else if (angular.isDate(modelValue)) {
date = modelValue;
} else if (options.timeType === 'string') {
date = dateParser.parse(modelValue, null, options.modelTimeFormat);
} else if (options.timeType === 'unix') {
date = new Date(modelValue * 1e3);
} else {
date = new Date(modelValue);
}
controller.$dateValue = dateParser.timezoneOffsetAdjust(date, options.timezone);
return getTimeFormattedString();
});
controller.$render = function() {
element.val(getTimeFormattedString());
};
function getTimeFormattedString() {
return !controller.$dateValue || isNaN(controller.$dateValue.getTime()) ? '' : formatDate(controller.$dateValue, options.timeFormat);
}
scope.$on('$destroy', function() {
if (timepicker) timepicker.destroy();
options = null;
timepicker = null;
});
}
};
} ]);