59 lines
2.0 KiB
JavaScript
59 lines
2.0 KiB
JavaScript
/* StreamGoals admin: copy buttons and confirm prompts. No dependencies. */
|
|||
|
|
( function () {
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
function flash( btn, text ) {
|
||
|
|
var old = btn.getAttribute( 'data-label' ) || btn.textContent;
|
||
|
|
btn.setAttribute( 'data-label', old );
|
||
|
|
btn.textContent = text;
|
||
|
|
window.setTimeout( function () { btn.textContent = old; }, 1500 );
|
||
|
|
}
|
||
|
|
|
||
|
|
function copyText( text, btn, input ) {
|
||
|
|
function fallback() {
|
||
|
|
var el = input;
|
||
|
|
var temp = null;
|
||
|
|
if ( ! el ) {
|
||
|
|
temp = el = document.createElement( 'textarea' );
|
||
|
|
el.value = text;
|
||
|
|
el.style.position = 'fixed';
|
||
|
|
el.style.opacity = '0';
|
||
|
|
document.body.appendChild( el );
|
||
|
|
}
|
||
|
|
el.select();
|
||
|
|
var ok = false;
|
||
|
|
try { ok = document.execCommand( 'copy' ); } catch ( e ) { ok = false; }
|
||
|
|
if ( temp ) { document.body.removeChild( temp ); }
|
||
|
|
flash( btn, ok ? 'Copied!' : 'Press Ctrl+C' );
|
||
|
|
}
|
||
|
|
if ( navigator.clipboard && window.isSecureContext ) {
|
||
|
|
navigator.clipboard.writeText( text ).then( function () { flash( btn, 'Copied!' ); }, fallback );
|
||
|
|
} else {
|
||
|
|
fallback();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
document.addEventListener( 'click', function ( e ) {
|
||
|
|
var btn = e.target.closest ? e.target.closest( '[data-sg-copy],[data-sg-copy-text]' ) : null;
|
||
|
|
if ( ! btn ) { return; }
|
||
|
|
e.preventDefault();
|
||
|
|
if ( btn.hasAttribute( 'data-sg-copy-text' ) ) {
|
||
|
|
copyText( btn.getAttribute( 'data-sg-copy-text' ), btn, null );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
var input = btn.parentNode.querySelector( 'input' );
|
||
|
|
if ( input ) { copyText( input.value, btn, input ); }
|
||
|
|
} );
|
||
|
|
|
||
|
|
document.addEventListener( 'focusin', function ( e ) {
|
||
|
|
if ( e.target.classList && e.target.classList.contains( 'sg-url' ) ) { e.target.select(); }
|
||
|
|
} );
|
||
|
|
|
||
|
|
document.addEventListener( 'submit', function ( e ) {
|
||
|
|
var form = e.target;
|
||
|
|
if ( ! form.classList || ! form.classList.contains( 'sg-confirm' ) ) { return; }
|
||
|
|
var src = e.submitter || form.querySelector( '[data-confirm]' );
|
||
|
|
var msg = src && src.getAttribute( 'data-confirm' );
|
||
|
|
if ( msg && ! window.confirm( msg ) ) { e.preventDefault(); }
|
||
|
|
} );
|
||
|
|
}() );
|