1 | /** |
---|
2 | * ----------------------------------------------------------------------------- |
---|
3 | * file: ui.inputDate.js |
---|
4 | * file version: 1.0.0 |
---|
5 | * date: 2012-06-17 |
---|
6 | * |
---|
7 | * A jQuery plugin provided by the piwigo's plugin "GrumPluginClasses" |
---|
8 | * |
---|
9 | * ----------------------------------------------------------------------------- |
---|
10 | * Author : Grum |
---|
11 | * email : grum@piwigo.com |
---|
12 | * website : http://photos.grum.fr |
---|
13 | * |
---|
14 | * << May the Little SpaceFrog be with you ! >> |
---|
15 | * ----------------------------------------------------------------------------- |
---|
16 | * |
---|
17 | * |
---|
18 | * |
---|
19 | * |
---|
20 | * :: HISTORY :: |
---|
21 | * |
---|
22 | * | release | date | |
---|
23 | * | 1.0.0 | 2012/06/17 | * first release |
---|
24 | * | | | |
---|
25 | * | | | |
---|
26 | * | | | |
---|
27 | * | | | |
---|
28 | * | | | |
---|
29 | * | | | |
---|
30 | * | | | |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | ( |
---|
37 | function($) |
---|
38 | { |
---|
39 | /* |
---|
40 | * plugin 'public' functions |
---|
41 | */ |
---|
42 | var publicMethods = |
---|
43 | { |
---|
44 | init : function (opt) |
---|
45 | { |
---|
46 | return this.each(function() |
---|
47 | { |
---|
48 | // default values for the plugin |
---|
49 | var $this=$(this), |
---|
50 | data = $this.data('options'), |
---|
51 | objects = $this.data('objects'), |
---|
52 | properties = $this.data('properties'), |
---|
53 | options = |
---|
54 | { |
---|
55 | dateType:'date', // date, datetime |
---|
56 | timepicker:{ |
---|
57 | timeFormat:'hh:mm', // hh:mm, hh:mm:ss |
---|
58 | minTime:null, |
---|
59 | maxTime:null |
---|
60 | }, |
---|
61 | datepicker:{ |
---|
62 | dateFormat:'yy-mm-dd' |
---|
63 | }, |
---|
64 | disabled:false, |
---|
65 | value:'', |
---|
66 | change:null |
---|
67 | }; |
---|
68 | |
---|
69 | // if options given, merge it |
---|
70 | // if(opt) $.extend(options, opt); ==> options are set by setters |
---|
71 | if(options.value=='' && $.trim($this.html())!='') options.value=$.trim($this.html()); |
---|
72 | |
---|
73 | $this.data('options', options); |
---|
74 | |
---|
75 | if(!properties) |
---|
76 | { |
---|
77 | $this.data('properties', |
---|
78 | { |
---|
79 | initialized:false, |
---|
80 | dateValue:'', |
---|
81 | timeValue:'', |
---|
82 | value:'', |
---|
83 | isValid:true |
---|
84 | } |
---|
85 | ); |
---|
86 | properties=$this.data('properties'); |
---|
87 | } |
---|
88 | |
---|
89 | if(!objects) |
---|
90 | { |
---|
91 | objects = |
---|
92 | { |
---|
93 | container:$('<div/>', |
---|
94 | { |
---|
95 | 'class':'ui-inputDate', |
---|
96 | css:{ |
---|
97 | width:'100%' |
---|
98 | } |
---|
99 | } |
---|
100 | ), |
---|
101 | inputFDate:$('<input/>', { 'type':'text', class:'ui-inputDate-date', value:''}), |
---|
102 | inputFTime:$('<input/>', { 'type':'text', class:'ui-inputDate-time', value:''}) |
---|
103 | }; |
---|
104 | |
---|
105 | $this |
---|
106 | .html('') |
---|
107 | .append( |
---|
108 | objects.container |
---|
109 | .append( |
---|
110 | objects.inputFDate |
---|
111 | .bind('keyup.inputDate', |
---|
112 | function (event) |
---|
113 | { |
---|
114 | return(privateMethods.keyUp($this, event)); |
---|
115 | } |
---|
116 | ) |
---|
117 | .bind('change.inputDate', |
---|
118 | function (event) |
---|
119 | { |
---|
120 | return(privateMethods.change($this, event)); |
---|
121 | } |
---|
122 | ) |
---|
123 | ) |
---|
124 | ); |
---|
125 | |
---|
126 | $this.data('objects', objects); |
---|
127 | } |
---|
128 | |
---|
129 | privateMethods.setOptions($this, opt); |
---|
130 | } |
---|
131 | ); |
---|
132 | }, // init |
---|
133 | destroy : function () |
---|
134 | { |
---|
135 | return this.each( |
---|
136 | function() |
---|
137 | { |
---|
138 | // default values for the plugin |
---|
139 | var $this=$(this), |
---|
140 | objects = $this.data('objects'); |
---|
141 | objects.inputFDate.unbind().datepicker('destroy').remove(); |
---|
142 | objects.inputFTime.unbind().remove(); |
---|
143 | objects.container.unbind().remove(); |
---|
144 | $this |
---|
145 | .unbind('.inputDate') |
---|
146 | .removeData() |
---|
147 | .css( |
---|
148 | { |
---|
149 | width:'', |
---|
150 | height:'' |
---|
151 | } |
---|
152 | ); |
---|
153 | delete $this; |
---|
154 | } |
---|
155 | ); |
---|
156 | }, // destroy |
---|
157 | |
---|
158 | options: function (value) |
---|
159 | { |
---|
160 | return( |
---|
161 | this.each( |
---|
162 | function() |
---|
163 | { |
---|
164 | privateMethods.setOptions($(this), value); |
---|
165 | } |
---|
166 | ) |
---|
167 | ); |
---|
168 | }, // options |
---|
169 | |
---|
170 | disabled: function (value) |
---|
171 | { |
---|
172 | if(value!=null) |
---|
173 | { |
---|
174 | return( |
---|
175 | this.each( |
---|
176 | function() |
---|
177 | { |
---|
178 | privateMethods.setDisabled($(this), value); |
---|
179 | } |
---|
180 | ) |
---|
181 | ); |
---|
182 | } |
---|
183 | else |
---|
184 | { |
---|
185 | var options = this.data('options'); |
---|
186 | |
---|
187 | if(options) |
---|
188 | { |
---|
189 | return(options.disabled); |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | return(''); |
---|
194 | } |
---|
195 | } |
---|
196 | }, // disabled |
---|
197 | |
---|
198 | value: function (value, language) |
---|
199 | { |
---|
200 | if(value!=null) |
---|
201 | { |
---|
202 | var options=this.data('options'); |
---|
203 | |
---|
204 | // set selected value |
---|
205 | return( |
---|
206 | this.each( |
---|
207 | function() |
---|
208 | { |
---|
209 | privateMethods.setValue($(this), value, true); |
---|
210 | } |
---|
211 | ) |
---|
212 | ); |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | // return the selected tags |
---|
217 | var properties=this.data('properties'); |
---|
218 | return(properties.value); |
---|
219 | } |
---|
220 | }, // value |
---|
221 | |
---|
222 | dateValue: function (value, language) |
---|
223 | { |
---|
224 | if(value!=null) |
---|
225 | { |
---|
226 | var options=this.data('options'); |
---|
227 | |
---|
228 | // set selected value |
---|
229 | return( |
---|
230 | this.each( |
---|
231 | function() |
---|
232 | { |
---|
233 | privateMethods.setDateValue($(this), value, true); |
---|
234 | } |
---|
235 | ) |
---|
236 | ); |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | // return the selected tags |
---|
241 | var properties=this.data('properties'); |
---|
242 | return(properties.dateValue); |
---|
243 | } |
---|
244 | }, // dateValue |
---|
245 | |
---|
246 | timeValue: function (value, language) |
---|
247 | { |
---|
248 | if(value!=null) |
---|
249 | { |
---|
250 | var options=this.data('options'); |
---|
251 | |
---|
252 | // set selected value |
---|
253 | return( |
---|
254 | this.each( |
---|
255 | function() |
---|
256 | { |
---|
257 | privateMethods.setTimeValue($(this), value, true); |
---|
258 | } |
---|
259 | ) |
---|
260 | ); |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | // return the selected tags |
---|
265 | var properties=this.data('properties'); |
---|
266 | return(properties.timeValue); |
---|
267 | } |
---|
268 | }, // timeValue |
---|
269 | |
---|
270 | isValid: function (value) |
---|
271 | { |
---|
272 | if(value!=null) |
---|
273 | { |
---|
274 | // set selected value |
---|
275 | return( |
---|
276 | this.each( |
---|
277 | function() |
---|
278 | { |
---|
279 | privateMethods.setIsValid($(this), value); |
---|
280 | } |
---|
281 | ) |
---|
282 | ); |
---|
283 | } |
---|
284 | else |
---|
285 | { |
---|
286 | // return the selected tags |
---|
287 | var properties=this.data('properties'); |
---|
288 | return(properties.isValid); |
---|
289 | } |
---|
290 | }, // isValid |
---|
291 | |
---|
292 | change: function (value) |
---|
293 | { |
---|
294 | if(value!=null && $.isFunction(value)) |
---|
295 | { |
---|
296 | // set selected value |
---|
297 | return( |
---|
298 | this.each( |
---|
299 | function() |
---|
300 | { |
---|
301 | privateMethods.setEventChange($(this), value); |
---|
302 | } |
---|
303 | ) |
---|
304 | ); |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | // return the selected value |
---|
309 | var options=this.data('options'); |
---|
310 | |
---|
311 | if(options) |
---|
312 | { |
---|
313 | return(options.change); |
---|
314 | } |
---|
315 | else |
---|
316 | { |
---|
317 | return(null); |
---|
318 | } |
---|
319 | } |
---|
320 | } // change |
---|
321 | |
---|
322 | }; // methods |
---|
323 | |
---|
324 | |
---|
325 | /* |
---|
326 | * plugin 'private' methods |
---|
327 | */ |
---|
328 | var privateMethods = |
---|
329 | { |
---|
330 | isDateValid : function (object, value) |
---|
331 | { |
---|
332 | var options=object.data('options'), |
---|
333 | properties=object.data('properties'), |
---|
334 | re=/^\d{4}-\d{2}-\d{2}$/i; |
---|
335 | |
---|
336 | if(options.datepicker.minDate!=null && options.datepicker.minDate!='' && value<options.datepicker.minDate) |
---|
337 | return(false); |
---|
338 | |
---|
339 | if(options.datepicker.maxDate!=null && options.datepicker.maxDate!='' && value>options.datepicker.maxDate) |
---|
340 | return(false); |
---|
341 | |
---|
342 | return(re.test(value)); |
---|
343 | }, |
---|
344 | |
---|
345 | isTimeValid : function (object, value) |
---|
346 | { |
---|
347 | var options=object.data('options'), |
---|
348 | properties=object.data('properties'), |
---|
349 | re=/^(\d{1,2}):(\d{2})(?::(\d{2})){0,1}$/i, |
---|
350 | hms=[]; |
---|
351 | |
---|
352 | if(re.test(value)) |
---|
353 | { |
---|
354 | hms=re.exec(value); |
---|
355 | |
---|
356 | if(hms[1]>'23') return(false); |
---|
357 | if(hms[2]>'59') return(false); |
---|
358 | if(hms[3]!=null && hms[3]>'59' && options.timepicker.timeFormat=='hh:mm:ss' || |
---|
359 | hms[3]!=null && options.timepicker.timeFormat=='hh:mm') return(false); |
---|
360 | |
---|
361 | if(hms[3]==null && options.timepicker.timeFormat=='hh:mm:ss') |
---|
362 | hms[3]='00'; //assuming the seconds equals... |
---|
363 | |
---|
364 | if(hms[1].length==1) hms[1]='0'+hms[1]; |
---|
365 | |
---|
366 | hms[0]=hms[1]+':'+hms[2]; |
---|
367 | if(options.timepicker.timeFormat=='hh:mm:ss') hms[0]=hms[0]+':'+hms[3]; |
---|
368 | |
---|
369 | if(options.timepicker.minTime!=null && options.timepicker.minTime!='' && hms[0]<options.datepicker.minTime) |
---|
370 | return(false); |
---|
371 | |
---|
372 | if(options.datepicker.maxTime!=null && options.datepicker.maxTime!='' && hms[0]>options.datepicker.maxTime) |
---|
373 | return(false); |
---|
374 | |
---|
375 | return(true); |
---|
376 | } |
---|
377 | return(false); |
---|
378 | }, |
---|
379 | |
---|
380 | setOptions : function (object, value) |
---|
381 | { |
---|
382 | var properties=object.data('properties'), |
---|
383 | options=object.data('options'); |
---|
384 | |
---|
385 | if(!$.isPlainObject(value)) return(false); |
---|
386 | |
---|
387 | properties.initialized=false; |
---|
388 | |
---|
389 | privateMethods.setDateType(object, (value.dateType!=null)?value.dateType:options.dateType); |
---|
390 | privateMethods.setTimePicker(object, (value.timepicker!=null)?value.timepicker:options.timepicker); |
---|
391 | privateMethods.setDatePicker(object, (value.datepicker!=null)?value.datepicker:options.datepicker); |
---|
392 | privateMethods.setValue(object, (value.value!=null)?value.value:options.value, true); |
---|
393 | privateMethods.setTimeValue(object, (value.timeValue!=null)?value.timeValue:options.timeValue, true, false); |
---|
394 | privateMethods.setDateValue(object, (value.dateValue!=null)?value.dateValue:options.dateValue, true); |
---|
395 | privateMethods.setDisabled(object, (value.disabled!=null)?value.disabled:options.disabled); |
---|
396 | privateMethods.setEventChange(object, (value.change!=null)?value.change:options.change); |
---|
397 | |
---|
398 | properties.initialized=true; |
---|
399 | }, |
---|
400 | |
---|
401 | setIsValid : function (object, value) |
---|
402 | { |
---|
403 | var options=object.data('options'), |
---|
404 | objects=object.data('objects'), |
---|
405 | properties=object.data('properties'); |
---|
406 | |
---|
407 | if(value=='check') |
---|
408 | { |
---|
409 | value=privateMethods.isDateValid(object, properties.dateValue); |
---|
410 | if(value && options.dateType=='datetime') |
---|
411 | value=privateMethods.isTimeValid(object, properties.timeValue); |
---|
412 | } |
---|
413 | |
---|
414 | if(properties.isValid!=value) |
---|
415 | { |
---|
416 | properties.isValid=value; |
---|
417 | if(properties.isValid) |
---|
418 | { |
---|
419 | objects.container.removeClass('ui-error'); |
---|
420 | objects.inputFDate.removeClass('ui-error'); |
---|
421 | objects.inputFTime.removeClass('ui-error'); |
---|
422 | } |
---|
423 | else |
---|
424 | { |
---|
425 | objects.container.addClass('ui-error'); |
---|
426 | objects.inputFDate.addClass('ui-error'); |
---|
427 | objects.inputFTime.addClass('ui-error'); |
---|
428 | } |
---|
429 | } |
---|
430 | return(properties.isValid); |
---|
431 | }, // setIsValid |
---|
432 | |
---|
433 | setDisabled : function (object, value) |
---|
434 | { |
---|
435 | var options=object.data('options'), |
---|
436 | objects=object.data('objects'), |
---|
437 | properties=object.data('properties'); |
---|
438 | |
---|
439 | if((!properties.initialized || options.disabled!=value) && (value==true || value==false)) |
---|
440 | { |
---|
441 | options.disabled=value; |
---|
442 | objects.inputFDate.attr('disabled', options.disabled); |
---|
443 | objects.inputFTime.attr('disabled', options.disabled); |
---|
444 | } |
---|
445 | return(options.disabled); |
---|
446 | }, //setDisabled |
---|
447 | |
---|
448 | setDateType : function (object, value) |
---|
449 | { |
---|
450 | var options=object.data('options'), |
---|
451 | objects=object.data('objects'), |
---|
452 | properties=object.data('properties'); |
---|
453 | |
---|
454 | if((!properties.initialized || options.dateType!=value) && (value=='date' || value=='datetime')) |
---|
455 | { |
---|
456 | options.dateType=value; |
---|
457 | if(value=='datetime') |
---|
458 | { |
---|
459 | objects.container |
---|
460 | .append( |
---|
461 | objects.inputFTime |
---|
462 | .bind('keyup.inputDate', |
---|
463 | function (event) |
---|
464 | { |
---|
465 | return(privateMethods.timeFKeyUp(object, event)); |
---|
466 | } |
---|
467 | ) |
---|
468 | .bind('keydown.inputDate', |
---|
469 | function (event) |
---|
470 | { |
---|
471 | return(privateMethods.timeFKeyDown(object, event)); |
---|
472 | } |
---|
473 | ) |
---|
474 | .bind('change.inputDate', |
---|
475 | function (event) |
---|
476 | { |
---|
477 | return(privateMethods.timeFChange(object, event)); |
---|
478 | } |
---|
479 | ) |
---|
480 | ); |
---|
481 | } |
---|
482 | else |
---|
483 | { |
---|
484 | objects.container |
---|
485 | .bind('click', |
---|
486 | function (event) |
---|
487 | { |
---|
488 | objects.inputFDate.focus(); |
---|
489 | } |
---|
490 | ); |
---|
491 | } |
---|
492 | |
---|
493 | } |
---|
494 | return(options.disabled); |
---|
495 | }, //setDateType |
---|
496 | |
---|
497 | setDatePicker : function (object, value) |
---|
498 | { |
---|
499 | var options=object.data('options'), |
---|
500 | objects=object.data('objects'), |
---|
501 | properties=object.data('properties'); |
---|
502 | |
---|
503 | if(!properties.initialized && $.isPlainObject(value)) |
---|
504 | { |
---|
505 | options.datepicker=value; |
---|
506 | options.datepicker |
---|
507 | .onSelect=function (dateText, inst) |
---|
508 | { |
---|
509 | privateMethods.setValue(object, dateText, false); |
---|
510 | }; |
---|
511 | objects.inputFDate.datepicker(options.datepicker) |
---|
512 | } |
---|
513 | return(options.datepicker); |
---|
514 | }, //setDatePicker |
---|
515 | |
---|
516 | setTimePicker : function (object, value) |
---|
517 | { |
---|
518 | var options=object.data('options'), |
---|
519 | objects=object.data('objects'), |
---|
520 | properties=object.data('properties'); |
---|
521 | |
---|
522 | if(!properties.initialized && $.isPlainObject(value)) |
---|
523 | { |
---|
524 | if(value.timeFormat!=null && |
---|
525 | (value.timeFormat=='hh:mm' || |
---|
526 | value.timeFormat=='hh:mm:ss')) |
---|
527 | options.timepicker.timeFormat=value.timeFormat; |
---|
528 | |
---|
529 | if(value.minTime!=null && privateMethods.isTimeValid(object, value.minTime)) |
---|
530 | options.timepicker.minTime=value.minTime; |
---|
531 | |
---|
532 | if(value.maxTime!=null && privateMethods.isTimeValid(object, value.maxTime)) |
---|
533 | options.timepicker.maxTime=value.maxTime; |
---|
534 | } |
---|
535 | return(options.timepicker); |
---|
536 | }, //setTimePicker |
---|
537 | |
---|
538 | |
---|
539 | setDateValue : function (object, value, apply) |
---|
540 | { |
---|
541 | var options=object.data('options'), |
---|
542 | properties=object.data('properties'), |
---|
543 | objects=object.data('objects'), |
---|
544 | reformatted=''; |
---|
545 | |
---|
546 | if(properties.initialized && properties.dateValue==value || value==null) |
---|
547 | { |
---|
548 | return(properties.dateValue); |
---|
549 | } |
---|
550 | |
---|
551 | reformatted=privateMethods.reformatDate(value); |
---|
552 | if(reformatted!=value) |
---|
553 | { |
---|
554 | value=reformatted; |
---|
555 | apply=true; |
---|
556 | } |
---|
557 | |
---|
558 | properties.dateValue=value; |
---|
559 | if(options.dateType=='datetime') |
---|
560 | { |
---|
561 | properties.value=properties.dateValue+' '+properties.timeValue; |
---|
562 | privateMethods.setIsValid(object, privateMethods.isDateValid(object, value) & privateMethods.isTimeValid(object, properties.timeValue)); |
---|
563 | } |
---|
564 | else |
---|
565 | { |
---|
566 | properties.value=properties.dateValue; |
---|
567 | privateMethods.setIsValid(object, privateMethods.isDateValid(object, value)); |
---|
568 | } |
---|
569 | |
---|
570 | |
---|
571 | if(apply) |
---|
572 | objects.inputFDate.datepicker('setDate', properties.dateValue); |
---|
573 | |
---|
574 | if(options.change) object.trigger('inputDateChange', properties.value); |
---|
575 | |
---|
576 | return(properties.dateValue); |
---|
577 | }, //setDateValue |
---|
578 | |
---|
579 | setTimeValue : function (object, value, apply, triggerEvent) |
---|
580 | { |
---|
581 | var options=object.data('options'), |
---|
582 | properties=object.data('properties'), |
---|
583 | objects=object.data('objects'), |
---|
584 | reformatted=''; |
---|
585 | |
---|
586 | if(properties.initialized && properties.timeValue==value || value==null) |
---|
587 | { |
---|
588 | return(properties.timeValue); |
---|
589 | } |
---|
590 | |
---|
591 | reformatted=privateMethods.reformatTime(value, options.timepicker.timeFormat); |
---|
592 | if(reformatted!=value) |
---|
593 | { |
---|
594 | value=reformatted; |
---|
595 | apply=true; |
---|
596 | } |
---|
597 | |
---|
598 | privateMethods.setIsValid(object, privateMethods.isDateValid(object, properties.dateValue) & privateMethods.isTimeValid(object, value)); |
---|
599 | |
---|
600 | properties.timeValue=value; |
---|
601 | properties.value=properties.dateValue+' '+properties.timeValue; |
---|
602 | |
---|
603 | if(apply) |
---|
604 | objects.inputFTime.val(properties.timeValue); |
---|
605 | |
---|
606 | if(options.change && triggerEvent) object.trigger('inputDateChange', properties.value); |
---|
607 | |
---|
608 | return(properties.timeValue); |
---|
609 | }, //setDateValue |
---|
610 | |
---|
611 | setValue : function (object, value, apply) |
---|
612 | { |
---|
613 | var options=object.data('options'), |
---|
614 | properties=object.data('properties'), |
---|
615 | objects=object.data('objects'), |
---|
616 | re=/([\d-]+)(?:\s+([\d:]+)){0,1}/, |
---|
617 | values=null; |
---|
618 | |
---|
619 | if(properties.initialized && properties.value==value || value==null) |
---|
620 | { |
---|
621 | return(properties.value); |
---|
622 | } |
---|
623 | |
---|
624 | if(re.test(value)) |
---|
625 | { |
---|
626 | values=re.exec(value); |
---|
627 | |
---|
628 | if(options.dateType=='datetime') |
---|
629 | privateMethods.setTimeValue(object, values[2], apply, false); |
---|
630 | |
---|
631 | privateMethods.setDateValue(object, values[1], apply); |
---|
632 | } |
---|
633 | |
---|
634 | return(properties.value); |
---|
635 | }, //setDateValue |
---|
636 | |
---|
637 | setEventChange : function (object, value) |
---|
638 | { |
---|
639 | var options=object.data('options'); |
---|
640 | |
---|
641 | options.change=value; |
---|
642 | object.unbind('inputDateChange'); |
---|
643 | if(value) object.bind('inputDateChange', options.change); |
---|
644 | return(options.change); |
---|
645 | }, //setEventChange |
---|
646 | |
---|
647 | keyUp : function (object, event) |
---|
648 | { |
---|
649 | var objects=object.data('objects'); |
---|
650 | |
---|
651 | if(event.keyCode==9 || //DOM_VK_TAB |
---|
652 | event.keyCode==12 || //DOM_VK_CLEAR |
---|
653 | event.keyCode==16 || //DOM_VK_SHIFT |
---|
654 | event.keyCode==17 || //DOM_VK_CONTROL |
---|
655 | event.keyCode==18 || //DOM_VK_ALT |
---|
656 | event.keyCode==33 || //DOM_VK_PAGE_UP |
---|
657 | event.keyCode==34 || //DOM_VK_PAGE_DOWN |
---|
658 | event.keyCode==35 || //DOM_VK_END |
---|
659 | event.keyCode==36 || //DOM_VK_HOME |
---|
660 | event.keyCode==37 || //DOM_VK_LEFT |
---|
661 | event.keyCode==38 || //DOM_VK_UP |
---|
662 | event.keyCode==39 || //DOM_VK_RIGHT |
---|
663 | event.keyCode==40 || //DOM_VK_DOWN |
---|
664 | event.keyCode==45 || //DOM_VK_INSERT |
---|
665 | event.keyCode==93 //DOM_VK_CONTEXT_MENU |
---|
666 | ) return(false); |
---|
667 | |
---|
668 | return(privateMethods.setDateValue(object, objects.inputFDate.val(), false)); |
---|
669 | }, //keyUp |
---|
670 | |
---|
671 | change : function (object, event) |
---|
672 | { |
---|
673 | var objects=object.data('objects'); |
---|
674 | |
---|
675 | return(privateMethods.setDateValue(object, objects.inputFDate.val(), false)); |
---|
676 | }, //change |
---|
677 | |
---|
678 | timeFKeyUp : function (object, event) |
---|
679 | { |
---|
680 | var objects=object.data('objects'); |
---|
681 | |
---|
682 | if(event.keyCode==9 || //DOM_VK_TAB |
---|
683 | event.keyCode==12 || //DOM_VK_CLEAR |
---|
684 | event.keyCode==16 || //DOM_VK_SHIFT |
---|
685 | event.keyCode==17 || //DOM_VK_CONTROL |
---|
686 | event.keyCode==18 || //DOM_VK_ALT |
---|
687 | event.keyCode==33 || //DOM_VK_PAGE_UP |
---|
688 | event.keyCode==34 || //DOM_VK_PAGE_DOWN |
---|
689 | event.keyCode==35 || //DOM_VK_END |
---|
690 | event.keyCode==36 || //DOM_VK_HOME |
---|
691 | event.keyCode==37 || //DOM_VK_LEFT |
---|
692 | event.keyCode==38 || //DOM_VK_UP |
---|
693 | event.keyCode==39 || //DOM_VK_RIGHT |
---|
694 | event.keyCode==40 || //DOM_VK_DOWN |
---|
695 | event.keyCode==45 || //DOM_VK_INSERT |
---|
696 | event.keyCode==93 //DOM_VK_CONTEXT_MENU |
---|
697 | ) return(false); |
---|
698 | |
---|
699 | return(privateMethods.setTimeValue(object, objects.inputFTime.val(), false, true)); |
---|
700 | }, //timeFKeyUp |
---|
701 | |
---|
702 | timeFKeyDown : function (object, event) |
---|
703 | { |
---|
704 | var objects=object.data('objects'), |
---|
705 | options=object.data('options'), |
---|
706 | char=''; |
---|
707 | |
---|
708 | if(objects.inputFTime.val().length>=options.timepicker.timeFormat.length && |
---|
709 | !(event.keyCode==8 || //DOM_VK_BACK_SPACE |
---|
710 | event.keyCode==9 || //DOM_VK_TAB |
---|
711 | event.keyCode==12 || //DOM_VK_CLEAR |
---|
712 | event.keyCode==16 || //DOM_VK_SHIFT |
---|
713 | event.keyCode==17 || //DOM_VK_CONTROL |
---|
714 | event.keyCode==18 || //DOM_VK_ALT |
---|
715 | event.keyCode==33 || //DOM_VK_PAGE_UP |
---|
716 | event.keyCode==34 || //DOM_VK_PAGE_DOWN |
---|
717 | event.keyCode==35 || //DOM_VK_END |
---|
718 | event.keyCode==36 || //DOM_VK_HOME |
---|
719 | event.keyCode==37 || //DOM_VK_LEFT |
---|
720 | event.keyCode==38 || //DOM_VK_UP |
---|
721 | event.keyCode==39 || //DOM_VK_RIGHT |
---|
722 | event.keyCode==40 || //DOM_VK_DOWN |
---|
723 | event.keyCode==45 || //DOM_VK_INSERT |
---|
724 | event.keyCode==46 || //DOM_VK_DELETE |
---|
725 | event.keyCode==93 || //DOM_VK_CONTEXT_MENU |
---|
726 | objects.inputFTime.get(0).selectionStart!=objects.inputFTime.get(0).selectionEnd |
---|
727 | ) |
---|
728 | ) return(false); |
---|
729 | |
---|
730 | if(event.keyCode==8 || //DOM_VK_BACK_SPACE |
---|
731 | event.keyCode==9 || //DOM_VK_TAB |
---|
732 | event.keyCode==12 || //DOM_VK_CLEAR |
---|
733 | event.keyCode==16 || //DOM_VK_SHIFT |
---|
734 | event.keyCode==17 || //DOM_VK_CONTROL |
---|
735 | event.keyCode==18 || //DOM_VK_ALT |
---|
736 | event.keyCode==33 || //DOM_VK_PAGE_UP |
---|
737 | event.keyCode==34 || //DOM_VK_PAGE_DOWN |
---|
738 | event.keyCode==35 || //DOM_VK_END |
---|
739 | event.keyCode==36 || //DOM_VK_HOME |
---|
740 | event.keyCode==37 || //DOM_VK_LEFT |
---|
741 | event.keyCode==38 || //DOM_VK_UP |
---|
742 | event.keyCode==39 || //DOM_VK_RIGHT |
---|
743 | event.keyCode==40 || //DOM_VK_DOWN |
---|
744 | event.keyCode==45 || //DOM_VK_INSERT |
---|
745 | event.keyCode==46 || //DOM_VK_DELETE |
---|
746 | event.keyCode==93) //DOM_VK_CONTEXT_MENU |
---|
747 | return(true); |
---|
748 | |
---|
749 | if(event.keyCode>=96 && event.keyCode<=105 || // 0 - 9 |
---|
750 | event.keyCode==59) |
---|
751 | { |
---|
752 | var currentValue=objects.inputFTime.val(), |
---|
753 | newValue=''; |
---|
754 | |
---|
755 | if(objects.inputFTime.get(0).selectionStart!=objects.inputFTime.get(0).selectionEnd) |
---|
756 | currentValue=currentValue.substring(0,objects.inputFTime.get(0).selectionStart)+currentValue.substr(objects.inputFTime.get(0).selectionEnd); |
---|
757 | |
---|
758 | if(event.keyCode==59) |
---|
759 | { |
---|
760 | char=':'; |
---|
761 | } |
---|
762 | else char=(event.keyCode-96).toString(); |
---|
763 | |
---|
764 | newValue=currentValue; |
---|
765 | |
---|
766 | if(currentValue=='' && char>='3') |
---|
767 | newValue='0'+char+':'; |
---|
768 | if(currentValue=='' && char<'3') |
---|
769 | newValue=char; |
---|
770 | |
---|
771 | if( |
---|
772 | (currentValue=='0' || |
---|
773 | currentValue=='1' || |
---|
774 | currentValue=='2') && |
---|
775 | char==':' |
---|
776 | ) |
---|
777 | newValue='0'+currentValue+':'; |
---|
778 | |
---|
779 | if((currentValue=='0' || |
---|
780 | currentValue=='1') && |
---|
781 | char!=':') |
---|
782 | newValue=currentValue+char+':'; |
---|
783 | |
---|
784 | if(currentValue=='2' && char>='0' && char<='3') |
---|
785 | newValue=currentValue+char+':'; |
---|
786 | |
---|
787 | if(currentValue.length==2 && char==':') |
---|
788 | newValue=currentValue+char; |
---|
789 | |
---|
790 | if(currentValue.length==2 && char<='5' && char!=':') |
---|
791 | newValue=currentValue+':'+char; |
---|
792 | |
---|
793 | if(currentValue.length==3 && char<='5' && char!=':') |
---|
794 | newValue=currentValue+char; |
---|
795 | |
---|
796 | if(currentValue.length==4 && char!=':') |
---|
797 | { |
---|
798 | newValue=currentValue+char; |
---|
799 | if(newValue.length<options.timepicker.timeFormat.length) |
---|
800 | newValue=newValue+':'; |
---|
801 | } |
---|
802 | |
---|
803 | if(currentValue.length==5 && char<='5' && char!=':') |
---|
804 | newValue=currentValue+':'+char; |
---|
805 | |
---|
806 | if(currentValue.length==5 && char==':') |
---|
807 | newValue=currentValue+char; |
---|
808 | |
---|
809 | if(currentValue.length==6 && char<='5' && char!=':') |
---|
810 | newValue=currentValue+char; |
---|
811 | |
---|
812 | if(currentValue.length==7 && char!=':') |
---|
813 | newValue=currentValue+char; |
---|
814 | |
---|
815 | objects.inputFTime.val(newValue); |
---|
816 | objects.inputFTime.get(0).selectionStart=newValue.length; |
---|
817 | objects.inputFTime.get(0).selectionEnd=newValue.length; |
---|
818 | } |
---|
819 | return(false); |
---|
820 | }, //timeFKeyDown |
---|
821 | |
---|
822 | timeFChange : function (object, event) |
---|
823 | { |
---|
824 | var objects=object.data('objects'); |
---|
825 | |
---|
826 | return(privateMethods.setTimeValue(object, objects.inputFTime.val(), false, true)); |
---|
827 | }, //timeFChange |
---|
828 | |
---|
829 | |
---|
830 | |
---|
831 | |
---|
832 | /** |
---|
833 | * try to reformat a date... |
---|
834 | * |
---|
835 | * dd-mm-yyyy => yyyy-mm-dd |
---|
836 | * dd/mm/yyyy => yyyy-mm-dd |
---|
837 | * |
---|
838 | * @param String value |
---|
839 | * @return String |
---|
840 | */ |
---|
841 | reformatDate: function (value) |
---|
842 | { |
---|
843 | var d=new Date(2012,11,31), //try to see locale date settings...(2012-12-31, december=11) |
---|
844 | dMonth=1, // in local settings, month is in first position (1=mm/dd/yyyy) or in second position (2=dd/mm/yyyy) |
---|
845 | returned='', |
---|
846 | year='', |
---|
847 | month='', |
---|
848 | day='', |
---|
849 | p1='', |
---|
850 | p2='', |
---|
851 | re=[ |
---|
852 | /^\d{2}[-\/]\d{2}[-\/]\d{4}$/i, |
---|
853 | /^\d{4}[-\/]\d{2}[-\/]\d{2}$/i |
---|
854 | ]; |
---|
855 | |
---|
856 | if(d.toLocaleDateString().substr(3,2)=='12') dMonth=2; |
---|
857 | |
---|
858 | for(var i=0;i<re.length;i++) |
---|
859 | { |
---|
860 | if(re[i].test(value)) |
---|
861 | { |
---|
862 | switch(i) |
---|
863 | { |
---|
864 | case 0: |
---|
865 | year=value.substr(6,4); |
---|
866 | p1=value.substr(0,2); |
---|
867 | p2=value.substr(3,2); |
---|
868 | break; |
---|
869 | case 1: |
---|
870 | year=value.substr(0,4); |
---|
871 | p1=value.substr(5,2); |
---|
872 | p2=value.substr(8,2); |
---|
873 | break; |
---|
874 | } |
---|
875 | |
---|
876 | if(p2>'12' && p1<='12') |
---|
877 | { |
---|
878 | month=p1; |
---|
879 | day=p2; |
---|
880 | } |
---|
881 | else if(p1>'12' && p2<='12') |
---|
882 | { |
---|
883 | month=p2; |
---|
884 | day=p1; |
---|
885 | } |
---|
886 | else if(i==0) |
---|
887 | { |
---|
888 | switch(dMonth) |
---|
889 | { |
---|
890 | case 1: |
---|
891 | month=p1; |
---|
892 | day=p2; |
---|
893 | break; |
---|
894 | case 2: |
---|
895 | month=p2; |
---|
896 | day=p1; |
---|
897 | break; |
---|
898 | } |
---|
899 | } |
---|
900 | else |
---|
901 | { |
---|
902 | month=p1; |
---|
903 | day=p2; |
---|
904 | } |
---|
905 | |
---|
906 | return(year+'-'+month+'-'+day); |
---|
907 | } |
---|
908 | } |
---|
909 | |
---|
910 | return(value); |
---|
911 | }, //reformatDate |
---|
912 | |
---|
913 | reformatTime : function (value, timeFormat) |
---|
914 | { |
---|
915 | var re=/^(\d{1,2}):(\d{2})(?::(\d{2})){0,1}$/i, |
---|
916 | hms=[]; |
---|
917 | |
---|
918 | if(re.test(value)) |
---|
919 | { |
---|
920 | hms=re.exec(value); |
---|
921 | |
---|
922 | if(hms[1].length==1) hms[1]='0'+hms[1]; |
---|
923 | if(hms[3]==null && timeFormat=='hh:mm:ss') |
---|
924 | hms[3]='00'; //assuming the seconds equals... |
---|
925 | |
---|
926 | hms[0]=hms[1]+':'+hms[2]; |
---|
927 | if(timeFormat=='hh:mm:ss') hms[0]=hms[0]+':'+hms[3]; |
---|
928 | |
---|
929 | return(hms[0]); |
---|
930 | } |
---|
931 | return(value); |
---|
932 | } //reformatTime |
---|
933 | |
---|
934 | }; |
---|
935 | |
---|
936 | |
---|
937 | $.fn.inputDate = function(method) |
---|
938 | { |
---|
939 | if(publicMethods[method]) |
---|
940 | { |
---|
941 | return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); |
---|
942 | } |
---|
943 | else if(typeof method === 'object' || ! method) |
---|
944 | { |
---|
945 | return publicMethods.init.apply(this, arguments); |
---|
946 | } |
---|
947 | else |
---|
948 | { |
---|
949 | $.error( 'Method ' + method + ' does not exist on jQuery.inputDate' ); |
---|
950 | } |
---|
951 | } // $.fn.inputDate |
---|
952 | |
---|
953 | } |
---|
954 | )(jQuery); |
---|
955 | |
---|
956 | |
---|