MediaWiki:Gadget-fa-bid-calculator.js: Difference between revisions
FA bid calculator |
Omit blank names, Points wording, copy button |
||
| Line 24: | Line 24: | ||
function pts( n ) { | function pts( n ) { | ||
return Math.round( n * 100 ) / 100; | return Math.round( n * 100 ) / 100; | ||
} | |||
/** A monospace post block with a copy button. */ | |||
function postBlock( text ) { | |||
var wrap = document.createElement( 'div' ); | |||
wrap.className = 'tc-calc-postwrap'; | |||
var pre = document.createElement( 'div' ); | |||
pre.className = 'tc-calc-post'; | |||
pre.textContent = text; | |||
wrap.appendChild( pre ); | |||
var btn = document.createElement( 'button' ); | |||
btn.className = 'tc-calc-copy'; | |||
btn.type = 'button'; | |||
btn.textContent = 'Copy'; | |||
btn.addEventListener( 'click', function () { | |||
function done( ok ) { | |||
btn.textContent = ok ? 'Copied' : 'Press \u2318C / Ctrl+C'; | |||
setTimeout( function () { btn.textContent = 'Copy'; }, 2000 ); | |||
} | |||
if ( navigator.clipboard && navigator.clipboard.writeText ) { | |||
navigator.clipboard.writeText( text ).then( function () { done( true ); }, | |||
function () { selectFallback( pre ); done( false ); } ); | |||
} else { | |||
selectFallback( pre ); | |||
done( false ); | |||
} | |||
} ); | |||
wrap.appendChild( btn ); | |||
return wrap; | |||
} | |||
/** Clipboard API unavailable or refused: select the text so the reader can copy it. */ | |||
function selectFallback( el ) { | |||
var range = document.createRange(); | |||
range.selectNodeContents( el ); | |||
var sel = window.getSelection(); | |||
sel.removeAllRanges(); | |||
sel.addRange( range ); | |||
} | } | ||
| Line 176: | Line 217: | ||
// --- ready-to-paste post --- | // --- ready-to-paste post --- | ||
// "Suns offer Caldwell Jones 2 Years @ $500,000 = 3.5 Points". | |||
// Whatever of team/player is missing simply drops out. | |||
var tm = team.value.trim(), | |||
pl = name.value.trim(), | |||
y + ' | prefix = tm && pl ? tm + ' offer ' + pl : ( pl || ( tm ? tm + ' offer' : '' ) ), | ||
out.appendChild( | offer = y + ( y === 1 ? ' Year' : ' Years' ) + ' @ ' + money( s ) + | ||
' = ' + pts( total ) + ' Points'; | |||
out.appendChild( postBlock( prefix ? prefix + ' ' + offer : offer ) ); | |||
var note = document.createElement( 'div' ); | var note = document.createElement( 'div' ); | ||