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

Last change on this file since 15555 was 8961, checked in by grum, 13 years ago

release 3.4.0
fix bug:1984, bug:2109
js file are minified, remove packed files

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