source: extensions/GrumPluginClasses/js/ui.inputCheckbox.js @ 19961

Last change on this file since 19961 was 16012, checked in by grum, 12 years ago

feature:2634- compatibility with Piwigo 2.4
+add some objects on js framework

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