-
Notifications
You must be signed in to change notification settings - Fork 1
/
timepicker.js
610 lines (555 loc) · 22.8 KB
/
timepicker.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
(function ($, window, document) {
"use strict";
if (typeof String.prototype.endsWith != 'function') {
/*
* Checks if this string end ends with another string
*
* @param {string} the string to be checked
*
* @return {bool}
*/
String.prototype.endsWith = function (string) {
return string.length > 0 && this.substring(this.length - string.length, this.length) === string;
}
}
var today = new Date();
var pluginName = "timepicker",
defaults = {
now: today.getHours() + ':' + today.getMinutes(),
twentyFour: false,
upArrow: 'timepicker__controls__control-up',
downArrow: 'timepicker__controls__control-down',
close: 'timepicker__close',
hoverState: 'hover-state',
title: 'Timepicker',
showSeconds: false,
timeSeparator: ' : ',
secondsInterval: 1,
minutesInterval: 1,
beforeShow: null,
afterShow: null,
show: null,
clearable: false,
closeOnClickOutside: true,
onClickOutside: function() {},
};
/*
* @param {object} The input object the timepicker is attached to.
* @param {object} The object containing options
*/
function timepicker(element, options) {
this.element = $(element);
this.options = $.extend({}, defaults, options);
this.element.addClass('hastimepicker');
// this.element.attr('onkeypress', 'return false;');
this.element.attr('aria-showingpicker', 'false');
this.createPicker();
this.timepicker = $('.timepicker');
this.up = $('.' + this.options.upArrow.split(/\s+/).join('.'));
this.down = $('.' + this.options.downArrow.split(/\s+/).join('.'));
this.separator = $('.timepicker__controls__control--separator');
this.hoursElem = $('.timepicker__controls__control--hours');
this.minutesElem = $('.timepicker__controls__control--minutes');
this.secondsElem = $('.timepicker__controls__control--seconds');
this.meridiemElem = $('.timepicker__controls__control--meridiem');
this.close = $('.' + this.options.close.split(/\s+/).join('.'));
//Create a new Date object based on the default or passing in now value
var time = this.timeArrayFromString(this.options.now);
this.options.now = new Date(today.getFullYear(), today.getMonth(), today.getDate(), time[0], time[1], time[2]);
this.selectedHour = this.parseHours(this.options.now.getHours());
this.selectedMin = this.parseSecMin(this.options.now.getMinutes());
this.selectedSec = this.parseSecMin(this.options.now.getSeconds());
this.selectedMeridiem = this.parseMeridiem(this.options.now.getHours());
this.setHoverState();
this.attach(element);
this.setText(element);
}
$.extend(timepicker.prototype, {
/*
* Show given input's timepicker
*
* @param {object} The input being clicked
*/
showPicker: function (element) {
//If there is a beforeShow function, then call it with the input calling the timepicker and the
// timepicker itself
if (typeof this.options.beforeShow === 'function') {
this.options.beforeShow(element, this.timepicker);
}
var timepickerPos = $(element).offset();
$(element).attr({'aria-showingpicker': 'true', 'tabindex': -1});
this.setText(element);
this.showHideMeridiemControl();
if (this.getText(element) !== this.getTime()) {
// Check meridiem
var text = this.getText(element);
var re = /\s[ap]m$/i;
var meridiem = re.test(text) ? text.substr(-2, 2) : null;
var inputTime = text.replace(re, '').split(this.options.timeSeparator);
var newTime = {};
newTime.hours = inputTime[0];
newTime.minutes = inputTime[1];
newTime.meridiem = meridiem;
if (this.options.showSeconds) {
newTime.seconds = inputTime[2];
}
this.setTime(newTime);
}
this.timepicker.css({
'z-index': this.element.css('z-index') + 1,
position: 'absolute',
left: timepickerPos.left,
top: timepickerPos.top + $(element)[0].offsetHeight
}).show();
//If there is a show function, then call it with the input calling the timepicker and the
// timepicker itself
if (typeof this.options.show === 'function') {
this.options.show(element, this.timepicker);
}
this.handleTimeAdjustments(element);
},
/*
* Hides the timepicker that is currently shown if it is not part of the timepicker
*
* @param {Object} The DOM object being clicked on the page
*
* BeinnLora: added trigger function to call on closing/hiding timepicker.
*/
hideTimepicker: function (element) {
this.timepicker.hide();
if (typeof this.options.afterShow === 'function') {
this.options.afterShow(element, this.timepicker);
}
var pickerHidden = {
start: function () {
var setShowPickerFalse = $.Deferred();
$('[aria-showingpicker="true"]').attr('aria-showingpicker', 'false');
return setShowPickerFalse.promise();
}
};
function setTabIndex(index) {
setTimeout(function () {
$('[aria-showingpicker="false"]').attr('tabindex', index);
}, 400);
}
pickerHidden.start().then(setTabIndex(0));
},
/*
* Create a new timepicker. A single timepicker per page
*/
createPicker: function () {
if ($('.timepicker').length === 0) {
var picker = '<div class="timepicker"><ul class="timepicker__controls"><li class="timepicker__controls__control"><span class="' + this.options.upArrow + '"></span><span class="timepicker__controls__control--hours" tabindex="-1">00</span><span class="' + this.options.downArrow + '"></span></li><li class="timepicker__controls__control--separator"><span class="timepicker__controls__control--separator-inner">:</span></li><li class="timepicker__controls__control"><span class="' + this.options.upArrow + '"></span><span class="timepicker__controls__control--minutes" tabindex="-1">00</span><span class="' + this.options.downArrow + '"></span></li>';
if (this.options.showSeconds) {
picker += '<li class="timepicker__controls__control--separator"><span class="timepicker__controls__control--separator-inner">:</span></li><li class="timepicker__controls__control"><span class="' + this.options.upArrow + '"></span><span class="timepicker__controls__control--seconds" tabindex="-1">00</span><span class="' + this.options.downArrow + '"></span> </li>';
}
picker += '<li class="timepicker__controls__control"><span class="' + this.options.upArrow + '"></span><span class="timepicker__controls__control--meridiem" tabindex="-1">AM</span><span class="' + this.options.downArrow + '"></span></li></ul></div>';
$('body').append(picker);
// this.attachKeyboardEvents();
}
},
/*
* Hides the meridiem control if this timepicker is a 24 hour clock
*/
showHideMeridiemControl: function () {
if (this.options.twentyFour === false) {
$(this.meridiemElem).parent().show();
}
else {
$(this.meridiemElem).parent().hide();
}
},
/*
* Hides the seconds control if this timepicker has showSeconds set to true
*/
showHideSecondsControl: function () {
if (this.options.showSeconds) {
$(this.secondsElem).parent().show();
}
else {
$(this.secondsElem).parent().hide();
}
},
/*
* Bind the click events to the input
*
* @param {object} The input element
*/
attach: function (element) {
var self = this;
if (this.options.clearable) {
self.makePickerInputClearable(element);
}
$(element).attr('tabindex', 0);
$(element).on('click focus', function (event) {
//Prevent multiple firings
if ($(self.timepicker).is(':hidden')) {
self.showPicker($(this));
window.lastTimePickerControl = $(this); //Put the reference on this timepicker into global scope for unsing that in afterShow function
$(self.hoursElem).focus();
}
});
//Handle click events for closing timepicker
var clickHandler = function (event) { //TODO: Fix double firing
//Only fire the hide event when you have to
if ($(self.timepicker).is(':visible')) {
//Clicking the X
if ($(event.target).is(self.close)) {
self.hideTimepicker(window.lastTimePickerControl);
} else if ($(event.target).closest(self.timepicker).length || $(event.target).closest($('.hastimepicker')).length) { //Clicking the timepicker or one of it's inputs
event.stopPropagation();
} else { //Everything else
if (typeof self.options.onClickOutside === 'function') {
self.options.onClickOutside();
}
else {
console.warn("Type of onClickOutside must be a function");
}
if (!self.options.closeOnClickOutside) {
return;
}
self.hideTimepicker(window.lastTimePickerControl);
}
window.lastTimePickerControl = null;
}
};
$(document).off('click', clickHandler).on('click', clickHandler);
},
/**
* Added keyboard functionality to improve usabil
*/
attachKeyboardEvents: function () {
$(document).on('keydown', $.proxy(function (event) {
switch (event.keyCode) {
case 9:
if (event.target.className !== 'hastimepicker') {
$(this.close).trigger('click');
}
break;
case 27:
$(this.close).trigger('click');
break;
case 37: //Left arrow
if (event.target.className !== this.hoursElem[0].className) {
$(event.target).parent().prevAll('li').not(this.separator.selector).first().children()[1].focus();
} else {
$(event.target).parent().siblings(':last').children()[1].focus();
}
break;
case 39: //Right arrow
if (event.target.className !== this.meridiemElem[0].className) {
$(event.target).parent().nextAll('li').not(this.separator.selector).first().children()[1].focus();
} else {
$(event.target).parent().siblings(':first').children()[1].focus();
}
break;
case 38: //Up arrow
$(':focus').prev().trigger('click');
break;
case 40: //Down arrow
$(':focus').next().trigger('click');
break;
default:
break;
}
}, this));
},
/*
* Set the time on the timepicker
*
* @param {object} The date being set
*/
setTime: function (time) {
this.setHours(time.hours);
this.setMinutes(time.minutes);
this.setMeridiem(time.meridiem);
if (this.options.showSeconds) {
this.setSeconds(time.seconds);
}
},
/*
* Get the time from the timepicker
*/
getTime: function () {
return [this.formatTime(this.getHours(), this.getMinutes(), this.getMeridiem(), this.getSeconds())];
},
/*
* Set the timpicker's hour(s) value
*
* @param {string} hours
*/
setHours: function (hours) {
var hour = new Date();
hour.setHours(hours);
var hoursText = this.parseHours(hour.getHours());
this.hoursElem.text(hoursText);
this.selectedHour = hoursText;
},
/*
* Get the hour(s) value from the timepicker
*
* @return {integer}
*/
getHours: function () {
var hours = new Date();
hours.setHours(this.hoursElem.text());
return hours.getHours();
},
/*
* Returns the correct hour value based on the type of clock, 12 or 24 hour
*
* @param {integer} The hours value before parsing
*
* @return {string|integer}
*/
parseHours: function (hours) {
return (this.options.twentyFour === false) ? ((hours + 11) % 12) + 1 : (hours < 10) ? '0' + hours : hours;
},
/*
* Sets the timpicker's minutes value
*
* @param {string} minutes
*/
setMinutes: function (minutes) {
var minute = new Date();
minute.setMinutes(minutes);
var minutesText = minute.getMinutes();
var min = this.parseSecMin(minutesText);
this.minutesElem.text(min);
this.selectedMin = min;
},
/*
* Get the minutes value from the timepicker
*
* @return {integer}
*/
getMinutes: function () {
var minutes = new Date();
minutes.setMinutes(this.minutesElem.text());
return minutes.getMinutes();
},
/*
* Return a human-readable minutes/seconds value
*
* @param {string} value seconds or minutes
*
* @return {string|integer}
*/
parseSecMin: function (value) {
return ((value < 10) ? '0' : '') + value;
},
/*
* Set the timepicker's meridiem value, AM or PM
*
* @param {string} The new meridiem
*/
setMeridiem: function (inputMeridiem) {
var newMeridiem = '';
if (inputMeridiem === undefined) {
var meridiem = this.getMeridiem();
newMeridiem = (meridiem === 'PM') ? 'AM' : 'PM';
} else {
newMeridiem = inputMeridiem;
}
this.meridiemElem.text(newMeridiem);
this.selectedMeridiem = newMeridiem;
},
/*
* Get the timepicker's meridiem value, AM or PM
*
* @return {string}
*/
getMeridiem: function () {
return this.meridiemElem.text();
},
/*
* Set the timepicker's seconds value
*
* @param {string} seconds
*/
setSeconds: function (seconds) {
var second = new Date();
second.setSeconds(seconds);
var secondsText = second.getSeconds();
var sec = this.parseSecMin(secondsText);
this.secondsElem.text(sec);
this.selectedSec = sec;
},
/*
* Get the timepicker's seconds value
*
* return {string}
*/
getSeconds: function () {
var seconds = new Date();
seconds.setSeconds(this.secondsElem.text());
return seconds.getSeconds();
},
/*
* Get the correct meridiem based on the hours given
*
* @param {string|integer} hours
*
* @return {string}
*/
parseMeridiem: function (hours) {
return (hours > 11) ? 'PM' : 'AM';
},
/*
* Handles time incrementing and decrementing and passes
* the operator, '+' or '-', the input to be set after the change
* and the current arrow clicked, to decipher if hours, ninutes, or meridiem.
*
* @param {object} The input element
*/
handleTimeAdjustments: function (element) {
var timeOut = 0;
//Click and click and hold timepicker incrementer and decrementer
$(this.up).add(this.down).off('mousedown click touchstart').on('mousedown click', {
'timepicker': this,
'input': element
}, function (event) {
if(event.which!=1) return false;
var operator = (this.className.indexOf('up') > -1) ? '+' : '-';
var passedData = event.data;
if (event.type == 'mousedown') {
timeOut = setInterval($.proxy(function (args) {
args.timepicker.changeValue(operator, args.input, this);
}, this, {'timepicker': passedData.timepicker, 'input': passedData.input}), 200);
} else {
passedData.timepicker.changeValue(operator, passedData.input, this);
}
}).bind('mouseup touchend', function () {
clearInterval(timeOut);
});
},
/*
* Change the timepicker's time base on what is clicked
*
* @param {string} The + or - operator
* @param {object} The timepicker's associated input to be set post change
* @param {object} The DOM arrow object clicked, determines if it is hours,
* minutes, or meridiem base on the operator and its siblings
*/
changeValue: function (operator, input, clicked) {
var target = (operator === '+') ? clicked.nextSibling : clicked.previousSibling;
var targetClass = $(target).attr('class');
if (targetClass.endsWith('hours')) {
this.setHours(eval(this.getHours() + operator + 1));
} else if (targetClass.endsWith('minutes')) {
this.setMinutes(eval(this.getMinutes() + operator + this.options.minutesInterval));
} else if (targetClass.endsWith('seconds')) {
this.setSeconds(eval(this.getSeconds() + operator + this.options.secondsInterval));
} else {
this.setMeridiem();
}
this.setText(input);
},
/*
* Sets the give input's text to the current timepicker's time
*
* @param {object} The input element
*/
setText: function (input) {
$(input).val(this.formatTime(this.selectedHour, this.selectedMin, this.selectedMeridiem, this.selectedSec)).change();
},
/*
* Get the given input's value
*
* @param {object} The input element
*
* @return {string}
*/
getText: function (input) {
return $(input).val();
},
/*
* Returns the correct time format as a string
*
* @param {string} hour
* @param {string} minutes
* @param {string} meridiem
*
* @return {string}
*/
formatTime: function (hour, min, meridiem, seconds) {
var formattedTime = hour + this.options.timeSeparator + min;
if (this.options.showSeconds) {
formattedTime += this.options.timeSeparator + seconds;
}
if (this.options.twentyFour === false) {
formattedTime += ' ' + meridiem;
}
return formattedTime;
},
/**
* Apply the hover class to the arrows and close icon fonts
*/
setHoverState: function () {
var self = this;
$(this.up).add(this.down).add(this.close).hover(function () {
$(this).toggleClass(self.options.hoverState);
});
},
/**
* Wrapping the given input field with the clearable container
* , add a span that will contain the x, and bind the clear
* input event to the span
*
* @param input
*/
makePickerInputClearable: function(input) {
$(input).wrap('<div class="clearable-picker"></div>').after('<span data-clear-picker>×</span>');
//When the x is clicked, clear its sibling input field
$('[data-clear-picker]').on('click', function(event) {
$(this).siblings('.hastimepicker').val('');
});
},
/**
* Convert the options time string format
* to an array
*
* returns => [hours, minutes, seconds]
*
* @param stringTime
* @returns {*}
*/
timeArrayFromString: function (stringTime) {
if (stringTime.length) {
var time = stringTime.split(':');
time[2] = (time.length < 3) ? '00' : time[2];
return time;
}
return false;
},
//public functions
/*
* Returns the requested input element's value
*/
_time: function () {
var inputValue = $(this.element).val();
return (inputValue === '') ? this.formatTime(this.selectedHour, this.selectedMin, this.selectedMeridiem, this.selectedSec) : inputValue;
},
_hide: function() {
this.hideTimepicker(this.element);
}
});
//optional index if multiple inputs share the same class
$.fn[pluginName] = function (options, index) {
if (!$.isFunction(timepicker.prototype['_' + options])) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new timepicker(this, options));
}
});
}
else if ($(this).hasClass('hastimepicker')) {
if (index !== undefined) {
return $.data($(this)[index], 'plugin_' + pluginName)['_' + options]();
}
else {
return $.data($(this)[0], 'plugin_' + pluginName)['_' + options]();
}
}
};
})(jQuery, window, document);