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

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

feature:2436 - Compatibility with Piwigo 2.4

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