| 1 |
/** |
| 2 |
* JavaScript code for the "Options" screen, without the CodeMirror handling. |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Views JavaScript |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* globals confirm */ |
| 11 |
|
| 12 |
/** |
| 13 |
* WordPress dependencies. |
| 14 |
*/ |
| 15 |
import { __, _x, sprintf } from '@wordpress/i18n'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Internal dependencies. |
| 19 |
*/ |
| 20 |
import { $ } from './_common-functions'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Enable/disable the regular textarea according to state of "Load Custom CSS" checkbox. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
*/ |
| 27 |
const $cb_use_custom_css = $( '#option-use-custom-css' ); |
| 28 |
if ( $cb_use_custom_css ) { // The checkbox field only exists for admins! |
| 29 |
$cb_use_custom_css.addEventListener( 'change', function () { |
| 30 |
$( '#option-custom-css' ).disabled = ! this.checked; |
| 31 |
} ); |
| 32 |
$cb_use_custom_css.dispatchEvent( new Event( 'change' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* On form submit: Enable disabled fields, so that they are sent in the HTTP POST request. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
document.querySelector( '#tablepress-page form' ).addEventListener( 'submit', function () { |
| 41 |
this.querySelectorAll( ':scope input, :scope select, :scope textarea' ).forEach( ( field ) => ( field.disabled = false ) ); |
| 42 |
} ); |
| 43 |
|
| 44 |
// Add keyboard shortcut as title attribute to the "Save Changes" button, with correct modifier key for Mac/non-Mac. |
| 45 |
const modifier_key = ( window?.navigator?.platform?.includes( 'Mac' ) ) ? |
| 46 |
_x( '⌘', 'keyboard shortcut modifier key on a Mac keyboard', 'tablepress' ) : |
| 47 |
_x( 'Ctrl+', 'keyboard shortcut modifier key on a non-Mac keyboard', 'tablepress' ); |
| 48 |
const $save_changes_button = $( '#tablepress-options-save-changes' ); |
| 49 |
const shortcut = sprintf( $save_changes_button.dataset.shortcut, modifier_key ); // eslint-disable-line @wordpress/valid-sprintf |
| 50 |
$save_changes_button.title = sprintf( __( 'Keyboard Shortcut: %s', 'tablepress' ), shortcut ); |
| 51 |
|
| 52 |
/** |
| 53 |
* Registers keyboard events and triggers corresponding actions by emulating button clicks. |
| 54 |
* |
| 55 |
* @since 2.1.1 |
| 56 |
* |
| 57 |
* @param {Event} event Keyboard event. |
| 58 |
*/ |
| 59 |
const keyboard_shortcuts = function ( event ) { |
| 60 |
let action = ''; |
| 61 |
|
| 62 |
if ( event.ctrlKey || event.metaKey ) { |
| 63 |
if ( 83 === event.keyCode ) { |
| 64 |
// Save Changes: Ctrl/Cmd + S. |
| 65 |
action = 'save-changes'; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if ( 'save-changes' === action ) { |
| 70 |
// Blur the focussed element to make sure that all change events were triggered. |
| 71 |
document.activeElement.blur(); // eslint-disable-line @wordpress/no-global-active-element |
| 72 |
|
| 73 |
// Emulate a click on the button corresponding to the action. |
| 74 |
$save_changes_button.click(); |
| 75 |
|
| 76 |
// Prevent the browser's native handling of the shortcut, i.e. showing the Save or Print dialogs. |
| 77 |
event.preventDefault(); |
| 78 |
} |
| 79 |
}; |
| 80 |
// Register keyboard shortcut handler. |
| 81 |
window.addEventListener( 'keydown', keyboard_shortcuts, true ); |
| 82 |
|
| 83 |
/** |
| 84 |
* Require double confirmation when wanting to uninstall TablePress. |
| 85 |
* |
| 86 |
* @since 1.0.0 |
| 87 |
*/ |
| 88 |
$( '#uninstall-tablepress' ).addEventListener( 'click', ( event ) => { |
| 89 |
if ( |
| 90 |
! confirm( __( 'Do you really want to uninstall TablePress and delete ALL data?', 'tablepress' ) ) || |
| 91 |
! confirm( __( 'Are you really sure?', 'tablepress' ) ) ) |
| 92 |
{ |
| 93 |
event.preventDefault(); |
| 94 |
} |
| 95 |
} ); |
| 96 |
|