root / docs / www / colonyscout / internal / jeditable / js / jquery.timepicker.js @ f59acf11
History | View | Annotate | Download (2.9 KB)
1 | f59acf11 | Dan Shope | /* jQuery timepicker
|
---|---|---|---|
2 | * replaces a single text input with a set of pulldowns to select hour, minute, and am/pm
|
||
3 | *
|
||
4 | * Copyright (c) 2007 Jason Huck/Core Five Creative (http://www.corefive.com/)
|
||
5 | * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
|
||
6 | * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
|
||
7 | *
|
||
8 | * Version 1.0
|
||
9 | */
|
||
10 | |||
11 | (function($){ |
||
12 | jQuery.fn.timepicker = function(){ |
||
13 | this.each(function(){ |
||
14 | // get the ID and value of the current element
|
||
15 | var i = this.id; |
||
16 | var v = $(this).val(); |
||
17 | |||
18 | // the options we need to generate
|
||
19 | var hrs = new Array('01','02','03','04','05','06','07','08','09','10','11','12'); |
||
20 | var mins = new Array('00','15','30','45'); |
||
21 | var ap = new Array('am','pm'); |
||
22 | |||
23 | // default to the current time
|
||
24 | var d = new Date; |
||
25 | var h = d.getHours();
|
||
26 | var m = d.getMinutes();
|
||
27 | var p = (h >= 12 ? 'pm' : 'am'); |
||
28 | |||
29 | // adjust hour to 12-hour format
|
||
30 | if(h > 12) h = h - 12; |
||
31 | |||
32 | // round minutes to nearest quarter hour
|
||
33 | for(mn in mins){ |
||
34 | if(m <= parseInt(mins[mn])){
|
||
35 | m = parseInt(mins[mn]); |
||
36 | break;
|
||
37 | } |
||
38 | } |
||
39 | |||
40 | // increment hour if we push minutes to next 00
|
||
41 | if(m > 45){ |
||
42 | m = 0;
|
||
43 | |||
44 | switch(h){
|
||
45 | case(11): |
||
46 | h += 1;
|
||
47 | p = (p == 'am' ? 'pm' : 'am'); |
||
48 | break;
|
||
49 | |||
50 | case(12): |
||
51 | h = 1;
|
||
52 | break;
|
||
53 | |||
54 | default:
|
||
55 | h += 1;
|
||
56 | break;
|
||
57 | } |
||
58 | } |
||
59 | |||
60 | // override with current values if applicable
|
||
61 | if(v.length == 7){ |
||
62 | h = parseInt(v.substr(0,2)); |
||
63 | m = parseInt(v.substr(3,2)); |
||
64 | p = v.substr(5);
|
||
65 | } |
||
66 | |||
67 | // build the new DOM objects
|
||
68 | var output = ''; |
||
69 | |||
70 | output += '<select id="h_' + i + '" class="h timepicker">'; |
||
71 | for(hr in hrs){ |
||
72 | output += '<option value="' + hrs[hr] + '"'; |
||
73 | if(parseInt(hrs[hr]) == h) output += ' selected'; |
||
74 | output += '>' + hrs[hr] + '</option>'; |
||
75 | } |
||
76 | output += '</select>';
|
||
77 | |||
78 | output += '<select id="m_' + i + '" class="m timepicker">'; |
||
79 | for(mn in mins){ |
||
80 | output += '<option value="' + mins[mn] + '"'; |
||
81 | if(parseInt(mins[mn]) == m) output += ' selected'; |
||
82 | output += '>' + mins[mn] + '</option>'; |
||
83 | } |
||
84 | output += '</select>';
|
||
85 | |||
86 | output += '<select id="p_' + i + '" class="p timepicker">'; |
||
87 | for(pp in ap){ |
||
88 | output += '<option value="' + ap[pp] + '"'; |
||
89 | if(ap[pp] == p) output += ' selected'; |
||
90 | output += '>' + ap[pp] + '</option>'; |
||
91 | } |
||
92 | output += '</select>';
|
||
93 | |||
94 | // hide original input and append new replacement inputs
|
||
95 | //$(this).attr('type','hidden').after(output);
|
||
96 | // Fix IE crash (tuupola@appelsiini.net)
|
||
97 | $(this).hide().after(output); |
||
98 | |||
99 | }); |
||
100 | |||
101 | |||
102 | $('select.timepicker').change(function(){ |
||
103 | var i = this.id.substr(2); |
||
104 | var h = $('#h_' + i).val(); |
||
105 | var m = $('#m_' + i).val(); |
||
106 | var p = $('#p_' + i).val(); |
||
107 | var v = h + ':' + m + p; |
||
108 | $('#' + i).val(v); |
||
109 | }); |
||
110 | |||
111 | return this; |
||
112 | }; |
||
113 | })(jQuery); |
||
114 | |||
115 | |||
116 | |||
117 | /* SVN: $Id: jquery.timepicker.js 456 2007-07-16 19:09:57Z Jason Huck $ */ |