+function Log(prefix: string = "vp-") {
+ var next_log_index = 0;
+ return {
+ apply: function (entry: string) {
+ const [timestamp, command, data] = splitN(entry, " ", 2);
+ if (command == "Create") {
+ return Model.addTask(timestamp, data);
+ }
+ if (command == "Edit") {
+ const [createTimestamp, description] = splitN(data, " ", 1);
+ return Model.edit(createTimestamp, description);
+ }
+ if (command == "Priority") {
+ const [createTimestamp, newPriority] = splitN(data, " ", 1);
+ return Model.setPriority(createTimestamp, parseFloat(newPriority));
+ }
+ if (command == "State") {
+ const [createTimestamp, state] = splitN(data, " ", 1);
+ return Model.setState(timestamp, createTimestamp, state);
+ }
+ if (command == "Tag") {
+ const [createTimestamp, tag] = splitN(data, " ", 1);
+ return Model.addTag(createTimestamp, tag);
+ }
+ if (command == "Untag") {
+ const [createTimestamp, tag] = splitN(data, " ", 1);
+ return Model.removeTag(createTimestamp, tag);
+ }
+ },
+
+ record: function (entry: string) {
+ window.localStorage.setItem(`${prefix}${next_log_index++}`, entry);
+ },
+
+ recordAndApply: function (entry: string) {
+ this.record(entry);
+ return this.apply(entry);
+ },
+
+ replay: function () {
+ while (true) {
+ const entry = window.localStorage.getItem(`${prefix}${next_log_index}`);
+ if (entry === null) {
+ break;
+ }
+ this.apply(entry);
+ next_log_index++;
+ }
+ },
+ };
+}
+const log = Log();
+
+function UI() {
+ const undoLog: string[] = [];
+ return {
+ addTask: function (description: string): Element {
+ const now = clock.now();
+ undoLog.push(`State ${now} deleted`);
+ return <Element>log.recordAndApply(`${now} Create ${description}`);
+ },
+ addTag: function (createTimestamp: string, tag: string) {
+ undoLog.push(`Untag ${createTimestamp} ${tag}`);
+ return log.recordAndApply(`${clock.now()} Tag ${createTimestamp} ${tag}`);
+ },
+ edit: function (createTimestamp: string, newDescription: string, oldDescription: string) {
+ undoLog.push(`Edit ${createTimestamp} ${oldDescription}`);
+ return log.recordAndApply(`${clock.now()} Edit ${createTimestamp} ${newDescription}`);
+ },
+ removeTag: function (createTimestamp: string, tag: string) {
+ undoLog.push(`Tag ${createTimestamp} ${tag}`);
+ return log.recordAndApply(`${clock.now()} Untag ${createTimestamp} ${tag}`);
+ },
+ setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) {
+ undoLog.push(`Priority ${createTimestamp} ${oldPriority}`);
+ return log.recordAndApply(`${clock.now()} Priority ${createTimestamp} ${newPriority}`);
+ },
+ setState: function (createTimestamp: string, newState: string, oldState: string) {
+ undoLog.push(`State ${createTimestamp} ${oldState}`);
+ return log.recordAndApply(`${clock.now()} State ${createTimestamp} ${newState}`);
+ },
+ undo: function () {
+ if (undoLog.length > 0) {
+ return log.recordAndApply(`${clock.now()} ${undoLog.pop()}`);
+ }
+ },
+ };
+}
+const ui = UI();
+
+enum CommitOrAbort {
+ Commit,
+ Abort,
+}
+
+function BrowserUI() {
+ var currentViewState = "todo";
+ var taskFocusedBeforeJumpingToInput: HTMLElement | null = null;
+ var lastTagNameEntered = "";
+ return {
+ addTask: function (event: KeyboardEvent) {
+ const input = <HTMLInputElement>document.getElementById("taskName");
+ if (input.value.match(/^ *$/)) return;
+ const task = ui.addTask(input.value);
+ if (currentViewState === "todo") {
+ task instanceof HTMLElement && task.focus();
+ } else if (this.returnFocusAfterInput()) {
+ } else {
+ this.firstVisibleTask()?.focus();
+ }
+ input.value = "";
+ if (event.getModifierState("Control")) {
+ this.makeTopPriority(task);
+ }
+ },
+
+ beginEdit: function (event: Event) {
+ const task = document.activeElement;
+ if (!task) return;
+ const input = document.createElement("input");
+ const desc = task.getElementsByClassName("desc")[0];
+ const oldDescription = desc.textContent!;
+ task.setAttribute("data-description", oldDescription);
+ input.value = oldDescription;
+ input.addEventListener("blur", this.completeEdit, { once: true });
+ desc.textContent = "";
+ task.insertBefore(input, task.firstChild);
+ input.focus();
+ event.preventDefault();
+ },
+
+ beginTagEdit: function (event: Event) {
+ const task = document.activeElement;
+ if (!task) return;
+ const input = document.createElement("input");
+ input.classList.add("tag");
+ input.addEventListener("blur", this.completeTagEdit, { once: true });
+ input.value = lastTagNameEntered;
+ task.appendChild(input);
+ input.focus();
+ input.select();
+ event.preventDefault();
+ },
+
+ completeEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) {
+ const input = event.target as HTMLInputElement;
+ const task = input.parentElement!;
+ const desc = task.getElementsByClassName("desc")[0];
+ const oldDescription = task.getAttribute("data-description")!;
+ const newDescription = input.value;
+ input.removeEventListener("blur", this.completeEdit);
+ task.removeChild(input);
+ task.removeAttribute("data-description");
+ task.focus();
+ if (resolution === CommitOrAbort.Abort || newDescription.match(/^ *$/) || newDescription === oldDescription) {
+ desc.textContent = oldDescription;
+ } else {
+ ui.edit(task.getAttribute("data-created")!, newDescription, oldDescription);
+ }
+ },
+
+ completeTagEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) {
+ const input = event.target as HTMLInputElement;
+ const task = input.parentElement!;
+ const newTagName = input.value;
+ input.removeEventListener("blur", this.completeTagEdit);
+ task.removeChild(input);
+ task.focus();
+ if (resolution === CommitOrAbort.Commit && !newTagName.match(/^ *$/) && !Model.hasTag(task, newTagName)) {
+ ui.addTag(task.getAttribute("data-created")!, newTagName);
+ lastTagNameEntered = newTagName;
+ }
+ },
+
+ firstVisibleTask: function () {
+ for (const task of document.getElementsByClassName("task")) {
+ if (task instanceof HTMLElement && task.getAttribute("data-state") === currentViewState) {
+ return task;
+ }
+ }
+ },
+
+ focusTaskNameInput: function (event: Event) {
+ if (document.activeElement instanceof HTMLElement) {
+ taskFocusedBeforeJumpingToInput = document.activeElement;
+ }