← All changes
|
libraries/cmb2-conditional-fields/js/conditional-fields.js
+209
-0
7.8.8
→
8.0.5
View file →
| @@ -1,0 +1,209 @@ | ||
| 1 | +(function( $ ) { | |
| 2 | + | |
| 3 | + // Initial check | |
| 4 | + $('.cmb2-metabox input, .cmb2-metabox select, .cmb2-metabox textarea').each(function() { | |
| 5 | + | |
| 6 | + cmb_conditional_fields_check_field_change( $(this), true ); | |
| 7 | + | |
| 8 | + }); | |
| 9 | + | |
| 10 | + // Change field | |
| 11 | + $('.cmb2-metabox input, .cmb2-metabox select, .cmb2-metabox textarea').on( 'change', function( e ) { | |
| 12 | + | |
| 13 | + cmb_conditional_fields_check_field_change( $(this), false ); | |
| 14 | + | |
| 15 | + }); | |
| 16 | + | |
| 17 | + // Change field | |
| 18 | + $('.cmb2-metabox input[type="text"], .cmb2-metabox input[type="number"]').on( 'keyup', function( e ) { | |
| 19 | + | |
| 20 | + cmb_conditional_fields_check_field_change( $(this), false ); | |
| 21 | + | |
| 22 | + }); | |
| 23 | + | |
| 24 | +})(jQuery); | |
| 25 | + | |
| 26 | +function cmb_conditional_fields_check_field_change( field, first_check ) { | |
| 27 | + | |
| 28 | + var $ = jQuery || $; | |
| 29 | + | |
| 30 | + var field_id = field.attr('id'); | |
| 31 | + var field_type = field.attr('type'); | |
| 32 | + | |
| 33 | + if( field_type === 'radio' ) { | |
| 34 | + if( ! field.prop('checked') ) { | |
| 35 | + return; | |
| 36 | + } | |
| 37 | + | |
| 38 | + field_id = field.attr('name'); | |
| 39 | + } | |
| 40 | + | |
| 41 | + var form = field.closest('.cmb2-metabox'); | |
| 42 | + | |
| 43 | + form.find( '.cmb-conditional-fields.cmb-show-if-field-' + field_id ).each(function() { | |
| 44 | + var target = $(this); | |
| 45 | + | |
| 46 | + cmb_conditional_fields_check_conditions( field, target, first_check, 'show' ); | |
| 47 | + | |
| 48 | + }); | |
| 49 | + | |
| 50 | + form.find( '.cmb-conditional-fields.cmb-hide-if-field-' + field_id ).each(function() { | |
| 51 | + var target = $(this); | |
| 52 | + | |
| 53 | + cmb_conditional_fields_check_conditions( field, target, first_check, 'hide' ); | |
| 54 | + | |
| 55 | + }); | |
| 56 | + | |
| 57 | +} | |
| 58 | + | |
| 59 | +function cmb_conditional_fields_check_conditions( field, target, instant, action ) { | |
| 60 | + | |
| 61 | + var $ = jQuery || $; | |
| 62 | + var meet_conditions = null; | |
| 63 | + | |
| 64 | + var field_id = field.attr('id'); | |
| 65 | + var field_type = field.attr('type'); | |
| 66 | + | |
| 67 | + if( field_type === 'radio' ) { | |
| 68 | + field_id = field.attr('name'); | |
| 69 | + } | |
| 70 | + | |
| 71 | + var conditions = target.attr('class').split(' '); | |
| 72 | + | |
| 73 | + conditions.forEach(function(condition) { | |
| 74 | + | |
| 75 | + if( condition.startsWith('cmb-' + action + '-if-field-' + field_id + '-condition') ) { | |
| 76 | + condition = condition.replace('cmb-' + action + '-if-field-' + field_id + '-condition-', ''); | |
| 77 | + | |
| 78 | + if( meet_conditions === null ) { | |
| 79 | + // First check | |
| 80 | + meet_conditions = cmb_conditional_fields_meet_condition( condition, field ); | |
| 81 | + } else { | |
| 82 | + // Multiples values as check (OR) | |
| 83 | + meet_conditions = cmb_conditional_fields_meet_condition( condition, field ) || meet_conditions; | |
| 84 | + | |
| 85 | + // TODO: Support for AND | |
| 86 | + } | |
| 87 | + | |
| 88 | + } | |
| 89 | + | |
| 90 | + }); | |
| 91 | + | |
| 92 | + if( action === 'show' ) { | |
| 93 | + if( meet_conditions ) { | |
| 94 | + cmb_conditional_fields_show( field, target, instant ); | |
| 95 | + } else { | |
| 96 | + cmb_conditional_fields_hide( field, target, instant ); | |
| 97 | + } | |
| 98 | + } else { | |
| 99 | + if( meet_conditions ) { | |
| 100 | + cmb_conditional_fields_hide( field, target, instant ); | |
| 101 | + } else { | |
| 102 | + cmb_conditional_fields_show( field, target, instant ); | |
| 103 | + } | |
| 104 | + } | |
| 105 | + | |
| 106 | + | |
| 107 | +} | |
| 108 | + | |
| 109 | +function cmb_conditional_fields_meet_condition( condition, field ) { | |
| 110 | + | |
| 111 | + var meet_conditions = null; | |
| 112 | + | |
| 113 | + switch ( condition ) { | |
| 114 | + case 'empty': | |
| 115 | + if( field.val().length ) { | |
| 116 | + meet_conditions = false; | |
| 117 | + } else { | |
| 118 | + meet_conditions = true; | |
| 119 | + } | |
| 120 | + break; | |
| 121 | + case 'not-empty': | |
| 122 | + if( ! field.val().length ) { | |
| 123 | + meet_conditions = false; | |
| 124 | + } else { | |
| 125 | + meet_conditions = true; | |
| 126 | + } | |
| 127 | + break; | |
| 128 | + case 'checked': | |
| 129 | + if( ! field.prop('checked') ) { | |
| 130 | + meet_conditions = false; | |
| 131 | + } else { | |
| 132 | + meet_conditions = true; | |
| 133 | + } | |
| 134 | + break; | |
| 135 | + case 'not-checked': | |
| 136 | + if( field.prop('checked') ) { | |
| 137 | + meet_conditions = false; | |
| 138 | + } else { | |
| 139 | + meet_conditions = true; | |
| 140 | + } | |
| 141 | + break; | |
| 142 | + default: | |
| 143 | + if( field.val() !== condition ) { | |
| 144 | + meet_conditions = false; | |
| 145 | + } else { | |
| 146 | + meet_conditions = true; | |
| 147 | + } | |
| 148 | + break; | |
| 149 | + } | |
| 150 | + | |
| 151 | + return meet_conditions; | |
| 152 | + | |
| 153 | +} | |
| 154 | + | |
| 155 | +function cmb_conditional_fields_show( field, target, instant ) { | |
| 156 | + | |
| 157 | + var $ = jQuery || $; | |
| 158 | + | |
| 159 | + if( instant ) { | |
| 160 | + | |
| 161 | + if( cmb_conditional_fields_is_tab_active( field ) ) { | |
| 162 | + target.show(); | |
| 163 | + } | |
| 164 | + | |
| 165 | + // Support for cmb2 tabs | |
| 166 | + target.removeClass('cmb2-tab-ignore'); | |
| 167 | + } else { | |
| 168 | + if( cmb_conditional_fields_is_tab_active( field ) ) { | |
| 169 | + target.slideDown('fast'); | |
| 170 | + | |
| 171 | + target.addClass('cmb-tab-active-item'); | |
| 172 | + } | |
| 173 | + | |
| 174 | + // Support for cmb2 tabs | |
| 175 | + target.removeClass('cmb2-tab-ignore'); | |
| 176 | + } | |
| 177 | + | |
| 178 | +} | |
| 179 | + | |
| 180 | +function cmb_conditional_fields_hide( field, target, instant ) { | |
| 181 | + | |
| 182 | + var $ = jQuery || $; | |
| 183 | + | |
| 184 | + if( instant ) { | |
| 185 | + target.hide(); | |
| 186 | + | |
| 187 | + // Support for cmb2 tabs | |
| 188 | + target.addClass('cmb2-tab-ignore'); | |
| 189 | + } else { | |
| 190 | + target.slideUp('fast'); | |
| 191 | + | |
| 192 | + if( cmb_conditional_fields_is_tab_active( field ) ) { | |
| 193 | + target.removeClass('cmb-tab-active-item'); | |
| 194 | + } | |
| 195 | + | |
| 196 | + // Support for cmb2 tabs | |
| 197 | + target.addClass('cmb2-tab-ignore'); | |
| 198 | + } | |
| 199 | + | |
| 200 | +} | |
| 201 | + | |
| 202 | +function cmb_conditional_fields_is_tab_active( field ) { | |
| 203 | + if( field.closest('.cmb-tabs-wrap').length ) { | |
| 204 | + return field.closest('.cmb-row').hasClass('cmb-tab-active-item'); | |
| 205 | + } | |
| 206 | + | |
| 207 | + return true; | |
| 208 | + | |
| 209 | +} | |