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) {
18 document.body.appendChild(this.createTask(timestamp, description)).focus();
21 createTask: function (timestamp: string, description: string) {
22 const task = document.createElement("div");
23 task.appendChild(document.createTextNode(description));
24 task.setAttribute("class", "task");
25 task.setAttribute("tabindex", "0");
26 task.setAttribute("data-created", timestamp);
30 destroyTask: function (createTimestamp: string) {
31 const task = this.getTask(createTimestamp);
33 task.parentElement!.removeChild(task);
37 getTask: function (createTimestamp: string) {
38 for (const task of document.getElementsByClassName("task")) {
39 if (task.getAttribute("data-created") === createTimestamp) {
45 setState: function (stateTimestamp: string, createTimestamp: string, state: string) {
46 const task = this.getTask(createTimestamp);
48 task.setAttribute(`data-${state}`, stateTimestamp);
49 if (task instanceof HTMLElement) {
50 task.style.display = "none"; // Until view filtering
56 function Log(prefix: string = "vp-") {
57 var next_log_index = 0;
59 apply: function (entry: string) {
60 const [timestamp, command, data] = splitN(entry, " ", 2);
61 if (command == "Create") {
62 Model.addTask(timestamp, data);
64 if (command == "Destroy") {
65 Model.destroyTask(data.split(" ", 1)[0]);
67 if (command == "State") {
68 const [createTimestamp, state] = splitN(data, " ", 1);
69 Model.setState(timestamp, createTimestamp, state);
73 record: function (entry: string) {
74 window.localStorage.setItem(`${prefix}${next_log_index++}`, entry);
77 recordAndApply: function (entry: string) {
84 const entry = window.localStorage.getItem(`${prefix}${next_log_index}`);
97 addTask: function (description: string) {
98 log.recordAndApply(`${Date.now()} Create ${description}`);
100 destroyTask: function (createTimestamp: string) {
101 log.recordAndApply(`${Date.now()} Destroy ${createTimestamp} ${Model.getTask(createTimestamp)?.textContent}`);
103 setState: function (createTimestamp: string, state: string) {
104 log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`);
109 addTask: function (form: any) {
110 if (form.taskName.value) {
111 UI.addTask(form.taskName.value);
112 form.taskName.value = "";
117 destroyTask: function () {
118 const createTimestamp = document.activeElement?.getAttribute("data-created");
119 this.moveCursor(1) || this.moveCursor(-1);
120 return UI.destroyTask(createTimestamp!);
123 focusTaskNameInput: function (event: any) {
124 document.getElementById("taskName")!.focus();
125 event.preventDefault();
128 visibleTaskAtOffset(task: Element, offset: number): Element {
129 var cursor: Element | null = task;
130 var valid_cursor = cursor;
131 const increment = offset / Math.abs(offset);
133 cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
134 if (!cursor || !(cursor instanceof HTMLElement)) break;
135 if (cursor.style.display !== "none") {
137 valid_cursor = cursor;
139 if (Math.abs(offset) < 0.5) break;
144 moveCursor: function (offset: number): boolean {
145 const active = document.activeElement;
146 if (!active) return false;
147 const dest = this.visibleTaskAtOffset(active, offset);
148 if (dest !== active && dest instanceof HTMLElement) {
155 setState: function (state: string) {
156 const createTimestamp = document.activeElement?.getAttribute("data-created");
157 this.moveCursor(1) || this.moveCursor(-1);
158 return UI.setState(createTimestamp!, state);
162 function handleKey(event: any) {
163 if (event.target.tagName !== "INPUT") {
164 if (event.key == "j") return BrowserUI.moveCursor(1);
165 if (event.key == "k") return BrowserUI.moveCursor(-1);
166 if (event.key == "n") return BrowserUI.focusTaskNameInput(event);
167 if (event.key == "s") return BrowserUI.setState("someday-maybe");
168 if (event.key == "w") return BrowserUI.setState("waiting");
169 if (event.key == "d") return BrowserUI.setState("done");
170 if (event.key == "c") return BrowserUI.setState("cancelled");
171 if (event.key == "X") return BrowserUI.destroyTask();
175 function browserInit() {
176 document.body.addEventListener("keydown", handleKey, { capture: false });