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;
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[] {
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));
17 addTask: function (timestamp: string, description: string): Element {
18 const task = document.createElement("div");
19 task.appendChild(document.createTextNode(description));
20 task.setAttribute("class", "task");
21 task.setAttribute("tabindex", "0");
22 task.setAttribute("data-created", timestamp);
23 document.getElementById("tasks")!.appendChild(task);
28 getPriority: function (task: Element): number {
29 if (task.hasAttribute("data-priority")) {
30 return parseFloat(task.getAttribute("data-priority")!);
32 return parseFloat(task.getAttribute("data-created")!);
35 getTask: function (createTimestamp: string) {
36 for (const task of document.getElementsByClassName("task")) {
37 if (task.getAttribute("data-created") === createTimestamp) {
43 setPriority: function (createTimestamp: string, priority: number) {
44 const target = this.getTask(createTimestamp);
46 target.setAttribute("data-priority", `${priority}`);
47 for (const task of document.getElementsByClassName("task")) {
48 if (task !== target && this.getPriority(task) > priority) {
49 task.parentElement!.insertBefore(target, task);
50 target instanceof HTMLElement && target.focus();
54 document.getElementById("tasks")!.appendChild(target);
55 target instanceof HTMLElement && target.focus();
58 setState: function (stateTimestamp: string, createTimestamp: string, state: string) {
59 const task = this.getTask(createTimestamp);
61 task.setAttribute(`data-${state}`, stateTimestamp);
62 if (task instanceof HTMLElement) {
63 task.style.display = "none"; // Until view filtering
69 function Log(prefix: string = "vp-") {
70 var next_log_index = 0;
72 apply: function (entry: string) {
73 const [timestamp, command, data] = splitN(entry, " ", 2);
74 if (command == "Create") {
75 return Model.addTask(timestamp, data);
77 if (command == "State") {
78 const [createTimestamp, state] = splitN(data, " ", 1);
79 return Model.setState(timestamp, createTimestamp, state);
81 if (command == "Priority") {
82 const [createTimestamp, newPriority] = splitN(data, " ", 1);
83 return Model.setPriority(createTimestamp, parseFloat(newPriority));
87 record: function (entry: string) {
88 window.localStorage.setItem(`${prefix}${next_log_index++}`, entry);
91 recordAndApply: function (entry: string) {
93 return this.apply(entry);
98 const entry = window.localStorage.getItem(`${prefix}${next_log_index}`);
111 addTask: function (description: string): Element {
112 return <Element>log.recordAndApply(`${Date.now()} Create ${description}`);
114 setPriority: function (createTimestamp: string, priority: number) {
115 return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${priority}`);
117 setState: function (createTimestamp: string, state: string) {
118 return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`);
123 addTask: function (event: KeyboardEvent) {
124 const input = <HTMLInputElement>document.getElementById("taskName");
126 const task = UI.addTask(input.value);
128 if (event.getModifierState("Control")) {
129 this.setPriority(task, null, document.getElementsByClassName("task")[0]);
134 firstVisibleTask: function () {
135 for (const task of document.getElementsByClassName("task")) {
136 if (task instanceof HTMLElement && task.style.display !== "none") {
142 focusTaskNameInput: function (event: Event) {
143 document.getElementById("taskName")!.focus();
144 event.preventDefault();
147 visibleTaskAtOffset(task: Element, offset: number): Element {
148 var cursor: Element | null = task;
149 var valid_cursor = cursor;
150 const increment = offset / Math.abs(offset);
152 cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
153 if (!cursor || !(cursor instanceof HTMLElement)) break;
154 if (cursor.style.display !== "none") {
156 valid_cursor = cursor;
158 if (Math.abs(offset) < 0.5) break;
163 moveCursor: function (offset: number): boolean {
164 const active = document.activeElement;
165 if (!active) return false;
166 const dest = this.visibleTaskAtOffset(active, offset);
167 if (dest !== active && dest instanceof HTMLElement) {
174 moveTask: function (offset: number) {
175 const active = document.activeElement;
177 const dest = this.visibleTaskAtOffset(active, offset);
178 if (dest === active) return; // Already extremal
179 var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset));
180 if (onePastDest == dest) onePastDest = null; // Will become extremal
182 this.setPriority(active, dest, onePastDest);
184 this.setPriority(active, onePastDest, dest);
188 // Change task's priority to be between other tasks a and b.
189 setPriority: function (task: Element, a: Element | null, b: Element | null) {
190 const aPriority = a === null ? 0 : Model.getPriority(a);
191 const bPriority = b === null ? Date.now() : Model.getPriority(b);
192 console.assert(aPriority < bPriority, aPriority, "<", bPriority);
193 const span = bPriority - aPriority;
194 const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random();
195 console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority);
196 const newPriorityRounded = Math.round(newPriority);
197 const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority;
198 UI.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority);
201 setState: function (state: string) {
202 const createTimestamp = document.activeElement?.getAttribute("data-created");
203 this.moveCursor(1) || this.moveCursor(-1);
204 return UI.setState(createTimestamp!, state);
208 function handleKey(event: any) {
209 if (event.target.tagName === "INPUT") {
210 if (event.key == "Enter") return BrowserUI.addTask(event);
212 if (event.key == "j") return BrowserUI.moveCursor(1);
213 if (event.key == "k") return BrowserUI.moveCursor(-1);
214 if (event.key == "J") return BrowserUI.moveTask(1);
215 if (event.key == "K") return BrowserUI.moveTask(-1);
216 if (event.key == "n") return BrowserUI.focusTaskNameInput(event);
217 if (event.key == "s") return BrowserUI.setState("someday-maybe");
218 if (event.key == "w") return BrowserUI.setState("waiting");
219 if (event.key == "d") return BrowserUI.setState("done");
220 if (event.key == "c") return BrowserUI.setState("cancelled");
221 if (event.key == "X") return BrowserUI.setState("deleted");
225 function browserInit() {
226 document.body.addEventListener("keydown", handleKey, { capture: false });
228 BrowserUI.firstVisibleTask()?.focus();