- }
- },
-
- focusTaskNameInput: function (event: Event) {
- document.getElementById("taskName")!.focus();
- event.preventDefault();
- },
-
- visibleTaskAtOffset(task: Element, offset: number): Element {
- var cursor: Element | null = task;
- var valid_cursor = cursor;
- const increment = offset / Math.abs(offset);
- while (true) {
- cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
- if (!cursor || !(cursor instanceof HTMLElement)) break;
- if (cursor.getAttribute("data-state")! === "todo") {
- offset -= increment;
- valid_cursor = cursor;
- }
- if (Math.abs(offset) < 0.5) break;
- }
- return valid_cursor;
- },
-
- moveCursor: function (offset: number): boolean {
- const active = document.activeElement;
- if (!active) return false;
- const dest = this.visibleTaskAtOffset(active, offset);
- if (dest !== active && dest instanceof HTMLElement) {
- dest.focus();
- return true;
- }
- return false;
- },
-
- moveTask: function (offset: number) {
- const active = document.activeElement;
- if (!active) return;
- const dest = this.visibleTaskAtOffset(active, offset);
- if (dest === active) return; // Already extremal
- var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset));
- if (onePastDest == dest) onePastDest = null; // Will become extremal
- if (offset > 0) {
- this.setPriority(active, dest, onePastDest);
- } else {
- this.setPriority(active, onePastDest, dest);
- }
- },
-
- // Change task's priority to be between other tasks a and b.
- setPriority: function (task: Element, a: Element | null, b: Element | null) {
- const aPriority = a === null ? 0 : Model.getPriority(a);
- const bPriority = b === null ? clock.now() : Model.getPriority(b);
- console.assert(aPriority < bPriority, aPriority, "<", bPriority);
- const span = bPriority - aPriority;
- const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random();
- 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));
- task instanceof HTMLElement && task.focus();
- },
-
- setState: function (newState: string) {
- const task = document.activeElement;
- if (!task) return;
- const oldState = task.getAttribute("data-state")!;
- if (newState === oldState) return;
- const createTimestamp = task.getAttribute("data-created")!;
- this.moveCursor(1) || this.moveCursor(-1);
- return UI.setState(createTimestamp, newState, oldState);
- },
-
- undo: function () {
- const ret = UI.undo();
- if (ret && ret instanceof HTMLElement) ret.focus();
- },
-};
+ },
+
+ completeContentEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) {
+ const input = event.target as HTMLInputElement;
+ const task = input.parentElement!;
+ const content = task.getElementsByClassName("content")[0];
+ const oldContent = task.getAttribute("data-content")!;
+ const newContent = input.value;
+ input.removeEventListener("blur", this.completeContentEdit);
+ task.removeChild(input);
+ task.removeAttribute("data-content");
+ task.focus();
+ if (resolution === CommitOrAbort.Abort || newContent === oldContent) {
+ if (content) content.textContent = oldContent;
+ } else {
+ ui.editContent(task.getAttribute("id")!, newContent, oldContent);
+ }
+ },
+
+ 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("id")!, newTagName);
+ lastTagNameEntered = newTagName;
+ }
+ },
+
+ currentTag: function (): Element | null {
+ var target = document.activeElement;
+ if (!target) return null;
+ if (target.classList.contains("task")) {
+ const tags = target.getElementsByClassName("tag");
+ target = tags[tags.length - 1];
+ }
+ if (!target || !target.classList.contains("tag")) return null;
+ return target;
+ },
+
+ currentTask: function (): HTMLElement | null {
+ var target = document.activeElement;
+ if (!target) return null;
+ if (target.classList.contains("tag")) target = target.parentElement!;
+ if (!target.classList.contains("task")) return null;
+ return target as HTMLElement;
+ },
+
+ firstVisibleTask: function (root: Element | null = null) {
+ if (root === null) root = document.body;
+ for (const task of root.getElementsByClassName("task")) {
+ const state = task.getAttribute("data-state");
+ if (
+ task instanceof HTMLElement &&
+ (state === currentViewState || (currentViewState === "all" && state !== "deleted")) &&
+ !task.classList.contains("hide")
+ ) {
+ return task;
+ }
+ }
+ },
+
+ focusTaskNameInput: function (event: Event) {
+ taskFocusedBeforeJumpingToInput = this.currentTask();
+ document.getElementById("taskName")!.focus();
+ window.scroll(0, 0);
+ event.preventDefault();
+ },
+
+ visibleTaskAtOffset(task: Element, offset: number): Element {
+ var cursor: Element | null = task;
+ var valid_cursor = cursor;
+ const increment = offset / Math.abs(offset);
+ while (true) {
+ cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
+ if (!cursor || !(cursor instanceof HTMLElement)) break;
+ const state = cursor.getAttribute("data-state")!;
+ if (
+ (state === currentViewState || (currentViewState === "all" && state !== "deleted")) &&
+ !cursor.classList.contains("hide")
+ ) {
+ offset -= increment;
+ valid_cursor = cursor;
+ }
+ if (Math.abs(offset) < 0.5) break;
+ }
+ return valid_cursor;
+ },
+
+ jumpCursor: function (position: number) {
+ const first = this.firstVisibleTask();
+ if (!first) return;
+ const dest = this.visibleTaskAtOffset(first, position - 1);
+ if (dest instanceof HTMLElement) dest.focus();
+ },
+
+ makeBottomPriority: function (task: Element | null = null) {
+ if (!task) task = this.currentTask();
+ if (!task) return;
+ this.setPriority(task, document.getElementById("tasks")!.lastElementChild, null);
+ },
+
+ makeTopPriority: function (task: Element | null = null) {
+ if (!task) task = this.currentTask();
+ if (!task) return;
+ ui.setPriority(task.getAttribute("id")!, clock.now(), model.getPriority(task));
+ task instanceof HTMLElement && task.focus();
+ },
+
+ moveCursorLeft: function () {
+ const active = this.currentTask();
+ if (!active) return false;
+ if (active.parentElement!.classList.contains("task")) {
+ active.parentElement!.focus();
+ }
+ },
+
+ moveCursorRight: function () {
+ const active = this.currentTask();
+ if (!active) return false;
+ (this.firstVisibleTask(active) as HTMLElement | null)?.focus();
+ },
+
+ moveCursorVertically: function (offset: number): boolean {
+ let active = this.currentTask();
+ if (!active) {
+ this.firstVisibleTask()?.focus();
+ active = this.currentTask();
+ }
+ if (!active) return false;
+ const dest = this.visibleTaskAtOffset(active, offset);
+ if (dest !== active && dest instanceof HTMLElement) {
+ dest.focus();
+ return true;
+ }
+ return false;
+ },
+
+ moveTask: function (offset: number) {
+ const active = this.currentTask();
+ if (!active) return;
+ const dest = this.visibleTaskAtOffset(active, offset);
+ if (dest === active) return; // Already extremal
+ var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset));
+ if (onePastDest == dest) onePastDest = null; // Will become extremal
+ if (offset > 0) {
+ this.setPriority(active, dest, onePastDest);
+ } else {
+ this.setPriority(active, onePastDest, dest);
+ }
+ },
+
+ removeTag: function () {
+ const target = this.currentTag();
+ if (!target) return;
+ ui.removeTag(target.parentElement!.getAttribute("id")!, target.textContent!);
+ },
+
+ resetTagView: function () {
+ currentTagView = null;
+ const taskList = document.getElementById("tasks")!;
+ for (const task of Array.from(document.getElementsByClassName("task"))) {
+ task.classList.remove("hide");
+ if (task.parentElement !== taskList) {
+ model.insertInPriorityOrder(task, taskList);
+ }
+ }
+ },