| 1 |
/** |
| 2 |
* Conditional Fields for jQuery |
| 3 |
* |
| 4 |
* @package WPZincDashboardWidget |
| 5 |
* @author WP Zinc |
| 6 |
*/ |
| 7 |
|
| 8 |
(function ( $ ) { |
| 9 |
|
| 10 |
"use strict"; |
| 11 |
|
| 12 |
/** |
| 13 |
* Create .conditional() function |
| 14 |
* |
| 15 |
* @param object options Override Default Settings |
| 16 |
*/ |
| 17 |
$.fn.conditional = function (options) { |
| 18 |
// Default Settings. |
| 19 |
var settings = $.extend( |
| 20 |
{ |
| 21 |
data: 'conditional', |
| 22 |
value: 'conditional-value', |
| 23 |
displayOnEnabled: 'conditional-display' |
| 24 |
}, |
| 25 |
options |
| 26 |
); |
| 27 |
|
| 28 |
// Setup conditionals on each DOM element. |
| 29 |
this.each( |
| 30 |
function () { |
| 31 |
// Check for conditional elements. |
| 32 |
if ( typeof $( this ).data( settings.data ) === 'undefined' ) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
// Setup vars. |
| 37 |
var conditionalElements, |
| 38 |
displayOnEnabled, |
| 39 |
value, |
| 40 |
displayElements; |
| 41 |
|
| 42 |
// Toggle + toggle on change. |
| 43 |
$( this ).on( |
| 44 |
'change', |
| 45 |
function () { |
| 46 |
// List the DOM elements to toggle. |
| 47 |
conditionalElements = $( this ).data( settings.data ).split( ',' ); |
| 48 |
|
| 49 |
// Determine whether to display DOM elements when the input is 'enabled'. |
| 50 |
displayOnEnabled = $( this ).data( settings.displayOnEnabled ); |
| 51 |
if ( typeof displayOnEnabled === 'undefined' ) { |
| 52 |
displayOnEnabled = true; |
| 53 |
} |
| 54 |
|
| 55 |
// Determine the value required to display elements. |
| 56 |
value = $( this ).data( settings.value ); |
| 57 |
if ( typeof value === 'undefined' ) { |
| 58 |
value = ''; |
| 59 |
} else { |
| 60 |
value = String( value ).split( ',' ); |
| 61 |
} |
| 62 |
|
| 63 |
// By default, don't display elements. |
| 64 |
displayElements = false; |
| 65 |
|
| 66 |
// Determine whether to display relational elements or not. |
| 67 |
switch ( $( this ).attr( 'type' ) ) { |
| 68 |
case 'checkbox': |
| 69 |
if ( displayOnEnabled ) { |
| 70 |
displayElements = $( this ).is( ':checked' ); |
| 71 |
} else { |
| 72 |
displayElements = ( $( this ).is( ':checked' ) ? false : true ); |
| 73 |
} |
| 74 |
break; |
| 75 |
|
| 76 |
default: |
| 77 |
if ( displayOnEnabled ) { |
| 78 |
if ( value.length > 0 ) { |
| 79 |
displayElements = ( ( value.indexOf( String( $( this ).val() ) ) == -1 ) ? false : true ); |
| 80 |
} else { |
| 81 |
displayElements = ( ( $( this ).val() === '' || $( this ).val() === '0' ) ? false : true ); |
| 82 |
} |
| 83 |
} else { |
| 84 |
if ( value.length > 0 ) { |
| 85 |
displayElements = ( ( value.indexOf( String( $( this ).val() ) ) == -1 ) ? true : false ); |
| 86 |
} else { |
| 87 |
displayElements = ( ( $( this ).val() === '' || $( this ).val() === '0' ) ? true : false ); |
| 88 |
} |
| 89 |
} |
| 90 |
break; |
| 91 |
} |
| 92 |
|
| 93 |
// Show/hide elements. |
| 94 |
var length = conditionalElements.length; |
| 95 |
for (var i = 0; i < length; i++) { |
| 96 |
// Check if we are finding an element by ID or class. |
| 97 |
var conditionalElement; |
| 98 |
if ( $( '#' + conditionalElements[i] ).length > 0 ) { |
| 99 |
conditionalElement = $( '#' + conditionalElements[i] ); |
| 100 |
} else { |
| 101 |
conditionalElement = $( '.' + conditionalElements[i], $( this ).parent() ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( displayElements ) { |
| 105 |
$( conditionalElement ).fadeIn( 300 ); |
| 106 |
} else { |
| 107 |
$( conditionalElement ).fadeOut( 300 ); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
); |
| 112 |
|
| 113 |
// Trigger a change on init so we run the above routine. |
| 114 |
$( this ).trigger( 'change' ); |
| 115 |
} |
| 116 |
); |
| 117 |
|
| 118 |
// Return DOM elements. |
| 119 |
return this; |
| 120 |
}; |
| 121 |
|
| 122 |
} )( jQuery ); |
| 123 |
|
| 124 |
/** |
| 125 |
* Initialize. |
| 126 |
*/ |
| 127 |
jQuery( document ).ready( |
| 128 |
function ( $ ) { |
| 129 |
|
| 130 |
$( 'input,select' ).conditional(); |
| 131 |
|
| 132 |
} |
| 133 |
); |
| 134 |
|