| 1 |
( function() { |
| 2 |
// The "Switch to OpenStation" node only renders in classic admin — |
| 3 |
// PHP skips it once the user is viewing the shell, where the |
| 4 |
// "Exit OpenStation" dock tile is the way back out. So this handler |
| 5 |
// only ever enables. |
| 6 |
var toggle = document.getElementById( 'wp-admin-bar-os-toggle' ); |
| 7 |
if ( ! toggle ) { |
| 8 |
return; |
| 9 |
} |
| 10 |
var cfg = window.openStationAdminBar || {}; |
| 11 |
toggle.addEventListener( 'click', function( e ) { |
| 12 |
e.preventDefault(); |
| 13 |
// Fallback target if the server response is missing a `redirect` |
| 14 |
// field (shouldn't happen, but keep the click functional either |
| 15 |
// way): the portal URL, so the shell takes over. |
| 16 |
var fallback = cfg.portalUrl; |
| 17 |
// The toggle lives in an admin bar that may be rendered either in |
| 18 |
// the top window (classic) or — today it's suppressed in iframes, |
| 19 |
// but a plugin could surface it — inside a chromeless iframe. In |
| 20 |
// either case we want the ENTIRE browser tab to navigate, so we |
| 21 |
// hit `window.top` and fall back to `window` if cross-origin |
| 22 |
// security blocks access. |
| 23 |
function navigate( url ) { |
| 24 |
try { |
| 25 |
window.top.location.href = url; |
| 26 |
} catch ( err ) { |
| 27 |
window.location.href = url; |
| 28 |
} |
| 29 |
} |
| 30 |
var body = new URLSearchParams(); |
| 31 |
body.set( 'action', 'save-openstation' ); |
| 32 |
body.set( 'nonce', cfg.nonce ); |
| 33 |
body.set( 'enabled', '1' ); |
| 34 |
// Tells the handler which admin this click came from; see the |
| 35 |
// note in `includes/ajax.php`. |
| 36 |
if ( cfg.network ) { |
| 37 |
body.set( 'network', '1' ); |
| 38 |
} |
| 39 |
var xhr = new XMLHttpRequest(); |
| 40 |
xhr.open( 'POST', cfg.ajaxUrl, true ); |
| 41 |
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); |
| 42 |
xhr.onload = function() { |
| 43 |
if ( xhr.status !== 200 ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
var target = fallback; |
| 47 |
try { |
| 48 |
var resp = JSON.parse( xhr.responseText ); |
| 49 |
if ( resp && resp.success && resp.data && resp.data.redirect ) { |
| 50 |
target = resp.data.redirect; |
| 51 |
} |
| 52 |
} catch ( parseErr ) {} |
| 53 |
navigate( target ); |
| 54 |
}; |
| 55 |
xhr.send( body.toString() ); |
| 56 |
} ); |
| 57 |
} )(); |
| 58 |
|