source: extensions/GrumPluginClasses/js/ui.tagSelector.js @ 8710

Last change on this file since 8710 was 7370, checked in by grum, 14 years ago

externalise and pack some js ; rename criteriaBuilder.js files ; improve templates & css theming ; fix bug and add functionnalities for request builder ; update key languages

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