This repository has been archived by the owner on Aug 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.timepicker.js
220 lines (174 loc) · 5.78 KB
/
jquery.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
/* jQuery timepicker
* Replaces a single text input with a set of select boxes for hour, minute, and am/pm (optional)
*
* Original copyright (c) 2007 Jason Huck/Core Five Creative (http://www.corefive.com/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Improved by Anuj Dutta (@andhapp) by adding specs and refactoring the code to remove duplication
* and improve code standards
*
* Version 1.1.1
*
*/
;(function($){
$.fn.timepicker = function(user_options) {
var elem = this
var CSS_CLASS = 'timepicker'
var PARENT_CONTAINER_ID = 'timepicker'
var ELEMENT_CSS_CLASS = 'timepickerElement'
return this.each(function() {
var options = $.extend({}, $.timepicker.selectableValues, $.timepicker, user_options)
var timePicker = new TimePicker(elem, options)
var htmlResult = timePicker.process()
if(options.inPopup) {
var popUpContainer = createPopupContainer()
popUpContainer.append(htmlResult)
elem.focusin(function() { focusInHandler(elem, popUpContainer) })
} else {
makeHidden(this) // Explicity setting the type property
elem.parent().append(htmlResult)
}
timePicker.handleEvents()
})
function focusInHandler(elem, object) {
setPosition(elem, object)
object.fadeIn(400, function() {
$(document).bind('click', function(event) {
if(canHidePopupContainer(event)) {
object.hide()
$(document).unbind('click')
}
})
})
}
function canHidePopupContainer(event) {
function isPopupContainer(event) {
return $(event.target).attr('class') === CSS_CLASS
}
function isInputBox(event) {
return $(event.target)[0] === elem[0]
}
function isTimepickerDropdowns(event) {
return $(event.target).attr('class') && $(event.target).attr('class').indexOf(ELEMENT_CSS_CLASS) > 0
}
return (
! ( isPopupContainer(event) ||
isInputBox(event) ||
isTimepickerDropdowns(event))
)
}
function createPopupContainer() {
var popUpContainer = {}
if ($("#" + PARENT_CONTAINER_ID).length > 0) {
popUpContainer = $('#' + PARENT_CONTAINER_ID)
} else {
popUpContainer = $('<div>', {id: PARENT_CONTAINER_ID, style:'display:none; position:absolute;', 'class': CSS_CLASS})
}
$('body').append(popUpContainer)
return setPosition(elem, popUpContainer)
}
function setPosition(source, object) {
var top = source.offset().top + source.outerHeight()
var left = source.offset().left
object.css({top: top, left: left})
return object
}
function makeHidden(object) {
object.type = "hidden"
}
}
function TimePicker(elem, options) {
this.element = elem
this.options = options
this.handleEvents = function() {
var self = this
$('.timepickerElement').change(function() {
var final_value = self.hours.selectedValue() + ':' + self.minutes.selectedValue()
if(self.ampm) {
final_value += self.ampm.selectedValue()
}
self.element.val(final_value)
})
}
this.process = function() {
var result = ''
var id = this.element.attr('id')
if(this.options.clock12) {
this.hours = new Hours(id, this.options.hrs12)
this.ampm = new AmPm(id, this.options.ap)
} else {
this.hours = new Hours(id, this.options.hrs24)
}
this.minutes = new Minutes(id, this.options.mins)
result += this.hours.createDOMElementHTML()
result += this.minutes.createDOMElementHTML()
if(this.ampm) {
result += this.ampm.createDOMElementHTML()
}
return result
}
function createSelectElementHTML(id, className, source) {
var source = source.split(',')
var result = '<select id="' + className + '_' + id + '" class="' + className + ' timepickerElement">'
$.each(source, function(index, value) {
result += '<option value="' + value + '"'
result += '>' + value + '</option>'
})
result += '</select>'
return result;
}
function Hours(id, value) {
this.id = id
this.value = value
this.className = 'h'
this.createDOMElementHTML = function() {
return createSelectElementHTML(this.id, this.className, this.value)
}
this.domElement = function() {
return $('#' + this.className + '_' + this.id)
}
this.selectedValue = function() {
return this.domElement().attr('value')
}
}
function Minutes(id, value) {
this.id = id
this.value = value
this.className = 'm'
this.createDOMElementHTML = function() {
return createSelectElementHTML(this.id, this.className, this.value)
}
this.domElement = function() {
return $('#' + this.className + '_' + this.id)
}
this.selectedValue = function() {
return this.domElement().attr('value')
}
}
function AmPm(id, value) {
this.id = id
this.value = value
this.className = 'p'
this.createDOMElementHTML = function() {
return createSelectElementHTML(this.id, this.className, this.value)
}
this.domElement = function() {
return $('#' + this.className + '_' + this.id)
}
this.selectedValue = function() {
return this.domElement().attr('value')
}
}
}
$.timepicker = {
selectableValues: {
ap: "am,pm",
mins: "00,15,30,45",
hrs12: "01,02,03,04,05,06,07,08,09,10,11,12",
hrs24: "00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23",
},
clock12: true,
inPopup: false
}
})(jQuery)