import * as HighScoreActions from '../../actions/high-score/high-score.actions'; let dispatch; let initials = [ { initial: 'A', active: true }, { initial: 'A', active: false }, { initial: 'A', active: false } ]; let active = 0; let code; const InitialsCtrl = { setDispatch: (d) => dispatch = d, getInitials: () => initials.reduce((acc, v) => { return acc + v.initial; }, ''), keydown: function(e) { switch (e.keyCode) { case 37: active--; if (active === -1) { active = initials.length - 1; } this.update(); break; case 38: code = initials[active].initial.charCodeAt(0); code--; if (code === 64) { code = 90; } initials[active].initial = String.fromCharCode(code); this.update(); break; case 39: active++; if (active === initials.length) { active = 0; } this.update(); break; case 40: code = initials[active].initial.charCodeAt(0); code++; if (code === 91) { code = 65; } initials[active].initial = String.fromCharCode(code); this.update(); break; } if (e.keyCode >= 65 && e.keyCode <= 90) { initials[active].initial = String.fromCharCode(e.keyCode); if (active < initials.length - 1) { active++; } this.update(); } }, update: () => { initials = initials.map((v) => { v.active = false; return v; }); initials[active].active = true; dispatch(HighScoreActions.updateInitials(initials)); } }; export default InitialsCtrl;