script.js
183 lines
| 1 | /* global almData, jQuery */ |
| 2 | ( function ( $ ) { |
| 3 | 'use strict'; |
| 4 | |
| 5 | /* ---- Toast notification ---- */ |
| 6 | function showToast( message, type ) { |
| 7 | var $toast = $( '#alm-toast' ); |
| 8 | if ( ! $toast.length ) { |
| 9 | $toast = $( '<div id="alm-toast" class="alm-notice"></div>' ).appendTo( 'body' ); |
| 10 | } |
| 11 | $toast |
| 12 | .removeClass( 'alm-notice-success alm-notice-error alm-notice-show' ) |
| 13 | .addClass( 'alm-notice-' + type ) |
| 14 | .text( message ); |
| 15 | |
| 16 | setTimeout( function () { $toast.addClass( 'alm-notice-show' ); }, 10 ); |
| 17 | setTimeout( function () { $toast.removeClass( 'alm-notice-show' ); }, 3500 ); |
| 18 | } |
| 19 | |
| 20 | /* ---- Generic toggle handler ---- */ |
| 21 | function handleToggle( $btn, actionType ) { |
| 22 | var optionName = $btn.data( 'option' ); |
| 23 | var $row = $btn.closest( 'tr' ); |
| 24 | |
| 25 | $btn.prop( 'disabled', true ).prepend( '<span class="alm-spinner"></span>' ); |
| 26 | |
| 27 | $.post( almData.ajaxUrl, { |
| 28 | action : 'alm_toggle_autoload', |
| 29 | nonce : almData.nonce, |
| 30 | option_name : optionName, |
| 31 | action_type : actionType |
| 32 | } ) |
| 33 | .done( function ( response ) { |
| 34 | if ( response.success ) { |
| 35 | showToast( response.data.message, 'success' ); |
| 36 | |
| 37 | if ( actionType === 'disable' ) { |
| 38 | moveToDisabled( $row, optionName ); |
| 39 | } else { |
| 40 | removeFromDisabled( $row ); |
| 41 | } |
| 42 | } else { |
| 43 | showToast( response.data.message || 'An error occurred.', 'error' ); |
| 44 | resetBtn( $btn ); |
| 45 | } |
| 46 | } ) |
| 47 | .fail( function () { |
| 48 | showToast( 'Request failed. Please try again.', 'error' ); |
| 49 | resetBtn( $btn ); |
| 50 | } ); |
| 51 | } |
| 52 | |
| 53 | function resetBtn( $btn ) { |
| 54 | $btn.find( '.alm-spinner' ).remove(); |
| 55 | $btn.prop( 'disabled', false ); |
| 56 | } |
| 57 | |
| 58 | /* ---- Move row from Section 1 → Section 2 ---- */ |
| 59 | function moveToDisabled( $row, optionName ) { |
| 60 | var $disabledTable = $( '#alm-disabled-table' ); |
| 61 | var $noDisabled = $( '#alm-no-disabled' ); |
| 62 | |
| 63 | // Update the autoload badge and swap button |
| 64 | $row.find( '.alm-badge' ) |
| 65 | .removeClass( 'alm-badge-enabled' ) |
| 66 | .addClass( 'alm-badge-disabled' ) |
| 67 | .text( 'no' ); |
| 68 | |
| 69 | var $newBtn = $( '<button class="button button-primary alm-enable-btn"></button>' ) |
| 70 | .attr( 'data-option', optionName ) |
| 71 | .attr( 'data-action', 'enable' ) |
| 72 | .text( 'Re-enable Autoload' ); |
| 73 | |
| 74 | $row.find( '.alm-action' ).html( $newBtn ); |
| 75 | $row.addClass( 'alm-row-disabled' ); |
| 76 | |
| 77 | var $clonedRow = $row.clone( true ); |
| 78 | |
| 79 | // Remove from top table |
| 80 | $row.fadeOut( 300, function () { $( this ).remove(); } ); |
| 81 | |
| 82 | // Build or refresh disabled table |
| 83 | if ( $disabledTable.length ) { |
| 84 | $noDisabled.hide(); |
| 85 | var newIndex = $disabledTable.find( 'tbody tr' ).length + 1; |
| 86 | $clonedRow.find( '.alm-index' ).text( newIndex ); |
| 87 | $disabledTable.find( 'tbody' ).append( $clonedRow.hide().fadeIn( 400 ) ); |
| 88 | updateDisabledCount(); |
| 89 | } else { |
| 90 | // Table doesn't exist yet — build it |
| 91 | $noDisabled.hide(); |
| 92 | var $table = $( |
| 93 | '<table class="widefat alm-table alm-table-disabled" id="alm-disabled-table">' + |
| 94 | '<thead><tr>' + |
| 95 | '<th>#</th><th>Option Name</th><th>Linked Plugin</th>' + |
| 96 | '<th>Size</th><th>Autoload</th><th>Action</th>' + |
| 97 | '</tr></thead><tbody></tbody></table>' |
| 98 | ); |
| 99 | $clonedRow.find( '.alm-index' ).text( 1 ); |
| 100 | $table.find( 'tbody' ).append( $clonedRow ); |
| 101 | $( '#alm-disabled-wrapper' ).html( $table ); |
| 102 | updateDisabledCount(); |
| 103 | } |
| 104 | |
| 105 | // Re-number top table |
| 106 | reindex( '#alm-active-table' ); |
| 107 | } |
| 108 | |
| 109 | /* ---- Remove row from Section 2 and move back to Section 1 ---- */ |
| 110 | function removeFromDisabled( $row ) { |
| 111 | var $activeTable = $( '#alm-active-table' ); |
| 112 | |
| 113 | $row.find( '.alm-badge' ) |
| 114 | .removeClass( 'alm-badge-disabled' ) |
| 115 | .addClass( 'alm-badge-enabled' ) |
| 116 | .text( 'yes' ); |
| 117 | |
| 118 | var optionName = $row.data( 'option' ); |
| 119 | var $newBtn = $( '<button class="button button-secondary alm-disable-btn"></button>' ) |
| 120 | .attr( 'data-option', optionName ) |
| 121 | .attr( 'data-action', 'disable' ) |
| 122 | .text( 'Disable Autoload' ); |
| 123 | |
| 124 | $row.find( '.alm-action' ).html( $newBtn ); |
| 125 | $row.removeClass( 'alm-row-disabled' ); |
| 126 | |
| 127 | var $clonedRow = $row.clone( true ); |
| 128 | |
| 129 | $row.fadeOut( 300, function () { |
| 130 | $( this ).remove(); |
| 131 | |
| 132 | // Check if disabled table is now empty |
| 133 | if ( $( '#alm-disabled-table tbody tr' ).length === 0 ) { |
| 134 | $( '#alm-disabled-table' ).remove(); |
| 135 | $( '#alm-no-disabled' ).show(); |
| 136 | $( '#alm-disabled-count' ).text( '0' ); |
| 137 | } else { |
| 138 | reindex( '#alm-disabled-table' ); |
| 139 | updateDisabledCount(); |
| 140 | } |
| 141 | } ); |
| 142 | |
| 143 | // Append back to active table (last position) |
| 144 | if ( $activeTable.length ) { |
| 145 | var newIndex = $activeTable.find( 'tbody tr:visible' ).length + 1; |
| 146 | $clonedRow.find( '.alm-index' ).text( newIndex ); |
| 147 | $activeTable.find( 'tbody' ).append( $clonedRow.hide().fadeIn( 400 ) ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* ---- Re-number visible rows ---- */ |
| 152 | function reindex( tableSelector ) { |
| 153 | $( tableSelector + ' tbody tr' ).each( function ( i ) { |
| 154 | $( this ).find( '.alm-index' ).text( i + 1 ); |
| 155 | } ); |
| 156 | } |
| 157 | |
| 158 | /* ---- Tab switching ---- */ |
| 159 | $( '.alm-tab-btn' ).on( 'click', function () { |
| 160 | var target = $( this ).data( 'tab' ); |
| 161 | $( '.alm-tab-btn' ).removeClass( 'alm-tab-active' ); |
| 162 | $( this ).addClass( 'alm-tab-active' ); |
| 163 | $( '.alm-tab-panel' ).addClass( 'alm-tab-hidden' ); |
| 164 | $( '#' + target ).removeClass( 'alm-tab-hidden' ); |
| 165 | } ); |
| 166 | |
| 167 | /* ---- Update disabled tab count badge ---- */ |
| 168 | function updateDisabledCount() { |
| 169 | var count = $( '#alm-disabled-table tbody tr' ).length; |
| 170 | $( '#alm-disabled-count' ).text( count ); |
| 171 | } |
| 172 | |
| 173 | /* ---- Event delegation (handles dynamically added rows) ---- */ |
| 174 | $( document ).on( 'click', '.alm-disable-btn', function () { |
| 175 | handleToggle( $( this ), 'disable' ); |
| 176 | } ); |
| 177 | |
| 178 | $( document ).on( 'click', '.alm-enable-btn', function () { |
| 179 | handleToggle( $( this ), 'enable' ); |
| 180 | } ); |
| 181 | |
| 182 | } )( jQuery ); |
| 183 |