|
| 1 | +/** |
| 2 | + * Handles conditional loading of specific setting fields. |
| 3 | + * |
| 4 | + * @package SM |
| 5 | + */ |
| 6 | + |
| 7 | +if ( typeof sm_conditionals !== 'undefined' ) { |
| 8 | + // Bind to elements. |
| 9 | + jQuery( |
| 10 | + function () { |
| 11 | + jQuery.each( |
| 12 | + sm_conditionals, |
| 13 | + function ( element_id, element_conditionals ) { |
| 14 | + /** |
| 15 | + * The element that should be shown or hidden. |
| 16 | + * |
| 17 | + * @type object |
| 18 | + */ |
| 19 | + let target_element = jQuery( '#' + element_id ); |
| 20 | + |
| 21 | + /** |
| 22 | + * The element's table row. |
| 23 | + * |
| 24 | + * @type object |
| 25 | + */ |
| 26 | + let table_row = target_element.closest( 'tr' ); |
| 27 | + |
| 28 | + // Go through each conditional. |
| 29 | + jQuery.each( |
| 30 | + element_conditionals, |
| 31 | + function ( index, condition_data ) { |
| 32 | + /** |
| 33 | + * The element's HTML ID & database ID. |
| 34 | + */ |
| 35 | + let conditional_element_id = condition_data.id; |
| 36 | + |
| 37 | + /** |
| 38 | + * The element itself. |
| 39 | + * |
| 40 | + * @type object |
| 41 | + */ |
| 42 | + let conditional_element = jQuery( '#' + conditional_element_id ); |
| 43 | + |
| 44 | + /** |
| 45 | + * The value that we are looking for in the element. |
| 46 | + * |
| 47 | + * @type string |
| 48 | + */ |
| 49 | + let target_value = sm_isset( condition_data[ "value" ] ) ? condition_data[ "value" ] : condition_data[ "!value" ]; |
| 50 | + |
| 51 | + /** |
| 52 | + * If we should invert the value. |
| 53 | + * |
| 54 | + * @type boolean |
| 55 | + */ |
| 56 | + let not = sm_isset( condition_data[ "!value" ] ); |
| 57 | + |
| 58 | + // Hook into element's change event, so we can act on it. |
| 59 | + conditional_element.on( |
| 60 | + 'change', |
| 61 | + function () { |
| 62 | + /** |
| 63 | + * Currently selected value. |
| 64 | + * |
| 65 | + * @type string |
| 66 | + */ |
| 67 | + let selected_value = this.value; |
| 68 | + |
| 69 | + // Hide or show the elements. |
| 70 | + sm_hide_show_elements( target_value, selected_value, not, table_row ); |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + // Call the function first time. |
| 75 | + sm_hide_show_elements( target_value, conditional_element.val(), not, table_row ); |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | + ); |
| 80 | + } |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Hides or shows the element based on its value. |
| 86 | + * |
| 87 | + * @param {string} target_value Value that we are looking for. |
| 88 | + * @param {string} current_value The current value of the element. |
| 89 | + * @param {boolean} not If we should invert the value. |
| 90 | + * @param {object} table_row The table row to hide or show. |
| 91 | + */ |
| 92 | +function sm_hide_show_elements( target_value, current_value, not, table_row ) { |
| 93 | + let element = table_row.find( 'select' ); |
| 94 | + |
| 95 | + /** |
| 96 | + * If we should hide the row. |
| 97 | + * |
| 98 | + * @type {boolean} |
| 99 | + */ |
| 100 | + let hide = target_value !== current_value; |
| 101 | + |
| 102 | + /** |
| 103 | + * If the element should get data via Ajax. |
| 104 | + * |
| 105 | + * @type boolean |
| 106 | + */ |
| 107 | + let is_ajax = element.data( 'ajax' ) ? element.data( 'ajax' ) : false; |
| 108 | + |
| 109 | + // Invert if needed. |
| 110 | + hide = not ? ! hide : hide; |
| 111 | + |
| 112 | + // Do hide. |
| 113 | + if ( ! is_ajax ) { |
| 114 | + if ( hide ) { |
| 115 | + table_row.addClass( 'hidden' ); |
| 116 | + } else { |
| 117 | + table_row.removeClass( 'hidden' ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // If we should get options via Ajax. |
| 122 | + if ( is_ajax ) { |
| 123 | + sm_reset_option_value( element, ! table_row.hasClass( 'hidden' ) ); |
| 124 | + |
| 125 | + let data = { |
| 126 | + 'action': 'sm_settings_get_select_data', |
| 127 | + 'id': current_value, |
| 128 | + }; |
| 129 | + |
| 130 | + // Request element data. |
| 131 | + jQuery.ajax( |
| 132 | + { |
| 133 | + method: 'POST', |
| 134 | + url: ajaxurl, |
| 135 | + data: data, |
| 136 | + } |
| 137 | + ).always( |
| 138 | + function () { |
| 139 | + sm_reset_option_value( element, ! table_row.hasClass( 'hidden' ) ); |
| 140 | + } |
| 141 | + ).done( |
| 142 | + function ( response ) { |
| 143 | + // Convert JSON to array/object. |
| 144 | + if ( 'false' === response ) { |
| 145 | + response = false; |
| 146 | + } else { |
| 147 | + try { |
| 148 | + response = JSON.parse( response ); |
| 149 | + } catch ( err ) { |
| 150 | + response = false; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Write received values to element. |
| 155 | + if ( typeof response === 'object' ) { |
| 156 | + table_row.removeClass( 'hidden' ); |
| 157 | + |
| 158 | + switch ( element.prop( 'tagName' ) ) { |
| 159 | + case 'SELECT': |
| 160 | + element.find( 'option' ).each( |
| 161 | + function () { |
| 162 | + jQuery( this.remove() ); |
| 163 | + } |
| 164 | + ); |
| 165 | + jQuery.each( |
| 166 | + response, |
| 167 | + function ( id, item ) { |
| 168 | + table_row.find( 'select' ).append( jQuery( '<option/>' ).val( id ).text( item ) ); |
| 169 | + } |
| 170 | + ); |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if ( false === response ) { |
| 176 | + table_row.addClass( 'hidden' ); |
| 177 | + } |
| 178 | + } |
| 179 | + ).fail( |
| 180 | + function () { |
| 181 | + // Write error message if response is invalid. |
| 182 | + switch ( element.prop( 'tabName' ) ) { |
| 183 | + case 'SELECT': |
| 184 | + element.append( jQuery( '<option/>' ).val( '' ).text( 'Error.' ) ); |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * Clears the current option value. |
| 194 | + * |
| 195 | + * @type {object} The option element. |
| 196 | + * @type {boolean} If we should show that it's loading. |
| 197 | + */ |
| 198 | +function sm_reset_option_value( element, write_loading ) { |
| 199 | + switch ( element.prop( 'tagName' ) ) { |
| 200 | + case 'SELECT': |
| 201 | + element.find( 'option' ).each( |
| 202 | + function () { |
| 203 | + jQuery( this.remove() ); |
| 204 | + } |
| 205 | + ); |
| 206 | + } |
| 207 | + if ( write_loading ) { |
| 208 | + element.append( jQuery( '<option/>' ).val( '' ).text( 'Loading...' ) ); |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +/** |
| 213 | + * Checks to see if a value is set. |
| 214 | + * |
| 215 | + * @param {*} value The value to check. |
| 216 | + */ |
| 217 | +function sm_isset( value ) { |
| 218 | + try { |
| 219 | + return typeof value !== 'undefined' |
| 220 | + } catch ( e ) { |
| 221 | + return false |
| 222 | + } |
| 223 | +} |
0 commit comments