Repository navigation
Expand file tree
/
Copy pathjs.js
More file actions
211 lines (182 loc) · 6.9 KB
/
Copy pathjs.js
File metadata and controls
211 lines (182 loc) · 6.9 KB
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
// главный чекбокс
var mainCheckbox = document.body.querySelector("#complete-all");
// настройки
var settingActive = document.body.querySelector(".setting-active");
var settingAll = document.body.querySelector(".setting-all");
var settingCompleted = document.body.querySelector(".setting-completed");
// главные элементы
var form = document.body.querySelector(".form")
var input = document.body.querySelector("#input");
function checkSetting() {
// При добавлении заметки, нужно проверить, какая группа выбрана
if (document.querySelector(".setting_selected")) {
var setting = document.querySelector(".setting_selected");
switch (setting.classList[0]) {
case "setting-active":
showActive();
break;
case "setting-completed":
showCompleted();
break;
default:
showAll();
break;
}
}
}
function showAll() {
// Показать все задачи
if (document.querySelector(".setting_selected")) {
document.querySelector(".setting_selected").classList.remove("setting_selected");
}
settingAll.classList.add("setting_selected");
var array = document.querySelectorAll(".note");
[].forEach.call(array, function(el) {
el.style.display = "block";
});
}
function showActive() {
// проказать активные задачи
if (document.querySelector(".setting_selected")) {
document.querySelector(".setting_selected").classList.remove("setting_selected");
}
settingActive.classList.add("setting_selected");
var array = document.querySelectorAll(".note");
[].forEach.call(array, function(el) {
el.style.display = "block";
});
array = document.querySelectorAll(".note_completed");
[].forEach.call(array, function(el) {
el.style.display = "none";
});
}
function showCompleted() {
//показать выполненные задачи
if (document.querySelector(".setting_selected")) {
document.querySelector(".setting_selected").classList.remove("setting_selected");
}
settingCompleted.classList.add("setting_selected");
var array = document.querySelectorAll(".note");
[].forEach.call(array, function(el) {
el.style.display = "none";
});
array = document.querySelectorAll(".note_completed");
[].forEach.call(array, function(el) {
el.style.display = "block";
});
}
function recreateNode() {
// проверяем текст в поле
if (this.value) {
// текст есть
var text = this.value;
var textNote = this.parentElement.querySelector(".note__text");
textNote.style.display = "";
textNote.textContent = text;
this.remove();
} else {
// текста нет
deleteNote.apply(this);
}
}
function editNote() {
// создаем input
var html = "<input type='text' class='note__input' value='" + this.textContent + "'>";
this.insertAdjacentHTML("beforeBegin", html);
this.style.display = "none";
this.parentElement.querySelector("input[type='text']").addEventListener("blur", recreateNode);
}
function completeAll(event) {
// Выполнить/Отменить выполнение всех задачи
if (this.disabled)
return;
var array = document.body.querySelectorAll(".note");
if (this.checked) {
for (var i = 0; i < array.length; i++) {
forceComplete.apply(array[i]);
}
checkSetting();
} else {
for (var i = 0; i < array.length; i++) {
forceUncomplete.apply(array[i]);
}
checkSetting();
}
}
function forceComplete() {
// Выполнить задачу
this.querySelector(".note__text").style.textDecoration = "line-through";
this.querySelector(".note__check").checked = true;
this.classList.add("note_completed");
}
function forceUncomplete() {
// Отменить выполнение задачи
this.querySelector(".note__text").style.textDecoration = "";
this.querySelector(".note__check").checked = false;
this.classList.remove("note_completed");
}
function completeNote() {
// щелкнут чекбокс задачи, выполнить/отменить задачу
if (this.checked) {
this.parentElement.querySelector(".note__text").style.textDecoration = "line-through";
this.parentElement.classList.add("note_completed");
} else {
this.parentElement.querySelector(".note__text").style.textDecoration = "";
this.parentElement.classList.remove("note_completed");
}
}
function deleteNote() {
// удалить задачу
this.parentElement.remove();
if (!document.body.querySelector(".note")) {
mainCheckbox.style.opacity = "0";
mainCheckbox.disabled = true;
}
}
function clearCompleted() {
// удалить выполненные
var array = document.body.querySelectorAll(".note_completed");
[].forEach.call(array, function(block) {
block.remove();
})
if (!document.body.querySelector(".note")) {
mainCheckbox.style.opacity = "0";
mainCheckbox.disabled = true;
}
}
function createNote(event) {
// создать задачу
if (event.keyCode != 13)
return;
if (input.value == "") {
console.log("заметка не создана");
} else {
var text = input.value;
var el = document.createElement("div");
el.innerHTML = `<input type="checkbox" class="note__check"><span class="note__text">${text}</span><span class="note__delete">x</span>`;
el.classList.add("note");
var checkbox = el.querySelector(".note__check");
checkbox.addEventListener("click", completeNote);
var deleteButton = el.querySelector(".note__delete");
deleteButton.addEventListener("click", deleteNote);
var noteText = el.querySelector(".note__text");
noteText.addEventListener("dblclick", editNote);
document.body.insertBefore(el, form.nextElementSibling)
console.log("заметка создана");
if (document.body.querySelector(".note")) {
mainCheckbox.style.opacity = "1";
mainCheckbox.disabled = false;
}
input.value = "";
checkSetting();
}
}
// основное текстовое поле
input.addEventListener("keypress", createNote);
// основной чекбокс для всех задач
mainCheckbox.addEventListener("click", completeAll);
settingActive.addEventListener("click", showActive);
settingAll.addEventListener("click", showAll);
settingCompleted.addEventListener("click", showCompleted);
var clearCompletedText = document.body.querySelector(".clear-completed");
clearCompletedText.addEventListener("click", clearCompleted);