X-Git-Url: http://git.scottworley.com/vopamoi/blobdiff_plain/caa93fd1819b0d57029de637029daf0bc867d17e..09657615b47a9634b20a1ab5abf2172d1575f56c:/vopamoi.ts diff --git a/vopamoi.ts b/vopamoi.ts index f127da9..56c4813 100644 --- a/vopamoi.ts +++ b/vopamoi.ts @@ -17,6 +17,7 @@ const Model = { createTask: function (timestamp: string, description: string) { const task = document.createElement("div"); task.appendChild(document.createTextNode(description)); + task.setAttribute("class", "task"); task.setAttribute("tabindex", "0"); task.setAttribute("data-created", timestamp); return task; @@ -26,19 +27,17 @@ const Model = { document.body.appendChild(this.createTask(timestamp, description)).focus(); }, - moveCursor: function (offset: number): boolean { - var active = document.activeElement; - if (offset === 1 && active) { - active = active.nextElementSibling; - } - if (offset === -1 && active) { - active = active.previousElementSibling; - } - if (active && active instanceof HTMLElement) { - active.focus(); - return true; + getTask: function (createTimestamp: string) { + for (const task of document.getElementsByClassName("task")) { + if (task.getAttribute("data-created") === createTimestamp) { + return task; + } } - return false; + }, + + destroyTask: function (createTimestamp: string) { + const task = this.getTask(createTimestamp); + task!.parentElement!.removeChild(task!); }, }; @@ -50,6 +49,9 @@ const Log = (function () { if (command == "Create") { Model.addTask(timestamp, data); } + if (command == "Destroy") { + Model.destroyTask(data.split(" ", 1)[0]); + } }, record: function (entry: string) { @@ -78,6 +80,9 @@ const UI = { addTask: function (description: string) { Log.recordAndApply(`${Date.now()} Create ${description}`); }, + destroyTask: function (createTimestamp: string) { + Log.recordAndApply(`${Date.now()} Destroy ${createTimestamp} ${Model.getTask(createTimestamp)?.textContent}`); + }, }; const BrowserUI = { @@ -88,18 +93,39 @@ const BrowserUI = { } return false; }, -}; -function focusTaskNameInput(event: any) { - document.getElementById("taskName")!.focus(); - event.preventDefault(); -} + destroyTask: function () { + const createTimestamp = document.activeElement?.getAttribute("data-created"); + return createTimestamp && UI.destroyTask(createTimestamp); + }, + + focusTaskNameInput: function (event: any) { + document.getElementById("taskName")!.focus(); + event.preventDefault(); + }, + + moveCursor: function (offset: number): boolean { + var active = document.activeElement; + if (offset === 1 && active) { + active = active.nextElementSibling; + } + if (offset === -1 && active) { + active = active.previousElementSibling; + } + if (active && active instanceof HTMLElement) { + active.focus(); + return true; + } + return false; + }, +}; function handleKey(event: any) { if (event.target.tagName !== "INPUT") { - if (event.key == "j") return Model.moveCursor(1); - if (event.key == "k") return Model.moveCursor(-1); - if (event.key == "c") return focusTaskNameInput(event); + if (event.key == "j") return BrowserUI.moveCursor(1); + if (event.key == "k") return BrowserUI.moveCursor(-1); + if (event.key == "c") return BrowserUI.focusTaskNameInput(event); + if (event.key == "X") return BrowserUI.destroyTask(); } }