PageSourceSearch

https://www.socialmotus.com/js/wl_Time.js

js socialmotus.com collected 2026-10-04 21:14:49 UTC 6,151 bytes, 224 lines download raw bytes

1/*----------------------------------------------------------------------*/
2/* wl_Time v 1.0 by revaxarts.com
3/* description: makes a timefield out of an input field
4/* dependency: jQuery Datepicker, mousewheel plugin, $.leadingZero
5/*----------------------------------------------------------------------*/
6
7
8$.fn.wl_Time = function (method) {
9
10	var args = arguments;
11	return this.each(function () {
12
13		var $this = $(this);
14
15		if ($.fn.wl_Time.methods[method]) {
16			return $.fn.wl_Time.methods[method].apply(this, Array.prototype.slice.call(args, 1));
17		} else if (typeof method === 'object' || !method) {
18			if ($this.data('wl_Time')) {
19				var opts = $.extend({}, $this.data('wl_Time'), method);
20			} else {
21				var opts = $.extend({}, $.fn.wl_Time.defaults, method, $this.data());
22			}
23		} else {
24			$.error('Method "' + method + '" does not exist');
25		}
26
27
28		if (!$this.data('wl_Time')) {
29
30			$this.data('wl_Time', {});
31
32			$this.bind({
33
34				'mousewheel.wl_Time': function (event, delta) {
35					var opts = $this.data('wl_Time') || opts;
36					if (opts.mousewheel) {
37						event.preventDefault();
38						
39						//delta must be 1 or -1 (different on macs and with shiftkey pressed)
40						delta = (delta < 0) ? -1 : 1;
41						
42						//scroll thru hours if shift key is pressed
43						if (event.shiftKey) delta *= 60 / opts.step;
44						$.fn.wl_Time.methods.change.call($this[0], delta);
45					}
46				},
47
48				'change.wl_Time': function () {
49					var opts = $this.data('wl_Time') || opts;
50					
51					//correct input value
52					$.fn.wl_Time.methods.correct.call($this[0]);
53					//print it
54					$.fn.wl_Time.methods.printTime.call($this[0]);
55					//callback
56					opts.onChange.call($this[0], opts.time);
57				}
58				
59				//for 12h format ad a little span after the input and set length to 5 (hh:mm)
60			}).after('<span class="timeformat"/>').attr('maxlength', 5);
61
62
63			//is connected to a datepicker
64			if (opts.connect !== null) {
65				var _date = $('#' + opts.connect),
66					_oldcallback = opts.onDateChange;
67
68				//set a callback if the time reaches another date
69				_callback = function (offset) {
70					var current = new Date(_date.datepicker('getDate')).getTime();
71					if (current) _date.datepicker('setDate', new Date(current + (864e5 * offset)));
72					_oldcallback.call($this[0], offset);
73				};
74				opts.onDateChange = _callback;
75			}
76
77
78			//value is set and has to get translated (self-explanatory) 
79			if (opts.value) {
80				var now = new Date().getTime(),
81					date;
82				switch (opts.value) {
83				case 'now':
84					date = new Date(now);
85					break;
86				default:
87					//if a valid number add them as days to the date field
88					if (!isNaN(opts.value)) date = new Date(now + (60000 * (opts.value % 60)));
89				}
90				
91				//set the time (hh:mm)
92				opts.time = $.leadingZero(date.getHours()) + ':' + $.leadingZero(date.getMinutes());
93				//write it into the input
94				$this.val(opts.time);
95			}
96
97		} else {
98
99		}
100
101		if (opts) $.extend($this.data('wl_Time'), opts);
102	});
103
104};
105
106$.fn.wl_Time.defaults = {
107	step: 5,
108	timeformat: 24,
109	roundtime: true,
110	time: null,
111	value: null,
112	mousewheel: true,
113	onDateChange: function (offset) {},
114	onHourChange: function (offset) {},
115	onChange: function (value) {}
116};
117$.fn.wl_Time.version = '1.0';
118
119
120
121$.fn.wl_Time.methods = {
122	change: function (delta) {
123		var $this = $(this),
124			opts = $this.data('wl_Time'),
125			_current = new Date('2010/01/01 ' + ($this.data('wl_Time').time || '00:00')),
126			_new = new Date(_current.getTime() + (delta * $this.data('wl_Time').step * 60000)),
127			_hours = _new.getHours(),
128			_minutes = _new.getMinutes();
129
130		//round the time on a mousewheel
131		if (opts.roundtime) {
132			_minutes -= (_minutes % $this.data('wl_Time').step);
133		}
134
135		//save time
136		$this.data('wl_Time').time = $.leadingZero(_hours) + ':' + $.leadingZero(_minutes);
137		
138		//and print it
139		$.fn.wl_Time.methods.printTime.call(this);
140
141		//callbacks
142		opts.onChange.call(this, $this.data('wl_Time').time);
143		if (Math.abs(_current.getMinutes() - _minutes) == (60 - opts.step)) {
144			opts.onHourChange.call(this, (_hours - _current.getHours()));
145		}
146		if (Math.abs(_current.getHours() - _hours) == (23)) {
147			opts.onDateChange.call(this, (_hours) ? -1 : 1);
148		}
149	},
150	printTime: function () {
151		var $this = $(this),
152			opts = $this.data('wl_Time'),
153			time = opts.time;
154	
155		if (time) {
156			time = time.split(':');
157			
158			//calculate the 12h format
159			if (opts.timeformat == 12) {
160				$this.val($.leadingZero(((time[0] % 12 == 0) ? 12 : time[0] % 12)) + ':' + $.leadingZero(time[1])).next().html((time[0] / 12 >= 1) ? 'pm' : 'am');
161			
162			//or set the 24h format
163			} else {
164				$this.val($.leadingZero(time[0]) + ':' + $.leadingZero(time[1]));
165			}
166		}
167	},
168	correct: function () {
169		var $this = $(this),
170			val = $this.val(),
171			time;
172		
173		//no value => stop
174		if (val == '') return;
175		
176		//it is not hh:mm format
177		if (!/^\d+:\d+$/.test(val)) {
178			
179			//convert the input (read the docs for more details)
180			if (val.length == 1) {
181				val = "0" + val + ":00";
182			} else if (val.length == 2) {
183				val = val + ":00";
184			} else if (val.length == 3) {
185				val = val.substr(0, 2) + ":" + val.substr(2, 3) + "0";
186			} else if (val.length == 4) {
187				val = val.substr(0, 2) + ":" + val.substr(2, 4);
188			}
189		}
190		time = val.split(':');
191		
192		//value is wrong or out of range
193		if (!/\d\d:\d\d$/.test(val) && val != "" || time[0] > 23 || time[1] > 59) {
194			$this.val('00:00').focus().select();
195			$this.data('wl_Time').time = '00:00';
196			$.fn.wl_Time.methods.printTime.call(this);
197		
198		//value is a time
199		} else {
200			//save it
201			$this.data('wl_Time').time = val;
202			//print it
203			$.fn.wl_Time.methods.printTime.call(this);
204		}
205	},
206	set: function () {
207		var $this = $(this),
208			options = {};
209		if (typeof arguments[0] === 'object') {
210			options = arguments[0];
211		} else if (arguments[0] && arguments[1] !== undefined) {
212			options[arguments[0]] = arguments[1];
213		}
214		$.each(options, function (key, value) {
215			if ($.fn.wl_Time.defaults[key] !== undefined || $.fn.wl_Time.defaults[key] == null) {
216				$this.data('wl_Time')[key] = value;
217				$this.trigger('change.wl_Time');
218			} else {
219				$.error('Key "' + key + '" is not defined');
220			}
221		});
222
223	}
224};

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.