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

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

release 3.4.0
fix bug:1984, bug:2109
js file are minified, remove packed files

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