source: extensions/GrumPluginClasses/js/ui.inputConsole.js @ 8961

Last change on this file since 8961 was 8961, checked in by grum, 13 years ago

release 3.4.0
fix bug:1984, bug:2109
js file are minified, remove packed files

  • Property svn:executable set to *
File size: 18.2 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.inputConsole.js
4 * file version: 1.0.0
5 * date: 2010-11-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 *   PWG user : http://forum.phpwebgallery.net/profile.php?id=3706
14 *
15 *   << May the Little SpaceFrog be with you ! >>
16 * -----------------------------------------------------------------------------
17 *
18 *
19 *
20 *
21 * :: HISTORY ::
22 *
23 * | release | date       |
24 * | 1.0.0   | 2010/10/10 | first release
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                      disabled:false,
56                      prompt:'>',
57                      historySize:8,
58                      historyHeight:60,
59                      change:null,
60                      submit:null
61                    };
62
63              // if options given, merge it
64              // if(opt) $.extend(options, opt); ==> options are set by setters
65
66              $this.data('options', options);
67
68
69              if(!properties)
70              {
71                $this.data('properties',
72                  {
73                    initialized:false,
74                    value:'',
75                    isValid:true,
76                    mouseIsOver:false,
77                    historyIsVisible:false,
78                    inputMargins:0
79                  }
80                );
81                properties=$this.data('properties');
82              }
83
84              if(!objects)
85              {
86                objects =
87                  {
88                    container:$('<div/>',
89                        {
90                          'class':'ui-inputConsole',
91                          css:{
92                            width:'100%'
93                          }
94                        }
95                    ).bind('click.inputConsole',
96                        function ()
97                        {
98                          objects.input.focus();
99                        }
100                      )
101                    .bind('mouseenter',
102                        function ()
103                        {
104                          properties.mouseIsOver=true;
105                        }
106                      )
107                    .bind('mouseleave',
108                        function ()
109                        {
110                          properties.mouseIsOver=false;
111                        }
112                      ),
113                    inputContainer:$('<div/>',
114                      {
115                        'class':'ui-inputConsole-input'
116                      }
117                    ),
118                    input:$('<input>',
119                      {
120                        type:"text",
121                        value:''
122                      }
123                    ).bind('focusout.inputConsole',
124                        function ()
125                        {
126                          privateMethods.lostFocus($this);
127                        }
128                      )
129                      .bind('focus.inputConsole',
130                          function ()
131                          {
132                            privateMethods.getFocus($this);
133                          }
134                        )
135                      .bind('keyup.inputConsole',
136                          function (event)
137                          {
138                            privateMethods.keyUp($this, event);
139                          }
140                        ),
141                    prompt:$('<div/>',
142                      {
143                        html:options.prompt,
144                        'class':'ui-inputConsole-prompt'
145                      }
146                    ),
147                    historyContainer:$('<div/>',
148                      {
149                        'class':'ui-inputConsole-history',
150                        css:{
151                          display:'none'
152                        }
153                      }
154                    ),
155                    historyList:$('<ul/>')
156
157                  };
158
159                $this
160                  .html('')
161                  .append(
162                    objects.container
163                    .append(
164                      objects.historyContainer.append(objects.historyList)
165                    )
166                    .append(
167                      objects.inputContainer.append(objects.prompt).append(objects.input)
168                    )
169                  ).bind('resize.inputConsole',
170                        function ()
171                        {
172                          privateMethods.setObjectsWidth($this);
173                        }
174                      );
175
176                properties.inputMargins=objects.input.outerWidth(true)-objects.input.width();
177
178                $this.data('objects', objects);
179              }
180
181              privateMethods.setOptions($this, opt);
182            }
183          );
184        }, // init
185      destroy : function ()
186        {
187          return this.each(
188            function()
189            {
190              // default values for the plugin
191              var $this=$(this),
192                  objects = $this.data('objects');
193              objects.input.unbind().remove();
194              objects.container.unbind().remove();
195              $this
196                .unbind('.inputConsole')
197                .css(
198                  {
199                    width:'',
200                    height:''
201                  }
202                );
203            }
204          );
205        }, // destroy
206
207      options: function (value)
208        {
209          return this.each(function()
210            {
211              privateMethods.setOptions($(this), value);
212            }
213          );
214        }, // options
215
216      disabled: function (value)
217        {
218          if(value!=null)
219          {
220            return this.each(function()
221              {
222                privateMethods.setDisabled($(this), value);
223              }
224            );
225          }
226          else
227          {
228            var options = this.data('options');
229
230            if(options)
231            {
232              return(options.disabled);
233            }
234            else
235            {
236              return('');
237            }
238          }
239        }, // disabled
240
241
242      prompt: function (value)
243        {
244          if(value!=null)
245          {
246            return this.each(function()
247              {
248                privateMethods.setPrompt($(this), value);
249              }
250            );
251          }
252          else
253          {
254            var options = this.data('options');
255
256            if(options)
257            {
258              return(options.prompt);
259            }
260            else
261            {
262              return('');
263            }
264          }
265        }, // prompt
266
267      historySize: function (value)
268        {
269          if(value!=null)
270          {
271            return this.each(function()
272              {
273                privateMethods.setHistorySize($(this), value);
274              }
275            );
276          }
277          else
278          {
279            var options = this.data('options');
280
281            if(options)
282            {
283              return(options.historySize);
284            }
285            else
286            {
287              return('');
288            }
289          }
290        }, // historySize
291
292      historyHeight: function (value)
293        {
294          if(value!=null)
295          {
296            return this.each(function()
297              {
298                privateMethods.setHistoryHeight($(this), value);
299              }
300            );
301          }
302          else
303          {
304            var options = this.data('options');
305
306            if(options)
307            {
308              return(options.historyHeight);
309            }
310            else
311            {
312              return('');
313            }
314          }
315        }, // historyHeight
316
317      value: function (value)
318        {
319          if(value!=null)
320          {
321            // set selected value
322            return this.each(function()
323              {
324                privateMethods.setValue($(this), value, true);
325              }
326            );
327          }
328          else
329          {
330            // return the selected tags
331            var properties=this.data('properties');
332            return(properties.value);
333          }
334        }, // value
335
336      history: function (value)
337        {
338          var objects=this.data('objects');
339
340          if(value!=null)
341          {
342            // set selected value
343            return this.each(function()
344              {
345                if(value=='clear')
346                {
347                  objects.historyList.html('');
348                }
349              }
350            );
351          }
352          else
353          {
354            // return the selected tags
355            var returned=[];
356            objects.historyList.children().each(
357              function (index, item)
358              {
359                returned.push($(item).text());
360              }
361            );
362            return(returned);
363          }
364        }, // value
365
366      isValid: function (value)
367        {
368          if(value!=null)
369          {
370            // set selected value
371            return this.each(function()
372              {
373                privateMethods.setIsValid($(this), value);
374              }
375            );
376          }
377          else
378          {
379            // return the selected tags
380            var properties=this.data('properties');
381            return(properties.isValid);
382          }
383        }, // isValid
384
385      change: function (value)
386        {
387          if(value!=null && $.isFunction(value))
388          {
389            // set selected value
390            return this.each(function()
391              {
392                privateMethods.setEventChange($(this), value);
393              }
394            );
395          }
396          else
397          {
398            // return the selected value
399            var options=this.data('options');
400
401            if(options)
402            {
403              return(options.change);
404            }
405            else
406            {
407              return(null);
408            }
409          }
410        }, // change
411
412
413      submit: function (value)
414        {
415          if(value!=null && $.isFunction(value))
416          {
417            // set selected value
418            return this.each(function()
419              {
420                privateMethods.setEventSubmit($(this), value);
421              }
422            );
423          }
424          else
425          {
426            // return the selected value
427            var options=this.data('options');
428
429            if(options)
430            {
431              return(options.submit);
432            }
433            else
434            {
435              return(null);
436            }
437          }
438        } // submit
439
440
441    }; // methods
442
443
444    /*
445     * plugin 'private' methods
446     */
447    var privateMethods =
448    {
449      setOptions : function (object, value)
450        {
451          var properties=object.data('properties'),
452              options=object.data('options');
453
454          if(!$.isPlainObject(value)) return(false);
455
456          properties.initialized=false;
457
458          privateMethods.setHistoryHeight(object, (value.historyHeight!=null)?value.historyHeight:options.historyHeight);
459          privateMethods.setHistorySize(object, (value.historySize!=null)?value.historySize:options.historySize);
460          privateMethods.setPrompt(object, (value.prompt!=null)?value.prompt:options.prompt);
461          privateMethods.setValue(object, (value.value!=null)?value.value:options.value, true);
462          privateMethods.setDisabled(object, (value.disabled!=null)?value.disabled:options.disabled);
463
464          privateMethods.setEventChange(object, (value.change!=null)?value.change:options.change);
465          privateMethods.setEventSubmit(object, (value.submit!=null)?value.submit:options.submit);
466
467          properties.initialized=true;
468        },
469
470      setPrompt: function (object, value)
471        {
472          var objects=object.data('objects'),
473              options=object.data('options'),
474              properties=object.data('properties');
475
476          if(!properties.initialized || options.prompt!=value)
477          {
478            options.prompt=value;
479            objects.prompt.html(options.prompt);
480            privateMethods.setObjectsWidth(object);
481          }
482          return(options.prompt);
483        },
484
485      setHistorySize: function (object, value)
486        {
487          var options=object.data('options'),
488              properties=object.data('properties');
489
490          if(!properties.initialized || options.historySize!=value)
491          {
492            options.historySize=value;
493            privateMethods.updateHistory(object, null);
494          }
495          return(options.historySize);
496        },
497
498      setHistoryHeight: function (object, value)
499        {
500          var objects=object.data('objects'),
501              options=object.data('options'),
502              properties=object.data('properties');
503
504          if(!properties.initialized || options.historyHeight!=value)
505          {
506            options.historyHeight=value;
507            objects.historyContainer.css(
508              {
509                height:options.historyHeight+'px',
510                'margin-top':(-options.historyHeight)+'px'
511              }
512            );
513          }
514          return(options.historyHeight);
515        },
516
517      setIsValid : function (object, value)
518        {
519          var objects=object.data('objects'),
520              properties=object.data('properties');
521
522          if(properties.isValid!=value)
523          {
524            properties.isValid=value;
525            if(properties.isValid)
526            {
527              objects.container.removeClass('ui-error');
528              objects.input.removeClass('ui-error');
529            }
530            else
531            {
532              objects.container.addClass('ui-error');
533              objects.input.addClass('ui-error');
534            }
535          }
536          return(properties.isValid);
537        },
538
539      setDisabled : function (object, value)
540        {
541          var options=object.data('options'),
542              properties=object.data('properties');
543
544          if((!properties.initialized || options.disabled!=value) && (value==true || value==false))
545          {
546            options.disabled=value;
547
548          }
549          return(options.disabled);
550        },
551
552      setValue : function (object, value, apply)
553        {
554          var options=object.data('options'),
555              properties=object.data('properties'),
556              objects=object.data('objects');
557
558          properties.value=value;
559
560          if(apply) objects.input.val(properties.value);
561
562          if(options.change) object.trigger('inputConsoleChange', properties.value);
563
564          return(true);
565        }, //setValue
566
567
568      getFocus : function (object)
569        {
570          var objects=object.data('objects');
571
572          objects.historyContainer.css('display', 'block');
573          privateMethods.setObjectsWidth(object);
574        },
575
576      lostFocus : function (object)
577        {
578          var objects=object.data('objects');
579
580          objects.historyContainer.css('display', 'none');
581        },
582
583      setEventChange : function (object, value)
584        {
585          var options=object.data('options');
586
587          options.change=value;
588          object.unbind('inputConsoleChange');
589          if(value) object.bind('inputConsoleChange', options.change);
590          return(options.change);
591        },
592
593      setEventSubmit : function (object, value)
594        {
595          var options=object.data('options');
596
597          options.submit=value;
598          object.unbind('inputConsoleSubmit');
599          if(value) object.bind('inputConsoleSubmit', options.submit);
600          return(options.submit);
601        },
602
603      keyUp : function (object, event)
604        {
605          var properties=object.data('properties'),
606              options=object.data('options'),
607              objects=object.data('objects');
608
609          if(event.keyCode==13 && // DOM_VK_ENTER
610             properties.isValid)
611          {
612            if(options.submit) object.trigger('inputConsoleSubmit', properties.value);
613            privateMethods.updateHistory(object, properties.value);
614            privateMethods.setValue(object, '', true);
615          }
616          else
617          {
618            privateMethods.setValue(object, objects.input.val(), false);
619          }
620        },
621
622      updateHistory : function (object, item)
623        {
624          var options=object.data('options'),
625              objects=object.data('objects');
626
627          if(item!='' && item!=null) objects.historyList.append($('<li/>', { html: item }));
628
629          while(objects.historyList.children().length>options.historySize)
630          {
631            objects.historyList.children(':first').remove();
632          }
633          objects.historyContainer.scrollTop(objects.historyList.height());
634        },
635
636      setObjectsWidth : function (object)
637        {
638          var objects=object.data('objects')
639              properties=object.data('properties');
640
641          if(objects.inputContainer.width()>0)
642          {
643            objects.input.css('width',
644              (objects.inputContainer.innerWidth()-objects.prompt.outerWidth(true)-properties.inputMargins)+'px'
645            );
646            objects.historyContainer.css(
647              {
648                width:objects.inputContainer.innerWidth()+'px',
649                'margin-left':((objects.historyContainer.width()-objects.historyContainer.outerWidth())/2)+'px'
650              }
651            );
652          }
653        }
654
655    };
656
657
658    $.fn.inputConsole = function(method)
659    {
660      if(publicMethods[method])
661      {
662        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
663      }
664      else if(typeof method === 'object' || ! method)
665      {
666        return publicMethods.init.apply(this, arguments);
667      }
668      else
669      {
670        $.error( 'Method ' +  method + ' does not exist on jQuery.inputConsole' );
671      }
672    } // $.fn.inputConsole
673
674  }
675)(jQuery);
676
677
Note: See TracBrowser for help on using the repository browser.