]> git.scottworley.com Git - vopamoi/blame - vopamoi.ts
Writes go through the Log
[vopamoi] / vopamoi.ts
CommitLineData
13c97b99
SW
1const Model = {
2 createTask: function (description: string) {
3 const task = document.createElement("div");
4 task.appendChild(document.createTextNode(description));
5 task.setAttribute("tabindex", "0");
6 return task;
7 },
974848d3 8
13c97b99 9 addTask: function (description: string) {
f69ff526 10 document.body.appendChild(this.createTask(description)).focus();
13c97b99 11 },
974848d3 12
13c97b99
SW
13 moveCursor: function (offset: number) {
14 var active = document.activeElement;
15 if (offset === 1 && active) {
16 active = active.nextElementSibling;
17 }
18 if (offset === -1 && active) {
19 active = active.previousElementSibling;
20 }
21 if (active && active instanceof HTMLElement) active.focus();
22 },
23};
f1afad9b 24
262705dd
SW
25const Log = {
26 addTask: function (description: string) {
27 Model.addTask(description);
28 },
29};
30
f1afad9b
SW
31function handleKey(event: any) {
32 if (event.target.tagName !== "INPUT") {
13c97b99
SW
33 if (event.key == "j") Model.moveCursor(1);
34 if (event.key == "k") Model.moveCursor(-1);
75a42da4
SW
35 if (event.key == "c") {
36 document.getElementById("taskName")!.focus();
37 event.preventDefault();
38 }
f1afad9b
SW
39 }
40}
41
974848d3 42function browserCreateTask(form: any) {
f1b121ab 43 if (form.taskName.value) {
262705dd 44 Log.addTask(form.taskName.value);
f1b121ab 45 }
974848d3
SW
46 form.taskName.value = "";
47 return false;
48}
f1afad9b
SW
49
50function browserInit() {
51 document.body.addEventListener("keydown", handleKey, { capture: false });
52}