}
const Model = {
- createTask: function (description: string) {
+ createTask: function (timestamp: string, description: string) {
const task = document.createElement("div");
task.appendChild(document.createTextNode(description));
task.setAttribute("tabindex", "0");
+ task.setAttribute("data-created", timestamp);
return task;
},
- addTask: function (description: string) {
- document.body.appendChild(this.createTask(description)).focus();
+ addTask: function (timestamp: string, description: string) {
+ document.body.appendChild(this.createTask(timestamp, description)).focus();
},
- moveCursor: function (offset: number) {
+ moveCursor: function (offset: number): boolean {
var active = document.activeElement;
if (offset === 1 && active) {
active = active.nextElementSibling;
if (offset === -1 && active) {
active = active.previousElementSibling;
}
- if (active && active instanceof HTMLElement) active.focus();
+ if (active && active instanceof HTMLElement) {
+ active.focus();
+ return true;
+ }
+ return false;
},
};
apply: function (entry: string) {
const [timestamp, command, data] = splitN(entry, " ", 2);
if (command == "Create") {
- Model.addTask(data);
+ Model.addTask(timestamp, data);
}
},
},
};
+function focusTaskNameInput(event: any) {
+ document.getElementById("taskName")!.focus();
+ event.preventDefault();
+}
+
function handleKey(event: any) {
if (event.target.tagName !== "INPUT") {
- if (event.key == "j") Model.moveCursor(1);
- if (event.key == "k") Model.moveCursor(-1);
- if (event.key == "c") {
- document.getElementById("taskName")!.focus();
- event.preventDefault();
- }
+ if (event.key == "j") return Model.moveCursor(1);
+ if (event.key == "k") return Model.moveCursor(-1);
+ if (event.key == "c") return focusTaskNameInput(event);
}
}