source: extensions/GrumPluginClasses/js/ui.inputList.js @ 16012

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

feature:2634- compatibility with Piwigo 2.4
+add some objects on js framework

  • Property svn:executable set to *
File size: 47.0 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.inputList.js
4 * file version: 1.0.1
5 * date: 2012-05-25
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   | 2010/10/10 | first release
24 * |         |            |
25 * | 1.0.1   | 2012/06/18 | * fix bug with jquery 1.7.2
26 * |         |            |   . display list now works :)
27 * |         |            |
28 * |         |            | * improve memory managment
29 * |         |            |
30 * |         |            |
31 * |         |            |
32 *
33 */
34
35
36
37(
38  function($)
39  {
40    /*
41     * plugin 'public' functions
42     */
43    var publicMethods =
44    {
45      init : function (opt)
46        {
47          return this.each(function()
48            {
49              // default values for the plugin
50              var $this=$(this),
51                  timeStamp=new Date(),
52                  data = $this.data('options'),
53                  objects = $this.data('objects'),
54                  properties = $this.data('properties'),
55                  options =
56                    {
57                      serverUrl:'',
58                      postData:{},
59                      autoLoad:true,
60                      listMaxWidth:0,
61                      listMaxHeight:0,
62                      multiple:false,
63                      downArrow:'', //&dArr;
64                      popupMode:'click',
65                      colsWidth:[],
66                      colsDisplayed:[],
67                      colsCss:[],
68                      disabled:false,
69                      popup:null,
70                      change:null,
71                      load:null,
72                      returnMode:'selected'
73                    };
74
75              // if options given, merge it
76              // if(opt) $.extend(options, opt); ==> options are set by setters
77
78              $this.data('options', options);
79
80              if(!properties)
81              {
82                $this.data('properties',
83                  {
84                    objectId:'il'+Math.ceil(timeStamp.getTime()*Math.random()),
85                    index:-1,
86                    initialized:false,
87                    selectorVisible:false,
88                    items:[],
89                    mouseOver:false,
90                    isValid:true,
91                    firstPopup:true
92                  }
93                );
94                properties=$this.data('properties');
95              }
96
97              if(!objects)
98              {
99                objects =
100                  {
101                    container:$('<div/>',
102                        {
103                          'class':'ui-inputList',
104                          tabindex:0,
105                          css:{
106                            width:'100%'
107                          }
108                        }
109                    ).bind('click.inputList',
110                        function ()
111                        {
112                          privateMethods.displaySelector($this, !$this.data('properties').selectorVisible);
113                          //$(this).focus();  // if get the focus, it hide the dorp-down list.. ?
114                        }
115                      ),
116                    containerValue:$('<div/>',
117                      {
118                        html: '&nbsp;',
119                        'class':'ui-inputList-value',
120                        css:{
121                          overflow:'hidden'
122                        }
123                      }
124                    ),
125                    containerList:null,
126                    containerArrow:$('<div/>',
127                      {
128                        html: '&dArr;',
129                        'class':'ui-inputList-arrow',
130                        css: {
131                          'float':'right',
132                          cursor:'pointer'
133                        }
134                      }
135                    ).bind('mousedown',
136                        function ()
137                        {
138                          $(this).addClass('ui-inputList-arrow-active');
139                        }
140                    ).bind('mouseup',
141                        function ()
142                        {
143                          $(this).removeClass('ui-inputList-arrow-active');
144                        }
145                    ),
146                    listContainer:$('<div/>',
147                        {
148                          html: "",
149                          'class':'ui-inputList-list',
150                          css: {
151                            overflow:"auto",
152                            display:'none',
153                            position:'absolute'
154                          }
155                        }
156                    ),
157                    list:$('<ul/>',
158                      {
159                        css: {
160                          listStyle:'none',
161                          padding:'0px',
162                          margin:'0px'
163                        }
164                      }
165                    )
166                  };
167              }
168
169              $this.data('objects', objects);
170              privateMethods.setOptions($this, opt);
171
172              if($this.text()!='')
173              {
174                var tmp=$.parseJSON($.trim($this.text())),
175                    selectedValues=[],
176                    values=[];
177
178                if($.isArray(tmp))
179                {
180                  values=tmp;
181                }
182                else if(tmp.values!=null)
183                {
184                  values=tmp.values;
185                }
186
187                if(tmp.selected!=null) selectedValues=tmp.selected;
188
189                privateMethods.setItems($this, values);
190                privateMethods.setValue($this, selectedValues);
191
192              }
193
194              $this
195                .html('')
196                .append(objects.container.append(objects.containerArrow).append(objects.containerValue))
197                .append(objects.listContainer.append(objects.list));
198
199            }
200          );
201        }, // init
202      destroy : function ()
203        {
204          return this.each(
205            function()
206            {
207              // default values for the plugin
208              var $this=$(this),
209                  properties = $this.data('properties'),
210                  objects = $this.data('objects');
211              objects.container.unbind().remove();
212              objects.list.children().unbind();
213              objects.listContainer.unbind().remove();
214              $(document).unbind('focusout.'+properties.objectId+' focusin.'+properties.objectId);
215              $this
216                .removeData()
217                .unbind('.inputList')
218                .css(
219                  {
220                    width:'',
221                    height:''
222                  }
223                );
224              delete $this;
225            }
226          );
227        }, // destroy
228
229      options: function (value)
230        {
231          return this.each(function()
232            {
233              privateMethods.setOptions($(this), value);
234            }
235          );
236        }, // autoLoad
237
238      autoLoad: function (value)
239        {
240          if(value!=null)
241          {
242            return this.each(function()
243              {
244                privateMethods.setAutoLoad($(this), value);
245              }
246            );
247          }
248          else
249          {
250            var options = this.data('options');
251
252            if(options)
253            {
254              return(options.autoLoad);
255            }
256            else
257            {
258              return(true);
259            }
260          }
261        }, // autoLoad
262
263      listMaxWidth: function (value)
264        {
265          if(value!=null)
266          {
267            return this.each(function()
268              {
269                privateMethods.setListMaxWidth($(this), value);
270              }
271            );
272          }
273          else
274          {
275            var options = this.data('options');
276
277            if(options)
278            {
279              return(options.listMaxWidth);
280            }
281            else
282            {
283              return(0);
284            }
285          }
286        }, // listMaxWidth
287
288      listMaxHeight: function (value)
289        {
290          if(value!=null)
291          {
292            return this.each(function()
293              {
294                privateMethods.setListMaxHeight($(this), value);
295              }
296            );
297          }
298          else
299          {
300            var options = this.data('options');
301
302            if(options)
303            {
304              return(options.listMaxHeight);
305            }
306            else
307            {
308              return(0);
309            }
310          }
311        }, // listMaxHeight
312
313      serverUrl: function (value)
314        {
315          if(value!=null)
316          {
317            return this.each(function()
318              {
319                privateMethods.setServerUrl($(this), value);
320              }
321            );
322          }
323          else
324          {
325            var options = this.data('options');
326
327            if(options)
328            {
329              return(options.serverUrl);
330            }
331            else
332            {
333              return('');
334            }
335          }
336        }, // serverUrl
337
338      postData: function (value)
339        {
340          if(value!=null)
341          {
342            // set selected value
343            return(
344              this.each(
345                function()
346                {
347                  privateMethods.setPostData($(this), value, true);
348                }
349              )
350            );
351          }
352          else
353          {
354            var options=this.data('options');
355            return(options.postData);
356          }
357        }, // postData
358
359      cols: function ()
360        {
361          var options=this.data('options'),
362              properties=this.data('properties');
363
364          if(!options.multiple)
365          {
366            return(properties.items[properties.index].cols);
367          }
368          else
369          {
370            var listCols=[];
371            for(var i=0;i<properties.index.length;i++)
372            {
373              listCols.push(properties.items[properties.index[i]].cols);
374            }
375            return(listCols);
376          }
377        }, // name
378
379      popupMode: function (value)
380        {
381          if(value!=null)
382          {
383            return this.each(function()
384              {
385                privateMethods.setPopupMode($(this), value);
386              }
387            );
388          }
389          else
390          {
391            var options = this.data('options');
392
393            if(options)
394            {
395              return(options.popupMode);
396            }
397            else
398            {
399              return(0);
400            }
401          }
402        }, // popupMode
403
404      downArrow: function (value)
405        {
406          if(value!=null)
407          {
408            return this.each(function()
409              {
410                privateMethods.setDownArrow($(this), value);
411              }
412            );
413          }
414          else
415          {
416            var options = this.data('options');
417
418            if(options)
419            {
420              return(options.downArrow);
421            }
422            else
423            {
424              return('');
425            }
426          }
427        }, // downArrow
428
429
430      returnMode: function (value)
431        {
432          if(value!=null)
433          {
434            return this.each(function()
435              {
436                privateMethods.setReturnMode($(this), value);
437              }
438            );
439          }
440          else
441          {
442            var options = this.data('options');
443
444            if(options)
445            {
446              return(options.returnMode);
447            }
448            else
449            {
450              return('selected');
451            }
452          }
453        }, // returnMode
454
455      colsWidth: function (value)
456        {
457          if(value!=null)
458          {
459            return this.each(function()
460              {
461                privateMethods.setColsWidth($(this), value);
462              }
463            );
464          }
465          else
466          {
467            var options = this.data('options');
468
469            if(options)
470            {
471              return(options.colsWidth);
472            }
473            else
474            {
475              return('');
476            }
477          }
478        }, // colsWidth
479
480      colsDisplayed: function (value)
481        {
482          if(value!=null)
483          {
484            return this.each(function()
485              {
486                privateMethods.setColsDisplayed($(this), value);
487              }
488            );
489          }
490          else
491          {
492            var options = this.data('options');
493
494            if(options)
495            {
496              return(options.colsDisplayed);
497            }
498            else
499            {
500              return('');
501            }
502          }
503        }, // colsDisplayed
504
505      colsCss: function (value)
506        {
507          if(value!=null)
508          {
509            return this.each(function()
510              {
511                privateMethods.setColsCss($(this), value);
512              }
513            );
514          }
515          else
516          {
517            var options = this.data('options');
518
519            if(options)
520            {
521              return(options.colsCss);
522            }
523            else
524            {
525              return('');
526            }
527          }
528        }, // colsCss
529
530
531      items: function (value)
532        {
533          if(value!=null)
534          {
535            return this.each(function()
536              {
537                privateMethods.setItems($(this), value);
538              }
539            );
540          }
541          else
542          {
543            var properties = this.data('properties');
544
545            if(properties)
546            {
547              return(properties.items);
548            }
549            else
550            {
551              return('');
552            }
553          }
554        }, //items
555
556      value: function (value)
557        {
558          if(value!=null)
559          {
560            // set selected value
561            return this.each(function()
562              {
563                privateMethods.setValue($(this), value);
564              }
565            );
566          }
567          else
568          {
569            // return the selected value
570            var properties=this.data('properties'),
571                options = this.data('options');
572
573            if(properties && properties.index!=null && !options.multiple && properties.index>-1 && properties.index<properties.items.length)
574            {
575              return(properties.items[properties.index].value);
576            }
577            else if(properties && properties.index!=null && options.multiple)
578            {
579              var returned=[];
580              if(options.returnMode=='selected')
581              {
582                for(var i=0;i<properties.index.length;i++)
583                {
584                  if(properties.index[i]>-1 && properties.index[i]<properties.items.length)
585                    returned.push(properties.items[properties.index[i]].value);
586                }
587              }
588              else
589              {
590                for(var i=0;i<properties.items.length;i++)
591                {
592                  if($.inArray(i, properties.index)==-1)
593                    returned.push(properties.items[i].value);
594                }
595              }
596              return(returned);
597            }
598            else
599            {
600              return(null);
601            }
602          }
603        }, // value
604
605      isValid: function (value)
606        {
607          if(value!=null)
608          {
609            // set selected value
610            return this.each(function()
611              {
612                privateMethods.setIsValid($(this), value);
613              }
614            );
615          }
616          else
617          {
618            // return the selected tags
619            var properties=this.data('properties');
620            return(properties.isValid);
621          }
622        }, // isValid
623
624      load: function (value)
625        {
626          /*
627           * two functionnalities :
628           *  - if value is set, use it to set the load event function
629           *  - if no value, loads data from server
630           */
631          if(value && $.isFunction(value))
632          {
633            // set selected value
634            return this.each(function()
635              {
636                privateMethods.setEventLoad($(this), value);
637              }
638            );
639          }
640          else
641          {
642            // loads data from server
643            privateMethods.load(this);
644          }
645        },
646
647      popup: function (value)
648        {
649          if(value && $.isFunction(value))
650          {
651            // set selected value
652            return this.each(function()
653              {
654                privateMethods.setEventPopup($(this), value);
655              }
656            );
657          }
658          else
659          {
660            // return the selected value
661            var options=this.data('options');
662
663            if(options)
664            {
665              return(options.popup);
666            }
667            else
668            {
669              return(null);
670            }
671          }
672        }, // popup
673
674      change: function (value)
675        {
676          if(value && $.isFunction(value))
677          {
678            // set selected value
679            return this.each(function()
680              {
681                privateMethods.setEventChange($(this), value);
682              }
683            );
684          }
685          else
686          {
687            // return the selected value
688            var options=this.data('options');
689
690            if(options)
691            {
692              return(options.change);
693            }
694            else
695            {
696              return(null);
697            }
698          }
699        }, // popup
700
701      numberOfItems: function ()
702        {
703          var properties=this.data('properties');
704
705          if(properties)
706          {
707            return(properties.items.length);
708          }
709          else
710          {
711            return(null);
712          }
713        }, // numberOfItems
714
715      properties: function (value)
716        {
717          var properties=this.data('properties'),
718              options=this.data('options');
719
720          if(properties && value==':first' && properties.items.length>0)
721          {
722            return(properties.items[0]);
723          }
724          else if(properties && properties.index!=null && (value==':selected' || value==null) && properties.items.length>0)
725          {
726            if(!options.multiple && properties.index>-1 && properties.index<properties.items.length)
727            {
728              return(properties.items[properties.index]);
729            }
730            else if(options.multiple)
731            {
732              var returned=[];
733              for(var i=0;i<properties.index.length;i++)
734              {
735                if(properties.index[i]>-1 && properties.index<properties.items.length)
736                  returned.push(properties.items[properties.index[i]]);
737              }
738              return(returned);
739            }
740            return(null);
741          }
742          else if(properties && value!=null)
743          {
744            var index=privateMethods.findIndexByValue(this, value);
745            if(index>-1)
746            {
747              return(properties.items[index]);
748            }
749            return(null);
750          }
751          else
752          {
753            return(null);
754          }
755        } // properties
756    }; // methods
757
758
759    /*
760     * plugin 'private' methods
761     */
762    var privateMethods =
763    {
764      setOptions : function (object, value)
765        {
766          var properties=object.data('properties'),
767              options=object.data('options');
768
769          if(!$.isPlainObject(value)) return(false);
770
771          properties.initialized=false;
772
773          privateMethods.setReturnMode(object, (value.returnMode!=null)?value.returnMode:options.returnMode);
774          privateMethods.setAutoLoad(object, (value.autoLoad!=null)?value.autoLoad:options.autoLoad);
775          privateMethods.setListMaxWidth(object, (value.listMaxWidth!=null)?value.listMaxWidth:options.listMaxWidth);
776          privateMethods.setListMaxHeight(object, (value.listMaxHeight!=null)?value.listMaxHeight:options.listMaxHeight);
777          privateMethods.setPostData(object, (value.postData!=null)?value.postData:options.postData);
778          privateMethods.setServerUrl(object, (value.serverUrl!=null)?value.serverUrl:options.serverUrl);
779          privateMethods.setPopupMode(object, (value.popupMode!=null)?value.popupMode:options.popupMode);
780          privateMethods.setDownArrow(object, (value.downArrow!=null)?value.downArrow:options.downArrow);
781          privateMethods.setEventPopup(object, (value.popup!=null)?value.popup:options.popup);
782          privateMethods.setEventChange(object, (value.change!=null)?value.change:options.change);
783          privateMethods.setEventLoad(object, (value.load!=null)?value.load:options.load);
784          privateMethods.setColsWidth(object, (value.colsWidth!=null)?value.colsWidth:options.colsWidth);
785          privateMethods.setColsDisplayed(object, (value.colsDisplayed!=null)?value.colsDisplayed:options.colsDisplayed);
786          privateMethods.setColsCss(object, (value.colsCss!=null)?value.colsCss:options.colsCss);
787          privateMethods.setItems(object, (value.items!=null)?value.items:options.items);
788          privateMethods.setMultiple(object, (value.multiple!=null)?value.multiple:options.multiple); // can be set only at the initialization
789
790          if(options.autoLoad && options.serverUrl!='')
791          {
792            privateMethods.load(object, (value.value!=null)?value.value:null);
793          }
794          else
795          {
796            privateMethods.setValue(object, (value.value!=null)?value.value:null);
797          }
798
799          properties.initialized=true;
800        },
801
802      setIsValid : function (object, value)
803        {
804          var objects=object.data('objects'),
805              properties=object.data('properties');
806
807          if(properties.isValid!=value)
808          {
809            properties.isValid=value;
810            if(properties.isValid)
811            {
812              objects.container.removeClass('ui-error');
813            }
814            else
815            {
816              objects.container.addClass('ui-error');
817            }
818          }
819          return(properties.isValid);
820        },
821
822      setAutoLoad : function (object, value)
823        {
824          var options=object.data('options'),
825              properties=object.data('properties');
826
827          if((!properties.initialized || options.autoLoad!=value) && (value==true || value==false))
828          {
829            options.autoLoad=value;
830          }
831          return(options.autoLoad);
832        },
833
834      setReturnMode : function (object, value)
835        {
836          var options=object.data('options'),
837              properties=object.data('properties');
838
839          if((!properties.initialized || options.returnMode!=value) && (value=='selected' || value=='notSelected'))
840          {
841            options.returnMode=value;
842          }
843          return(options.returnMode);
844        },
845
846      setColsWidth : function (object, value)
847        {
848          var options=object.data('options'),
849              properties=object.data('properties'),
850              width=0;
851
852          if((!properties.initialized || options.colsWidth!=value) && $.isArray(value))
853          {
854            options.colsWidth=value;
855          }
856          return(options.colsWidth);
857        },
858
859      setColsDisplayed : function (object, value)
860        {
861          var options=object.data('options'),
862              properties=object.data('properties');
863
864          if((!properties.initialized || options.colsDisplayed!=value) && $.isArray(value))
865          {
866            options.colsDisplayed=value;
867          }
868          return(options.colsDisplayed);
869        },
870
871      setColsCss : function (object, value)
872        {
873          var options=object.data('options'),
874              properties=object.data('properties');
875
876          if((!properties.initialized || options.colsCss!=value) && $.isArray(value))
877          {
878            options.colsCss=value;
879          }
880          return(options.colsCss);
881        },
882
883      setListMaxWidth : function (object, value)
884        {
885          var options=object.data('options'),
886              properties=object.data('properties'),
887              objects=object.data('objects');
888
889          if((!properties.initialized || options.listMaxWidth!=value) && value>=0)
890          {
891            options.listMaxWidth=value;
892            if(options.listMaxWidth>0)
893            {
894              objects.listContainer.css('max-width', options.listMaxWidth+'px');
895            }
896            else
897            {
898              objects.listContainer.css('max-width', '');
899            }
900          }
901          return(options.listMaxWidth);
902        },
903
904      setListMaxHeight : function (object, value)
905        {
906          var options=object.data('options'),
907              properties=object.data('properties'),
908              objects=object.data('objects');
909
910          if((!properties.initialized || options.listMaxHeight!=value) && value>=0)
911          {
912            options.listMaxHeight=value;
913            if(options.listMaxHeight>0)
914            {
915              objects.listContainer.css('max-height', options.listMaxHeight+'px');
916            }
917            else
918            {
919              objects.listContainer.css('max-height', '');
920            }
921          }
922          return(options.listMaxHeight);
923        },
924
925      setServerUrl : function (object, value)
926        {
927          var options=object.data('options'),
928              properties=object.data('properties');
929
930          if(!properties.initialized || options.serverUrl!=value)
931          {
932            options.serverUrl=value;
933            if(options.autoLoad && properties.initialized) privateMethods.load(object);
934          }
935          return(options.serverUrl);
936        },
937
938      setPostData : function (object, value)
939        {
940          var properties=object.data('properties'),
941              options=object.data('options');
942
943          if(!properties.initialized || value!=options.postData)
944          {
945            options.postData=value;
946          }
947
948          return(options.postData);
949        }, // setPostData
950
951      setMultiple : function (object, value)
952        {
953          var options=object.data('options'),
954              properties=object.data('properties'),
955              objects=object.data('objects');
956
957          if((!properties.initialized || options.multiple!=value) && (value==true || value==false))
958          {
959            if(!value)
960            {
961              properties.index=-1;
962              if(objects.containerList!=null)
963              {
964                objects.containerList.remove();
965                objects.containerList=null;
966              }
967            }
968            else
969            {
970              properties.index=[];
971              objects.listContainer.addClass('ui-inputList-multiple');
972              if(objects.containerList==null)
973              {
974                objects.containerList=$('<ul/>',
975                  {
976                    html:'<li>&nbsp;</li>'
977                  }
978                );
979                objects.containerValue.html('').append(objects.containerList);
980              }
981            }
982            options.multiple=value;
983          }
984          return(options.multiple);
985        }, //setMultiple
986
987      setPopupMode : function (object, value)
988        {
989          var options=object.data('options'),
990              properties=object.data('properties'),
991              objects=object.data('objects');
992
993          if((!properties.initialized || options.popupMode!=value) && (value=='click' || value=='mouseout'))
994          {
995            options.popupMode=value;
996
997            if(value=='mouseout')
998            {
999              objects.listContainer
1000                .unbind('mouseleave.inputList')
1001                .unbind('mouseenter.inputList')
1002                .bind('mouseleave.inputList',
1003                  function ()
1004                  {
1005                    privateMethods.displaySelector(object, false);
1006                  }
1007                );
1008            }
1009            else
1010            {
1011              objects.listContainer
1012                .unbind('mouseleave.inputList')
1013                .bind('mouseleave.inputList',
1014                  function ()
1015                  {
1016                    properties.mouseOver=false;
1017                  }
1018                )
1019                .bind('mouseenter.inputList',
1020                  function ()
1021                  {
1022                    properties.mouseOver=true;
1023                  }
1024                );
1025                $(document).bind('focusout.'+properties.objectId+' focusin.'+properties.objectId,
1026                  function (event)
1027                  {
1028                    if($.isPlainObject(properties) && !properties.mouseOver) privateMethods.displaySelector(object, false);
1029                    event.stopPropagation();
1030                  }
1031              );
1032            }
1033          }
1034          return(options.popupMode);
1035        }, //setPopupMode
1036
1037      setDownArrow : function (object, value)
1038        {
1039          var options=object.data('options'),
1040              properties=object.data('properties'),
1041              objects=object.data('objects');
1042
1043          if(!properties.initialized || options.downArrow!=value)
1044          {
1045            options.downArrow=value;
1046            objects.containerArrow.html(options.downArrow);
1047          }
1048          return(options.downArrow);
1049        }, //setDownArrow
1050
1051      setItems : function (object, value)
1052        {
1053          var properties=object.data('properties');
1054
1055          if(value=='' || value==null)
1056          {
1057            value=[];
1058          }
1059          else if(!$.isArray(value))
1060          {
1061            try
1062            {
1063              value=$.parseJSON($.trim(value));
1064            }
1065            catch (e)
1066            {
1067              return(false);
1068            }
1069          }
1070
1071          privateMethods.listClear(object);
1072          if(value.length>0) privateMethods.listAddItems(object, value);
1073        },
1074
1075
1076      setValue : function (object, value, trigger)
1077        {
1078          var options=object.data('options'),
1079              properties=object.data('properties'),
1080              objects=object.data('objects'),
1081              index=-1;
1082
1083          re=/^(:invert|:all|:none)(?:(=|<|>)(\d+))$/i;
1084          target=re.exec(value);
1085          if(target!=null) value=target[1];
1086
1087          switch(value)
1088          {
1089            case ':first':
1090              if(properties.items.length>0) index=0;
1091              break;
1092            case ':last':
1093              index=properties.items.length-1;
1094              break;
1095            case ':invert':
1096              if(!options.multiple) return(false);
1097              properties.index=[];
1098              objects.list.find('.ui-inputList-item').each(
1099                function ()
1100                {
1101                  var $this=$(this),
1102                      apply=true;
1103
1104                  if(target!=null)
1105                  {
1106                    switch(target[2])
1107                    {
1108                      case '=':
1109                        apply=($this.attr('level')==target[3]);
1110                        break;
1111                      case '>':
1112                        apply=($this.attr('level')>=target[3]);
1113                        break;
1114                      case '<':
1115                        apply=($this.attr('level')<=target[3]);
1116                        break;
1117                    }
1118                  }
1119
1120                  if(apply)
1121                  {
1122                    if($this.hasClass('ui-inputList-selected-item'))
1123                    {
1124                      $this.removeClass('ui-inputList-selected-item');
1125                    }
1126                    else
1127                    {
1128                      $this.addClass('ui-inputList-selected-item');
1129                      tmp=privateMethods.findIndexByValue(object, $this.attr('idvalue'));
1130                      if(tmp>-1) properties.index.push(tmp);
1131                    }
1132                  }
1133                }
1134              );
1135              privateMethods.setValue(object, [], false);
1136              return(false);
1137              break;
1138            case ':none':
1139              if(!options.multiple) return(false);
1140
1141              properties.index=[];
1142              objects.list.find('.ui-inputList-selected-item').each(
1143                function ()
1144                {
1145                  var $this=$(this),
1146                      apply=true;
1147
1148                  if(target!=null)
1149                  {
1150                    switch(target[2])
1151                    {
1152                      case '=':
1153                        apply=($this.attr('level')==target[3]);
1154                        break;
1155                      case '>':
1156                        apply=($this.attr('level')>=target[3]);
1157                        break;
1158                      case '<':
1159                        apply=($this.attr('level')<=target[3]);
1160                        break;
1161                    }
1162                  }
1163
1164                  if(apply) $this.removeClass('ui-inputList-selected-item');
1165                }
1166              );
1167              privateMethods.setValue(object, [], false);
1168              return(false);
1169              break;
1170            case ':all':
1171              if(!options.multiple) return(false);
1172              properties.index=[];
1173              objects.list.find('.ui-inputList-item').each(
1174                function ()
1175                {
1176                  var $this=$(this),
1177                      apply=true;
1178
1179                  if(target!=null)
1180                  {
1181                    switch(target[2])
1182                    {
1183                      case '=':
1184                        apply=($this.attr('level')==target[3]);
1185                        break;
1186                      case '>':
1187                        apply=($this.attr('level')>=target[3]);
1188                        break;
1189                      case '<':
1190                        apply=($this.attr('level')<=target[3]);
1191                        break;
1192                    }
1193                  }
1194                  if(apply)
1195                  {
1196                    tmp=privateMethods.findIndexByValue(object, $this.attr('idvalue'));
1197                    if(tmp>-1) properties.index.push(tmp);
1198
1199                    $this.addClass('ui-inputList-selected-item');
1200                  }
1201                }
1202              );
1203              privateMethods.setValue(object, [], false);
1204              return(false);
1205              break;
1206            default:
1207              if($.isArray(value) && options.multiple)
1208              {
1209                index=[];
1210                for(var i=0;i<value.length;i++)
1211                {
1212                  tmp=privateMethods.findIndexByValue(object, value[i]);
1213                  if(tmp>-1) index.push(tmp);
1214                }
1215              }
1216              else
1217              {
1218                index=privateMethods.findIndexByValue(object, value);
1219              }
1220
1221              break;
1222          }
1223
1224          if(!options.multiple && (!properties.initialized || properties.index!=index) && index>-1)
1225          {
1226            objects.list.find('.ui-inputList-selected-item').removeClass('ui-inputList-selected-item');
1227            objects.list.find('[idvalue="'+value+'"]').addClass('ui-inputList-selected-item');
1228            properties.index=index;
1229
1230            privateMethods.setItemContent(object, index, objects.containerValue);
1231            if(trigger) object.trigger('inputListChange', [properties.items[properties.index].value]);
1232            if(properties.index>-1) return(properties.items[properties.index].value);
1233          }
1234          else if(options.multiple)
1235          {
1236            if(!$.isArray(index))
1237            {
1238              if(index<0 || index==null) return(-1);
1239              index=[index];
1240            }
1241            tmp=[];
1242            for(var i=0;i<index.length;i++)
1243            {
1244              var item=objects.list.find('[idvalue="'+properties.items[index[i]].value+'"]');
1245              tmp.push(properties.items[index[i]].value);
1246              if(item.hasClass('ui-inputList-selected-item'))
1247              {
1248                item.removeClass('ui-inputList-selected-item');
1249
1250                tmpIndex=$.inArray(index[i] ,properties.index);
1251                if(tmpIndex>-1) properties.index.splice(tmpIndex, 1);
1252              }
1253              else
1254              {
1255                item.addClass('ui-inputList-selected-item');
1256                properties.index.push(index[i]);
1257              }
1258            }
1259            objects.containerList.html('');
1260            objects.list.children('.ui-inputList-selected-item').each(
1261              function ()
1262              {
1263                var value=$(this).attr('idvalue'),
1264                    index=privateMethods.findIndexByValue(object, value),
1265                    li=$('<li/>',
1266                    {
1267                      'class':'ui-inputList-selected-item'
1268                    }
1269                  );
1270                privateMethods.setItemContent(object, index, li);
1271                objects.containerList.append(
1272                  li.prepend(
1273                    $('<span/>',
1274                      {
1275                        'html':'x',
1276                        'class':'ui-inputList-delete-item'
1277                      }
1278                     ).bind('click.inputList',
1279                        {object:object, value:value},
1280                        function (event)
1281                        {
1282                          event.stopPropagation();
1283                          privateMethods.setValue(event.data.object, event.data.value, true);
1284                        }
1285                      )
1286                  )
1287                );
1288              }
1289            );
1290
1291            if(objects.containerList.children().length==0) objects.containerList.append('<li>&nbsp;</li>');
1292
1293            if(trigger) object.trigger('inputListChange', [tmp]);
1294            return(tmp);
1295          }
1296          return(null);
1297        },
1298
1299      displaySelector : function (object, value)
1300        {
1301          var options=object.data('options'),
1302              properties=object.data('properties'),
1303              objects=object.data('objects'),
1304              scrollBarWidth=0;
1305
1306          if(properties.selectorVisible!=value)
1307          {
1308            properties.selectorVisible=value;
1309
1310            if(properties.selectorVisible && properties.items.length>0)
1311            {
1312              var index=0;
1313              objects.listContainer
1314                .css(
1315                  {
1316                    display:'block',
1317                    'min-width':objects.listContainer.parent().css('width')
1318                  }
1319                );
1320
1321              if($.isArray(properties.index))
1322              {
1323                if (properties.index.length>0) index=properties.index[0];
1324              }
1325              else if(properties.index>-1)
1326              {
1327                index=properties.index;
1328              }
1329
1330              scrollBarWidth=objects.listContainer.width()-objects.list.width();
1331              if(scrollBarWidth>0 && properties.firstPopup)
1332              {
1333                objects.listContainer.width(objects.listContainer.width()+scrollBarWidth);
1334                properties.firstPopup=false;
1335              }
1336
1337              objects.listContainer.scrollTop(objects.listContainer.scrollTop()+objects.list.find('[idValue="'+properties.items[index].value+'"]').position().top);
1338            }
1339            else
1340            {
1341              objects.listContainer.css('display', 'none');
1342            }
1343            if(options.popup) object.trigger('inputListPopup', [properties.selectorVisible]);
1344          }
1345          return(properties.selectorVisible);
1346        },
1347
1348      load : function (object, defaultValue)
1349        {
1350          // load datas from server through an asynchronous ajax call
1351          var options=object.data('options'),
1352              properties=object.data('properties'),
1353              objects=object.data('objects');
1354
1355          if(options.serverUrl=='') return(false);
1356
1357          $.ajax(
1358            {
1359              type: "POST",
1360              url: options.serverUrl,
1361              data:options.postData,
1362              async: true,
1363              success: function(msg)
1364                {
1365                  privateMethods.setItems(object, msg);
1366
1367                  properties.initialized=false;
1368                  if(options.multiple)
1369                  {
1370                    if(defaultValue!=null)
1371                    {
1372                      privateMethods.setValue(object, defaultValue);
1373                    }
1374                    else
1375                    {
1376                      privateMethods.setValue(object, ':none');
1377                    }
1378                  }
1379                  else
1380                  {
1381                    if(defaultValue!=null)
1382                    {
1383                      privateMethods.setValue(object, defaultValue);
1384                    }
1385                    else
1386                    {
1387                      privateMethods.setValue(object, ':first');
1388                    }
1389                  }
1390                  properties.initialized=true;
1391
1392                  if(options.load) object.trigger('inputListLoad');
1393                },
1394              error: function(msg)
1395                {
1396                  objects.listContainer.html('Error ! '+msg);
1397                }
1398            }
1399         );
1400        },
1401
1402      listClear : function (object)
1403        {
1404          // clear the items list
1405          var objects=object.data('objects'),
1406              options=object.data('options'),
1407              properties=object.data('properties');
1408
1409          objects.list.children().unbind();
1410          objects.list.html('');
1411          if(options.multiple)
1412          {
1413            properties.index=[];
1414          }
1415          else
1416          {
1417            properties.index=-1;
1418          }
1419          properties.items=[];
1420          properties.firstPopup=true;
1421        },
1422
1423      listAddItems : function (object, listItems)
1424        {
1425          // add the items to the items list
1426          var options=object.data('options'),
1427              properties=object.data('properties'),
1428              objects=object.data('objects'),
1429              width=0;
1430
1431          for(var i=0;i<listItems.length;i++)
1432          {
1433            properties.items.push(
1434              {
1435                value:listItems[i].value,
1436                cols:listItems[i].cols
1437              }
1438            );
1439
1440            var content=$('<div/>',
1441                      {
1442                        'class':'ui-inputList-value'
1443                      }
1444                    ),
1445                li=$('<li/>',
1446                      {
1447                        'class':'ui-inputList-item',
1448                        'idValue':listItems[i].value
1449                      }
1450                    ).bind('click.inputList',
1451                        {object:object},
1452                        function (event)
1453                        {
1454                          privateMethods.setValue(event.data.object, $(this).attr('idValue'), true);
1455                          if(options.multiple)
1456                          {
1457                          }
1458                          else
1459                          {
1460                            privateMethods.displaySelector(event.data.object, false);
1461                          }
1462
1463                          if(options.multiple) objects.container.focus();
1464                        }
1465                      );
1466
1467            for(var j=0;j<listItems[i].cols.length;j++)
1468            {
1469              content.append($('<span/>',
1470                  {
1471                    html:listItems[i].cols[j],
1472                    css:
1473                      {
1474                        width:privateMethods.getColWidth(object, j)
1475                      },
1476                    'class':privateMethods.getColCss(object, j)
1477                  }
1478                )
1479              );
1480            }
1481
1482            li.append(content);
1483            if(options.multiple)
1484            {
1485              li.children().prepend('<div class="ui-inputList-check"></div>');
1486            }
1487            objects.list.append(li);
1488          }
1489        },
1490
1491      findIndexByValue : function (object, value)
1492        {
1493          /*
1494           * search an item inside the item list and return the index
1495           * in the array
1496           */
1497          var properties=object.data('properties');
1498
1499          for(var i=0;i<properties.items.length;i++)
1500          {
1501            if(properties.items[i].value==value) return(i);
1502          }
1503          return(-1);
1504        },
1505
1506      setEventPopup : function (object, value)
1507        {
1508          var options=object.data('options');
1509
1510          options.popup=value;
1511          object.unbind('inputListPopup');
1512          if(value) object.bind('inputListPopup', options.popup);
1513          return(options.popup);
1514        },
1515
1516      setEventChange : function (object, value)
1517        {
1518          var options=object.data('options');
1519
1520          options.change=value;
1521          object.unbind('inputListChange');
1522          if(value) object.bind('inputListChange', options.change);
1523          return(options.change);
1524        },
1525
1526      setEventLoad : function (object, value)
1527        {
1528          var options=object.data('options');
1529
1530          options.load=value;
1531          object.unbind('inputListLoad');
1532          if(value) object.bind('inputListLoad', options.load);
1533          return(options.load);
1534        },
1535
1536      getColWidth : function (object, index)
1537        {
1538          var options=object.data('options');
1539
1540          if(index>=0 && index<options.colsWidth.length && options.colsWidth[index]!='' && options.colsWidth[index]!=0)
1541          {
1542            return(options.colsWidth[index]+'px');
1543          }
1544          return('');
1545        },
1546
1547      getColCss : function (object, index)
1548        {
1549          var options=object.data('options');
1550
1551          if(index>=0 && index<options.colsCss.length)
1552          {
1553            return(options.colsCss[index]);
1554          }
1555          return('');
1556        },
1557
1558      getColContent : function (object, itemIndex, index)
1559        {
1560          var properties=object.data('properties');
1561
1562          if(index>=0 && index<=properties.items[itemIndex].cols.length)
1563          {
1564            return(properties.items[itemIndex].cols[index]);
1565          }
1566          return('');
1567        },
1568
1569      setItemContent : function (object, index, container)
1570        {
1571          var options=object.data('options'),
1572              properties=object.data('properties'),
1573              colContent='',
1574              colsDisplayed=[];
1575
1576          container.html('');
1577
1578          if(options.colsDisplayed.length==0)
1579          {
1580            for(var j=0;j<properties.items[index].cols.length;j++)
1581            {
1582              colsDisplayed.push(j);
1583            }
1584          }
1585          else
1586          {
1587            colsDisplayed=options.colsDisplayed;
1588          }
1589
1590          for(var j=0;j<colsDisplayed.length;j++)
1591          {
1592            colContent=privateMethods.getColContent(object, index, colsDisplayed[j]);
1593
1594            if(colContent!=null)
1595            {
1596              container.append($('<span/>',
1597                  {
1598                    'html':colContent,
1599                    css:
1600                      {
1601                        width:privateMethods.getColWidth(object, colsDisplayed[j])
1602                      },
1603                    'class':privateMethods.getColCss(object, colsDisplayed[j])
1604                  }
1605                )
1606              );
1607            }
1608          }
1609        }
1610    };
1611
1612
1613    $.fn.inputList = function(method)
1614    {
1615      if(publicMethods[method])
1616      {
1617        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
1618      }
1619      else if(typeof method === 'object' || ! method)
1620      {
1621        return publicMethods.init.apply(this, arguments);
1622      }
1623      else
1624      {
1625        $.error( 'Method ' +  method + ' does not exist on jQuery.inputList' );
1626      }
1627    } // $.fn.inputList
1628
1629  }
1630)(jQuery);
1631
1632
Note: See TracBrowser for help on using the repository browser.