MediaWiki:Gadget-resign-calculator.js: Difference between revisions
Re-sign calculator |
Omit blank names, Points wording, copy button |
||
| Line 32: | Line 32: | ||
nonPositive: capped <= 0 | nonPositive: capped <= 0 | ||
}; | }; | ||
} | |||
/** 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 168: | Line 209: | ||
first = parseInt( season.value, 10 ), | first = parseInt( season.value, 10 ), | ||
endYear = isNaN( first ) ? null : first + years - 1, | endYear = isNaN( first ) ? null : first + years - 1, | ||
who = name.value.trim() | who = name.value.trim(); | ||
var | var lines = []; | ||
if ( who ) { | |||
lines.push( who ); | |||
} | |||
lines.push( '(' + scores.join( ', ' ) + ' = ' + | |||
out.appendChild( | ( Number.isInteger( r.average ) ? r.average : r.average.toFixed( 2 ) ) + ')' ); | ||
lines.push( money( r.salary ) + ( endYear ? ' (' + endYear + ')' : ' (' + years + ' years)' ) ); | |||
out.appendChild( postBlock( lines.join( '\n' ) ) ); | |||
var note = document.createElement( 'div' ); | var note = document.createElement( 'div' ); | ||