Template:Groningen/stager js

function Stager(stages, elTitle, elDesc, elOut, elKey){ this.stages = stages; this.elTitle = elTitle; this.elDesc = elDesc; this.elOut = elOut;

this.current = {idx: 0, data: null, display: null}; }

Stager.prototype.advance = function(prev){ var idx = prev ? prev.idx : 0, cur = this.stages[idx], data = prev.data, disp;

if(!cur){ return cur; }

try{ this.elOut.classList.remove('err');

if(cur.transform){ data = cur.transform(data); }

disp = data; if(cur.display){ disp = cur.display(data); } }catch(ex){ this.elOut.classList.add('err'); disp = ex.message; }

return {stage: cur, idx: (idx + 1) % this.stages.length, data: data, display: disp}; };

Stager.prototype.show = function(){ this.current = this.advance(this.current);

this.elOut.value = this.current.display; this.elTitle.innerHTML = this.current.stage.name; this.elDesc.innerHTML = this.current.stage.description; };

Stager.prototype.reset = function(){ this.current = {idx: 0, data: null, display: null}; this.show(); };

Stager.prototype.init = function(elIn, elKey, elBtn){ elIn.oninput = elIn.onkeyup = this.reset.bind(this); elKey.oninput = elKey.onkeyup = this.reset.bind(this); elBtn.onclick = this.show.bind(this);

this.reset(); };