1 | /* file: ui.tagSelector.js - v1.0.0 | minified on 2011/01/27 with http://jscompress.com/ */ |
---|
2 | (function($) |
---|
3 | {var publicMethods={init:function(opt) |
---|
4 | {return this.each(function() |
---|
5 | {var $this=$(this),data=$this.data('options'),objects=$this.data('objects'),properties=$this.data('properties'),options={ignoreCase:true,serverUrl:'plugins/GrumPluginClasses/gpc_ajax.php',serverCallDelay:250,listMaxWidth:0,listMaxHeight:0,maximumTagLoaded:0,textStart:'Start to type text...',textFound:'%s tags found',textDisplay:'display only %s tags',mode:'public',filter:'affected',inputNumCar:5,add:null,remove:null,popup:null,load:null};$this.data('options',options);if(!properties) |
---|
6 | {$this.data('properties',{initialized:false,selectorVisible:false,totalTags:0,tags:[],cache:[],timerHandle:null,isValid:true});properties=$this.data('properties');} |
---|
7 | if(!objects) |
---|
8 | {objects={container:$('<div/>',{'class':'ui-tag-selector-input',css:{width:'100%'}}).bind('click.tagSelector',function() |
---|
9 | {objects.input.focus();}),selectedTagList:$('<ul/>',{html:'','class':'ui-tag-selector-selected-tag-list'}),input:$('<input>',{type:"text",value:''}).bind('focusout.tagSelector',function() |
---|
10 | {privateMethods.lostFocus($this);}).bind('focus.tagSelector',function() |
---|
11 | {privateMethods.getFocus($this);}).bind('keypress.tagSelector',function() |
---|
12 | {privateMethods.setTimerHandle($this);}),selectorList:$('<div/>',{html:"",'class':'ui-tag-selector-list',css:{display:'none',position:'absolute',zIndex:9999}}).bind('mouseleave.tagSelector',function() |
---|
13 | {privateMethods.displaySelector($this,false);}),tagList:$('<ul/>',{css:{listStyle:'none',padding:'0px',margin:'0px',overflow:"auto"}}),textArea:$('<div/>',{html:'','class':'ui-tag-selector-text'})};$this.html('').append(objects.container.append(objects.selectedTagList.append($('<li/>').append(objects.input)))).append(objects.selectorList.append(objects.tagList).append(objects.textArea));$this.data('objects',objects);} |
---|
14 | privateMethods.setOptions($this,opt);});},destroy:function() |
---|
15 | {return this.each(function() |
---|
16 | {var $this=$(this),objects=$this.data('objects');objects.selectedTagList.children().unbind();objects.input.unbind().remove();objects.container.unbind().remove();objects.selectorList.unbind().remove();objects.tagList.remove();$this.unbind('.categorySelector').css({width:'',height:''});});},options:function(value) |
---|
17 | {return this.each(function() |
---|
18 | {privateMethods.setOptions($(this),value);});},ignoreCase:function(value) |
---|
19 | {if(value!=null) |
---|
20 | {return this.each(function() |
---|
21 | {privateMethods.setIgnoreCase($(this),value);});} |
---|
22 | else |
---|
23 | {var options=this.data('options');if(options) |
---|
24 | {return(options.ignoreCase);} |
---|
25 | else |
---|
26 | {return(true);}}},inputNumCar:function(value) |
---|
27 | {if(value!=null) |
---|
28 | {return this.each(function() |
---|
29 | {privateMethods.setInputNumCar($(this),value);});} |
---|
30 | else |
---|
31 | {var options=this.data('options');if(options) |
---|
32 | {return(options.inputNumCar);} |
---|
33 | else |
---|
34 | {return(true);}}},maximumTagLoaded:function(value) |
---|
35 | {if(value!=null) |
---|
36 | {return this.each(function() |
---|
37 | {privateMethods.setMaximumTagLoaded($(this),value);});} |
---|
38 | else |
---|
39 | {var options=this.data('options');if(options) |
---|
40 | {return(options.maximumTagLoaded);} |
---|
41 | else |
---|
42 | {return(0);}}},listMaxWidth:function(value) |
---|
43 | {if(value!=null) |
---|
44 | {return this.each(function() |
---|
45 | {privateMethods.setListMaxWidth($(this),value);});} |
---|
46 | else |
---|
47 | {var options=this.data('options');if(options) |
---|
48 | {return(options.listMaxWidth);} |
---|
49 | else |
---|
50 | {return(0);}}},listMaxHeight:function(value) |
---|
51 | {if(value!=null) |
---|
52 | {return this.each(function() |
---|
53 | {privateMethods.setListMaxHeight($(this),value);});} |
---|
54 | else |
---|
55 | {var options=this.data('options');if(options) |
---|
56 | {return(options.listMaxHeight);} |
---|
57 | else |
---|
58 | {return(0);}}},serverCallDelay:function(value) |
---|
59 | {if(value!=null) |
---|
60 | {return this.each(function() |
---|
61 | {privateMethods.setServerCallDelay($(this),value);});} |
---|
62 | else |
---|
63 | {var options=this.data('options');if(options) |
---|
64 | {return(options.serverCallDelay);} |
---|
65 | else |
---|
66 | {return(0);}}},serverUrl:function(value) |
---|
67 | {if(value!=null) |
---|
68 | {return this.each(function() |
---|
69 | {privateMethods.setServerUrl($(this),value);});} |
---|
70 | else |
---|
71 | {var options=this.data('options');if(options) |
---|
72 | {return(options.serverUrl);} |
---|
73 | else |
---|
74 | {return('');}}},textStart:function(value) |
---|
75 | {if(value!=null) |
---|
76 | {return this.each(function() |
---|
77 | {privateMethods.setTextStart($(this),value);});} |
---|
78 | else |
---|
79 | {var options=this.data('options');if(options) |
---|
80 | {return(options.textStart);} |
---|
81 | else |
---|
82 | {return('');}}},textFound:function(value) |
---|
83 | {if(value!=null) |
---|
84 | {return this.each(function() |
---|
85 | {privateMethods.setTextFound($(this),value);});} |
---|
86 | else |
---|
87 | {var options=this.data('options');if(options) |
---|
88 | {return(options.textFound);} |
---|
89 | else |
---|
90 | {return('');}}},textDisplay:function(value) |
---|
91 | {if(value!=null) |
---|
92 | {return this.each(function() |
---|
93 | {privateMethods.setTextDisplay($(this),value);});} |
---|
94 | else |
---|
95 | {var options=this.data('options');if(options) |
---|
96 | {return(options.textDisplay);} |
---|
97 | else |
---|
98 | {return('');}}},filter:function(value) |
---|
99 | {if(value!=null) |
---|
100 | {return this.each(function() |
---|
101 | {privateMethods.setFilter($(this),value);});} |
---|
102 | else |
---|
103 | {var options=this.data('options');if(options) |
---|
104 | {return(options.filter);} |
---|
105 | else |
---|
106 | {return(true);}}},mode:function(value) |
---|
107 | {if(value!=null) |
---|
108 | {return this.each(function() |
---|
109 | {privateMethods.setMode($(this),value);});} |
---|
110 | else |
---|
111 | {var options=this.data('options');if(options) |
---|
112 | {return(options.mode);} |
---|
113 | else |
---|
114 | {return(true);}}},value:function(value) |
---|
115 | {if(value!=null) |
---|
116 | {return this.each(function() |
---|
117 | {privateMethods.setValue($(this),value);});} |
---|
118 | else |
---|
119 | {var properties=this.data('properties');return(properties.tags);}},isValid:function(value) |
---|
120 | {if(value!=null) |
---|
121 | {return this.each(function() |
---|
122 | {privateMethods.setIsValid($(this),value);});} |
---|
123 | else |
---|
124 | {var properties=this.data('properties');return(properties.isValid);}},load:function(value) |
---|
125 | {if(value&&$.isFunction(value)) |
---|
126 | {return this.each(function() |
---|
127 | {privateMethods.setEventLoad($(this),value);});} |
---|
128 | else |
---|
129 | {privateMethods.load(this);}},popup:function(value) |
---|
130 | {if(value&&$.isFunction(value)) |
---|
131 | {return this.each(function() |
---|
132 | {privateMethods.setEventPopup($(this),value);});} |
---|
133 | else |
---|
134 | {var options=this.data('options');if(options) |
---|
135 | {return(options.popup);} |
---|
136 | else |
---|
137 | {return(null);}}},add:function(value) |
---|
138 | {if(value&&$.isFunction(value)) |
---|
139 | {return this.each(function() |
---|
140 | {privateMethods.setEventAdd($(this),value);});} |
---|
141 | else |
---|
142 | {var options=this.data('options');if(options) |
---|
143 | {return(options.add);} |
---|
144 | else |
---|
145 | {return(null);}}},remove:function(value) |
---|
146 | {if(value&&$.isFunction(value)) |
---|
147 | {return this.each(function() |
---|
148 | {privateMethods.setEventRemove($(this),value);});} |
---|
149 | else |
---|
150 | {var options=this.data('options');if(options) |
---|
151 | {return(options.remove);} |
---|
152 | else |
---|
153 | {return(null);}}},numberOfTags:function() |
---|
154 | {var properties=this.data('properties');if(properties) |
---|
155 | {return(properties.tags.length);} |
---|
156 | else |
---|
157 | {return(null);}}};var privateMethods={setOptions:function(object,value) |
---|
158 | {var properties=object.data('properties'),options=object.data('options');if(!$.isPlainObject(value))return(false);properties.initialized=false;privateMethods.setIgnoreCase(object,(value.ignoreCase!=null)?value.ignoreCase:options.ignoreCase);privateMethods.setInputNumCar(object,(value.inputNumCar!=null)?value.inputNumCar:options.inputNumCar);privateMethods.setValue(object,(value.value!=null)?value.value:[]);privateMethods.setMaximumTagLoaded(object,(value.maximumTagLoaded!=null)?value.maximumTagLoaded:options.maximumTagLoaded);privateMethods.setTextStart(object,(value.textStart!=null)?value.textStart:options.textStart);privateMethods.setTextFound(object,(value.textFound!=null)?value.textFound:options.textFound);privateMethods.setTextDisplay(object,(value.textDisplay!=null)?value.textDisplay:options.textDisplay);privateMethods.setListMaxWidth(object,(value.listMaxWidth!=null)?value.listMaxWidth:options.listMaxWidth);privateMethods.setListMaxHeight(object,(value.listMaxHeight!=null)?value.listMaxHeight:options.listMaxHeight);privateMethods.setServerCallDelay(object,(value.serverCallDelay!=null)?value.serverCallDelay:options.serverCallDelay);privateMethods.setServerUrl(object,(value.serverUrl!=null)?value.serverUrl:options.serverUrl);privateMethods.setMode(object,(value.mode!=null)?value.mode:options.mode);privateMethods.setFilter(object,(value.filter!=null)?value.filter:options.filter);privateMethods.setEventPopup(object,(value.popup!=null)?value.popup:options.popup);privateMethods.setEventAdd(object,(value.add!=null)?value.add:options.add);privateMethods.setEventRemove(object,(value.remove!=null)?value.remove:options.remove);privateMethods.setEventLoad(object,(value.load!=null)?value.load:options.load);if(options.autoLoad)privateMethods.load(object);properties.initialized=true;},setIsValid:function(object,value) |
---|
159 | {var objects=object.data('objects'),properties=object.data('properties');if(properties.isValid!=value) |
---|
160 | {properties.isValid=value;if(properties.isValid) |
---|
161 | {objects.container.removeClass('ui-error');objects.input.removeClass('ui-error');} |
---|
162 | else |
---|
163 | {objects.container.addClass('ui-error');objects.input.addClass('ui-error');}} |
---|
164 | return(properties.isValid);},setIgnoreCase:function(object,value) |
---|
165 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.ignoreCase!=value)&&(value==true||value==false)) |
---|
166 | {options.ignoreCase=value;} |
---|
167 | return(options.ignoreCase);},setInputNumCar:function(object,value) |
---|
168 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if((!properties.initialized||options.inputNumCar!=value)&&value>0) |
---|
169 | {options.inputNumCar=value;objects.input.attr('size',options.inputNumCar);} |
---|
170 | return(options.inputNumCar);},setMaximumTagLoaded:function(object,value) |
---|
171 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.setMaximumTagLoaded!=value)&&value>=0) |
---|
172 | {options.maximumTagLoaded=value;} |
---|
173 | return(options.maximumTagLoaded);},setTextStart:function(object,value) |
---|
174 | {var options=object.data('options'),properties=object.data('properties');if(!properties.initialized||options.textStart!=value) |
---|
175 | {options.textStart=value;} |
---|
176 | return(options.textStart);},setTextFound:function(object,value) |
---|
177 | {var options=object.data('options'),properties=object.data('properties');if(!properties.initialized||options.textFound!=value) |
---|
178 | {options.textFound=value;} |
---|
179 | return(options.textFound);},setTextDisplay:function(object,value) |
---|
180 | {var options=object.data('options'),properties=object.data('properties');if(!properties.initialized||options.textDisplay!=value) |
---|
181 | {options.textDisplay=value;} |
---|
182 | return(options.textDisplay);},setListMaxWidth:function(object,value) |
---|
183 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects');if((!properties.initialized||options.listMaxWidth!=value)&&value>=0) |
---|
184 | {options.listMaxWidth=value;if(options.listMaxWidth>0) |
---|
185 | {objects.selectorList.css('max-width',options.listMaxWidth+'px');} |
---|
186 | else |
---|
187 | {objects.selectorList.css('max-width','');}} |
---|
188 | return(options.listMaxWidth);},setListMaxHeight:function(object,value) |
---|
189 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects');if((!properties.initialized||options.listMaxHeight!=value)&&value>=0) |
---|
190 | {options.listMaxHeight=value;if(options.listMaxHeight>0) |
---|
191 | {objects.tagList.css('max-height',options.listMaxHeight+'px');} |
---|
192 | else |
---|
193 | {objects.tagList.css('max-height','');}} |
---|
194 | return(options.listMaxHeight);},setServerCallDelay:function(object,value) |
---|
195 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.serverCallDelay!=value)&&value>0) |
---|
196 | {options.serverCallDelay=value;} |
---|
197 | return(options.serverCallDelay);},setServerUrl:function(object,value) |
---|
198 | {var options=object.data('options'),properties=object.data('properties');if(!properties.initialized||options.serverUrl!=value) |
---|
199 | {options.serverUrl=value;if(options.autoLoad&&properties.initialized)privateMethods.load(object);} |
---|
200 | return(options.serverUrl);},setMode:function(object,value) |
---|
201 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.mode!=value)&&(value=='admin'||value=='public')) |
---|
202 | {options.mode=value;} |
---|
203 | return(options.mode);},setFilter:function(object,value) |
---|
204 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.filter!=value)&&(value=='all'||value=='affected')) |
---|
205 | {options.filter=value;} |
---|
206 | return(options.filter);},setValue:function(object,value) |
---|
207 | {var properties=object.data('properties'),objects=object.data('objects');if(value=='clear') |
---|
208 | {properties.tags=[];objects.selectedTagList.children('.ui-tag-selector-selected-tag').remove();privateMethods.cacheClear(object);objects.input.val('');} |
---|
209 | else |
---|
210 | {if(!$.isArray(value)) |
---|
211 | {value=[value];} |
---|
212 | for(var i=0;i<value.length;i++) |
---|
213 | {if(value[i].id!=null&&value[i].name!=null) |
---|
214 | {if(privateMethods.removeTag(object,value[i].id)==-1)privateMethods.addTag(object,value[i].id,value[i].name);} |
---|
215 | else |
---|
216 | {privateMethods.removeTag(object,value[i]);}}} |
---|
217 | return(null);},displaySelector:function(object,value) |
---|
218 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects'),popup=false;if(properties.selectorVisible!=value)popup=true;;properties.selectorVisible=value;if(properties.selectorVisible) |
---|
219 | {if(properties.cache.length>0) |
---|
220 | {objects.tagList.css('display','block');if(properties.cache.length<properties.totalTags) |
---|
221 | {objects.textArea.html(options.textFound.replace('%s',properties.totalTags)+', '+ |
---|
222 | options.textDisplay.replace('%s',properties.cache.length)).css('display','block');} |
---|
223 | else |
---|
224 | {objects.textArea.html(options.textFound.replace('%s',properties.cache.length)).css('display','block');}} |
---|
225 | else if(options.textStart!='') |
---|
226 | {objects.tagList.css('display','none');objects.textArea.html(options.textStart).css('display','block');} |
---|
227 | else |
---|
228 | {objects.textArea.html('').css('display','none');} |
---|
229 | objects.selectorList.css({display:'block','min-width':objects.selectorList.parent().css('width')});} |
---|
230 | else |
---|
231 | {objects.selectorList.css('display','none');} |
---|
232 | if(options.popup&&popup)object.trigger('tagSelectorPopup',[properties.selectorVisible]);return(properties.selectorVisible);},load:function(object) |
---|
233 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects');privateMethods.clearTimerHandle(object);if(objects.input.val()=='') |
---|
234 | {privateMethods.cacheClear(object);privateMethods.displaySelector(object,true);return(false);} |
---|
235 | $.ajax({type:"POST",url:options.serverUrl,data:{ajaxfct:options.mode+'.tagSelector.get',filter:options.filter,maxTags:options.maximumTagLoaded,ignoreCase:options.ignoreCase,letters:objects.input.val()},async:true,success:function(msg) |
---|
236 | {list=$.parseJSON(msg);properties.totalTags=list.totalNbTags;privateMethods.cacheClear(object);privateMethods.cacheAddItems(object,list.tags);if(options.load)object.trigger('tagSelectorLoad');privateMethods.displaySelector(object,true);},error:function(msg) |
---|
237 | {objects.selectorList.html('Error ! '+msg);}});},cacheClear:function(object) |
---|
238 | {var objects=object.data('objects'),properties=object.data('properties');objects.tagList.children().unbind().remove();properties.cache=[];},cacheAddItems:function(object,listItems) |
---|
239 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects');for(var i=0;i<listItems.length;i++) |
---|
240 | {properties.cache.push({id:listItems[i].id,name:listItems[i].name});if(options.ignoreCase) |
---|
241 | {var re=new RegExp('(.*)('+objects.input.val()+')(.*)','i');} |
---|
242 | else |
---|
243 | {var re=new RegExp('(.*)('+objects.input.val()+')(.*)');} |
---|
244 | tmpResult=re.exec(listItems[i].name);if(tmpResult!=null) |
---|
245 | {tmpResult=tmpResult[1]+'<span class="ui-tag-selector-highlight">'+tmpResult[2]+'</span>'+tmpResult[3];} |
---|
246 | else |
---|
247 | {tmpResult=listItems[i].name;} |
---|
248 | var li=$('<li/>',{html:tmpResult,value:listItems[i].id,'class':'ui-tag-selector-list-item'}).bind('mousedown.tagSelector',{object:object},function(event) |
---|
249 | {privateMethods.addTag(event.data.object,$(this).attr('value'),$(this).text());});objects.tagList.append(li);}},addTag:function(object,id,name) |
---|
250 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects');if(privateMethods.findTagById(object,id)==-1) |
---|
251 | {properties.tags.push({id:id,name:name});var li=$('<li/>',{value:id,html:name,'class':'ui-tag-selector-selected-tag'}).prepend($('<span/>',{html:'x'}).bind('click.tagSelector',{object:object},function(event) |
---|
252 | {privateMethods.removeTag(event.data.object,$(this).parent().attr('value'));}));objects.input.val('').parent().before(li);if(options.add)object.trigger('tagSelectorAdd',id);}},removeTag:function(object,id) |
---|
253 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects');var index=privateMethods.findTagById(object,id);if(index>-1) |
---|
254 | {properties.tags.splice(index,1);objects.selectedTagList.children('[value='+id+']').remove();if(options.remove)object.trigger('tagSelectorRemove',id);} |
---|
255 | return(index);},findTagById:function(object,value) |
---|
256 | {var properties=object.data('properties');for(var i=0;i<properties.tags.length;i++) |
---|
257 | {if(properties.tags[i].id==value)return(i);} |
---|
258 | return(-1);},getFocus:function(object) |
---|
259 | {privateMethods.displaySelector(object,true);},lostFocus:function(object) |
---|
260 | {privateMethods.displaySelector(object,false);},setEventPopup:function(object,value) |
---|
261 | {var options=object.data('options');options.popup=value;object.unbind('tagSelectorPopup');if(value)object.bind('tagSelectorPopup',options.popup);return(options.popup);},setEventAdd:function(object,value) |
---|
262 | {var options=object.data('options');options.add=value;object.unbind('tagSelectorAdd');if(value)object.bind('tagSelectorAdd',options.add);return(options.add);},setEventRemove:function(object,value) |
---|
263 | {var options=object.data('options');options.remove=value;object.unbind('tagSelectorRemove');if(value)object.bind('tagSelectorRemove',options.remove);return(options.remove);},setEventLoad:function(object,value) |
---|
264 | {var options=object.data('options');options.load=value;object.unbind('categorySelectorLoad');if(value)object.bind('tagSelectorLoad',options.load);return(options.load);},clearTimerHandle:function(object) |
---|
265 | {var properties=object.data('properties');if(properties.timerHandle!=null) |
---|
266 | {window.clearInterval(properties.timerHandle);properties.timerHandle=null;}},setTimerHandle:function(object) |
---|
267 | {var properties=object.data('properties'),options=object.data('options');privateMethods.clearTimerHandle(object);properties.timerHandle=window.setInterval(function(){privateMethods.load(object);},options.serverCallDelay);},};$.fn.tagSelector=function(method) |
---|
268 | {if(publicMethods[method]) |
---|
269 | {return publicMethods[method].apply(this,Array.prototype.slice.call(arguments,1));} |
---|
270 | else if(typeof method==='object'||!method) |
---|
271 | {return publicMethods.init.apply(this,arguments);} |
---|
272 | else |
---|
273 | {$.error('Method '+method+' does not exist on jQuery.tagSelector');}}})(jQuery); |
---|