| 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 |
/* globals confirm, prompt, tb_show, ajaxurl */ |
| 11 |
|
| 12 |
/** |
| 13 |
* WordPress dependencies. |
| 14 |
*/ |
| 15 |
import { __, _n } from '@wordpress/i18n'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Internal dependencies. |
| 19 |
*/ |
| 20 |
import { $ } from './common/functions'; |
| 21 |
|
| 22 |
document.querySelector( '.tablepress-all-tables' ).addEventListener( 'click', ( event ) => { |
| 23 |
if ( ! event.target ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Show a popup box with the table's Shortcode. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
*/ |
| 32 |
if ( event.target.matches( '.shortcode a' ) ) { |
| 33 |
prompt( __( 'To embed this table into a post or page, use this Shortcode:', 'tablepress' ), event.target.title ); |
| 34 |
event.preventDefault(); |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Load a Thickbox with a table preview. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
if ( event.target.matches( '.table-preview a' ) ) { |
| 44 |
const width = window.innerWidth - 120; |
| 45 |
const height = window.innerHeight - 120; |
| 46 |
tb_show( event.target.title, `${ event.target.href }#TB_iframe=true&height=${ height }&width=${ width }`, false ); |
| 47 |
event.preventDefault(); |
| 48 |
return; |
| 49 |
} |
| 50 |
} ); |
| 51 |
|
| 52 |
/** |
| 53 |
* Process links with an "ajax-link" class with AJAX. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
*/ |
| 57 |
$( '#tablepress-page' ).addEventListener( 'click', ( event ) => { |
| 58 |
if ( ! event.target ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if ( event.target.matches( '.ajax-link' ) ) { |
| 63 |
fetch( `${ ajaxurl }?${ event.target.href.split( '?' )[1] }` ) // Append original link's query string to AJAX endpoint. |
| 64 |
.then( ( response ) => response.text() ) |
| 65 |
.then( ( result ) => { |
| 66 |
if ( '1' !== result ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if ( 'hide_message' === event.target.dataset.action ) { |
| 71 |
// Remove original message. |
| 72 |
event.target.closest( 'div' ).remove(); |
| 73 |
} |
| 74 |
} ); |
| 75 |
|
| 76 |
event.preventDefault(); |
| 77 |
return; |
| 78 |
} |
| 79 |
} ); |
| 80 |
|
| 81 |
/** |
| 82 |
* Submit Bulk Actions only if an action was selected and at least one table was selected. |
| 83 |
* |
| 84 |
* Only the top button and the top bulk selector dropdown have to be evaluated, as WP mirrors them. |
| 85 |
* |
| 86 |
* @since 1.0.0 |
| 87 |
*/ |
| 88 |
const bulk_action_dropdown = $( '#doaction' ); |
| 89 |
// 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. |
| 90 |
if ( bulk_action_dropdown ) { |
| 91 |
bulk_action_dropdown.addEventListener( 'click', ( event ) => { |
| 92 |
const action = $( '#bulk-action-selector-top' ).value; |
| 93 |
const num_selected = $( '.tablepress-all-tables tbody input:checked' ).length; |
| 94 |
|
| 95 |
// Do nothing if no action or no tables were selected. |
| 96 |
if ( '-1' === action || 0 === num_selected ) { |
| 97 |
event.preventDefault(); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
// Show AYS prompt when deleting tables. |
| 102 |
if ( 'delete' === action ) { |
| 103 |
if ( ! confirm( _n( 'Do you really want to delete this table?', 'Do you really want to delete these tables?', num_selected, 'tablepress' ) ) ) { |
| 104 |
event.preventDefault(); |
| 105 |
return; |
| 106 |
} |
| 107 |
} |
| 108 |
} ); |
| 109 |
} |
| 110 |
|