source: extensions/GrumPluginClasses/js/ui.inputDate.js @ 17759

Last change on this file since 17759 was 17759, checked in by grum, 12 years ago

version 3.5.4
inputDate

. add possibility for time to be optional

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