-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjquery.clockpick.1.2.9.js
576 lines (518 loc) · 16 KB
/
jquery.clockpick.1.2.9.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
/*
ClockPick, by Josh Nathanson
Version 1.2.9
Timepicker plugin for jQuery
See copyright at end of file
name clockpick
type jQuery
param options hash object containing config options
param options[starthour] int starting hour (use military int)
param options[endhour] int ending hour (use military int)
param options[showminutes] bool show minutes
param options[minutedivisions] int number of divisions, i.e. 4 = :00, :15, :30, :45
param options[military] bool use 24hr time if true
param options[event] string mouse event to trigger plugin
param options[layout] string set div layout to vertical or horizontal
('vertical','horizontal')
param options[valuefield] string field to insert time value, if not same as click field
(name of input field)
param options[hoursopacity] float set opacity of hours container
param options[minutesopacity] float set opacity of minutes container
param callback function callback function - gets passed back the time value as a
string
*/
jQuery.fn.clockpick = function(options, callback) {
var settings = {
starthour : 8,
endhour : 18,
showminutes : true,
minutedivisions : 4,
military : false,
event : 'click',
layout : 'vertical',
valuefield : null,
hoursopacity : 1,
minutesopacity : 1
};
if(options) {
jQuery.extend(settings, options);
};
var callback = callback || function() { },
v = (settings.layout == 'vertical'); // boolean for vertical, shorten footprint
errorcheck();
jQuery(this)[settings.event](function(e) {
var self = this,
$self = jQuery( this ),
$body = jQuery( "body" );
if ( !settings.valuefield ) {
$self.unbind( "keydown" ).bind( "keydown", keyhandler );
}
else {
var inputfield = jQuery("[name=" + settings.valuefield + "]");
inputfield
.unbind( "keydown" )
.bind( "keydown", keyhandler)[0]
.focus();
inputfield
.bind("click", function() { inputfield.unbind("keydown"); } );
}
// clear any malingerers
jQuery("#CP_hourcont,#CP_minutecont").remove();
// append hourcont to body
// add class "CP" for mouseout recognition, although there is only
// one hourcont on the screen at a time
var $hourcont = jQuery("<div id='CP_hourcont' class='CP' />").appendTo( $body );
$hourcont.css("opacity",settings.hoursopacity);
binder( $hourcont );
var $hourcol1 = jQuery("<div class='CP_hourcol' id='hourcol1' />").appendTo( $body );
var $hourcol2 = jQuery("<div class='CP_hourcol' id='hourcol2' />").appendTo( $body );
// if showminutes, append minutes cont to body
if (settings.showminutes) {
var $mc = jQuery("<div id='CP_minutecont' class='CP' />").appendTo( $body );
$mc.css("opacity",settings.minutesopacity)
binder($mc);
}
if ( !v ) {
$hourcont.css("width","auto");
if (settings.showminutes) {
$mc.css("width","auto");
}
}
else {
$hourcol1.addClass('floatleft');
$hourcol2.addClass('floatleft');
}
// all the action right here
// fill in the hours container (minutes rendered in hour mouseover)
// then make hour container visible
renderhours();
putcontainer();
/*----------------------helper functions below-------------------------*/
function renderhours() {
// fill in the $hourcont div
var c = 1;
// counter as index 2 of hr id, gives us index
// in group of hourdivs for calculating where to put minutecont on keydown
for (var h=settings.starthour; h<=settings.endhour; h++) {
if(h==12) { c = 1; } // reset counter for col 2
var displayhours = ((!settings.military && h > 12) ? h - 12 : h);
// rectify zero hour
if (!settings.military && h == 0) {
displayhours = '12';
}
if ( settings.military && h < 10 ) {
displayhours = '0' + displayhours;
}
if ( settings.military ) {
displayhours += ':00';
}
var $hd = jQuery("<div class='CP_hour' id='hr_" + h + "_" + c + "'>" + displayhours + set_tt(h) + "</div>");
// shrink width a bit if military
binder($hd);
if (!v) {
$hd.css("float","left");
}
(h<12) ? $hourcol1.append($hd) : $hourcol2.append($hd);
c++;
}
$hourcont.append($hourcol1);
!v ? $hourcont.append("<div style='clear:left' />") : '';
$hourcont.append($hourcol2);
}
function renderminutes( h ) {
var realhours = h;
var displayhours = (!settings.military && h > 12) ? h - 12 : h;
if (!settings.military && h == 0) {
displayhours = '12';
}
if ( settings.military && h < 10 ) {
displayhours = '0' + displayhours;
}
$mc.empty();
var n = 60 / settings.minutedivisions,
tt = set_tt(realhours),
counter = 1;
for(var m=0;m<60;m=m+n) {
$md = jQuery("<div class='CP_minute' id='" + realhours + "_" + m + "'>"
+ displayhours + ":" + ((m<10) ? "0" : "") + m + tt
+ "</div>");
if ( !v ) {
$md.css("float","left");
if (settings.minutedivisions > 6
&& counter == settings.minutedivisions / 2 + 1) {
// long horizontal, kick in extra row after half
$mc.append("<div style='clear:left' />");
}
}
$mc.append($md);
binder($md);
counter++;
}
}
function set_tt(realhours) {
if (!settings.military) {
return (realhours >= 12) ? ' PM' : ' AM';
}
else {
return '';
}
}
function putcontainer() {
if ( e.type != 'focus') {
$hourcont[0].style.left = e.pageX - 5 + 'px';
$hourcont[0].style.top = e.pageY - (Math.floor($hourcont.height() / 2)) + 'px';
rectify($hourcont);
}
else {
$self.after($hourcont);
}
$hourcont.slideDown('fast');
}
function rectify($obj) {
// if a div is off the screen, move it accordingly
var ph = document.documentElement.clientHeight
? document.documentElement.clientHeight
: document.body.clientHeight;
var pw = document.documentElement.clientWidth
? document.documentElement.clientWidth
: document.body.clientWidth;
var t = parseInt( $obj[0].style.top );
var l = parseInt( $obj[0].style.left );
var st = document.documentElement.scrollTop
? document.documentElement.scrollTop
: document.body.scrollTop;
// run off top
if ( t <= st && !$obj.is("#CP_minutecont") ) {
$obj.css("top",st+10+'px');
}
else if (t + $obj.height() - st > ph) {
$obj.css("top",st + ph - $obj.height() - 10 + 'px');
}
if ( l <= 0 ) {
$obj.css("left", '10px');
}
}
function binder($obj) {
// all the binding is done here
// event handlers have been abstracted out,
// so they can handle mouse or key events
// bindings for hc (hours container)
if($obj.attr("id") == 'CP_hourcont') {
$obj.mouseout(function(e) { hourcont_out(e) });
}
// bindings for mc (minute container)
else if ($obj.attr("id") == 'CP_minutecont') {
$obj.mouseout(function(e) { minutecont_out(e) });
}
// bindings for $hd (hour divs)
else if ($obj.attr("class") == 'CP_hour') {
$obj.mouseover(function(e) { hourdiv_over($obj, e) });
$obj.mouseout(function() { hourdiv_out($obj) });
$obj.click(function() { hourdiv_click($obj) });
}
// bindings for $md (minute divs)
else if ($obj.attr("class") == 'CP_minute') {
$obj.mouseover(function() { minutediv_over($obj) });
$obj.mouseout(function() { minutediv_out($obj) });
$obj.click(function() { minutediv_click($obj) });
}
};
function hourcont_out(e) {
/*
this tells divs to clear only if rolling all the way
out of hourcont.
relatedTarget "looks ahead" to see where the mouse
has moved to on mouseOut.
IE uses the more sensible "toElement".
try/catch for Mozilla bug on relatedTarget-input field.
*/
try {
var t = (e.toElement) ? e.toElement : e.relatedTarget;
if (!(jQuery(t).is("div[class^=CP], iframe"))) {
// Safari incorrect mouseover/mouseout
//if (!jQuery.browser.safari) {
cleardivs();
//}
}
}
catch(e) {
cleardivs();
}
}
function minutecont_out(e) {
try {
var t = (e.toElement) ? e.toElement : e.relatedTarget;
if (!(jQuery(t).is("div[class^=CP], iframe"))) {
cleardivs();
}
}
catch(e) {
cleardivs();
}
}
function hourdiv_over($obj, e) {
var h = $obj.attr("id").split('_')[1],
i = $obj.attr("id").split('_')[2],
l,
t;
$obj.addClass("CP_over");
if ( settings.showminutes ) {
$mc.hide();
renderminutes(h);
// set position & show minutes container
if (v) {
t = e.type == 'mouseover'
? e.pageY - 15
: $hourcont.offset().top + 2 + ($obj.height() * i);
if ( h < 12 )
l = $hourcont.offset().left - $mc.width() - 2;
else
l = $hourcont.offset().left + $hourcont.width() + 2;
}
else {
l = (e.type == 'mouseover')
? e.pageX - 10
: $hourcont.offset().left + ($obj.width()-5) * i;
if(h<12) {
t = $hourcont.offset().top - $mc.height() - 2;
}
else {
t = $hourcont.offset().top + $hourcont.height();
}
}
$mc.css("left",l+'px').css("top",t+'px');
rectify( $mc );
$mc.show();
}
return false;
}
function hourdiv_out($obj) {
$obj.removeClass("CP_over");
return false;
}
function hourdiv_click($obj) {
var h = $obj.attr("id").split('_')[1],
tt = set_tt(h),
str = $obj.text();
if(str.indexOf(' ') != -1) {
var cleanstr = str.substring(0,str.indexOf(' '));
}
else {
var cleanstr = str;
}
$obj.text(cleanstr + ':00' + tt);
setval($obj);
cleardivs();
}
function minutediv_over($obj) {
$obj.addClass("CP_over");
return false;
}
function minutediv_out($obj) {
$obj.removeClass("CP_over");
return false;
}
function minutediv_click($obj) {
setval($obj);
cleardivs();
}
function setval($obj) { // takes either hour or minute obj
if(!settings.valuefield) {
self.value = $obj.text();
}
else {
jQuery("input[name=" + settings.valuefield + "]").val($obj.text());
}
callback.apply( $self, [ $obj.text() ]);
// unbind keydown handler, otherwise it will double-bind if
// field is activated more than once
$self.unbind( "keydown", keyhandler );
}
function cleardivs() {
if (settings.showminutes) {
$mc.hide();
}
$hourcont.slideUp('fast');
$self.unbind( "keydown", keyhandler );
}
// keyboard handling
function keyhandler( e ) {
// $obj is current active div
var $obj = $("div.CP_over").size() ? $("div.CP_over") : $("div.CP_hour:first"),
divtype = $obj.is(".CP_hour") ? 'hour' : 'minute',
hi = (divtype == 'hour') ? $obj[0].id.split('_')[2] : 0, // hour index
h = (divtype == 'minute') ? $obj[0].id.split('_')[0] : $obj[0].id.split('_')[1]; // real hour
if (divtype == 'minute')
{ var curloc = h<12 ? 'm1' : 'm2' }
else
{ var curloc = h<12 ? 'h1' : 'h2' }
function divprev($obj) {
if ($obj.prev().size()) {
eval(divtype + 'div_out($obj)');
eval(divtype + 'div_over($obj.prev(), e)');
}
else { return false; }
}
function divnext($obj) {
if ($obj.next().size()) {
eval(divtype + 'div_out($obj)');
eval(divtype + 'div_over($obj.next(), e)');
}
else { return false; }
}
function hourtohour($obj) {
var ctx = h>=12 ? '#hourcol1' : '#hourcol2';
$newobj = jQuery(".CP_hour[id$=_" + hi + "]", ctx );
if ($newobj.size()) {
hourdiv_out($obj);
hourdiv_over($newobj, e);
}
else { return false; }
}
function hourtominute($obj) {
hourdiv_out($obj);
minutediv_over($(".CP_minute:first"));
}
function minutetohour($obj) {
minutediv_out($obj);
var ctx = h>=12 ? '#hourcol2' : '#hourcol1';
// extract hour from minutediv, then find hourdiv with that hour
var $newobj = jQuery(".CP_hour[id^=hr_" + h + "]", ctx);
hourdiv_over($newobj, e);
}
switch (e.keyCode) {
case 37: // left arrow
if (v) {
switch (curloc) {
case 'm1':
return false;
break;
case 'm2':
minutetohour($obj);
break;
case 'h1':
hourtominute($obj);
break;
case 'h2':
hourtohour($obj);
break;
}
}
else {
divprev($obj);
}
break;
case 38: // up arrow
if(v) {
divprev($obj);
}
else {
switch (curloc) {
case 'm1':
return false;
break;
case 'm2':
minutetohour($obj);
break;
case 'h1':
hourtominute($obj);
break;
case 'h2':
hourtohour($obj);
break;
}
}
break;
case 39: // right arrow
if (v) {
switch (curloc) {
case 'm1':
minutetohour($obj);
break;
case 'm2':
return false;
break;
case 'h1':
hourtohour($obj);
break;
case 'h2':
hourtominute($obj);
break;
}
}
else {
divnext($obj);
}
break;
case 40: // down arrow
if(v) {
divnext($obj);
}
else {
switch (curloc) {
case 'm1':
minutetohour($obj);
break;
case 'm2':
return false;
break;
case 'h1':
hourtohour($obj);
break;
case 'h2':
hourtominute($obj);
break;
}
}
break;
case 13: // return
eval(divtype + 'div_click($obj)');
break;
default:
return true;
}
return false;
}
return false;
});
function errorcheck() {
if (settings.starthour >= settings.endhour) {
alert('Error - start hour must be less than end hour.');
return false;
}
else if (60 % settings.minutedivisions != 0) {
alert('Error - param minutedivisions must divide evenly into 60.');
return false;
}
}
return this;
}
/*
+-----------------------------------------------------------------------+
| Copyright (c) 2007-2013 Josh Nathanson |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions |
| are met: |
| |
| o Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| o Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution.|
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
+-----------------------------------------------------------------------+
*/