redux-switch.js (2229B)
1 /*global redux_change, redux*/ 2 3 /** 4 * Switch 5 * Dependencies : jquery 6 * Feature added by : Smartik - http://smartik.ws/ 7 * Date : 03.17.2013 8 */ 9 10 (function( $ ) { 11 'use strict'; 12 13 redux.field_objects = redux.field_objects || {}; 14 redux.field_objects.switch = redux.field_objects.switch || {}; 15 16 redux.field_objects.switch.init = function( selector ) { 17 selector = $.redux.getSelector( selector, 'switch' ); 18 19 $( selector ).each( 20 function() { 21 var el = $( this ); 22 var parent = el; 23 24 if ( ! el.hasClass( 'redux-field-container' ) ) { 25 parent = el.parents( '.redux-field-container:first' ); 26 } 27 28 if ( parent.is( ':hidden' ) ) { 29 return; 30 } 31 32 if ( parent.hasClass( 'redux-field-init' ) ) { 33 parent.removeClass( 'redux-field-init' ); 34 } else { 35 return; 36 } 37 38 el.find( '.cb-enable' ).on( 39 'click', 40 function() { 41 var parent; 42 var obj; 43 var $fold; 44 45 if ( $( this ).hasClass( 'selected' ) ) { 46 return; 47 } 48 49 parent = $( this ).parents( '.switch-options' ); 50 51 $( '.cb-disable', parent ).removeClass( 'selected' ); 52 $( this ).addClass( 'selected' ); 53 $( '.checkbox-input', parent ).val( 1 ).trigger( 'change' ); 54 55 redux_change( $( '.checkbox-input', parent ) ); 56 57 // Fold/unfold related options. 58 obj = $( this ); 59 $fold = '.f_' + obj.data( 'id' ); 60 61 el.find( $fold ).slideDown( 'normal', 'swing' ); 62 } 63 ); 64 65 el.find( '.cb-disable' ).on( 66 'click', 67 function() { 68 var parent; 69 var obj; 70 var $fold; 71 72 if ( $( this ).hasClass( 'selected' ) ) { 73 return; 74 } 75 76 parent = $( this ).parents( '.switch-options' ); 77 78 $( '.cb-enable', parent ).removeClass( 'selected' ); 79 $( this ).addClass( 'selected' ); 80 $( '.checkbox-input', parent ).val( 0 ).trigger( 'change' ); 81 82 redux_change( $( '.checkbox-input', parent ) ); 83 84 // Fold/unfold related options. 85 obj = $( this ); 86 $fold = '.f_' + obj.data( 'id' ); 87 88 el.find( $fold ).slideUp( 'normal', 'swing' ); 89 } 90 ); 91 92 el.find( '.cb-enable span, .cb-disable span' ).find().attr( 'unselectable', 'on' ); 93 } 94 ); 95 }; 96 })( jQuery );