| @@ -6,111 +6,70 @@ | ||
| 6 | 6 | * @author Tobias Bäthge |
| 7 | 7 | * @since 1.0.0 |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -/* global prompt, confirm, tablepress_common, tablepress_list, tb_show, ajaxurl */ | |
| 10 | +/** | |
| 11 | + * WordPress dependencies. | |
| 12 | + */ | |
| 13 | +import { _n } from '@wordpress/i18n'; | |
| 11 | 14 | |
| 12 | -jQuery( document ).ready( function( $ ) { | |
| 15 | +/** | |
| 16 | + * Internal dependencies. | |
| 17 | + */ | |
| 18 | +import { $ } from './common/functions'; | |
| 19 | +import { initializeReactComponent } from './common/react-loader'; | |
| 20 | +import Screen from './list/screen'; | |
| 13 | 21 | |
| 14 | - 'use strict'; | |
| 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 | + } | |
| 15 | 35 | |
| 16 | - /** | |
| 17 | - * Show a popup box with the table's Shortcode | |
| 18 | - * | |
| 19 | - * @since 1.0.0 | |
| 20 | - */ | |
| 21 | - $( '.tablepress-all-tables' ).on( 'click', '.shortcode a', function( /* event */ ) { | |
| 22 | - prompt( tablepress_list.shortcode_popup, $(this).attr( 'title' ) ); | |
| 23 | - return false; | |
| 24 | - } ); | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Load a Thickbox with a table preview | |
| 28 | - * | |
| 29 | - * @since 1.0.0 | |
| 30 | - */ | |
| 31 | - $( '.tablepress-all-tables' ).on( 'click', '.table-preview a', function( /* event */ ) { | |
| 32 | - var width = $( window ).width() - 120, | |
| 33 | - height = $( window ).height() - 120, | |
| 34 | - $this = $(this); | |
| 35 | - if ( $( '#wpadminbar' ).length ) { | |
| 36 | - height -= parseInt( $( '#wpadminbar' ).css( 'height' ), 10 ); | |
| 37 | - } | |
| 38 | - tb_show( $this.text(), $this.attr( 'href' ) + 'TB_iframe=true&height=' + height + '&width=' + width, false ); | |
| 39 | - return false; | |
| 40 | - } ); | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Process links with a class "ajax-link" with AJAX | |
| 44 | - * | |
| 45 | - * @since 1.0.0 | |
| 46 | - */ | |
| 47 | - $( '#tablepress-page' ).on( 'click', '.ajax-link', function( /* event */ ) { | |
| 48 | - var $link = $(this), | |
| 49 | - action = $link.data( 'action' ), | |
| 50 | - item = $link.data( 'item' ), | |
| 51 | - target = $link.data( 'target' ); | |
| 52 | - $.get( | |
| 53 | - ajaxurl, | |
| 54 | - this.href.split('?')['1'], /* query string of the link */ | |
| 55 | - function( result ) { | |
| 56 | - if ( '1' !== result ) { | |
| 57 | - return; | |
| 58 | - } | |
| 59 | - | |
| 60 | - switch ( action ) { | |
| 61 | - case 'hide_message': | |
| 62 | - /* Donation message links show new message */ | |
| 63 | - if ( 'donation_nag' === item && '' !== target ) { | |
| 64 | - $link.closest( 'div' ).after( '<div class="donation-message-after-click-message notice notice-success"><p><strong>' + tablepress_list['donation-message-' + target] + '</strong></p></div>' ); | |
| 65 | - $( '.donation-message-after-click-message' ).delay( 10000 ).fadeOut( 2000, function() { $(this).remove(); } ); | |
| 66 | - } | |
| 67 | - | |
| 68 | - /* Remove original message */ | |
| 69 | - $link.closest( 'div' ).remove(); | |
| 70 | - break; | |
| 71 | - } | |
| 36 | + if ( 'hide_message' === event.target.dataset.action ) { | |
| 37 | + // Remove original message. | |
| 38 | + event.target.closest( '.notice' ).remove(); | |
| 72 | 39 | } |
| 73 | - ); | |
| 74 | - return false; | |
| 40 | + } ); | |
| 41 | + event.preventDefault(); | |
| 75 | 42 | } ); |
| 43 | +} ); | |
| 76 | 44 | |
| 77 | - /** | |
| 78 | - * Submit Bulk Actions only if an action was selected an a table's checkbox was checked | |
| 79 | - * | |
| 80 | - * @since 1.0.0 | |
| 81 | - */ | |
| 82 | - $( '#doaction, #doaction2' ).on( 'click', function() { | |
| 83 | - var bulk_action, | |
| 84 | - confirm_message, | |
| 85 | - num_selected = $( '.tablepress-all-tables' ).find( 'tbody' ).find( 'input:checked' ).length; | |
| 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 | + // Set the ID of the bulk action dropdown in the global variable to the correct `name` attribute of the dropdown, as the default of `action` can not be used. | |
| 57 | + window.bulkActionObserverIds.bulk_action = 'bulk-action-selector-top'; | |
| 86 | 58 | |
| 87 | - // determine location of clicked bulk action controls | |
| 88 | - if ( 'doaction' === this.id ) { | |
| 89 | - bulk_action = 'top'; | |
| 90 | - } else { | |
| 91 | - bulk_action = 'bottom'; | |
| 92 | - } | |
| 59 | + const action = $( '#bulk-action-selector-top' ).value; | |
| 60 | + const num_selected = $( '.tablepress-all-tables tbody input:checked' ).length; | |
| 93 | 61 | |
| 94 | - // check whether an action was selected, and whether tables were selected | |
| 95 | - if ( '-1' === $( '#bulk-action-' + bulk_action ).val() ) { | |
| 96 | - return false; | |
| 97 | - } | |
| 98 | - if ( 0 === num_selected ) { | |
| 99 | - return false; | |
| 100 | - } | |
| 101 | - | |
| 102 | - // Show AYS prompt for deletion | |
| 103 | - if ( 'delete' === $( '#bulk-action-' + bulk_action ).val() ) { | |
| 104 | - if ( 1 === num_selected ) { | |
| 105 | - confirm_message = tablepress_common.ays_delete_single_table; | |
| 106 | - } else { | |
| 107 | - confirm_message = tablepress_common.ays_delete_multiple_tables; | |
| 62 | + // Show AYS prompt when deleting tables. | |
| 63 | + if ( 'delete' === action ) { | |
| 64 | + if ( ! confirm( _n( 'Do you really want to delete this table?', 'Do you really want to delete these tables?', num_selected, 'tablepress' ) ) ) { | |
| 65 | + event.preventDefault(); | |
| 66 | + return; | |
| 108 | 67 | } |
| 109 | - | |
| 110 | - if ( ! confirm( confirm_message ) ) { | |
| 111 | - return false; | |
| 112 | - } | |
| 113 | 68 | } |
| 114 | 69 | } ); |
| 70 | +} | |
| 115 | 71 | |
| 116 | -} ); | |
| 72 | +initializeReactComponent( | |
| 73 | + 'tablepress-list-screen', | |
| 74 | + <Screen />, | |
| 75 | +); | |