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