PageSourceSearch

https://ween.tn/tpl/js/mdtimepicker.js

js ween.tn collected 2026-10-02 07:33:52 UTC 14,049 bytes, 435 lines download raw bytes

1/* -- DO NOT REMOVE --
2 * jQuery MDTimePicker v1.0 plugin
3 * 
4 * Author: Dionlee Uy
5 * Email: [email protected]
6 *
7 * Date: Tuesday, August 28 2017
8 *
9 * @requires jQuery
10 * -- DO NOT REMOVE -- */
11 if (typeof jQuery === 'undefined') { throw new Error('MDTimePicker: This plugin requires jQuery'); }
12+function ($) {
13	var MDTP_DATA = "mdtimepicker", HOUR_START_DEG = 120, MIN_START_DEG = 90, END_DEG = 360, HOUR_DEG_INCR = 30, MIN_DEG_INCR = 6,
14		EX_KEYS = [9, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123];
15
16	var Time = function (hour, minute) {
17		this.hour = hour;
18		this.minute = minute;
19
20		this.format = function(format, hourPadding) {
21			var that = this, is24Hour = (format.match(/h/g) || []).length > 1;
22
23			return $.trim(format.replace(/(hh|h|mm|ss|tt|t)/g, function (e) { 
24				switch(e.toLowerCase()){
25					case 'h':
26						var hour = that.getHour(true);
27
28						return (hourPadding && hour < 10 ? '0' + hour : hour);
29					case 'hh': return (that.hour < 10 ? '0' + that.hour : that.hour);
30					case 'mm': return (that.minute < 10 ? '0' + that.minute : that.minute);
31					case 'ss': return '00';
32					case 't': return is24Hour ? '' : that.getT().toLowerCase();
33					case 'tt': return is24Hour ? '' : that.getT();
34				}
35			}));
36		};
37
38		this.setHour = function (value) { this.hour = value; };
39		this.getHour = function (is12Hour) { return is12Hour ? this.hour === 0 || this.hour === 12 ? 12 : (this.hour % 12) : this.hour; };
40		this.invert = function () {
41			if (this.getT() === 'AM') this.setHour(this.getHour() + 12);
42			else this.setHour(this.getHour() - 12);
43		};
44		this.setMinutes = function (value) { this.minute = value; }
45		this.getMinutes = function (value) { return this.minute; }
46		this.getT = function() { return this.hour < 12 ? 'AM' : 'PM'; };
47	};
48
49	var MDTimePicker = function (input, config) {
50		var that = this;
51
52		this.visible = false;
53		this.activeView = 'hours';
54		this.hTimeout = null;
55		this.mTimeout = null;
56		this.input = $(input);
57		this.config = config;
58		this.time = new Time(0, 0);
59		this.selected = new Time(0,0);
60		this.timepicker = {
61			overlay : $('<div class="mdtimepicker hidden"></div>'),
62			wrapper : $('<div class="mdtp__wrapper"></div>'),
63			timeHolder : {
64				wrapper: $('<section class="mdtp__time_holder"></section>'),
65				hour: $('<span class="mdtp__time_h">12</span>'),
66				dots: $('<span class="mdtp__timedots">:</span>'),
67				minute: $('<span class="mdtp__time_m">00</span>'),
68				am_pm: $('<span class="mdtp__ampm">AM</span>')
69			},
70			clockHolder : {
71				wrapper: $('<section class="mdtp__clock_holder"></section>'),
72				am: $('<span class="mdtp__am">AM</span>'),
73				pm: $('<span class="mdtp__pm">PM</span>'),
74				clock: {
75					wrapper: $('<div class="mdtp__clock"></div>'),
76					dot: $('<span class="mdtp__clock_dot"></span>'),
77					hours: $('<div class="mdtp__hour_holder"></div>'),
78					minutes: $('<div class="mdtp__minute_holder"></div>')
79				},
80				buttonsHolder : {
81					wrapper: $('<div class="mdtp__buttons">'),
82					btnOk : $('<span class="mdtp__button ok">Ok</span>'),
83					btnCancel: $('<span class="mdtp__button cancel">Cancel</span>')
84				}
85			}
86		};
87
88		var picker = that.timepicker;
89
90		that.setup(picker).appendTo('body');
91
92		picker.clockHolder.am.click(function () { if (that.selected.getT() !== 'AM') that.setT('am'); });
93		picker.clockHolder.pm.click(function () { if (that.selected.getT() !== 'PM') that.setT('pm'); });
94		picker.timeHolder.hour.click(function () { if (that.activeView !== 'hours') that.switchView('hours'); });
95		picker.timeHolder.minute.click(function () { if (that.activeView !== 'minutes') that.switchView('minutes'); });
96		picker.clockHolder.buttonsHolder.btnOk.click(function () {
97			that.setValue(that.selected);
98
99			var formatted = that.getFormattedTime();
100
101			that.input.trigger($.Event('timechanged', { time: formatted.time, value: formatted.value }))
102				.trigger('onchange');	// for ASP.Net postback
103				
104			that.hide();
105		});
106		picker.clockHolder.buttonsHolder.btnCancel.click(function () { that.hide(); });
107
108		that.input.on('keydown', function (e) { 
109			if (e.keyCode === 13) that.show();
110			return !(EX_KEYS.indexOf(e.which) < 0 && that.config.readOnly); })
111			.on('click', function () { that.show(); })
112			.prop('readonly', that.config.readOnly);
113
114		if (that.input.val() !== '') {
115			var time = that.parseTime(that.input.val(), that.config.format);
116
117			that.setValue(time);
118		} else {
119			var time = that.getSystemTime();
120
121			that.time = new Time(time.hour, time.minute);
122		}
123
124		that.resetSelected();
125		that.switchView(that.activeView);
126	};
127
128	MDTimePicker.prototype = {
129		constructor : MDTimePicker,
130
131		setup : function (picker) {
132			if (typeof picker === 'undefined') throw new Error('Expecting a value.');
133
134			var that = this, overlay = picker.overlay, wrapper = picker.wrapper,
135				time = picker.timeHolder, clock = picker.clockHolder;
136
137			// Setup time holder
138			time.wrapper.append(time.hour)
139				.append(time.dots)
140				.append(time.minute)
141				.append(time.am_pm)
142				.appendTo(wrapper);
143
144			// Setup hours
145			for (var i = 0; i < 12; i++) {
146				var value = i + 1, deg = (HOUR_START_DEG + (i * HOUR_DEG_INCR)) % END_DEG,
147					hour = $('<div class="mdtp__digit rotate-' + deg + '" data-hour="' + value + '"><span>'+ value +'</span></div>');
148				
149				hour.find('span').click(function () {
150					var _data = parseInt($(this).parent().data('hour')),
151						_selectedT = that.selected.getT(),
152						_value = (_data + (_selectedT === 'PM' || (_selectedT === 'AM' && _data === 12) ? 12 : 0)) % 24;
153
154					that.setHour(_value);
155					that.switchView('minutes');
156				});
157
158				clock.clock.hours.append(hour);
159			}
160
161			// Setup minutes
162			for (var i = 0; i < 60; i++) {
163				var min = i < 10 ? '0' + i : i, deg = (MIN_START_DEG + (i * MIN_DEG_INCR)) % END_DEG,
164					minute = $('<div class="mdtp__digit rotate-' + deg + '" data-minute="' + i + '"></div>');
165
166				if (i % 5 === 0) minute.addClass('marker').html('<span>' + min + '</span>');
167				else minute.html('<span></span>');
168
169				minute.find('span').click(function () {
170					that.setMinute($(this).parent().data('minute'));
171				});
172
173				clock.clock.minutes.append(minute);
174			}
175
176			// Setup clock
177			clock.clock.wrapper
178				.append(clock.am).append(clock.pm)
179				.append(clock.clock.dot)
180				.append(clock.clock.hours)
181				.append(clock.clock.minutes)
182				.appendTo(clock.wrapper);
183
184			// Setup buttons
185			clock.buttonsHolder.wrapper.append(clock.buttonsHolder.btnCancel)
186				.append(clock.buttonsHolder.btnOk)
187				.appendTo(clock.wrapper);
188
189			clock.wrapper.appendTo(wrapper);
190
191			switch(that.config.theme) {
192				case 'red':
193				case 'blue':
194				case 'green':
195				case 'purple':
196				case 'indigo':
197				case 'teal':
198					wrapper.attr('data-theme', that.config.theme);
199				break;
200				default:
201					wrapper.attr('data-theme', $.fn.mdtimepicker.defaults.theme);
202				break;
203			}
204
205			wrapper.appendTo(overlay);
206
207			return overlay;
208		},
209
210		setHour : function (hour) {
211			if (typeof hour === 'undefined') throw new Error('Expecting a value.');
212
213			var that = this;
214
215			this.selected.setHour(hour);
216			this.timepicker.timeHolder.hour.text(this.selected.getHour(true));
217
218			this.timepicker.clockHolder.clock.hours.children('div').each(function (idx, div) {
219				var el = $(div), val = el.data('hour');
220
221				el[val === that.selected.getHour(true) ? 'addClass' : 'removeClass']('active');
222			});
223		},
224
225		setMinute : function (minute) {
226			if (typeof minute === 'undefined') throw new Error('Expecting a value.');
227
228			this.selected.setMinutes(minute);
229			this.timepicker.timeHolder.minute.text(minute < 10 ? '0' + minute : minute);
230
231			this.timepicker.clockHolder.clock.minutes.children('div').each(function (idx, div) {
232				var el = $(div), val = el.data('minute');
233
234				el[val === minute ? 'addClass' : 'removeClass']('active');
235			});
236		},
237
238		setT : function (value) {
239			if (typeof value === 'undefined') throw new Error('Expecting a value.');
240
241			if (this.selected.getT() !== value.toUpperCase()) this.selected.invert();
242
243			var t = this.selected.getT();
244
245			this.timepicker.timeHolder.am_pm.text(t);
246			this.timepicker.clockHolder.am[t === 'AM' ? 'addClass' : 'removeClass']('active');
247			this.timepicker.clockHolder.pm[t === 'PM' ? 'addClass' : 'removeClass']('active');
248		},
249
250		setValue : function (value) {
251			if (typeof value === 'undefined') throw new Error('Expecting a value.');
252
253			var time = typeof value === 'string' ? this.parseTime(value, this.config.format) : value;
254
255			this.time = new Time(time.hour, time.minute);
256
257			var formatted = this.getFormattedTime();
258
259			this.input.val(formatted.value)
260				.attr('data-time', formatted.time)
261				.attr('value', formatted.value);
262		},
263
264		resetSelected : function () {
265			this.setHour(this.time.hour);
266			this.setMinute(this.time.minute);
267			this.setT(this.time.getT());
268		},
269
270		getFormattedTime : function () {
271			var time = this.time.format(this.config.timeFormat, false),
272				tValue = this.time.format(this.config.format, this.config.hourPadding);
273
274			return { time: time, value: tValue };
275		},
276
277		getSystemTime : function () {
278			var now = new Date();
279
280			return new Time (now.getHours(), now.getMinutes());
281		},
282
283		parseTime : function (time, tFormat) {
284			var that = this, format = typeof tFormat === 'undefined' ? that.config.format : tFormat,
285                hLength = (format.match(/h/g) || []).length,
286				is24Hour = hLength > 1,
287				mLength = (format.match(/m/g) || []).length, tLength = (format.match(/t/g) || []).length,
288				timeLength = time.length,
289				fH = format.indexOf('h'), lH = format.lastIndexOf('h'),
290				hour = '', min = '', t = '';
291
292			// Parse hour
293			if (that.config.hourPadding || is24Hour) {
294				hour = time.substr(fH, 2);
295			} else {
296				var prev = format.substring(fH - 1, fH), next = format.substring(lH + 1, lH + 2);
297
298				if (lH === format.length - 1) {
299					hour = time.substring(time.indexOf(prev, fH - 1) + 1, timeLength);
300				} else if (fH === 0) {
301					hour = time.substring(0, time.indexOf(next, fH));
302				} else {
303					hour = time.substring(time.indexOf(prev, fH - 1) + 1, time.indexOf(next, fH + 1));
304				}
305			}
306
307			format = format.replace(/(hh|h)/g, hour);
308
309			var fM = format.indexOf('m'), lM = format.lastIndexOf('m'),
310				fT = format.indexOf('t');
311
312			// Parse minute
313			var prevM = format.substring(fM - 1, fM), nextM = format.substring(lM + 1, lM + 2);
314
315			if (lM === format.length - 1) {
316				min = time.substring(time.indexOf(prevM, fM - 1) + 1, timeLength);
317			} else if (fM === 0) {
318				min = time.substring(0, 2);
319			} else {
320				min = time.substr(fM, 2);
321			}
322
323			// Parse t (am/pm)
324			if (is24Hour) t = parseInt(hour) > 11 ? (tLength > 1 ? 'PM' : 'pm') : (tLength > 1 ? 'AM' : 'am');
325			else t = time.substr(fT, 2);
326
327			var isPm = t.toLowerCase() === 'pm',
328				outTime = new Time(parseInt(hour), parseInt(min));
329			if ((isPm && parseInt(hour) < 12) || (!isPm && parseInt(hour) === 12)) {
330			    outTime.invert();
331			}
332
333			return outTime;
334		},
335
336		switchView : function (view) {
337			var that = this, picker = this.timepicker, anim_speed = 350;
338
339			if (view !== 'hours' && view !== 'minutes') return;
340
341			that.activeView = view;
342
343			picker.timeHolder.hour[view === 'hours' ? 'addClass' : 'removeClass']('active');
344			picker.timeHolder.minute[view === 'hours' ? 'removeClass' : 'addClass']('active');
345
346			picker.clockHolder.clock.hours.addClass('animate');
347			if (view === 'hours') picker.clockHolder.clock.hours.removeClass('hidden');
348
349			clearTimeout(that.hTimeout);
350
351			that.hTimeout = setTimeout(function() {
352				if (view !== 'hours') picker.clockHolder.clock.hours.addClass('hidden');
353				picker.clockHolder.clock.hours.removeClass('animate');
354			}, view === 'hours' ? 20 : anim_speed);
355
356			picker.clockHolder.clock.minutes.addClass('animate');
357			if (view === 'minutes') picker.clockHolder.clock.minutes.removeClass('hidden');
358
359			clearTimeout(that.mTimeout);
360
361			that.mTimeout = setTimeout(function() {
362				if (view !== 'minutes') picker.clockHolder.clock.minutes.addClass('hidden');
363				picker.clockHolder.clock.minutes.removeClass('animate');
364			}, view === 'minutes' ? 20 : anim_speed);
365		},
366
367		show : function () {
368			var that = this;
369
370			if (that.input.val() === '') {
371				var time = that.getSystemTime();
372				this.time = new Time(time.hour, time.minute);
373			}
374
375			that.resetSelected();
376
377			$('body').attr('mdtimepicker-display', 'on');
378
379			that.timepicker.wrapper.addClass('animate');
380			that.timepicker.overlay.removeClass('hidden').addClass('animate');
381			setTimeout(function() {
382				that.timepicker.overlay.removeClass('animate');
383				that.timepicker.wrapper.removeClass('animate');
384
385				that.visible = true;
386				that.input.blur();
387			}, 10);
388		},
389
390		hide : function () {
391			var that = this;
392
393			that.timepicker.overlay.addClass('animate');
394			that.timepicker.wrapper.addClass('animate');
395			setTimeout(function() {
396				that.switchView('hours');
397				that.timepicker.overlay.addClass('hidden').removeClass('animate');
398				that.timepicker.wrapper.removeClass('animate');
399
400				$('body').removeAttr('mdtimepicker-display');
401
402				that.visible = false;
403				that.input.focus();
404			}, 300);
405		}
406	};
407
408	$.fn.mdtimepicker = function (config) {
409		return $(this).each(function (idx, el) {
410			var that = this,
411				$that = $(this),
412				picker = $(this).data(MDTP_DATA);
413				options = $.extend({}, $.fn.mdtimepicker.defaults, $that.data(), typeof config === 'object' && config);
414
415			if (!picker) {
416				$that.data(MDTP_DATA, (picker = new MDTimePicker(that, options)));
417			}
418			if(typeof config === 'string') picker[config]();
419
420			$(document).on('keydown', function (e) {
421				if(e.keyCode !== 27) return;
422
423				if (picker.visible) picker.hide();
424			});
425		});
426	}
427
428	$.fn.mdtimepicker.defaults = {
429		timeFormat: 'hh:mm:ss.000',	// format of the time value (data-time attribute)
430		format: 'h:mm tt',			// format of the input value
431		theme: 'blue',				// theme of the timepicker
432		readOnly: true,				// determines if input is readonly
433		hourPadding: false			// determines if display value has zero padding for hour value less than 10 (i.e. 05:30 PM); 24-hour format has padding by default
434	};
435}(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.