page-trainer.js (1440B)
1 const m = require("mithril"); 2 const keyBoxes = require("./key-boxes.js"); 3 const training = require("../modules/training.js"); 4 const input = require("../modules/input.js"); 5 6 function fixSpace(c) { 7 if (c == " ") return "_"; 8 return c; 9 } 10 11 module.exports = () => { 12 let test; 13 let showHint = false; 14 let hintWasShown = false; 15 let wrongSubmit = false; 16 let hinthandler = (e) => { 17 if (e.key == input.hintkey) showHint = !showHint; 18 if (showHint) hintWasShown = true; 19 m.redraw(); 20 } 21 let newTest = () => { 22 test = training.getTest(); 23 showHint = false; 24 hintWasShown = false; 25 wrongSubmit = false; 26 } 27 let oninput = (c) => { 28 if (test[0] == c) { 29 training.saveTest(test[0], !hintWasShown && !wrongSubmit); 30 newTest(); 31 } 32 else wrongSubmit = true; 33 } 34 return { 35 oninit: () => { 36 newTest(); 37 }, 38 view: () => m("div", 39 m(".trainerTop", 40 m(".bigletter", { 41 class: wrongSubmit ? "red" : "" 42 }, m("span", fixSpace(test[0]))), 43 m(".hint", { 44 class: showHint ? "" : "invisible" 45 }, "Hint: " + test[1]) 46 ), 47 m(".trainerBottom", m(keyBoxes, { 48 inputCallback: oninput, 49 keyCallback: hinthandler 50 })) 51 ) 52 } 53 };