source: extensions/GrumPluginClasses/js/ui.inputRadio.js @ 31634

Last change on this file since 31634 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: 11.1 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.inputRadio.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(
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                      disabled:[],
57                      hidden:[],
58                      value:'',
59                      change:null
60                    };
61
62              // if options given, merge it
63              // if(opt) $.extend(options, opt); ==> options are set by setters
64
65              $this.data('options', options);
66
67              if(!properties)
68              {
69                $this.data('properties',
70                  {
71                    initialized:false,
72                    value:'',
73                    isValid:true
74                  }
75                );
76                properties=$this.data('properties');
77              }
78
79              if(!objects)
80              {
81                objects =
82                  {
83                    radio:[]
84                  };
85
86                $this.data('objects', objects);
87              }
88
89              $this.find('input[type=radio]').each(
90                function (index, item)
91                {
92                  objects.radio.push($(item));
93                  $(item).attr('name', 'ir_'+$this.get(0).id).bind('click',
94                    function (event)
95                    {
96                      privateMethods.setValue($this, $(this).attr('value'), false);
97                    }
98                  );
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                  objects = $this.data('objects');
114              for(var i=0;i<objects.radio.length;i++)
115              {
116                objects.radio.unbind();
117              }
118              objects.container.unbind().remove();
119              $this
120                .unbind('.inputRadio')
121                .removeData()
122                .css(
123                  {
124                    width:'',
125                    height:''
126                  }
127                );
128              delete $this;
129            }
130          );
131        }, // destroy
132
133      options: function (value)
134        {
135          return(
136            this.each(
137              function()
138              {
139                privateMethods.setOptions($(this), value);
140              }
141            )
142          );
143        }, // options
144
145      disabled: function (value)
146        {
147          if(value!=null)
148          {
149            return(
150              this.each(
151                function()
152                {
153                  privateMethods.setDisabled($(this), value);
154                }
155              )
156            );
157          }
158          else
159          {
160            return(privateMethods.getDisabled($(this)));
161          }
162        }, // disabled
163
164      hidden: function (value)
165        {
166          if(value!=null)
167          {
168            return(
169              this.each(
170                function()
171                {
172                  privateMethods.setHidden($(this), value);
173                }
174              )
175            );
176          }
177          else
178          {
179            return(privateMethods.getHidden($(this)));
180          }
181        }, // disabled
182
183      value: function (value)
184        {
185          if(value!=null)
186          {
187            // set selected value
188            return(
189              this.each(
190                function()
191                {
192                  privateMethods.setValue($(this), value, true);
193                }
194              )
195            );
196          }
197          else
198          {
199            // return the selected tags
200            var properties=this.data('properties');
201            return(properties.value);
202          }
203        }, // value
204
205      isValid: function (value)
206        {
207          if(value!=null)
208          {
209            // set selected value
210            return(
211              this.each(
212                function()
213                {
214                  privateMethods.setIsValid($(this), value);
215                }
216              )
217            );
218          }
219          else
220          {
221            // return the selected tags
222            var properties=this.data('properties');
223
224            return(properties.isValid);
225          }
226        }, // isValid
227
228      change: function (value)
229        {
230          if(value!=null && $.isFunction(value))
231          {
232            // set selected value
233            return(
234              this.each(
235                function()
236                {
237                  privateMethods.setEventChange($(this), value);
238                }
239              )
240            );
241          }
242          else
243          {
244            // return the selected value
245            var options=this.data('options');
246
247            if(options)
248            {
249              return(options.change);
250            }
251            else
252            {
253              return(null);
254            }
255          }
256        } // change
257
258    }; // methods
259
260
261    /*
262     * plugin 'private' methods
263     */
264    var privateMethods =
265    {
266      setOptions : function (object, value)
267        {
268          var properties=object.data('properties'),
269              options=object.data('options');
270
271          if(!$.isPlainObject(value)) return(false);
272
273          properties.initialized=false;
274
275          privateMethods.setDisabled(object, (value.disabled!=null)?value.disabled:options.disabled);
276          privateMethods.setHidden(object, (value.hidden!=null)?value.hidden:options.hidden);
277          privateMethods.setValue(object, (value.value!=null)?value.value:options.value, true);
278
279          privateMethods.setEventChange(object, (value.change!=null)?value.change:options.change);
280
281          properties.initialized=true;
282        },
283
284      setIsValid : function (object, value)
285        {
286          var objects=object.data('objects'),
287              properties=object.data('properties');
288
289          if(properties.isValid!=value && properties.initialized)
290          {
291            properties.isValid=value;
292            if(properties.isValid)
293            {
294              objects.container.removeClass('ui-error');
295              objects.input.removeClass('ui-error');
296            }
297            else
298            {
299              objects.container.addClass('ui-error');
300              objects.input.addClass('ui-error');
301            }
302          }
303          return(properties.isValid);
304        },
305
306      setDisabled : function (object, value)
307        {
308          var objects=object.data('objects'),
309              index=-1;
310
311          if(!$.isArray(value)) value=[value];
312
313          for(var i=0;i<objects.radio.length;i++)
314          {
315            index=$.inArray(objects.radio[i].attr('value'), value);
316            objects.radio[i].attr('disabled', (index>-1));
317          }
318          return(privateMethods.getDisabled(object));
319        },
320
321      getDisabled : function (object)
322        {
323          var objects=object.data('objects'),
324              returned=[];
325
326          for(var i=0;i<objects.radio.length;i++)
327          {
328            if(objects.radio[i].attr('disabled')) returned.push(objects.radio[i].attr('value'));
329          }
330          return(returned);
331        },
332
333
334      setHidden : function (object, value)
335        {
336          var objects=object.data('objects');
337
338          if(!$.isArray(value)) value=[value];
339
340          for(var i=0;i<objects.radio.length;i++)
341          {
342            if($.inArray(objects.radio[i].attr('value'), value)>-1)
343            {
344              if(objects.radio[i].parent().attr('nodeName')=='LABEL')
345              {
346                objects.radio[i].parent().hide();
347              }
348              else
349              {
350                objects.radio[i].hide();
351              }
352            }
353            else
354            {
355              if(objects.radio[i].parent().attr('nodeName')=='LABEL')
356              {
357                objects.radio[i].parent().show();
358              }
359              else
360              {
361                objects.radio[i].show();
362              }
363            }
364          }
365          return(privateMethods.getHidden(object));
366        },
367
368      getHidden : function (object)
369        {
370          var objects=object.data('objects'),
371              returned=[];
372
373          for(var i=0;i<objects.radio.length;i++)
374          {
375            if(objects.radio[i].css('display')=='none') returned.push(objects.radio[i].attr('value'));
376          }
377          return(returned);
378        },
379
380      setValue : function (object, value, apply)
381        {
382          var options=object.data('options'),
383              properties=object.data('properties'),
384              objects=object.data('objects'),
385              index=privateMethods.findRadioByVal(object, value);
386
387          if(index==-1) return(false);
388
389          properties.value=value;
390
391          if(apply)
392          {
393            objects.radio[index].attr('checked', true);
394          }
395
396          if(options.change) object.trigger('inputRadioChange', properties.value);
397
398          return(true);
399        }, //setValue
400
401      setEventChange : function (object, value)
402        {
403          var options=object.data('options');
404
405          options.change=value;
406          object.unbind('inputRadioChange');
407          if(value) object.bind('inputRadioChange', options.change);
408          return(options.change);
409        },
410
411      findRadioByVal : function (object, value)
412        {
413          var objects=object.data('objects');
414
415          for(var i=0;i<objects.radio.length;i++)
416          {
417            if(objects.radio[i].attr('value')==value) return(i);
418          }
419          return(-1);
420        }
421
422    };
423
424
425    $.fn.inputRadio = function(method)
426    {
427      if(publicMethods[method])
428      {
429        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
430      }
431      else if(typeof method === 'object' || ! method)
432      {
433        return publicMethods.init.apply(this, arguments);
434      }
435      else
436      {
437        $.error( 'Method ' +  method + ' does not exist on jQuery.inputRadio' );
438      }
439    } // $.fn.inputRadio
440
441  }
442)(jQuery);
443
444
Note: See TracBrowser for help on using the repository browser.