1 | /** |
---|
2 | * ----------------------------------------------------------------------------- |
---|
3 | * file: ui.inputCheckbox.js |
---|
4 | * file version: 1.0.0 |
---|
5 | * date: 2010-11-04 |
---|
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/11/04 | 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( |
---|
47 | 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 | values: |
---|
57 | { |
---|
58 | forced:false, |
---|
59 | checked:'yes', |
---|
60 | unchecked:'no' |
---|
61 | }, |
---|
62 | returnMode:'selected', |
---|
63 | change:null |
---|
64 | }; |
---|
65 | |
---|
66 | // if options given, merge it |
---|
67 | // if(opt) $.extend(options, opt); ==> options are set by setters |
---|
68 | |
---|
69 | $this |
---|
70 | .data('options', options) |
---|
71 | .addClass('ui-inputCheckbox'); |
---|
72 | |
---|
73 | if(!properties) |
---|
74 | { |
---|
75 | $this.data('properties', |
---|
76 | { |
---|
77 | initialized:false, |
---|
78 | isValid:true, |
---|
79 | checkboxList:null, |
---|
80 | isCB:true |
---|
81 | } |
---|
82 | ); |
---|
83 | properties=$this.data('properties'); |
---|
84 | } |
---|
85 | |
---|
86 | if($this.get(0).tagName=='INPUT' && $this.get(0).type=='checkbox') |
---|
87 | { |
---|
88 | properties.checkboxList=$this; |
---|
89 | } |
---|
90 | else |
---|
91 | { |
---|
92 | properties.checkboxList=$this.find('input[type=checkbox]'); |
---|
93 | properties.isCB=false; |
---|
94 | } |
---|
95 | |
---|
96 | properties.checkboxList.bind('click.inputCheckbox', |
---|
97 | function (event) |
---|
98 | { |
---|
99 | privateMethods.setValue($this, $(this).attr('id'), $(this).attr('checked')?options.values.checked:options.values.unchecked,false); |
---|
100 | } |
---|
101 | ); |
---|
102 | |
---|
103 | privateMethods.setOptions($this, opt); |
---|
104 | } |
---|
105 | ); |
---|
106 | }, // init |
---|
107 | destroy : function () |
---|
108 | { |
---|
109 | return this.each( |
---|
110 | function() |
---|
111 | { |
---|
112 | // default values for the plugin |
---|
113 | var properties=this.data('properties'); |
---|
114 | properties.checkboxList.unbind('.inputCheckbox'); |
---|
115 | this.removeClass('ui-inputCheckbox'); |
---|
116 | } |
---|
117 | ); |
---|
118 | }, // destroy |
---|
119 | |
---|
120 | options: function (value) |
---|
121 | { |
---|
122 | return( |
---|
123 | this.each( |
---|
124 | function() |
---|
125 | { |
---|
126 | privateMethods.setOptions($(this), value); |
---|
127 | } |
---|
128 | ) |
---|
129 | ); |
---|
130 | }, // options |
---|
131 | |
---|
132 | disabled: function (value) |
---|
133 | { |
---|
134 | if(value!=null) |
---|
135 | { |
---|
136 | return( |
---|
137 | this.each( |
---|
138 | function() |
---|
139 | { |
---|
140 | privateMethods.setDisabled($(this), value); |
---|
141 | } |
---|
142 | ) |
---|
143 | ); |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | return(privateMethods.getDisabled($(this))); |
---|
148 | } |
---|
149 | }, // disabled |
---|
150 | |
---|
151 | values: function (value) |
---|
152 | { |
---|
153 | if(value!=null) |
---|
154 | { |
---|
155 | // set selected value |
---|
156 | return( |
---|
157 | this.each( |
---|
158 | function() |
---|
159 | { |
---|
160 | privateMethods.setValues($(this), value); |
---|
161 | } |
---|
162 | ) |
---|
163 | ); |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | var options=this.data('options'); |
---|
168 | // return the selected tags |
---|
169 | return(options.values); |
---|
170 | } |
---|
171 | }, // value |
---|
172 | |
---|
173 | value: function (id, value) |
---|
174 | { |
---|
175 | var properties=this.data('properties'); |
---|
176 | |
---|
177 | if(properties.isCB) |
---|
178 | { |
---|
179 | value=id; |
---|
180 | id=$(this).get(0).id; |
---|
181 | } |
---|
182 | |
---|
183 | if(value!=null || id==':all' || id==':invert' || id==':none') |
---|
184 | { |
---|
185 | // set selected value |
---|
186 | return( |
---|
187 | this.each( |
---|
188 | function() |
---|
189 | { |
---|
190 | privateMethods.setValue($(this), id, value, true); |
---|
191 | } |
---|
192 | ) |
---|
193 | ); |
---|
194 | } |
---|
195 | else if(id!=null && id!='') |
---|
196 | { |
---|
197 | var options=this.data('options'); |
---|
198 | // return the selected tags |
---|
199 | return($(this).find('#'+id).attr('checked')?options.values.checked:options.values.unchecked); |
---|
200 | } |
---|
201 | else |
---|
202 | { |
---|
203 | var options=this.data('options'), |
---|
204 | returned=[]; |
---|
205 | |
---|
206 | if(options.values.forced) |
---|
207 | { |
---|
208 | $(this).find('input').each( |
---|
209 | function () |
---|
210 | { |
---|
211 | returned.push( {id:$(this).attr('id'), value:$(this).attr('checked')?options.values.checked:options.values.unchecked } ); |
---|
212 | } |
---|
213 | ); |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | var filter=(options.returnMode=='selected'?':checked':':not(:checked)'); |
---|
218 | $(this).find('input'+filter).each( |
---|
219 | function () |
---|
220 | { |
---|
221 | returned.push($(this).attr('value')); |
---|
222 | } |
---|
223 | ); |
---|
224 | } |
---|
225 | |
---|
226 | return(returned); |
---|
227 | } |
---|
228 | |
---|
229 | }, // value |
---|
230 | |
---|
231 | returnMode: function (value) |
---|
232 | { |
---|
233 | if(value!=null) |
---|
234 | { |
---|
235 | return this.each(function() |
---|
236 | { |
---|
237 | privateMethods.setReturnMode($(this), value); |
---|
238 | } |
---|
239 | ); |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | var options = this.data('options'); |
---|
244 | |
---|
245 | if(options) |
---|
246 | { |
---|
247 | return(options.returnMode); |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | return('selected'); |
---|
252 | } |
---|
253 | } |
---|
254 | }, // returnMode |
---|
255 | |
---|
256 | isValid: function (value) |
---|
257 | { |
---|
258 | if(value!=null) |
---|
259 | { |
---|
260 | // set selected value |
---|
261 | return( |
---|
262 | this.each( |
---|
263 | function() |
---|
264 | { |
---|
265 | privateMethods.setIsValid($(this), value); |
---|
266 | } |
---|
267 | ) |
---|
268 | ); |
---|
269 | } |
---|
270 | else |
---|
271 | { |
---|
272 | // return the selected tags |
---|
273 | var properties=this.data('properties'); |
---|
274 | |
---|
275 | return(properties.isValid); |
---|
276 | } |
---|
277 | }, // isValid |
---|
278 | |
---|
279 | change: function (value) |
---|
280 | { |
---|
281 | if(value!=null && $.isFunction(value)) |
---|
282 | { |
---|
283 | // set selected value |
---|
284 | return( |
---|
285 | this.each( |
---|
286 | function() |
---|
287 | { |
---|
288 | privateMethods.setEventChange($(this), value); |
---|
289 | } |
---|
290 | ) |
---|
291 | ); |
---|
292 | } |
---|
293 | else |
---|
294 | { |
---|
295 | // return the selected value |
---|
296 | var options=this.data('options'); |
---|
297 | |
---|
298 | if(options) |
---|
299 | { |
---|
300 | return(options.change); |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | return(null); |
---|
305 | } |
---|
306 | } |
---|
307 | } // change |
---|
308 | |
---|
309 | }; // methods |
---|
310 | |
---|
311 | |
---|
312 | /* |
---|
313 | * plugin 'private' methods |
---|
314 | */ |
---|
315 | var privateMethods = |
---|
316 | { |
---|
317 | setOptions : function (object, value) |
---|
318 | { |
---|
319 | var properties=object.data('properties'), |
---|
320 | options=object.data('options'); |
---|
321 | |
---|
322 | if(!$.isPlainObject(value)) return(false); |
---|
323 | |
---|
324 | properties.initialized=false; |
---|
325 | |
---|
326 | privateMethods.setReturnMode(object, (value.returnMode!=null)?value.returnMode:options.returnMode); |
---|
327 | privateMethods.setValues(object, (value.values!=null)?value.values:options.values); |
---|
328 | //privateMethods.setValue(object, (value.value!=null)?value.value:options.value, true); |
---|
329 | |
---|
330 | privateMethods.setEventChange(object, (value.change!=null)?value.change:options.change); |
---|
331 | |
---|
332 | properties.initialized=true; |
---|
333 | }, |
---|
334 | |
---|
335 | setIsValid : function (object, value) |
---|
336 | { |
---|
337 | var properties=object.data('properties'); |
---|
338 | |
---|
339 | if(properties.isValid!=value && properties.initialized) |
---|
340 | { |
---|
341 | properties.isValid=value; |
---|
342 | if(properties.isValid) |
---|
343 | { |
---|
344 | if(properties.isCB) |
---|
345 | { |
---|
346 | object.parent().removeClass('ui-inputCheckbox ui-error'); |
---|
347 | } |
---|
348 | else |
---|
349 | { |
---|
350 | object.removeClass('ui-error'); |
---|
351 | } |
---|
352 | } |
---|
353 | else |
---|
354 | { |
---|
355 | if(properties.isCB) |
---|
356 | { |
---|
357 | object.parent().addClass('ui-inputCheckbox ui-error'); |
---|
358 | } |
---|
359 | else |
---|
360 | { |
---|
361 | object.addClass('ui-error'); |
---|
362 | } |
---|
363 | } |
---|
364 | } |
---|
365 | return(properties.isValid); |
---|
366 | }, |
---|
367 | |
---|
368 | setReturnMode : function (object, value) |
---|
369 | { |
---|
370 | var options=object.data('options'), |
---|
371 | properties=object.data('properties'); |
---|
372 | |
---|
373 | if((!properties.initialized || options.returnMode!=value) && (value=='selected' || value=='notSelected')) |
---|
374 | { |
---|
375 | options.returnMode=value; |
---|
376 | } |
---|
377 | return(options.returnMode); |
---|
378 | }, |
---|
379 | |
---|
380 | setValues : function (object, value) |
---|
381 | { |
---|
382 | var options=object.data('options'); |
---|
383 | |
---|
384 | if(value.forced!=null) options.values.forced=value.forced; |
---|
385 | if(value.checked!=null) options.values.checked=value.checked; |
---|
386 | if(value.unchecked!=null) options.values.unchecked=value.unchecked; |
---|
387 | |
---|
388 | return(options.values); |
---|
389 | }, //setValue |
---|
390 | |
---|
391 | setValue : function (object, id, value, apply) |
---|
392 | { |
---|
393 | var options=object.data('options'), |
---|
394 | properties=object.data('properties'); |
---|
395 | |
---|
396 | if(apply) |
---|
397 | { |
---|
398 | if(id==':all' || id==':none' || id==':invert') |
---|
399 | { |
---|
400 | //generic command |
---|
401 | switch(id) |
---|
402 | { |
---|
403 | case ':all': |
---|
404 | properties.checkboxList.attr('checked', true); |
---|
405 | break; |
---|
406 | case ':none': |
---|
407 | properties.checkboxList.attr('checked', false); |
---|
408 | break; |
---|
409 | case ':invert': |
---|
410 | properties.checkboxList.each( |
---|
411 | function () |
---|
412 | { |
---|
413 | $(this).attr('checked', !$(this).attr('checked')); |
---|
414 | } |
---|
415 | ); |
---|
416 | break; |
---|
417 | } |
---|
418 | } |
---|
419 | else if($.isArray(value) && !properties.isCB) |
---|
420 | { |
---|
421 | /* array of values : |
---|
422 | * ['value1', 'value2', ..., 'valueN'] |
---|
423 | * or array of object |
---|
424 | * [{id:'id1', value:'value1'}, {id:'idN', value:'valueN'}, ..., {id:'idN', value:'valueN'}] |
---|
425 | */ |
---|
426 | properties.checkboxList.attr('checked', false); |
---|
427 | |
---|
428 | for(var i=0;i<value.length;i++) |
---|
429 | { |
---|
430 | if(value[i].id!=null && value[i].value!=null) |
---|
431 | { |
---|
432 | if($('#'+value[i].id).attr('value')==value[i].value) $('#'+value[i].id).attr('checked', true); |
---|
433 | } |
---|
434 | else |
---|
435 | { |
---|
436 | properties.checkboxList.filter('[value='+value[i]+']').attr('checked', true); |
---|
437 | } |
---|
438 | } |
---|
439 | |
---|
440 | } |
---|
441 | else |
---|
442 | { |
---|
443 | // a single value |
---|
444 | |
---|
445 | if(options.values.checked==value) |
---|
446 | { |
---|
447 | if(properties.isCB) |
---|
448 | { |
---|
449 | properties.checkboxList.attr('checked', true); |
---|
450 | } |
---|
451 | else |
---|
452 | { |
---|
453 | if(id=='') |
---|
454 | { |
---|
455 | properties.checkboxList.attr('checked', true); |
---|
456 | } |
---|
457 | else |
---|
458 | { |
---|
459 | properties.checkboxList.filter('#'+id).attr('checked', true); |
---|
460 | } |
---|
461 | } |
---|
462 | } |
---|
463 | else |
---|
464 | { |
---|
465 | if(properties.isCB) |
---|
466 | { |
---|
467 | properties.checkboxList.attr('checked', false); |
---|
468 | } |
---|
469 | else |
---|
470 | { |
---|
471 | if(id=='') |
---|
472 | { |
---|
473 | properties.checkboxList.attr('checked', false); |
---|
474 | } |
---|
475 | else |
---|
476 | { |
---|
477 | properties.checkboxList.filter('#'+id).attr('checked', false); |
---|
478 | } |
---|
479 | } |
---|
480 | } |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | if(options.change) object.trigger('inputCheckboxChange', {id:id, state:value}); |
---|
485 | |
---|
486 | return(true); |
---|
487 | }, //setValue |
---|
488 | |
---|
489 | setEventChange : function (object, value) |
---|
490 | { |
---|
491 | var options=object.data('options'); |
---|
492 | |
---|
493 | options.change=value; |
---|
494 | object.unbind('inputCheckboxChange'); |
---|
495 | if(value) object.bind('inputCheckboxChange', options.change); |
---|
496 | return(options.change); |
---|
497 | } |
---|
498 | |
---|
499 | }; |
---|
500 | |
---|
501 | |
---|
502 | $.fn.inputCheckbox = function(method) |
---|
503 | { |
---|
504 | if(publicMethods[method]) |
---|
505 | { |
---|
506 | return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); |
---|
507 | } |
---|
508 | else if(typeof method === 'object' || ! method) |
---|
509 | { |
---|
510 | return publicMethods.init.apply(this, arguments); |
---|
511 | } |
---|
512 | else |
---|
513 | { |
---|
514 | $.error( 'Method ' + method + ' does not exist on jQuery.inputCheckbox' ); |
---|
515 | } |
---|
516 | } // $.fn.inputCheckbox |
---|
517 | |
---|
518 | } |
---|
519 | )(jQuery); |
---|
520 | |
---|
521 | |
---|