/
index.js
165 lines (161 loc) · 4.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const CH_LEN = 62;
const genId = length => {
let result = [];
for (let i = 0; i < length; i++) {
result.push(CHARS.charAt(Math.floor(Math.random() * CH_LEN)));
}
return result.join``;
};
class Model {
constructor() {
this.tasks = JSON.parse(localStorage.getItem("tasks")) || [];
}
bindListChange(cb) {
this.onListChange = cb;
}
_update(tasks) {
this.onListChange(tasks);
localStorage.setItem("tasks", JSON.stringify(tasks));
}
add({ task }) {
const todo = {
id: genId(8),
task,
completed: false,
deleted: false
};
this.tasks.push(todo);
this._update(this.tasks);
}
completed({ id }) {
let idx = this.tasks.findIndex(task => task.id === id);
this.tasks[idx] = {
...this.tasks[idx],
completed: !this.tasks[idx].completed
};
this._update(this.tasks);
}
delete({ id }) {
let idx = this.tasks.findIndex(task => task.id === id);
if (idx > -1) {
this.tasks.splice(idx, 1);
}
this._update(this.tasks);
}
}
class View {
constructor() {
this.comp = this.getElement("#root");
this.comp.classList.add("comp-wrapper");
this.title = this.createElement("h3", "title-text");
this.title.innerText = "Reminders";
this.form = this.createElement("form", "task-input-wrapper");
this.form.setAttribute("autocomplete", "off");
this.input = this.createElement("input");
this.input.setAttribute("autocomplete", "none");
this.input.type = "text";
this.input.name = genId(12);
this.formBtn = this.createElement("button");
this.formBtn.innerText = "Add";
this.formBtn.type = "submit";
this.form.append(this.input, this.formBtn);
this.listWrapper = this.createElement("div", "task-list-wrapper");
this.list = this.createElement("ul", "task-list");
this.listWrapper.append(this.list);
this.main = this.createElement("div", "main-card");
this.main.append(this.title, this.form, this.listWrapper);
this.comp.append(this.main);
}
createElement(tag, className) {
const element = document.createElement(tag);
if (className) element.classList.add(className);
return element;
}
getElement(selector) {
return document.querySelector(selector);
}
displayList(tasks = []) {
while (this.list.firstChild) {
this.list.removeChild(this.list.firstChild);
}
if (tasks.length > 0) {
tasks.forEach(task => {
const li = this.createElement("li", "task-item");
li.id = task.id;
const text = this.createElement("span", "task-item-text");
if (task.completed === true) {
let strike = this.createElement("s");
strike.innerText = task.task;
text.append(strike);
} else {
text.append(task.task);
}
const actions = this.createElement("span", "task-item-actions");
actions.id = task.id;
const compBtn = this.createElement("span", "complete");
compBtn.innerText = task.completed
? "Mark as incompleted"
: "Mark as completed";
const delBtn = this.createElement("span", "delete");
delBtn.innerText = "Del";
actions.append(compBtn, delBtn);
li.append(text, actions);
this.list.appendChild(li);
});
} else {
let noTaskText = this.createElement("p", "no-tasks");
noTaskText.innerText = "Create a new reminder!";
this.list.append(noTaskText);
}
}
bindAddTodo(handler) {
this.form.addEventListener("submit", e => {
e.preventDefault();
if (this.input.value !== "") {
handler({ task: this.input.value });
this.input.value = "";
}
});
}
bindCompleteTodo(handler) {
this.list.addEventListener("click", e => {
if (e.target.className === "complete") {
let id = e.target.parentElement.id;
handler({ id });
}
});
}
bindDeleteTodo(handler) {
this.list.addEventListener("click", e => {
if (e.target.className === "delete") {
let id = e.target.parentElement.id;
handler({ id });
}
});
}
}
class TodoMvc {
constructor(model, view) {
this.model = model;
this.view = view;
this.model.bindListChange(this.onListChange);
this.view.bindAddTodo(this.handleAdd);
this.view.bindCompleteTodo(this.handleComplete);
this.view.bindDeleteTodo(this.handleDelete);
this.onListChange(this.model.tasks);
}
onListChange(tasks) {
this.view.displayList(tasks);
};
handleAdd(task){
this.model.add(task);
};
handleComplete(id) {
this.model.completed(id);
};
handleDelete(id) {
this.model.delete(id);
};
}
new TodoMvc(new Model(), new View());