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

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

add the ui.tagSelector.js jQuery plugin

  • Property svn:executable set to *
File size: 35.5 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          }
946          else
947          {
948            if(!$.isArray(value))
949            {
950              value=[value]; //works with array only
951            }
952
953            for(var i=0;i<value.length;i++)
954            {
955              if(value[i].id!=null && value[i].name!=null)
956              {
957                // remove tag if present, otherwise add it
958                if(privateMethods.removeTag(object, value[i].id)==-1) privateMethods.addTag(object, value[i].id, value[i].name);
959              }
960              else
961              {
962                //not an object, consider it's a tag id to be removed
963                privateMethods.removeTag(object, value[i]);
964              }
965            }
966          }
967
968          return(null);
969        }, //setValue
970
971
972      displaySelector : function (object, value)
973        {
974          var options=object.data('options'),
975              properties=object.data('properties'),
976              objects=object.data('objects'),
977              popup=false;
978
979          if(properties.selectorVisible!=value) popup=true;;
980
981          properties.selectorVisible=value;
982
983          if(properties.selectorVisible)
984          {
985            if(properties.cache.length>0)
986            {
987              objects.tagList.css('display', 'block');
988              if(properties.cache.length<properties.totalTags)
989              {
990                objects.textArea.html(
991                  options.textFound.replace('%s', properties.totalTags)+', '+
992                  options.textDisplay.replace('%s', properties.cache.length)
993                ).css('display', 'block');
994              }
995              else
996              {
997                objects.textArea.html(options.textFound.replace('%s', properties.cache.length)).css('display', 'block');
998              }
999            }
1000            else if(options.textStart!='')
1001            {
1002              objects.tagList.css('display', 'none');
1003              objects.textArea.html(options.textStart).css('display', 'block');
1004            }
1005            else
1006            {
1007              objects.textArea.html('').css('display', 'none');
1008            }
1009
1010            objects.selectorList
1011              .css(
1012                {
1013                  display:'block',
1014                  'min-width':objects.selectorList.parent().css('width')
1015                }
1016              );
1017          }
1018          else
1019          {
1020            objects.selectorList.css('display', 'none');
1021          }
1022
1023          if(options.popup && popup) object.trigger('tagSelectorPopup', [properties.selectorVisible]);
1024
1025          return(properties.selectorVisible);
1026        }, //displaySelector
1027
1028      load : function (object)
1029        {
1030          // load datas from server through an asynchronous ajax call
1031          var options=object.data('options'),
1032              properties=object.data('properties'),
1033              objects=object.data('objects');
1034
1035          privateMethods.clearTimerHandle(object);
1036
1037          $.ajax(
1038            {
1039              type: "POST",
1040              url: options.serverUrl,
1041              data: {
1042                ajaxfct:options.mode+'.tagSelector.get',
1043                filter:options.filter,
1044                maxTags:options.maximumTagLoaded,
1045                ignoreCase:options.ignoreCase,
1046                letters:objects.input.val()
1047              },
1048              async: true,
1049              success: function(msg)
1050                {
1051                  list=$.parseJSON(msg);
1052
1053                  properties.totalTags=list.totalNbTags;
1054                  privateMethods.cacheClear(object);
1055                  privateMethods.cacheAddItems(object, list.tags);
1056                  if(options.load) object.trigger('tagSelectorLoad');
1057
1058                  privateMethods.displaySelector(object, true);
1059                },
1060              error: function(msg)
1061                {
1062                  objects.selectorList.html('Error ! '+msg);
1063                },
1064            }
1065         );
1066        },
1067
1068      cacheClear : function (object)
1069        {
1070          // clear the cache tag list
1071          var objects=object.data('objects'),
1072              options=object.data('options'),
1073              properties=object.data('properties');
1074
1075          objects.tagList.children().unbind().remove();
1076          properties.cache=[];
1077        },
1078      cacheAddItems : function (object, listItems)
1079        {
1080          // add the items to the cache list
1081          var options=object.data('options'),
1082              properties=object.data('properties'),
1083              objects=object.data('objects');
1084
1085          for(var i=0;i<listItems.length;i++)
1086          {
1087            properties.cache.push(
1088              {
1089                id:listItems[i].id,
1090                name:listItems[i].name,
1091              }
1092            );
1093
1094            if(options.ignoreCase)
1095            {
1096              var re=new RegExp('(.*)('+objects.input.val()+')(.*)', 'i');
1097            }
1098            else
1099            {
1100              var re=new RegExp('(.*)('+objects.input.val()+')(.*)');
1101            }
1102            tmpResult=re.exec(listItems[i].name);
1103            if(tmpResult!=null)
1104            {
1105              tmpResult=tmpResult[1]+'<span class="ui-tag-selector-highlight">'+tmpResult[2]+'</span>'+tmpResult[3];
1106            }
1107            else
1108            {
1109              tmpResult=listItems[i].name;
1110            }
1111
1112            var li=$('<li/>',
1113                      {
1114                        html:tmpResult,
1115                        value:listItems[i].id,
1116                        'class':'ui-tag-selector-list-item'
1117                      }
1118                    ).bind('mousedown.tagSelector',
1119                          {object:object},
1120                          function (event)
1121                          {
1122                            privateMethods.addTag(event.data.object, $(this).attr('value'), $(this).text());
1123                          }
1124                      );
1125            objects.tagList.append(li);
1126          }
1127        },
1128
1129      addTag : function (object, id, name)
1130        {
1131          var options=object.data('options'),
1132              properties=object.data('properties'),
1133              objects=object.data('objects');
1134
1135          if(privateMethods.findTagById(object, id)==-1)
1136          {
1137            //add only if not already selected..
1138            properties.tags.push({id:id, name:name});
1139
1140            var li=$('<li/>',
1141                      {
1142                        value:id,
1143                        html:name,
1144                        'class':'ui-tag-selector-selected-tag'
1145                      }
1146                    ).prepend(
1147                      $('<span/>',
1148                        {
1149                          html:'x'
1150                        }
1151                       ).bind('click.tagSelector',
1152                          {object:object},
1153                          function (event)
1154                          {
1155                            privateMethods.removeTag(event.data.object, $(this).parent().attr('value'));
1156                          }
1157                        )
1158                      );
1159            objects.input.val('').parent().before(li);
1160            if(options.add) object.trigger('tagSelectorAdd', id);
1161          }
1162        },
1163
1164      removeTag : function (object, id)
1165        {
1166          var options=object.data('options'),
1167              properties=object.data('properties'),
1168              objects=object.data('objects');
1169
1170          var index=privateMethods.findTagById(object, id);
1171          if(index>-1)
1172          {
1173            properties.tags.splice(index,1);
1174            item=objects.selectedTagList.children('[value='+id+']').remove();
1175
1176            if(options.remove) object.trigger('tagSelectorRemove', id);
1177          }
1178          return(index);
1179        },
1180
1181      findTagById : function (object, value)
1182        {
1183          var properties=object.data('properties');
1184
1185          for(var i=0;i<properties.tags.length;i++)
1186          {
1187            if(properties.tags[i].id==value) return(i);
1188          }
1189          return(-1);
1190        },
1191
1192      getFocus : function (object)
1193        {
1194          privateMethods.displaySelector(object, true);
1195        },
1196
1197      lostFocus : function (object)
1198        {
1199          privateMethods.displaySelector(object, false);
1200        },
1201
1202      setEventPopup : function (object, value)
1203        {
1204          var options=object.data('options');
1205          options.popup=value;
1206          object.unbind('tagSelectorPopup');
1207          if(value) object.bind('tagSelectorPopup', options.popup);
1208          return(options.popup);
1209        },
1210      setEventAdd : function (object, value)
1211        {
1212          var options=object.data('options');
1213          options.add=value;
1214          object.unbind('tagSelectorAdd');
1215          if(value) object.bind('tagSelectorAdd', options.add);
1216          return(options.add);
1217        },
1218      setEventRemove : function (object, value)
1219        {
1220          var options=object.data('options');
1221          options.remove=value;
1222          object.unbind('tagSelectorRemove');
1223          if(value) object.bind('tagSelectorRemove', options.remove);
1224          return(options.remove);
1225        },
1226      setEventLoad : function (object, value)
1227        {
1228          var options=object.data('options');
1229          options.load=value;
1230          object.unbind('categorySelectorLoad');
1231          if(value) object.bind('tagSelectorLoad', options.load);
1232          return(options.load);
1233        },
1234
1235      clearTimerHandle : function(object)
1236        {
1237          var properties=object.data('properties');
1238
1239          if(properties.timerHandle!=null)
1240          {
1241            window.clearInterval(properties.timerHandle);
1242            properties.timerHandle=null;
1243          }
1244        },
1245      setTimerHandle : function(object)
1246        {
1247          var properties=object.data('properties'),
1248              options=object.data('options');
1249
1250          privateMethods.clearTimerHandle(object);
1251          properties.timerHandle=window.setInterval(function () { privateMethods.load(object); }, options.serverCallDelay);
1252        },
1253    };
1254
1255
1256    $.fn.tagSelector = function(method)
1257    {
1258      if(publicMethods[method])
1259      {
1260        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
1261      }
1262      else if(typeof method === 'object' || ! method)
1263      {
1264        return publicMethods.init.apply(this, arguments);
1265      }
1266      else
1267      {
1268        $.error( 'Method ' +  method + ' does not exist on jQuery.tagSelector' );
1269      }
1270    } // $.fn.tagSelector
1271
1272  }
1273)(jQuery);
1274
1275
Note: See TracBrowser for help on using the repository browser.