| 1 |
/** |
| 2 |
* ===================================================================================================================== |
| 3 |
* JavaScript Util Functions ../includes/__js/utils/wpbc_utils.js |
| 4 |
* ===================================================================================================================== |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* Trim strings and array joined with (,) |
| 9 |
* |
| 10 |
* @param string_to_trim string / array |
| 11 |
* @returns string |
| 12 |
*/ |
| 13 |
function wpbc_trim(string_to_trim) { |
| 14 |
|
| 15 |
if ( Array.isArray( string_to_trim ) ) { |
| 16 |
string_to_trim = string_to_trim.join( ',' ); |
| 17 |
} |
| 18 |
|
| 19 |
if ( 'string' == typeof (string_to_trim) ) { |
| 20 |
string_to_trim = string_to_trim.trim(); |
| 21 |
} |
| 22 |
|
| 23 |
return string_to_trim; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Check if element in array |
| 28 |
* |
| 29 |
* @param array_here array |
| 30 |
* @param p_val element to check |
| 31 |
* @returns {boolean} |
| 32 |
*/ |
| 33 |
function wpbc_in_array(array_here, p_val) { |
| 34 |
for ( var i = 0, l = array_here.length; i < l; i++ ) { |
| 35 |
if ( array_here[i] == p_val ) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
} |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Prevent opening blank windows on WordPress playground for pseudo links like this: <a href="javascript:void(0)"> or # to stay in the same tab. |
| 44 |
*/ |
| 45 |
(function () { |
| 46 |
'use strict'; |
| 47 |
|
| 48 |
function is_playground_origin() { |
| 49 |
return location.origin === 'https://playground.wordpress.net'; |
| 50 |
} |
| 51 |
|
| 52 |
function is_pseudo_link(a) { |
| 53 |
if ( !a || !a.getAttribute ) return true; |
| 54 |
var href = (a.getAttribute( 'href' ) || '').trim().toLowerCase(); |
| 55 |
return ( |
| 56 |
!href || |
| 57 |
href === '#' || |
| 58 |
href.indexOf( '#' ) === 0 || |
| 59 |
href.indexOf( 'javascript:' ) === 0 || |
| 60 |
href.indexOf( 'mailto:' ) === 0 || |
| 61 |
href.indexOf( 'tel:' ) === 0 |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
function fix_target(a) { |
| 66 |
if ( ! a ) return; |
| 67 |
if ( is_pseudo_link( a ) || a.hasAttribute( 'data-wp-no-blank' ) ) { |
| 68 |
a.target = '_self'; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
function init_fix() { |
| 73 |
// Optional: clean up current DOM (harmless—affects only pseudo/datamarked links). |
| 74 |
var nodes = document.querySelectorAll( 'a[href]' ); |
| 75 |
for ( var i = 0; i < nodes.length; i++ ) fix_target( nodes[i] ); |
| 76 |
|
| 77 |
// Late bubble-phase listeners (run after Playground's handlers) |
| 78 |
document.addEventListener( 'click', function (e) { |
| 79 |
var a = e.target && e.target.closest ? e.target.closest( 'a[href]' ) : null; |
| 80 |
if ( a ) fix_target( a ); |
| 81 |
}, false ); |
| 82 |
|
| 83 |
document.addEventListener( 'focusin', function (e) { |
| 84 |
var a = e.target && e.target.closest ? e.target.closest( 'a[href]' ) : null; |
| 85 |
if ( a ) fix_target( a ); |
| 86 |
} ); |
| 87 |
} |
| 88 |
|
| 89 |
function schedule_init() { |
| 90 |
if ( !is_playground_origin() ) return; |
| 91 |
setTimeout( init_fix, 1000 ); // ensure we attach after Playground's script. |
| 92 |
} |
| 93 |
|
| 94 |
if ( document.readyState === 'loading' ) { |
| 95 |
document.addEventListener( 'DOMContentLoaded', schedule_init ); |
| 96 |
} else { |
| 97 |
schedule_init(); |
| 98 |
} |
| 99 |
})(); |