From c498a516e9c942e0dbc50d0c665698ffc7c61c1b Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 1 Mar 2021 10:16:18 +0000 Subject: [PATCH 1/6] Target existing window if available rather than continue opening new --- .../inline-help/class-inline-help.php | 12 ++++++++++ .../inline-help/inline-help-template.php | 2 +- .../masterbar/inline-help/inline-help.js | 22 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/class-inline-help.php b/projects/plugins/jetpack/modules/masterbar/inline-help/class-inline-help.php index b2c227b88f5a0..fb2705ae22126 100644 --- a/projects/plugins/jetpack/modules/masterbar/inline-help/class-inline-help.php +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/class-inline-help.php @@ -40,6 +40,8 @@ public function register_actions() { add_action( 'admin_footer', array( $this, 'add_fab_icon' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'add_fab_styles' ) ); + + add_action( 'admin_enqueue_scripts', array( $this, 'add_fab_scripts' ) ); } /** @@ -109,4 +111,14 @@ public function add_fab_icon() { public function add_fab_styles() { wp_enqueue_style( 'a8c-faux-inline-help', plugins_url( 'inline-help.css', __FILE__ ), array(), JETPACK__VERSION ); } + + public function add_fab_scripts() { + wp_enqueue_script( + 'a8c-faux-inline-help', + plugins_url( 'inline-help.js', __FILE__ ), + array(), + JETPACK__VERSION, + true + ); + } } diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help-template.php b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help-template.php index 6d9aba31035a5..032b03538eb56 100644 --- a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help-template.php +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help-template.php @@ -9,7 +9,7 @@ ?>
- +
diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js new file mode 100644 index 0000000000000..d2fd3003f6fc2 --- /dev/null +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js @@ -0,0 +1,22 @@ +( function () { + var windowObjectReference = null; + function init() { + document.addEventListener( 'click', function ( e ) { + if ( e.target.dataset.fauxInlineHelp !== undefined ) { + e.preventDefault(); + + if ( windowObjectReference == null || windowObjectReference.closed ) { + windowObjectReference = window.open( e.target.href, e.target.target ); + } else { + windowObjectReference.focus(); + } + } + } ); + } + + if ( document.readyState === 'loading' ) { + document.addEventListener( 'DOMContentLoaded', init ); + } else { + init(); + } +} )(); From 0bb6e2e5151530e1748b534cf720b4af474b3ad5 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 1 Mar 2021 11:42:02 +0000 Subject: [PATCH 2/6] Fix linting --- .../modules/masterbar/inline-help/class-inline-help.php | 5 +++++ .../jetpack/modules/masterbar/inline-help/inline-help.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/class-inline-help.php b/projects/plugins/jetpack/modules/masterbar/inline-help/class-inline-help.php index fb2705ae22126..e793c18b6288d 100644 --- a/projects/plugins/jetpack/modules/masterbar/inline-help/class-inline-help.php +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/class-inline-help.php @@ -112,6 +112,11 @@ public function add_fab_styles() { wp_enqueue_style( 'a8c-faux-inline-help', plugins_url( 'inline-help.css', __FILE__ ), array(), JETPACK__VERSION ); } + /** + * Enqueues FAB JS scripts. + * + * @return void + */ public function add_fab_scripts() { wp_enqueue_script( 'a8c-faux-inline-help', diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js index d2fd3003f6fc2..13e508d95a236 100644 --- a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js @@ -5,7 +5,7 @@ if ( e.target.dataset.fauxInlineHelp !== undefined ) { e.preventDefault(); - if ( windowObjectReference == null || windowObjectReference.closed ) { + if ( windowObjectReference === null || windowObjectReference.closed ) { windowObjectReference = window.open( e.target.href, e.target.target ); } else { windowObjectReference.focus(); From f1fc30ef909ff8c6fb3135f0fd54a9c78b7ebb9f Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 1 Mar 2021 11:46:16 +0000 Subject: [PATCH 3/6] Avoid event delegation - no longer needed in this context --- .../modules/masterbar/inline-help/inline-help.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js index 13e508d95a236..83cfd78505881 100644 --- a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js @@ -1,15 +1,13 @@ ( function () { var windowObjectReference = null; function init() { - document.addEventListener( 'click', function ( e ) { - if ( e.target.dataset.fauxInlineHelp !== undefined ) { - e.preventDefault(); + document.querySelector( '[data-faux-inline-help]' ).addEventListener( 'click', function ( e ) { + e.preventDefault(); - if ( windowObjectReference === null || windowObjectReference.closed ) { - windowObjectReference = window.open( e.target.href, e.target.target ); - } else { - windowObjectReference.focus(); - } + if ( windowObjectReference === null || windowObjectReference.closed ) { + windowObjectReference = window.open( e.target.href, e.target.target ); + } else { + windowObjectReference.focus(); } } ); } From 4683263fd5b247781b39e11b559251d9209b53d2 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 1 Mar 2021 11:55:30 +0000 Subject: [PATCH 4/6] Guard possible null on querySelector --- .../masterbar/inline-help/inline-help.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js index 83cfd78505881..cbfb28c03be67 100644 --- a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js @@ -1,15 +1,17 @@ ( function () { var windowObjectReference = null; function init() { - document.querySelector( '[data-faux-inline-help]' ).addEventListener( 'click', function ( e ) { - e.preventDefault(); + var fauxInlineHelpButton = document.querySelector( '[data-faux-inline-help]' ); + fauxInlineHelpButton && + fauxInlineHelpButton.addEventListener( 'click', function ( e ) { + e.preventDefault(); - if ( windowObjectReference === null || windowObjectReference.closed ) { - windowObjectReference = window.open( e.target.href, e.target.target ); - } else { - windowObjectReference.focus(); - } - } ); + if ( windowObjectReference === null || windowObjectReference.closed ) { + windowObjectReference = window.open( e.target.href, e.target.target ); + } else { + windowObjectReference.focus(); + } + } ); } if ( document.readyState === 'loading' ) { From e700271b1cef8902c73ddea7a0cf5c5f9e5b0c34 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 1 Mar 2021 12:35:59 +0000 Subject: [PATCH 5/6] Ensure always reload URL in re-focused tab --- .../jetpack/modules/masterbar/inline-help/inline-help.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js index cbfb28c03be67..203d1e0495ddd 100644 --- a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js @@ -1,16 +1,10 @@ ( function () { - var windowObjectReference = null; function init() { var fauxInlineHelpButton = document.querySelector( '[data-faux-inline-help]' ); fauxInlineHelpButton && fauxInlineHelpButton.addEventListener( 'click', function ( e ) { e.preventDefault(); - - if ( windowObjectReference === null || windowObjectReference.closed ) { - windowObjectReference = window.open( e.target.href, e.target.target ); - } else { - windowObjectReference.focus(); - } + window.open( e.target.href, e.target.target ); } ); } From 14b7238cf60a629ee682f49647ab42e2a278e290 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 1 Mar 2021 14:32:55 +0000 Subject: [PATCH 6/6] Update projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js Co-authored-by: Tom Cafferkey --- .../jetpack/modules/masterbar/inline-help/inline-help.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js index 203d1e0495ddd..7aac53b2acc85 100644 --- a/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js +++ b/projects/plugins/jetpack/modules/masterbar/inline-help/inline-help.js @@ -1,7 +1,12 @@ ( function () { function init() { var fauxInlineHelpButton = document.querySelector( '[data-faux-inline-help]' ); - fauxInlineHelpButton && + if ( fauxInlineHelpButton ) { + fauxInlineHelpButton.addEventListener( 'click', function ( e ) { + e.preventDefault(); + window.open( e.target.href, e.target.target ); + } ); + } fauxInlineHelpButton.addEventListener( 'click', function ( e ) { e.preventDefault(); window.open( e.target.href, e.target.target );