source: extensions/GrumPluginClasses/js/ui.inputTag.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

File size: 36.9 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.inputTag.js
4 * file version: 1.1.1
5 * date: 2012-06-18
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.1.0   | 2010/11/03 | * add 'isValid' method
26 * |         |            |
27 * | 1.1.1   | 2012/06/18 | * improve memory managment
28 * |         |            |
29 * |         |            |
30 * |         |            |
31 *
32 */
33
34
35
36(
37  function($)
38  {
39    /*
40     * plugin 'public' functions
41     */
42    var publicMethods =
43    {
44      init : function (opt)
45        {
46          return this.each(function()
47            {
48              // default values for the plugin
49              var $this=$(this),
50                  data = $this.data('options'),
51                  objects = $this.data('objects'),
52                  properties = $this.data('properties'),
53                  options =
54                    {
55                      ignoreCase:true,
56                      //allowCreate:false,
57                      serverUrl:'',
58                      serverCallDelay:250,
59                      postData:{},  // additional free data sent to the server
60
61                      listMaxWidth:0,
62                      listMaxHeight:0,
63                      maximumTagLoaded:0,  //0 = no limits
64
65                      textStart:'Start to type text...',
66                      textFound:'%s tags found',
67                      textDisplay:'display only %s tags',
68
69                      mode:'public',
70                      filter:'affected',
71
72                      inputNumCar:5,
73
74                      add:null,
75                      remove:null,
76                      popup:null,
77                      load:null
78                    };
79
80              // if options given, merge it
81              // if(opt) $.extend(options, opt); ==> options are set by setters
82
83              $this.data('options', options);
84
85
86              if(!properties)
87              {
88                $this.data('properties',
89                  {
90                    initialized:false,
91                    selectorVisible:false,
92                    totalTags:0,
93                    tags:[], // a tag = {id:0, name:''}
94                    cache:[],
95                    timerHandle:null,
96                    isValid:true
97                  }
98                );
99                properties=$this.data('properties');
100              }
101
102              if(!objects)
103              {
104                objects =
105                  {
106                    container:$('<div/>',
107                        {
108                          'class':'ui-tag-selector-input',
109                          css:{
110                            width:'100%'
111                          }
112                        }
113                    ).bind('click.inputTag',
114                        function ()
115                        {
116                          objects.input.focus();
117                        }
118                      ),
119                    selectedTagList:$('<ul/>',
120                      {
121                        html: '',
122                        'class':'ui-tag-selector-selected-tag-list'
123                      }
124                    ),
125                    input:$('<input>',
126                      {
127                        type:"text",
128                        value:''
129                      }
130                    ).bind('focusout.inputTag',
131                        function ()
132                        {
133                          privateMethods.lostFocus($this);
134                        }
135                      )
136                      .bind('focus.inputTag',
137                          function ()
138                          {
139                            privateMethods.getFocus($this);
140                          }
141                        )
142                      .bind('keypress.inputTag',
143                          function ()
144                          {
145                            privateMethods.setTimerHandle($this);
146                          }
147                        ),
148                    selectorList:$('<div/>',
149                        {
150                          html: "",
151                          'class':'ui-tag-selector-list',
152                          css: {
153                            display:'none',
154                            position:'absolute',
155                            zIndex:9999
156                          }
157                        }
158                    ).bind('mouseleave.inputTag',
159                        function ()
160                        {
161                          privateMethods.displaySelector($this, false);
162                        }
163                      ),
164                    tagList:$('<ul/>',
165                      {
166                        css: {
167                          listStyle:'none',
168                          padding:'0px',
169                          margin:'0px',
170                          overflow:"auto"
171                        }
172                      }
173                    ),
174                    textArea:$('<div/>',
175                      {
176                        html:'',
177                        'class':'ui-tag-selector-text'
178                      }
179                    )
180                  };
181
182                $this
183                  .html('')
184                  .append(objects.container.append(objects.selectedTagList.append($('<li/>').append(objects.input) ) ) )
185                  .append(objects.selectorList.append(objects.tagList).append(objects.textArea));
186
187                $this.data('objects', objects);
188              }
189
190              privateMethods.setOptions($this, opt);
191            }
192          );
193        }, // init
194      destroy : function ()
195        {
196          return this.each(
197            function()
198            {
199              // default values for the plugin
200              var $this=$(this),
201                  objects = $this.data('objects');
202              objects.selectedTagList.children().unbind();
203              objects.input.unbind().remove();
204              objects.container.unbind().remove();
205              objects.selectorList.unbind().remove();
206              objects.tagList.remove();
207              $this
208                .unbind('.categorySelector')
209                .removeData()
210                .css(
211                  {
212                    width:'',
213                    height:''
214                  }
215                );
216              delete $this;
217            }
218          );
219        }, // destroy
220
221      options: function (value)
222        {
223          return this.each(function()
224            {
225              privateMethods.setOptions($(this), value);
226            }
227          );
228        }, // autoLoad
229
230
231      ignoreCase: function (value)
232        {
233          if(value!=null)
234          {
235            return this.each(function()
236              {
237                privateMethods.setIgnoreCase($(this), value);
238              }
239            );
240          }
241          else
242          {
243            var options = this.data('options');
244
245            if(options)
246            {
247              return(options.ignoreCase);
248            }
249            else
250            {
251              return(true);
252            }
253          }
254        }, // ignoreCase
255
256      inputNumCar: function (value)
257        {
258          if(value!=null)
259          {
260            return this.each(function()
261              {
262                privateMethods.setInputNumCar($(this), value);
263              }
264            );
265          }
266          else
267          {
268            var options = this.data('options');
269
270            if(options)
271            {
272              return(options.inputNumCar);
273            }
274            else
275            {
276              return(true);
277            }
278          }
279        }, // ignoreCase
280
281/*
282      allowCreate: function (value)
283        {
284          if(value)
285          {
286            this.each(function()
287              {
288                var $this=$(this);
289                privateMethods.setAllowCreate($this, value);
290                return($this);
291              }
292            );
293          }
294          else
295          {
296            var options = this.data('options');
297
298            if(options)
299            {
300              return(options.allowCreate);
301            }
302            else
303            {
304              return(false);
305            }
306          }
307        }, // allowCreate
308*/
309      maximumTagLoaded: function (value)
310        {
311          if(value!=null)
312          {
313            return this.each(function()
314              {
315                privateMethods.setMaximumTagLoaded($(this), value);
316              }
317            );
318          }
319          else
320          {
321            var options = this.data('options');
322
323            if(options)
324            {
325              return(options.maximumTagLoaded);
326            }
327            else
328            {
329              return(0);
330            }
331          }
332        }, // maximumTagLoaded
333
334      listMaxWidth: function (value)
335        {
336          if(value!=null)
337          {
338            return this.each(function()
339              {
340                privateMethods.setListMaxWidth($(this), value);
341              }
342            );
343          }
344          else
345          {
346            var options = this.data('options');
347
348            if(options)
349            {
350              return(options.listMaxWidth);
351            }
352            else
353            {
354              return(0);
355            }
356          }
357        }, // listMaxWidth
358
359      listMaxHeight: function (value)
360        {
361          if(value!=null)
362          {
363            return this.each(function()
364              {
365                privateMethods.setListMaxHeight($(this), value);
366              }
367            );
368          }
369          else
370          {
371            var options = this.data('options');
372
373            if(options)
374            {
375              return(options.listMaxHeight);
376            }
377            else
378            {
379              return(0);
380            }
381          }
382        }, // listMaxHeight
383
384
385      serverCallDelay: function (value)
386        {
387          if(value!=null)
388          {
389            return this.each(function()
390              {
391                privateMethods.setServerCallDelay($(this), value);
392              }
393            );
394          }
395          else
396          {
397            var options = this.data('options');
398
399            if(options)
400            {
401              return(options.serverCallDelay);
402            }
403            else
404            {
405              return(0);
406            }
407          }
408        }, // serverCallDelay
409
410
411      serverUrl: function (value)
412        {
413          if(value!=null)
414          {
415            return this.each(function()
416              {
417                privateMethods.setServerUrl($(this), value);
418              }
419            );
420          }
421          else
422          {
423            var options = this.data('options');
424
425            if(options)
426            {
427              return(options.serverUrl);
428            }
429            else
430            {
431              return('');
432            }
433          }
434        }, // serverUrl
435
436      postData: function (value)
437        {
438          if(value!=null)
439          {
440            // set selected value
441            return(
442              this.each(
443                function()
444                {
445                  privateMethods.setPostData($(this), value, true);
446                }
447              )
448            );
449          }
450          else
451          {
452            var options=this.data('options');
453            return(options.postData);
454          }
455        }, // postData
456
457      textStart: function (value)
458        {
459          if(value!=null)
460          {
461            return this.each(function()
462              {
463                privateMethods.setTextStart($(this), value);
464              }
465            );
466          }
467          else
468          {
469            var options = this.data('options');
470
471            if(options)
472            {
473              return(options.textStart);
474            }
475            else
476            {
477              return('');
478            }
479          }
480        }, // textStart
481
482      textFound: function (value)
483        {
484          if(value!=null)
485          {
486            return this.each(function()
487              {
488                privateMethods.setTextFound($(this), value);
489              }
490            );
491          }
492          else
493          {
494            var options = this.data('options');
495
496            if(options)
497            {
498              return(options.textFound);
499            }
500            else
501            {
502              return('');
503            }
504          }
505        }, // textFound
506
507      textDisplay: function (value)
508        {
509          if(value!=null)
510          {
511            return this.each(function()
512              {
513                privateMethods.setTextDisplay($(this), value);
514              }
515            );
516          }
517          else
518          {
519            var options = this.data('options');
520
521            if(options)
522            {
523              return(options.textDisplay);
524            }
525            else
526            {
527              return('');
528            }
529          }
530        }, // textDisplay
531
532      filter: function (value)
533        {
534          if(value!=null)
535          {
536            return this.each(function()
537              {
538                privateMethods.setFilter($(this), value);
539              }
540            );
541          }
542          else
543          {
544            var options = this.data('options');
545
546            if(options)
547            {
548              return(options.filter);
549            }
550            else
551            {
552              return(true);
553            }
554          }
555        }, // filter
556
557      mode: function (value)
558        {
559          if(value!=null)
560          {
561            return this.each(function()
562              {
563                privateMethods.setMode($(this), value);
564              }
565            );
566          }
567          else
568          {
569            var options = this.data('options');
570
571            if(options)
572            {
573              return(options.mode);
574            }
575            else
576            {
577              return(true);
578            }
579          }
580        }, // mode
581
582      value: function (value)
583        {
584          if(value!=null)
585          {
586            // set selected value
587            return this.each(function()
588              {
589                privateMethods.setValue($(this), value);
590              }
591            );
592          }
593          else
594          {
595            // return the selected tags
596            var properties=this.data('properties');
597
598            return(properties.tags);
599          }
600        }, // value
601
602      isValid: function (value)
603        {
604          if(value!=null)
605          {
606            // set selected value
607            return this.each(function()
608              {
609                privateMethods.setIsValid($(this), value);
610              }
611            );
612          }
613          else
614          {
615            // return the selected tags
616            var properties=this.data('properties');
617
618            return(properties.isValid);
619          }
620        }, // isValid
621
622      load: function (value)
623        {
624          /*
625           * two functionnalities :
626           *  - if value is set, use it to set the load event function
627           *  - if no value, loads data from server
628           */
629          if(value && $.isFunction(value))
630          {
631            // set selected value
632            return this.each(function()
633              {
634                privateMethods.setEventLoad($(this), value);
635              }
636            );
637          }
638          else
639          {
640            // loads data from server
641            privateMethods.load(this);
642          }
643        },
644
645      popup: function (value)
646        {
647          if(value && $.isFunction(value))
648          {
649            // set selected value
650            return this.each(function()
651              {
652                privateMethods.setEventPopup($(this), value);
653              }
654            );
655          }
656          else
657          {
658            // return the selected value
659            var options=this.data('options');
660
661            if(options)
662            {
663              return(options.popup);
664            }
665            else
666            {
667              return(null);
668            }
669          }
670        }, // popup
671      add: function (value)
672        {
673          if(value && $.isFunction(value))
674          {
675            // set selected value
676            return this.each(function()
677              {
678                privateMethods.setEventAdd($(this), value);
679              }
680            );
681          }
682          else
683          {
684            // return the selected value
685            var options=this.data('options');
686
687            if(options)
688            {
689              return(options.add);
690            }
691            else
692            {
693              return(null);
694            }
695          }
696        }, // add
697      remove: function (value)
698        {
699          if(value && $.isFunction(value))
700          {
701            // set selected value
702            return this.each(function()
703              {
704                privateMethods.setEventRemove($(this), value);
705              }
706            );
707          }
708          else
709          {
710            // return the selected value
711            var options=this.data('options');
712
713            if(options)
714            {
715              return(options.remove);
716            }
717            else
718            {
719              return(null);
720            }
721          }
722        }, // remove
723      numberOfTags: function ()
724        {
725          var properties=this.data('properties');
726
727          if(properties)
728          {
729            return(properties.tags.length);
730          }
731          else
732          {
733            return(null);
734          }
735        } // numberOfTags
736
737    }; // methods
738
739
740    /*
741     * plugin 'private' methods
742     */
743    var privateMethods =
744    {
745      setOptions : function (object, value)
746        {
747          var properties=object.data('properties'),
748              options=object.data('options');
749
750          if(!$.isPlainObject(value)) return(false);
751
752          properties.initialized=false;
753
754          privateMethods.setIgnoreCase(object, (value.ignoreCase!=null)?value.ignoreCase:options.ignoreCase);
755          privateMethods.setInputNumCar(object, (value.inputNumCar!=null)?value.inputNumCar:options.inputNumCar);
756          //privateMethods.setAllowCreate(object, (value.allowCreate!=null)?value.allowCreate:options.allowCreate);
757          privateMethods.setValue(object, (value.value!=null)?value.value:[]);
758          privateMethods.setMaximumTagLoaded(object, (value.maximumTagLoaded!=null)?value.maximumTagLoaded:options.maximumTagLoaded);
759          privateMethods.setTextStart(object, (value.textStart!=null)?value.textStart:options.textStart);
760          privateMethods.setTextFound(object, (value.textFound!=null)?value.textFound:options.textFound);
761          privateMethods.setTextDisplay(object, (value.textDisplay!=null)?value.textDisplay:options.textDisplay);
762          privateMethods.setListMaxWidth(object, (value.listMaxWidth!=null)?value.listMaxWidth:options.listMaxWidth);
763          privateMethods.setListMaxHeight(object, (value.listMaxHeight!=null)?value.listMaxHeight:options.listMaxHeight);
764          privateMethods.setPostData(object, (value.postData!=null)?value.postData:options.postData);
765          privateMethods.setServerCallDelay(object, (value.serverCallDelay!=null)?value.serverCallDelay:options.serverCallDelay);
766          privateMethods.setServerUrl(object, (value.serverUrl!=null)?value.serverUrl:options.serverUrl);
767          privateMethods.setMode(object, (value.mode!=null)?value.mode:options.mode);
768          privateMethods.setFilter(object, (value.filter!=null)?value.filter:options.filter);
769          privateMethods.setEventPopup(object, (value.popup!=null)?value.popup:options.popup);
770          privateMethods.setEventAdd(object, (value.add!=null)?value.add:options.add);
771          privateMethods.setEventRemove(object, (value.remove!=null)?value.remove:options.remove);
772          privateMethods.setEventLoad(object, (value.load!=null)?value.load:options.load);
773
774          if(options.autoLoad) privateMethods.load(object);
775
776          properties.initialized=true;
777        },
778
779      setIsValid : function (object, value)
780        {
781          var objects=object.data('objects'),
782              properties=object.data('properties');
783
784          if(properties.isValid!=value)
785          {
786            properties.isValid=value;
787            if(properties.isValid)
788            {
789              objects.container.removeClass('ui-error');
790              objects.input.removeClass('ui-error');
791            }
792            else
793            {
794              objects.container.addClass('ui-error');
795              objects.input.addClass('ui-error');
796            }
797          }
798          return(properties.isValid);
799        },
800
801      setIgnoreCase : function (object, value)
802        {
803          var options=object.data('options'),
804              properties=object.data('properties');
805
806          if((!properties.initialized || options.ignoreCase!=value) && (value==true || value==false))
807          {
808            options.ignoreCase=value;
809          }
810          return(options.ignoreCase);
811        },
812
813      setInputNumCar : function (object, value)
814        {
815          var options=object.data('options'),
816              objects=object.data('objects'),
817              properties=object.data('properties');
818
819          if((!properties.initialized || options.inputNumCar!=value) && value>0)
820          {
821            options.inputNumCar=value;
822            objects.input.attr('size', options.inputNumCar);
823          }
824          return(options.inputNumCar);
825        },
826
827/*
828      setAllowCreate : function (object, value)
829        {
830          var options=object.data('options'),
831              properties=object.data('properties');
832          if((!properties.initialized || options.allowCreate!=value) && (value==true || value==false))
833          {
834            options.allowCreate=value;
835          }
836          return(options.allowCreate);
837        },
838*/
839
840      setMaximumTagLoaded : function (object, value)
841        {
842          var options=object.data('options'),
843              properties=object.data('properties');
844
845          if((!properties.initialized || options.setMaximumTagLoaded!=value) && value>=0)
846          {
847            options.maximumTagLoaded=value;
848          }
849          return(options.maximumTagLoaded);
850        },
851
852
853      setTextStart : function (object, value)
854        {
855          var options=object.data('options'),
856              properties=object.data('properties');
857
858          if(!properties.initialized || options.textStart!=value)
859          {
860            options.textStart=value;
861          }
862          return(options.textStart);
863        },
864
865      setTextFound : function (object, value)
866        {
867          var options=object.data('options'),
868              properties=object.data('properties');
869
870          if(!properties.initialized || options.textFound!=value)
871          {
872            options.textFound=value;
873          }
874          return(options.textFound);
875        },
876
877      setTextDisplay : function (object, value)
878        {
879          var options=object.data('options'),
880              properties=object.data('properties');
881
882          if(!properties.initialized || options.textDisplay!=value)
883          {
884            options.textDisplay=value;
885          }
886          return(options.textDisplay);
887        },
888
889      setListMaxWidth : function (object, value)
890        {
891          var options=object.data('options'),
892              properties=object.data('properties'),
893              objects=object.data('objects');
894
895          if((!properties.initialized || options.listMaxWidth!=value) && value>=0)
896          {
897            options.listMaxWidth=value;
898            if(options.listMaxWidth>0)
899            {
900              objects.selectorList.css('max-width', options.listMaxWidth+'px');
901            }
902            else
903            {
904              objects.selectorList.css('max-width', '');
905            }
906          }
907          return(options.listMaxWidth);
908        },
909
910      setListMaxHeight : function (object, value)
911        {
912          var options=object.data('options'),
913              properties=object.data('properties'),
914              objects=object.data('objects');
915
916          if((!properties.initialized || options.listMaxHeight!=value) && value>=0)
917          {
918            options.listMaxHeight=value;
919            if(options.listMaxHeight>0)
920            {
921              objects.tagList.css('max-height', options.listMaxHeight+'px');
922            }
923            else
924            {
925              objects.tagList.css('max-height', '');
926            }
927          }
928          return(options.listMaxHeight);
929        },
930
931      setServerCallDelay : function (object, value)
932        {
933          var options=object.data('options'),
934              properties=object.data('properties');
935
936          if((!properties.initialized || options.serverCallDelay!=value) && value>0 )
937          {
938            options.serverCallDelay=value;
939          }
940          return(options.serverCallDelay);
941        },
942
943      setServerUrl : function (object, value)
944        {
945          var options=object.data('options'),
946              properties=object.data('properties');
947
948          if(!properties.initialized || options.serverUrl!=value)
949          {
950            options.serverUrl=value;
951            if(options.autoLoad && properties.initialized) privateMethods.load(object);
952          }
953          return(options.serverUrl);
954        },
955
956      setPostData : function (object, value)
957        {
958          var properties=object.data('properties'),
959              options=object.data('options');
960
961          if(!properties.initialized || value!=options.postData)
962          {
963            options.postData=value;
964          }
965
966          return(options.postData);
967        }, // setPostData
968
969      setMode : function (object, value)
970        {
971          var options=object.data('options'),
972              properties=object.data('properties');
973
974          if((!properties.initialized || options.mode!=value) && (value=='admin' || value=='public'))
975          {
976            options.mode=value;
977          }
978          return(options.mode);
979        },
980
981      setFilter : function (object, value)
982        {
983          var options=object.data('options'),
984              properties=object.data('properties');
985
986          if((!properties.initialized || options.filter!=value) && (value=='all' || value=='affected'))
987          {
988            options.filter=value;
989          }
990          return(options.filter);
991        },
992
993
994      setValue : function (object, value)
995        {
996          var properties=object.data('properties'),
997              objects=object.data('objects');
998
999          if(value=='clear')
1000          {
1001            properties.tags=[];
1002            objects.selectedTagList.children('.ui-tag-selector-selected-tag').remove();
1003            privateMethods.cacheClear(object);
1004            objects.input.val('');
1005          }
1006          else
1007          {
1008            if(!$.isArray(value))
1009            {
1010              value=[value]; //works with array only
1011            }
1012
1013            for(var i=0;i<value.length;i++)
1014            {
1015              if(value[i].id!=null && value[i].name!=null)
1016              {
1017                // remove tag if present, otherwise add it
1018                if(privateMethods.removeTag(object, value[i].id)==-1) privateMethods.addTag(object, value[i].id, value[i].name);
1019              }
1020              else
1021              {
1022                //not an object, consider it's a tag id to be removed
1023                privateMethods.removeTag(object, value[i]);
1024              }
1025            }
1026          }
1027
1028          return(null);
1029        }, //setValue
1030
1031
1032      displaySelector : function (object, value)
1033        {
1034          var options=object.data('options'),
1035              properties=object.data('properties'),
1036              objects=object.data('objects'),
1037              popup=false;
1038
1039          if(properties.selectorVisible!=value) popup=true;;
1040
1041          properties.selectorVisible=value;
1042
1043          if(properties.selectorVisible)
1044          {
1045            if(properties.cache.length>0)
1046            {
1047              objects.tagList.css('display', 'block');
1048              if(properties.cache.length<properties.totalTags)
1049              {
1050                objects.textArea.html(
1051                  options.textFound.replace('%s', properties.totalTags)+', '+
1052                  options.textDisplay.replace('%s', properties.cache.length)
1053                ).css('display', 'block');
1054              }
1055              else
1056              {
1057                objects.textArea.html(options.textFound.replace('%s', properties.cache.length)).css('display', 'block');
1058              }
1059            }
1060            else if(options.textStart!='')
1061            {
1062              objects.tagList.css('display', 'none');
1063              objects.textArea.html(options.textStart).css('display', 'block');
1064            }
1065            else
1066            {
1067              objects.textArea.html('').css('display', 'none');
1068            }
1069
1070            objects.selectorList
1071              .css(
1072                {
1073                  display:'block',
1074                  'min-width':objects.selectorList.parent().css('width')
1075                }
1076              );
1077          }
1078          else
1079          {
1080            objects.selectorList.css('display', 'none');
1081          }
1082
1083          if(options.popup && popup) object.trigger('inputTagPopup', [properties.selectorVisible]);
1084
1085          return(properties.selectorVisible);
1086        }, //displaySelector
1087
1088      load : function (object)
1089        {
1090          // load datas from server through an asynchronous ajax call
1091          var options=object.data('options'),
1092              properties=object.data('properties'),
1093              objects=object.data('objects');
1094
1095          privateMethods.clearTimerHandle(object);
1096
1097          if(objects.input.val()=='')
1098          {
1099            privateMethods.cacheClear(object);
1100            privateMethods.displaySelector(object, true);
1101            return(false);
1102          }
1103
1104
1105          $.ajax(
1106            {
1107              type: "POST",
1108              url: options.serverUrl,
1109              data: {
1110                ajaxfct:options.mode+'.inputTag.get',
1111                filter:options.filter,
1112                maxTags:options.maximumTagLoaded,
1113                ignoreCase:options.ignoreCase,
1114                letters:objects.input.val(),
1115                data:options.postData
1116              },
1117              async: true,
1118              success: function(msg)
1119                {
1120                  list=$.parseJSON(msg);
1121
1122                  properties.totalTags=list.totalNbTags;
1123                  privateMethods.cacheClear(object);
1124                  privateMethods.cacheAddItems(object, list.tags);
1125                  if(options.load) object.trigger('inputTagLoad');
1126
1127                  privateMethods.displaySelector(object, true);
1128                },
1129              error: function(msg)
1130                {
1131                  objects.selectorList.html('Error ! '+msg);
1132                }
1133            }
1134         );
1135        },
1136
1137      cacheClear : function (object)
1138        {
1139          // clear the cache tag list
1140          var objects=object.data('objects'),
1141              properties=object.data('properties');
1142
1143          objects.tagList.children().unbind().remove();
1144          properties.cache=[];
1145        },
1146
1147      cacheAddItems : function (object, listItems)
1148        {
1149          // add the items to the cache list
1150          var options=object.data('options'),
1151              properties=object.data('properties'),
1152              objects=object.data('objects');
1153
1154          for(var i=0;i<listItems.length;i++)
1155          {
1156            properties.cache.push(
1157              {
1158                id:listItems[i].id,
1159                name:listItems[i].name
1160              }
1161            );
1162
1163            if(options.ignoreCase)
1164            {
1165              var re=new RegExp('(.*)('+objects.input.val()+')(.*)', 'i');
1166            }
1167            else
1168            {
1169              var re=new RegExp('(.*)('+objects.input.val()+')(.*)');
1170            }
1171            tmpResult=re.exec(listItems[i].name);
1172            if(tmpResult!=null)
1173            {
1174              tmpResult=tmpResult[1]+'<span class="ui-tag-selector-highlight">'+tmpResult[2]+'</span>'+tmpResult[3];
1175            }
1176            else
1177            {
1178              tmpResult=listItems[i].name;
1179            }
1180
1181            var li=$('<li/>',
1182                      {
1183                        html:tmpResult,
1184                        value:listItems[i].id,
1185                        'class':'ui-tag-selector-list-item'
1186                      }
1187                    ).bind('mousedown.inputTag',
1188                          {object:object},
1189                          function (event)
1190                          {
1191                            privateMethods.addTag(event.data.object, $(this).attr('value'), $(this).text());
1192                          }
1193                      );
1194            objects.tagList.append(li);
1195          }
1196        },
1197
1198      addTag : function (object, id, name)
1199        {
1200          var options=object.data('options'),
1201              properties=object.data('properties'),
1202              objects=object.data('objects');
1203
1204          if(privateMethods.findTagById(object, id)==-1)
1205          {
1206            //add only if not already selected..
1207            properties.tags.push({id:id, name:name});
1208
1209            var li=$('<li/>',
1210                      {
1211                        value:id,
1212                        html:name,
1213                        'class':'ui-tag-selector-selected-tag'
1214                      }
1215                    ).prepend(
1216                      $('<span/>',
1217                        {
1218                          html:'x'
1219                        }
1220                       ).bind('click.inputTag',
1221                          {object:object},
1222                          function (event)
1223                          {
1224                            privateMethods.removeTag(event.data.object, $(this).parent().attr('value'));
1225                          }
1226                        )
1227                      );
1228            objects.input.val('').parent().before(li);
1229            if(options.add) object.trigger('inputTagAdd', id);
1230          }
1231        },
1232
1233      removeTag : function (object, id)
1234        {
1235          var options=object.data('options'),
1236              properties=object.data('properties'),
1237              objects=object.data('objects');
1238
1239          var index=privateMethods.findTagById(object, id);
1240          if(index>-1)
1241          {
1242            properties.tags.splice(index,1);
1243            objects.selectedTagList.children('[value='+id+']').remove();
1244
1245            if(options.remove) object.trigger('inputTagRemove', id);
1246          }
1247          return(index);
1248        },
1249
1250      findTagById : function (object, value)
1251        {
1252          var properties=object.data('properties');
1253
1254          for(var i=0;i<properties.tags.length;i++)
1255          {
1256            if(properties.tags[i].id==value) return(i);
1257          }
1258          return(-1);
1259        },
1260
1261      getFocus : function (object)
1262        {
1263          privateMethods.displaySelector(object, true);
1264        },
1265
1266      lostFocus : function (object)
1267        {
1268          privateMethods.displaySelector(object, false);
1269        },
1270
1271      setEventPopup : function (object, value)
1272        {
1273          var options=object.data('options');
1274
1275          options.popup=value;
1276          object.unbind('inputTagPopup');
1277          if(value) object.bind('inputTagPopup', options.popup);
1278          return(options.popup);
1279        },
1280
1281      setEventAdd : function (object, value)
1282        {
1283          var options=object.data('options');
1284
1285          options.add=value;
1286          object.unbind('inputTagAdd');
1287          if(value) object.bind('inputTagAdd', options.add);
1288          return(options.add);
1289        },
1290
1291      setEventRemove : function (object, value)
1292        {
1293          var options=object.data('options');
1294
1295          options.remove=value;
1296          object.unbind('inputTagRemove');
1297          if(value) object.bind('inputTagRemove', options.remove);
1298          return(options.remove);
1299        },
1300
1301      setEventLoad : function (object, value)
1302        {
1303          var options=object.data('options');
1304          options.load=value;
1305          object.unbind('categorySelectorLoad');
1306          if(value) object.bind('inputTagLoad', options.load);
1307          return(options.load);
1308        },
1309
1310      clearTimerHandle : function(object)
1311        {
1312          var properties=object.data('properties');
1313
1314          if(properties.timerHandle!=null)
1315          {
1316            window.clearInterval(properties.timerHandle);
1317            properties.timerHandle=null;
1318          }
1319        },
1320
1321      setTimerHandle : function(object)
1322        {
1323          var properties=object.data('properties'),
1324              options=object.data('options');
1325
1326          privateMethods.clearTimerHandle(object);
1327          properties.timerHandle=window.setInterval(function () { privateMethods.load(object); }, options.serverCallDelay);
1328        },
1329    };
1330
1331
1332    $.fn.inputTag = function(method)
1333    {
1334      if(publicMethods[method])
1335      {
1336        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
1337      }
1338      else if(typeof method === 'object' || ! method)
1339      {
1340        return publicMethods.init.apply(this, arguments);
1341      }
1342      else
1343      {
1344        $.error( 'Method ' +  method + ' does not exist on jQuery.inputTag' );
1345      }
1346    } // $.fn.inputTag
1347
1348  }
1349)(jQuery);
1350
1351
Note: See TracBrowser for help on using the repository browser.