-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimingfield.js
263 lines (237 loc) · 10.3 KB
/
timingfield.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
(function( $ ) {
var TimingField = function(element, options)
{
this.elem = $(element);
this.disabled = false;
this.settings = $.extend({}, $.fn.timingfield.defaults, options);
this.tpl = $($.fn.timingfield.template);
this.init();
};
TimingField.prototype = {
init: function () {
// remove the secopnds block if not wanted
if (!this.settings.hasSeconds) {
this.tpl.find('.timingfield_seconds').remove();
}
this.elem.after(this.tpl);
this.elem.hide();
var timeoutId = 0;
if (this.elem.is(':disabled')) {
this.disable();
}
this.getHours().value = this.tsToHours(this.elem.val());
this.getMinutes().value = this.tsToMinutes(this.elem.val());
if (this.settings.hasSeconds) {
this.getSeconds().value = this.tsToSeconds(this.elem.val());
}
this.tpl.width(this.settings.width);
this.tpl.find('.timingfield_hours .input-group-addon').text(this.settings.hoursText);
this.tpl.find('.timingfield_minutes .input-group-addon').text(this.settings.minutesText);
this.tpl.find('.timingfield_seconds .input-group-addon').text(this.settings.secondsText);
// +/- triggers
this.tpl.find('.timingfield_hours .timingfield_next').on('mousedown', $.proxy(this.upHour, this));
this.tpl.find('.timingfield_hours .timingfield_prev').on('mousedown', $.proxy(this.downHour, this));
this.tpl.find('.timingfield_minutes .timingfield_next').on('mousedown', $.proxy(this.upMin, this));
this.tpl.find('.timingfield_minutes .timingfield_prev').on('mousedown', $.proxy(this.downMin, this));
this.tpl.find('.timingfield_seconds .timingfield_next').on('mousedown', $.proxy(this.upSec, this));
this.tpl.find('.timingfield_seconds .timingfield_prev').on('mousedown', $.proxy(this.downSec, this));
// input triggers
this.tpl.find('.timingfield_hours input').on('keyup', $.proxy(this.inputHour, this));
this.tpl.find('.timingfield_minutes input').on('keyup', $.proxy(this.inputMin, this));
this.tpl.find('.timingfield_seconds input').on('keyup', $.proxy(this.inputSec, this));
// change on elem
this.elem.on('change', $.proxy(this.change, this));
},
getHours: function() {
return this.tpl.find('.timingfield_hours input')[0];
},
getMinutes: function() {
return this.tpl.find('.timingfield_minutes input')[0];
},
getSeconds: function() {
return this.tpl.find('.timingfield_seconds input')[0];
},
tsToHours: function(timestamp) {
return parseInt(timestamp/3600);
},
tsToMinutes: function(timestamp) {
return parseInt((timestamp%3600) / 60);
},
tsToSeconds: function(timestamp) {
return parseInt((timestamp%3600) % 60);
},
updateElem: function() {
var timestamp = parseInt(this.getHours().value)*3600 + parseInt(this.getMinutes().value)*60;
if (this.settings.hasSeconds) {
timestamp += parseInt(this.getSeconds().value);
}
this.elem.val(timestamp).trigger( "change" ).trigger( "input" );
},
upHour: function() {
if (!this.disabled) {
if (this.getHours().value < this.settings.maxHour) {
this.getHours().value = parseInt(this.getHours().value) + 1;
this.updateElem();
return true;
}
}
return false;
},
downHour: function() {
if (!this.disabled) {
if (this.getHours().value > 0) {
this.getHours().value = parseInt(this.getHours().value) - 1;
this.updateElem();
return true;
}
}
return false;
},
inputHour: function() {
if (!this.disabled) {
if (this.getHours().value < 0) {
this.getHours().value = 0;
} else if (this.getHours().value > this.settings.maxHour) {
this.getHours().value = this.settings.maxHour;
}
}
this.updateElem();
},
upMin: function() {
if (!this.disabled) {
if (this.getMinutes().value < 59) {
this.getMinutes().value = parseInt(this.getMinutes().value) + 1;
this.updateElem();
return true;
} else if (this.upHour()) {
this.getMinutes().value = 0;
this.updateElem();
return true;
}
}
return false;
},
downMin: function() {
if (!this.disabled) {
if (this.getMinutes().value > 0) {
this.getMinutes().value = parseInt(this.getMinutes().value) - 1;
this.updateElem();
return true;
} else if (this.downHour()) {
this.getMinutes().value = 59;
this.updateElem();
return true;
}
}
return false;
},
inputMin: function() {
if (!this.disabled) {
if (this.getMinutes().value < 0) {
this.getMinutes().value = 0;
} else if (this.getMinutes().value > 59) {
this.getMinutes().value = 59;
}
this.updateElem();
}
},
upSec: function() {
if (!this.disabled) {
if (this.getSeconds().value < 59) {
this.getSeconds().value = parseInt(this.getSeconds().value) + 1;
this.updateElem();
return true;
} else if (this.upMin()) {
this.getSeconds().value = 0;
this.updateElem();
return true;
}
}
return false;
},
downSec: function() {
if (!this.disabled) {
if (this.getSeconds().value > 0) {
this.getSeconds().value = parseInt(this.getSeconds().value) - 1;
this.updateElem();
return true;
} else if (this.downMin()) {
this.getSeconds().value = 59;
this.updateElem();
return true;
}
}
return false;
},
inputSec: function() {
if (!this.disabled) {
if (this.getSeconds().value < 0) {
this.getSeconds().value = 0;
} else if (this.getSeconds().value > 59) {
this.getSeconds().value = 59;
}
this.updateElem();
}
},
disable: function() {
this.disabled = true;
this.tpl.find('input:text').prop('disabled', true);
},
enable: function() {
this.disabled = false;
this.tpl.find('input:text').prop('disabled', false);
},
change: function() {
if (this.elem.is(':disabled')) {
this.disable();
} else {
this.enable();
}
},
};
$.fn.timingfield = function(options) {
// Iterate and reformat each matched element.
return this.each(function() {
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('timingfield')) return;
var timingfield = new TimingField(this, options);
// Store plugin object in this element's data
element.data('timingfield', timingfield);
});
};
$.fn.timingfield.defaults = {
maxHour: 23,
width: 263,
hoursText: 'H',
minutesText: 'M',
secondsText: 'S',
hasSeconds: true
};
$.fn.timingfield.template = '<div class="timingfield">\
<div class="timingfield_hours">\
<button type="button" class="timingfield_next btn btn-default btn-xs btn-block" tabindex="-1"><span class="glyphicon glyphicon-plus"></span></button>\
<div class="input-group">\
<input type="text" class="form-control">\
<span class="input-group-addon"></span>\
</div>\
<button type="button" class="timingfield_prev btn btn-default btn-xs btn-block" tabindex="-1"><span class="glyphicon glyphicon-minus"></span></button>\
</div>\
<div class="timingfield_minutes">\
<button type="button" class="timingfield_next btn btn-default btn-xs btn-block" tabindex="-1"><span class="glyphicon glyphicon-plus"></span></button>\
<span class="input-group">\
<input type="text" class="form-control">\
<span class="input-group-addon"></span>\
</span>\
<button type="button" class="timingfield_prev btn btn-default btn-xs btn-block" tabindex="-1"><span class="glyphicon glyphicon-minus"></span></button>\
</div>\
<div class="timingfield_seconds">\
<button type="button" class="timingfield_next btn btn-default btn-xs btn-block" tabindex="-1"><span class="glyphicon glyphicon-plus"></span></button>\
<span class="input-group">\
<input type="text" class="form-control">\
<span class="input-group-addon"></span>\
</span>\
<button type="button" class="timingfield_prev btn btn-default btn-xs btn-block" tabindex="-1"><span class="glyphicon glyphicon-minus"></span></button>\
</div>\
</div>';
}( jQuery ));