]> git.scottworley.com Git - vopamoi/commitdiff
Use created-date as id
authorScott Worley <scottworley@scottworley.com>
Sun, 6 Mar 2022 00:06:36 +0000 (16:06 -0800)
committerScott Worley <scottworley@scottworley.com>
Sun, 6 Mar 2022 00:06:36 +0000 (16:06 -0800)
This makes getTask() O(1) instead of O(n), which makes replay() 3x
faster.

vopamoi.ts

index f0c893059db774572966bfbc7ad7fc5705e5b669..962add79a711c27efb8613a2e24fff2e69e8ad09 100644 (file)
@@ -45,7 +45,7 @@ function Model() {
       task.appendChild(desc);
       task.classList.add("task");
       task.setAttribute("tabindex", "0");
-      task.setAttribute("data-created", timestamp);
+      task.setAttribute("id", timestamp);
       task.setAttribute("data-state", "todo");
       const tasks = document.getElementById("tasks")!;
       tasks.insertBefore(task, tasks.firstElementChild);
@@ -110,15 +110,11 @@ function Model() {
       if (task.hasAttribute("data-priority")) {
         return parseFloat(task.getAttribute("data-priority")!);
       }
-      return parseFloat(task.getAttribute("data-created")!);
+      return parseFloat(task.getAttribute("id")!);
     },
 
     getTask: function (createTimestamp: string) {
-      for (const task of document.getElementsByClassName("task")) {
-        if (task.getAttribute("data-created") === createTimestamp) {
-          return task;
-        }
-      }
+      return document.getElementById(createTimestamp);
     },
 
     insertInPriorityOrder: function (task: Element, dest: Element) {
@@ -348,7 +344,7 @@ function BrowserUI() {
       if (resolution === CommitOrAbort.Abort || newDescription.match(/^ *$/) || newDescription === oldDescription) {
         desc.textContent = oldDescription;
       } else {
-        ui.edit(task.getAttribute("data-created")!, newDescription, oldDescription);
+        ui.edit(task.getAttribute("id")!, newDescription, oldDescription);
       }
     },
 
@@ -360,7 +356,7 @@ function BrowserUI() {
       task.removeChild(input);
       task.focus();
       if (resolution === CommitOrAbort.Commit && !newTagName.match(/^ *$/) && !model.hasTag(task, newTagName)) {
-        ui.addTag(task.getAttribute("data-created")!, newTagName);
+        ui.addTag(task.getAttribute("id")!, newTagName);
         lastTagNameEntered = newTagName;
       }
     },
@@ -440,7 +436,7 @@ function BrowserUI() {
     makeTopPriority: function (task: Element | null = null) {
       if (!task) task = this.currentTask();
       if (!task) return;
-      ui.setPriority(task.getAttribute("data-created")!, clock.now(), model.getPriority(task));
+      ui.setPriority(task.getAttribute("id")!, clock.now(), model.getPriority(task));
       task instanceof HTMLElement && task.focus();
     },
 
@@ -486,7 +482,7 @@ function BrowserUI() {
     removeTag: function () {
       const target = this.currentTag();
       if (!target) return;
-      ui.removeTag(target.parentElement!.getAttribute("data-created")!, target.textContent!);
+      ui.removeTag(target.parentElement!.getAttribute("id")!, target.textContent!);
     },
 
     resetTagView: function () {
@@ -523,7 +519,7 @@ function BrowserUI() {
       console.assert(aPriority > newPriority && newPriority > bPriority, aPriority, ">", newPriority, ">", bPriority);
       const newPriorityRounded = Math.round(newPriority);
       const okToRound = aPriority > newPriorityRounded && newPriorityRounded > bPriority;
-      ui.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, model.getPriority(task));
+      ui.setPriority(task.getAttribute("id")!, okToRound ? newPriorityRounded : newPriority, model.getPriority(task));
       task instanceof HTMLElement && task.focus();
     },
 
@@ -532,7 +528,7 @@ function BrowserUI() {
       if (!task) return;
       const oldState = task.getAttribute("data-state")!;
       if (newState === oldState) return;
-      const createTimestamp = task.getAttribute("data-created")!;
+      const createTimestamp = task.getAttribute("id")!;
       if (currentViewState !== "all" || newState == "deleted") {
         this.moveCursorVertically(1) || this.moveCursorVertically(-1);
       }