source: extensions/GrumPluginClasses/js/ui.inputSwitchButton.js @ 16012

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

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

File size: 10.0 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.inputSwitchButton.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   | 2011/06/18 | 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                          checked:'yes',
58                          unchecked:'no'
59                        },
60                      change:null,
61                      group:''
62                    };
63
64              // if options given, merge it
65              // if(opt) $.extend(options, opt); ==> options are set by setters
66
67              $this
68                .data('options', options)
69                .addClass('ui-inputSwitchButton ui-inputSwitchButton-unchecked');
70
71              if(!properties)
72              {
73                $this.data('properties',
74                  {
75                    initialized:false,
76                    checked:false
77                  }
78                );
79                properties=$this.data('properties');
80              }
81
82
83              $this.bind('click.inputSwitchButton',
84                function (event)
85                {
86                  privateMethods.switchValue($this);
87                }
88              );
89
90              privateMethods.setOptions($this, opt);
91            }
92          );
93        }, // init
94      destroy : function ()
95        {
96          return this.each(
97            function()
98            {
99              // default values for the plugin
100              var $this=$(this);
101
102              $this
103                .unbind('.inputSwitchButton')
104                .removeData()
105                .removeClass('ui-inputSwitchButton ui-inputSwitchButton-unchecked ui-inputSwitchButton-checked');
106              delete $this;
107            }
108          );
109        }, // destroy
110
111      options: function (value)
112        {
113          return(
114            this.each(
115              function()
116              {
117                privateMethods.setOptions($(this), value);
118              }
119            )
120          );
121        }, // options
122
123      disabled: function (value)
124        {
125          if(value!=null)
126          {
127            return(
128              this.each(
129                function()
130                {
131                  privateMethods.setDisabled($(this), value);
132                }
133              )
134            );
135          }
136          else
137          {
138            return(privateMethods.getDisabled($(this)));
139          }
140        }, // disabled
141
142      values: function (values)
143        {
144          if(value!=null)
145          {
146            // set selected value
147            return(
148              this.each(
149                function()
150                {
151                  privateMethods.setValues($(this), value);
152                }
153              )
154            );
155          }
156          else
157          {
158            var options=this.data('options');
159            // return the selected tags
160            return(options.values);
161          }
162      },
163
164      switchValue: function ()
165        {
166          return(
167            this.each(
168              function()
169              {
170                privateMethods.switchValue($(this));
171              }
172            )
173          );
174
175        }, // value
176
177      value: function (value)
178        {
179          var properties=this.data('properties');
180
181          if(value!=null)
182          {
183            // set selected value
184            return(
185              this.each(
186                function()
187                {
188                  privateMethods.setValue($(this), value, true);
189                }
190              )
191            );
192          }
193          else
194          {
195            var options = this.data('options');
196
197            return(properties.checked?options.values.checked:options.values.unchecked);
198          }
199
200        }, // value
201
202      group: function (value)
203        {
204          var options=this.data('options');
205
206          if(value!=null)
207          {
208            // set selected group
209            return(
210              this.each(
211                function()
212                {
213                  privateMethods.setGroup($(this), value);
214                }
215              )
216            );
217          }
218          else
219          {
220            return(options.group);
221          }
222
223        }, // group
224
225      change: function (value)
226        {
227          if(value!=null && $.isFunction(value))
228          {
229            // set selected value
230            return(
231              this.each(
232                function()
233                {
234                  privateMethods.setEventChange($(this), value);
235                }
236              )
237            );
238          }
239          else
240          {
241            // return the selected value
242            var options=this.data('options');
243
244            if(options)
245            {
246              return(options.change);
247            }
248            else
249            {
250              return(null);
251            }
252          }
253        } // change
254
255    }; // methods
256
257
258    /*
259     * plugin 'private' methods
260     */
261    var privateMethods =
262    {
263      setOptions : function (object, value)
264        {
265          var properties=object.data('properties'),
266              options=object.data('options');
267
268          if(!$.isPlainObject(value)) return(false);
269
270          properties.initialized=false;
271
272          privateMethods.setGroup(object, (value.group!=null)?value.group:options.group, true);
273          privateMethods.setValue(object, (value.value!=null)?value.value:options.value, true);
274          privateMethods.setEventChange(object, (value.change!=null)?value.change:options.change);
275
276          properties.initialized=true;
277        },
278
279      setValues : function (object, value)
280        {
281          var options=object.data('options');
282
283          if(value.checked!=null) options.values.checked=value.checked;
284          if(value.unchecked!=null) options.values.unchecked=value.unchecked;
285
286          return(options.values);
287        }, //setValues
288
289
290      setGroup: function (object, value)
291        {
292          var options=object.data('options'),
293              properties=object.data('properties');
294
295          if(options.group!=value)
296          {
297            if(options.group!="")
298            {
299              var listGroup=$(document).data('isbGroup_'+options.group),
300                  p=-1;
301              if(listGroup==null) listGroup=[];
302              p=$.inArray(object.attr('id'), listGroup);
303              if(p>-1) listGroup.splice(p,1);
304              $(document).data('isbGroup_'+options.group, listGroup);
305            }
306            options.group=value;
307            listGroup=$(document).data('isbGroup_'+value);
308            if(listGroup==null) listGroup=[];
309            listGroup.push(object.attr('id'));
310            $(document).data('isbGroup_'+value, listGroup);
311          }
312        },
313
314      switchValue: function (object)
315        {
316          var options=object.data('options'),
317              properties=object.data('properties');
318
319          if(options.values.checked==properties.checked)
320          {
321            privateMethods.setValue(object, options.values.unchecked, true);
322          }
323          else
324          {
325            privateMethods.setValue(object, options.values.checked, true);
326          }
327        },
328
329      setValue : function (object, value, apply)
330        {
331          var options=object.data('options'),
332              properties=object.data('properties');
333
334          if(options.values.checked==value)
335          {
336            if(options.group!="")
337            {
338              listGroup=$(document).data('isbGroup_'+options.group);
339              if(listGroup==null) listGroup=[];
340              for(i=0;i<listGroup.length;i++)
341              {
342                if(listGroup[i]!=object.attr('id')) $('#'+listGroup[i]).inputSwitchButton('value', options.values.unchecked);
343              }
344            }
345
346            object
347              .addClass('ui-inputSwitchButton-checked')
348              .removeClass('ui-inputSwitchButton-unchecked');
349            properties.checked=value;
350          }
351          else if(options.values.unchecked==value)
352          {
353            object
354              .addClass('ui-inputSwitchButton-unchecked')
355              .removeClass('ui-inputSwitchButton-checked');
356            properties.checked=value;
357          }
358
359          if(options.change) object.trigger('inputSwitchButtonChange', {checked:value});
360
361          return(true);
362        }, //setValue
363
364      setEventChange : function (object, value)
365        {
366          var options=object.data('options');
367
368          options.change=value;
369          object.unbind('inputSwitchButtonChange');
370          if(value) object.bind('inputSwitchButtonChange', options.change);
371          return(options.change);
372        }
373
374    };
375
376
377    $.fn.inputSwitchButton = function(method)
378    {
379      if(publicMethods[method])
380      {
381        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
382      }
383      else if(typeof method === 'object' || ! method)
384      {
385        return publicMethods.init.apply(this, arguments);
386      }
387      else
388      {
389        $.error( 'Method ' +  method + ' does not exist on jQuery.inputSwitchButton' );
390      }
391    } // $.fn.inputSwitchButton
392
393  }
394)(jQuery);
395
396
Note: See TracBrowser for help on using the repository browser.