]>
Commit | Line | Data |
---|---|---|
121d9948 SW |
1 | // Typescript doesn't know about MAX_SAFE_INTEGER?? This was supposed to be |
2 | // fixed in typescript 2.0.1 in 2016, but is not working for me in typescript | |
3 | // 4.2.4 in 2022. :( https://github.com/microsoft/TypeScript/issues/9937 | |
4 | //const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER; | |
5 | const MAX_SAFE_INTEGER = 9007199254740991; | |
6 | ||
7 | // A sane split that splits N *times*, leaving the last chunk unsplit. | |
8 | function splitN(str: string, delimiter: string, limit: number = MAX_SAFE_INTEGER): string[] { | |
9 | if (limit < 1) { | |
10 | return [str]; | |
11 | } | |
12 | const at = str.indexOf(delimiter); | |
13 | return at === -1 ? [str] : [str.substring(0, at)].concat(splitN(str.substring(at + delimiter.length), delimiter, limit - 1)); | |
14 | } | |
15 | ||
13c97b99 | 16 | const Model = { |
412e3663 | 17 | addTask: function (timestamp: string, description: string) { |
4546c7b7 | 18 | document.getElementById("tasks")!.appendChild(this.createTask(timestamp, description)).focus(); |
412e3663 SW |
19 | }, |
20 | ||
4101e1b1 | 21 | createTask: function (timestamp: string, description: string) { |
13c97b99 SW |
22 | const task = document.createElement("div"); |
23 | task.appendChild(document.createTextNode(description)); | |
799f4e89 | 24 | task.setAttribute("class", "task"); |
13c97b99 | 25 | task.setAttribute("tabindex", "0"); |
4101e1b1 | 26 | task.setAttribute("data-created", timestamp); |
13c97b99 SW |
27 | return task; |
28 | }, | |
974848d3 | 29 | |
412e3663 SW |
30 | destroyTask: function (createTimestamp: string) { |
31 | const task = this.getTask(createTimestamp); | |
32 | if (task) { | |
33 | task.parentElement!.removeChild(task); | |
34 | } | |
13c97b99 | 35 | }, |
974848d3 | 36 | |
68a72fde SW |
37 | getPriority: function (task: Element): number { |
38 | if (task.hasAttribute("data-priority")) { | |
39 | return parseFloat(task.getAttribute("data-priority")!); | |
40 | } | |
41 | return parseFloat(task.getAttribute("data-created")!); | |
42 | }, | |
43 | ||
799f4e89 SW |
44 | getTask: function (createTimestamp: string) { |
45 | for (const task of document.getElementsByClassName("task")) { | |
46 | if (task.getAttribute("data-created") === createTimestamp) { | |
47 | return task; | |
48 | } | |
49 | } | |
50 | }, | |
51 | ||
68a72fde SW |
52 | setPriority: function (createTimestamp: string, priority: number) { |
53 | const target = this.getTask(createTimestamp); | |
54 | if (!target) return; | |
55 | target.setAttribute("data-priority", `${priority}`); | |
56 | for (const task of document.getElementsByClassName("task")) { | |
57 | if (task !== target && this.getPriority(task) > priority) { | |
58 | task.parentElement!.insertBefore(target, task); | |
59 | target instanceof HTMLElement && target.focus(); | |
60 | return; | |
61 | } | |
62 | } | |
63 | document.getElementById("tasks")!.appendChild(target); | |
64 | target instanceof HTMLElement && target.focus(); | |
65 | }, | |
66 | ||
01f41859 SW |
67 | setState: function (stateTimestamp: string, createTimestamp: string, state: string) { |
68 | const task = this.getTask(createTimestamp); | |
69 | if (task) { | |
70 | task.setAttribute(`data-${state}`, stateTimestamp); | |
71 | if (task instanceof HTMLElement) { | |
72 | task.style.display = "none"; // Until view filtering | |
73 | } | |
74 | } | |
799f4e89 | 75 | }, |
13c97b99 | 76 | }; |
f1afad9b | 77 | |
d03daa19 | 78 | function Log(prefix: string = "vp-") { |
60a63831 SW |
79 | var next_log_index = 0; |
80 | return { | |
e88c099c | 81 | apply: function (entry: string) { |
60a63831 SW |
82 | const [timestamp, command, data] = splitN(entry, " ", 2); |
83 | if (command == "Create") { | |
4101e1b1 | 84 | Model.addTask(timestamp, data); |
60a63831 | 85 | } |
799f4e89 SW |
86 | if (command == "Destroy") { |
87 | Model.destroyTask(data.split(" ", 1)[0]); | |
88 | } | |
01f41859 SW |
89 | if (command == "State") { |
90 | const [createTimestamp, state] = splitN(data, " ", 1); | |
91 | Model.setState(timestamp, createTimestamp, state); | |
92 | } | |
68a72fde SW |
93 | if (command == "Priority") { |
94 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
95 | Model.setPriority(createTimestamp, parseFloat(newPriority)); | |
96 | } | |
60a63831 SW |
97 | }, |
98 | ||
e88c099c | 99 | record: function (entry: string) { |
d03daa19 | 100 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); |
60a63831 SW |
101 | }, |
102 | ||
e88c099c SW |
103 | recordAndApply: function (entry: string) { |
104 | this.record(entry); | |
105 | this.apply(entry); | |
60a63831 SW |
106 | }, |
107 | ||
108 | replay: function () { | |
109 | while (true) { | |
d03daa19 | 110 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); |
60a63831 SW |
111 | if (entry === null) { |
112 | break; | |
113 | } | |
e88c099c | 114 | this.apply(entry); |
60a63831 SW |
115 | next_log_index++; |
116 | } | |
117 | }, | |
118 | }; | |
d03daa19 SW |
119 | } |
120 | const log = Log(); | |
262705dd | 121 | |
e88c099c SW |
122 | const UI = { |
123 | addTask: function (description: string) { | |
d03daa19 | 124 | log.recordAndApply(`${Date.now()} Create ${description}`); |
e88c099c | 125 | }, |
799f4e89 | 126 | destroyTask: function (createTimestamp: string) { |
d03daa19 | 127 | log.recordAndApply(`${Date.now()} Destroy ${createTimestamp} ${Model.getTask(createTimestamp)?.textContent}`); |
799f4e89 | 128 | }, |
68a72fde SW |
129 | setPriority: function (createTimestamp: string, priority: number) { |
130 | log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${priority}`); | |
131 | }, | |
01f41859 | 132 | setState: function (createTimestamp: string, state: string) { |
d03daa19 | 133 | log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`); |
01f41859 | 134 | }, |
e88c099c SW |
135 | }; |
136 | ||
caa93fd1 SW |
137 | const BrowserUI = { |
138 | addTask: function (form: any) { | |
139 | if (form.taskName.value) { | |
140 | UI.addTask(form.taskName.value); | |
141 | form.taskName.value = ""; | |
142 | } | |
143 | return false; | |
144 | }, | |
09657615 | 145 | |
799f4e89 SW |
146 | destroyTask: function () { |
147 | const createTimestamp = document.activeElement?.getAttribute("data-created"); | |
61535898 SW |
148 | this.moveCursor(1) || this.moveCursor(-1); |
149 | return UI.destroyTask(createTimestamp!); | |
799f4e89 | 150 | }, |
caa93fd1 | 151 | |
09657615 SW |
152 | focusTaskNameInput: function (event: any) { |
153 | document.getElementById("taskName")!.focus(); | |
154 | event.preventDefault(); | |
155 | }, | |
156 | ||
23be73e3 SW |
157 | visibleTaskAtOffset(task: Element, offset: number): Element { |
158 | var cursor: Element | null = task; | |
5fa4704c SW |
159 | var valid_cursor = cursor; |
160 | const increment = offset / Math.abs(offset); | |
161 | while (true) { | |
162 | cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling; | |
163 | if (!cursor || !(cursor instanceof HTMLElement)) break; | |
164 | if (cursor.style.display !== "none") { | |
165 | offset -= increment; | |
166 | valid_cursor = cursor; | |
167 | } | |
168 | if (Math.abs(offset) < 0.5) break; | |
09657615 | 169 | } |
23be73e3 SW |
170 | return valid_cursor; |
171 | }, | |
172 | ||
173 | moveCursor: function (offset: number): boolean { | |
174 | const active = document.activeElement; | |
175 | if (!active) return false; | |
176 | const dest = this.visibleTaskAtOffset(active, offset); | |
177 | if (dest !== active && dest instanceof HTMLElement) { | |
178 | dest.focus(); | |
09657615 SW |
179 | return true; |
180 | } | |
181 | return false; | |
182 | }, | |
01f41859 | 183 | |
68a72fde SW |
184 | moveTask: function (offset: number) { |
185 | const active = document.activeElement; | |
186 | if (!active) return; | |
187 | const dest = this.visibleTaskAtOffset(active, offset); | |
188 | if (dest === active) return; // Already extremal | |
189 | var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset)); | |
190 | if (onePastDest == dest) onePastDest = null; // Will become extremal | |
191 | if (offset > 0) { | |
192 | this.setPriority(active, dest, onePastDest); | |
193 | } else { | |
194 | this.setPriority(active, onePastDest, dest); | |
195 | } | |
196 | }, | |
197 | ||
198 | // Change task's priority to be between other tasks a and b. | |
199 | setPriority: function (task: Element, a: Element | null, b: Element | null) { | |
200 | const aPriority = a === null ? 0 : Model.getPriority(a); | |
201 | const bPriority = b === null ? Date.now() : Model.getPriority(b); | |
202 | console.assert(aPriority < bPriority, aPriority, "<", bPriority); | |
203 | const span = bPriority - aPriority; | |
204 | const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random(); | |
205 | console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority); | |
206 | const newPriorityRounded = Math.round(newPriority); | |
207 | const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority; | |
208 | UI.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority); | |
209 | }, | |
210 | ||
01f41859 SW |
211 | setState: function (state: string) { |
212 | const createTimestamp = document.activeElement?.getAttribute("data-created"); | |
213 | this.moveCursor(1) || this.moveCursor(-1); | |
214 | return UI.setState(createTimestamp!, state); | |
215 | }, | |
09657615 | 216 | }; |
06ee32a1 | 217 | |
f1afad9b SW |
218 | function handleKey(event: any) { |
219 | if (event.target.tagName !== "INPUT") { | |
09657615 SW |
220 | if (event.key == "j") return BrowserUI.moveCursor(1); |
221 | if (event.key == "k") return BrowserUI.moveCursor(-1); | |
68a72fde SW |
222 | if (event.key == "J") return BrowserUI.moveTask(1); |
223 | if (event.key == "K") return BrowserUI.moveTask(-1); | |
01f41859 SW |
224 | if (event.key == "n") return BrowserUI.focusTaskNameInput(event); |
225 | if (event.key == "s") return BrowserUI.setState("someday-maybe"); | |
226 | if (event.key == "w") return BrowserUI.setState("waiting"); | |
227 | if (event.key == "d") return BrowserUI.setState("done"); | |
228 | if (event.key == "c") return BrowserUI.setState("cancelled"); | |
799f4e89 | 229 | if (event.key == "X") return BrowserUI.destroyTask(); |
f1afad9b SW |
230 | } |
231 | } | |
232 | ||
f1afad9b SW |
233 | function browserInit() { |
234 | document.body.addEventListener("keydown", handleKey, { capture: false }); | |
d03daa19 | 235 | log.replay(); |
f1afad9b | 236 | } |