| 1 |
/** |
| 2 |
* JavaScript code for the "List Tables" screen |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Views JavaScript |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* WordPress dependencies. |
| 12 |
*/ |
| 13 |
import { _n } from '@wordpress/i18n'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Internal dependencies. |
| 17 |
*/ |
| 18 |
import { $ } from './common/functions'; |
| 19 |
import { initializeReactComponent } from './common/react-loader'; |
| 20 |
import Screen from './list/screen'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Process links with an "ajax-link" class with AJAX. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
*/ |
| 27 |
document.querySelectorAll( '#tablepress-page .ajax-link' ).forEach( ( link ) => { |
| 28 |
link.addEventListener( 'click', ( event ) => { |
| 29 |
fetch( `${ ajaxurl }?${ event.target.href.split( '?' )[1] }` ) // Append original link's query string to AJAX endpoint. |
| 30 |
.then( ( response ) => response.text() ) |
| 31 |
.then( ( result ) => { |
| 32 |
if ( '1' !== result ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
if ( 'hide_message' === event.target.dataset.action ) { |
| 37 |
// Remove original message. |
| 38 |
event.target.closest( '.notice' ).remove(); |
| 39 |
} |
| 40 |
} ); |
| 41 |
event.preventDefault(); |
| 42 |
} ); |
| 43 |
} ); |
| 44 |
|
| 45 |
/** |
| 46 |
* Submit Bulk Actions only if an action was selected and at least one table was selected. |
| 47 |
* |
| 48 |
* Only the top button and the top bulk selector dropdown have to be evaluated, as WP mirrors them. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
*/ |
| 52 |
const bulk_action_dropdown = $( '#doaction' ); |
| 53 |
// The bulk action dropdown is only in the DOM if at least one table is shown in the list, thus an existence check is needed. |
| 54 |
if ( bulk_action_dropdown ) { |
| 55 |
bulk_action_dropdown.addEventListener( 'click', ( event ) => { |
| 56 |
const action = $( '#bulk-action-selector-top' ).value; |
| 57 |
const num_selected = $( '.tablepress-all-tables tbody input:checked' ).length; |
| 58 |
|
| 59 |
// Do nothing if no action or no tables were selected. |
| 60 |
if ( '-1' === action || 0 === num_selected ) { |
| 61 |
event.preventDefault(); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Show AYS prompt when deleting tables. |
| 66 |
if ( 'delete' === action ) { |
| 67 |
if ( ! confirm( _n( 'Do you really want to delete this table?', 'Do you really want to delete these tables?', num_selected, 'tablepress' ) ) ) { |
| 68 |
event.preventDefault(); |
| 69 |
return; |
| 70 |
} |
| 71 |
} |
| 72 |
} ); |
| 73 |
} |
| 74 |
|
| 75 |
initializeReactComponent( |
| 76 |
'tablepress-list-screen', |
| 77 |
<Screen />, |
| 78 |
); |
| 79 |
|