click-to-copy.js
3 months ago
post-type-help-doc-imports.js
3 months ago
post-type-help-docs.js
3 months ago
qs-add.js
3 months ago
qs-remove.js
3 months ago
post-type-help-docs.js
146 lines
| 1 | jQuery( document ).ready( function( $ ) { |
| 2 | |
| 3 | const POST_PHP_ENCODED = 'cG9zdC5waHA='; |
| 4 | const MAIN_ENCODED = 'bWFpbg=='; |
| 5 | const ADMIN_BAR_ENCODED = 'YWRtaW5fYmFy'; |
| 6 | |
| 7 | function checkExclusivity() { |
| 8 | const $allSelects = $( 'select[id$="_site_location"]' ); |
| 9 | const selectedValues = []; |
| 10 | |
| 11 | $allSelects.each( function() { |
| 12 | const val = $( this ).val(); |
| 13 | if ( val === MAIN_ENCODED || val === ADMIN_BAR_ENCODED ) { |
| 14 | selectedValues.push( val ); |
| 15 | } |
| 16 | } ); |
| 17 | |
| 18 | $allSelects.each( function() { |
| 19 | const $currentSelect = $( this ); |
| 20 | const currentVal = $currentSelect.val(); |
| 21 | |
| 22 | $currentSelect.find( 'option' ).each( function() { |
| 23 | const optVal = $( this ).val(); |
| 24 | |
| 25 | if ( optVal === MAIN_ENCODED || optVal === ADMIN_BAR_ENCODED ) { |
| 26 | if ( selectedValues.includes( optVal ) && currentVal !== optVal ) { |
| 27 | $( this ).prop( 'disabled', true ); |
| 28 | } else { |
| 29 | $( this ).prop( 'disabled', false ); |
| 30 | } |
| 31 | } |
| 32 | } ); |
| 33 | } ); |
| 34 | } |
| 35 | |
| 36 | function updateRow( $row ) { |
| 37 | const site_location_el = $row.find( 'select[id$="_site_location"]' ); |
| 38 | const page_location_el = $row.find( 'select[id$="_page_location"]' ); |
| 39 | const site_value_encoded = site_location_el.val(); |
| 40 | |
| 41 | checkExclusivity(); |
| 42 | |
| 43 | // 1. Specific logic for the "Side" option visibility |
| 44 | const $sideOption = page_location_el.find( 'option[value="side"]' ); |
| 45 | if ( site_value_encoded === POST_PHP_ENCODED ) { |
| 46 | $sideOption.show(); |
| 47 | } else { |
| 48 | // If user previously selected 'side' and it's now hidden, reset to default |
| 49 | if ( page_location_el.val() === 'side' ) { |
| 50 | page_location_el.val( page_location_el.find( 'option:first' ).val() ); |
| 51 | } |
| 52 | $sideOption.hide(); |
| 53 | } |
| 54 | |
| 55 | // 2. Process localized rules for showing/hiding entire fields |
| 56 | const rules = helpdocs_help_docs.location_rules[ site_value_encoded ] || []; |
| 57 | let fields_to_show = []; |
| 58 | |
| 59 | rules.forEach( rule => { |
| 60 | if ( rule.controller ) { |
| 61 | const $controller = $row.find( '[id$="_' + rule.controller + '"]' ); |
| 62 | if ( $controller.val() === rule.controller_value ) { |
| 63 | fields_to_show.push( rule.dependent ); |
| 64 | } |
| 65 | } else if ( rule.field ) { |
| 66 | fields_to_show.push( rule.field ); |
| 67 | } |
| 68 | } ); |
| 69 | |
| 70 | const allFields = [ |
| 71 | 'page_location', 'custom', 'addt_params', 'post_types', |
| 72 | 'order', 'toc', 'function_example', 'admin_bar_tips', 'dashboard_warning', 'css_selector' |
| 73 | ]; |
| 74 | |
| 75 | allFields.forEach( field => { |
| 76 | const shouldShow = fields_to_show.includes( field ); |
| 77 | const $el = $row.find( '[id$="_' + field + '"]' ); |
| 78 | |
| 79 | if ( ! $el.length ) return; |
| 80 | |
| 81 | let $container; |
| 82 | if ( field === 'post_types' ) { |
| 83 | $container = $el.closest( '.helpdocs-post-types' ); |
| 84 | } else if ( field === 'addt_params' || field === 'toc' ) { |
| 85 | $container = $el.closest( 'label' ); |
| 86 | } else if ( field === 'function_example' || field === 'admin_bar_tips' || field === 'dashboard_warning' ) { |
| 87 | $container = $el; |
| 88 | } else if ( field === 'custom' ) { |
| 89 | $container = $el.closest( '.helpdocs-custom-url' ); |
| 90 | } else { |
| 91 | $container = $el.add( $el.prev( 'label' ) ); |
| 92 | } |
| 93 | |
| 94 | $container.toggle( shouldShow ); |
| 95 | |
| 96 | if ( shouldShow ) { |
| 97 | if ( $el.is( 'input[type="text"], input[type="number"]' ) ) { |
| 98 | $el.css( 'display', 'inline-block' ); |
| 99 | } else if ( $el.is( 'select' ) || $el.is( 'span' ) ) { |
| 100 | $el.css( 'display', 'block' ); |
| 101 | } |
| 102 | } |
| 103 | } ); |
| 104 | } |
| 105 | |
| 106 | $( '#helpdocs-location-repeater' ).on( 'change', 'select', function() { |
| 107 | updateRow( $( this ).closest( '.helpdocs-location-row' ) ); |
| 108 | } ); |
| 109 | |
| 110 | $( '#add-location' ).on( 'click', function( e ) { |
| 111 | e.preventDefault(); |
| 112 | const $repeater = $( '#helpdocs-location-repeater' ); |
| 113 | const $rows = $repeater.find( '.helpdocs-location-row' ); |
| 114 | const newIndex = $rows.length; |
| 115 | |
| 116 | const $newRow = $rows.first().clone(); |
| 117 | |
| 118 | $newRow.find( 'input, select, label, span' ).each( function() { |
| 119 | const $item = $( this ); |
| 120 | [ 'name', 'id', 'for' ].forEach( attr => { |
| 121 | let val = $item.attr( attr ); |
| 122 | if ( val ) { |
| 123 | val = val.replace( /\[\s*\d+\s*\]/, '[ ' + newIndex + ' ]' ) |
| 124 | .replace( /_\d+_/, '_' + newIndex + '_' ); |
| 125 | $item.attr( attr, val ); |
| 126 | } |
| 127 | } ); |
| 128 | } ); |
| 129 | |
| 130 | $newRow.find( 'input[type="text"], input[type="number"], select' ).val( '' ); |
| 131 | $newRow.find( 'input[type="checkbox"]' ).prop( 'checked', false ); |
| 132 | |
| 133 | if ( $newRow.find( '.remove-location' ).length === 0 ) { |
| 134 | $newRow.append( '<button type="button" class="remove-location button" title="Remove Location"><span class="dashicons dashicons-trash"></span></button>' ); |
| 135 | } |
| 136 | |
| 137 | $repeater.append( $newRow ); |
| 138 | updateRow( $newRow ); |
| 139 | } ); |
| 140 | |
| 141 | $( '#helpdocs-location-repeater' ).on( 'click', '.remove-location', function( e ) { |
| 142 | e.preventDefault(); |
| 143 | $( this ).closest( '.helpdocs-location-row' ).remove(); |
| 144 | checkExclusivity(); |
| 145 | } ); |
| 146 | } ); |