]> git.scottworley.com Git - vopamoi/commitdiff
Tasks have one state. Enables undo for state changes
authorScott Worley <scottworley@scottworley.com>
Thu, 27 Jan 2022 01:51:51 +0000 (17:51 -0800)
committerScott Worley <scottworley@scottworley.com>
Thu, 27 Jan 2022 20:21:55 +0000 (12:21 -0800)
vopamoi.ts

index a88408f639f3c8663ff16d92f38c4476741d93a3..298e513fde6ff3df3abe46fc3080435ae9a854b8 100644 (file)
@@ -32,6 +32,10 @@ const Model = {
     return parseFloat(task.getAttribute("data-created")!);
   },
 
+  getState: function (task: Element): string {
+    return task.getAttribute("data-state") ?? "todo";
+  },
+
   getTask: function (createTimestamp: string) {
     for (const task of document.getElementsByClassName("task")) {
       if (task.getAttribute("data-created") === createTimestamp) {
@@ -59,9 +63,9 @@ const Model = {
   setState: function (stateTimestamp: string, createTimestamp: string, state: string) {
     const task = this.getTask(createTimestamp);
     if (task) {
-      task.setAttribute(`data-${state}`, stateTimestamp);
+      task.setAttribute("data-state", state);
       if (task instanceof HTMLElement) {
-        task.style.display = "none"; // Until view filtering
+        task.style.display = state == "todo" ? "block" : "none"; // Until view filtering
       }
     }
   },
@@ -120,8 +124,9 @@ const UI = {
     undoLog.push(`Priority ${createTimestamp} ${oldPriority}`);
     return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${newPriority}`);
   },
-  setState: function (createTimestamp: string, state: string) {
-    return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`);
+  setState: function (createTimestamp: string, newState: string, oldState: string) {
+    undoLog.push(`State ${createTimestamp} ${oldState}`);
+    return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${newState}`);
   },
   undo: function () {
     if (undoLog.length > 0) {
@@ -210,9 +215,11 @@ const BrowserUI = {
   },
 
   setState: function (state: string) {
-    const createTimestamp = document.activeElement?.getAttribute("data-created");
+    const task = document.activeElement;
+    if (!task) return;
+    const createTimestamp = task.getAttribute("data-created")!;
     this.moveCursor(1) || this.moveCursor(-1);
-    return UI.setState(createTimestamp!, state);
+    return UI.setState(createTimestamp, state, Model.getState(task));
   },
 
   undo: function () {