forked from AquilaRegnum/demoToDo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
244 lines (197 loc) · 6.2 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"use strict";
class ToDoApp {
constructor(element) {
this.todo = element;
this.todoItems = [];
this.#init();
}
#init() {
this.#findElements();
this.#addListeners();
this.toolbar.classList.add('dont-render');
}
#findElements() {
this.form = this.todo.querySelector(".todo__form");
this.input = this.form.querySelector(".form__input");
this.addButton = this.form.querySelector(".form__button");
this.list = this.todo.querySelector(".todo__list");
// Toolbar actions
this.toolbar = this.todo.querySelector(".todo__toolbar");
this.searchItem = document.getElementById('toolbar-search');
this.searchButton = document.getElementById('toolbar-search-button');
this.sortPrority = document.getElementById('toolbar-sort');
this.filterImportant = document.getElementById('filter-important');
this.filterDone = document.getElementById('filter-done');
}
#addListeners() {
this.form.addEventListener("submit", this.#handleFormSubmit.bind(this));
this.searchButton.addEventListener("click", this.#handleSearchClick.bind(this));
this.sortPrority.addEventListener("change", this.#handleSortSelect.bind(this));
this.filterImportant.addEventListener("click", this.#handleFilterClick.bind(this));
this.filterDone.addEventListener("click", this.#handleFilterClick.bind(this));
}
// Handlers
#handleFormSubmit(e) {
e.preventDefault();
const value = this.input.value;
this.input.value = "";
if (value.trim().length === 0) return;
this.#createListItem(value);
this.toolbar.classList.remove('dont-render');
}
#handleSearchClick(e) {
const input_value = this.searchItem.value;
this.todoItems.forEach(tdi => {
tdi.reference.classList.remove("dont-render");
});
if (input_value !== "") {
this.todoItems.forEach(tdi => {
if (!tdi.name.includes(input_value)) {
tdi.reference.classList.add("dont-render");
}
});
}
}
#handleSortSelect(e) {
let sorter;
switch (this.sortPrority.value) {
case "no":
sorter = new SorterByDate(this.todoItems, this.list);
break;
case "important":
sorter = new SorterByImportant(this.todoItems, this.list);
break;
case "done":
sorter = new SorterByDone(this.todoItems, this.list);
break;
default:
sorter = new SorterByDate(this.todoItems, this.list);
break;
}
sorter.sort();
}
#handleFilterClick () {
this.todoItems.forEach(tdi => {
tdi.reference.classList.remove("dont-render");
if (this.filterImportant.checked === true && tdi.important !== this.filterImportant.checked) {
tdi.reference.classList.add("dont-render");
}
if (this.filterDone.checked === true && tdi.done !== this.filterDone.checked) {
tdi.reference.classList.add("dont-render");
}
});
}
// End handlers
#createListItem(value) {
const newItem = new ToDoItem(value, this);
this.todoItems.push(newItem);
this.list.appendChild(newItem.reference);
}
deleteItem(t) {
t.reference.remove();
this.todoItems.splice(this.todoItems.indexOf(t), 1);
if (this.todoItems.length <= 0) {
this.toolbar.classList.add('dont-render');
}
}
}
class ToDoItem {
constructor(name, app_instance) {
this.name = name;
this.date = new Date();
this.app = app_instance;
this.important = false;
this.done = false;
const listItem = document.createElement("li");
listItem.classList.add("list__item");
const textSpan = document.createElement("span");
textSpan.classList.add("list__text");
textSpan.textContent = name;
this.importantButton = this.#createButton(
"list__button_important",
"Важно"
);
this.importantButton.addEventListener("click", this.#switchImportant.bind(this));
this.completeButton = this.#createButton(
"list__button_complete",
"Выполнить"
);
this.completeButton.addEventListener("click", this.#switchDone.bind(this));
this.deleteButton = this.#createButton("list__button_delete", "Удалить");
this.deleteButton.addEventListener("click", this.#removeButton.bind(this));
listItem.append(textSpan, this.importantButton, this.completeButton, this.deleteButton);
this.reference = listItem;
}
#switchImportant() {
this.important = !this.important;
this.reference.classList.toggle("list__item_important");
this.importantButton.innerText = this.important === true ? "Не важно" : "Важно";
}
#createButton(buttonClass, buttonText) {
const button = document.createElement("button");
button.classList.add("list__button", buttonClass);
button.textContent = buttonText;
return button;
}
#switchDone() {
this.done = !this.done;
this.reference.classList.toggle("list__item_completed");
this.completeButton.innerText =
this.done === true ? "Не выполнено" : "Выполнить";
}
#removeButton() {
this.app.deleteItem(this);
}
}
// Sort Logic
class Sorter {
constructor(obj, dom) {
this.obj = obj;
this.dom = dom;
}
sort() {
let listItems = Array.from(this.dom.children);
listItems.sort((a,b) => {
const indexA = this.obj.findIndex(item => item.reference === a);
const indexB = this.obj.findIndex(item => item.reference === b);
return indexA - indexB;
});
listItems.forEach(item => this.dom.appendChild(item));
}
}
class SorterByDate extends Sorter {
sort() {
this.obj.sort((c1,c2) => c1.date.getTime() - c2.date.getTime())
super.sort()
}
}
class SorterByImportant extends Sorter {
sort() {
this.obj.sort((c1,c2) => {
if (c1.important && !c2.important) {
return -1;
} else if (!c1.important && c2.important) {
return 1;
} else {
return 0;
}
});
super.sort()
}
}
class SorterByDone extends Sorter {
sort() {
this.obj.sort((c1,c2) => {
if (c1.done && !c2.done) {
return -1;
} else if (!c1.done && c2.done) {
return 1;
} else {
return 0;
}
});
super.sort()
}
}
// Main Flow
let app = new ToDoApp(document.querySelector(".todo"));