-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
224 lines (183 loc) · 6.46 KB
/
script.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
let allFilters = document.querySelectorAll(".filter div");
let modalVisible = false;
let addBtn = document.querySelector(".add");
let body = document.querySelector("body");
let grid = document.querySelector(".grid");
let uid = new ShortUniqueId();
let colors = {
pink: "#d595aa",
blue: "#5ecdde",
green: "#91e6c7",
black: "black",
};
let colorClasses = ["pink", "blue", "green", "black"];
let deleteState = false;
let deleteBtn = document.querySelector(".delete");
//initialisation step
if (!localStorage.getItem("tasks")) {
localStorage.setItem("tasks", JSON.stringify([]));
}
deleteBtn.addEventListener("click", function (e) {
if (deleteState) {
deleteState = false;
e.currentTarget.classList.remove("delete-state");
} else {
deleteState = true;
e.currentTarget.classList.add("delete-state");
}
});
addBtn.addEventListener("click", function () {
if (modalVisible) return;
if (deleteBtn.classList.contains("delete-state")) {
deleteState = false;
deleteBtn.classList.remove("delete-state");
}
let modal = document.createElement("div");
modal.classList.add("modal-container");
modal.setAttribute("click-first", true);
modal.innerHTML = ` <div class="writing-area" contenteditable>Enter Your Task</div>
<div class="filter-area">
<div class="modal-filter pink"></div>
<div class="modal-filter blue"></div>
<div class="modal-filter green"></div>
<div class="modal-filter black active-modal-filter"></div>
</div>`;
let allModalFilters = modal.querySelectorAll(".modal-filter");
for (let i = 0; i < allModalFilters.length; i++) {
allModalFilters[i].addEventListener("click", function (e) {
for (let j = 0; j < allModalFilters.length; j++) {
allModalFilters[j].classList.remove("active-modal-filter");
}
e.currentTarget.classList.add("active-modal-filter");
});
}
let wa = modal.querySelector(".writing-area");
wa.addEventListener("click", function (e) {
if (modal.getAttribute("click-first") == "true") {
wa.innerHTML = "";
modal.setAttribute("click-first", false);
}
});
wa.addEventListener("keypress", function (e) {
if (e.key == "Enter") {
let task = e.currentTarget.innerText;
let selectedModalFilter = document.querySelector(".active-modal-filter");
let color = selectedModalFilter.classList[1];
let ticket = document.createElement("div");
let id = uid();
ticket.classList.add("ticket");
ticket.innerHTML = `<div class="ticket-color ${color}"></div>
<div class="ticket-id">#${id}</div>
<div class="ticket-box" contenteditable>
${task}
</div>
</div>`;
saveTicketInLocalStorage(id, color, task);
let ticketWritingArea = ticket.querySelector(".ticket-box");
ticketWritingArea.addEventListener("input", ticketWritingAreaHandler);
ticket.addEventListener("click", function (e) {
if (deleteState) {
let id = e.currentTarget
.querySelector(".ticket-id")
.innerText.split("#")[1];
let tasksArr = JSON.parse(localStorage.getItem("tasks"));
tasksArr = tasksArr.filter(function (el) {
return el.id != id;
});
localStorage.setItem("tasks", JSON.stringify(tasksArr));
e.currentTarget.remove();
}
});
let ticketColorDiv = ticket.querySelector(".ticket-color");
ticketColorDiv.addEventListener("click", ticketColorHandler);
grid.appendChild(ticket);
modal.remove();
modalVisible = false;
}
});
body.appendChild(modal);
modalVisible = true;
});
for (let i = 0; i < allFilters.length; i++) {
allFilters[i].addEventListener("click", function (e) {
let color = e.currentTarget.classList[0].split("-")[0];
grid.style.backgroundColor = colors[color];
});
}
function saveTicketInLocalStorage(id, color, task) {
let requiredObj = { id, color, task };
let tasksArr = JSON.parse(localStorage.getItem("tasks"));
tasksArr.push(requiredObj);
localStorage.setItem("tasks", JSON.stringify(tasksArr));
}
function ticketColorHandler(e) {
let id = e.currentTarget.parentElement
.querySelector(".ticket-id")
.innerText.split("#")[1];
let tasksArr = JSON.parse(localStorage.getItem("tasks"));
let reqIndex = -1;
for (let i = 0; i < tasksArr.length; i++) {
if (tasksArr[i].id == id) {
reqIndex = i;
break;
}
}
let currColor = e.currentTarget.classList[1];
let index = colorClasses.indexOf(currColor);
index++;
index = index % 4;
e.currentTarget.classList.remove(currColor);
e.currentTarget.classList.add(colorClasses[index]);
tasksArr[reqIndex].color = colorClasses[index];
localStorage.setItem("tasks", JSON.stringify(tasksArr));
}
function ticketWritingAreaHandler(e) {
let id = e.currentTarget.parentElement
.querySelector(".ticket-id")
.innerText.split("#")[1];
let tasksArr = JSON.parse(localStorage.getItem("tasks"));
let reqIndex = -1;
for (let i = 0; i < tasksArr.length; i++) {
if (tasksArr[i].id == id) {
reqIndex = i;
break;
}
}
tasksArr[reqIndex].task = e.currentTarget.innerText;
localStorage.setItem("tasks", JSON.stringify(tasksArr));
}
function loadTasks() {
let tasks = JSON.parse(localStorage.getItem("tasks"));
for (let i = 0; i < tasks.length; i++) {
let id = tasks[i].id;
let color = tasks[i].color;
let taskValue = tasks[i].task;
let ticket = document.createElement("div");
ticket.classList.add("ticket");
ticket.innerHTML = `<div class="ticket-color ${color}"></div>
<div class="ticket-id">#${id}</div>
<div class="ticket-box" contenteditable>
${taskValue}
</div>
</div>`;
let ticketWritingArea = ticket.querySelector(".ticket-box");
ticketWritingArea.addEventListener("input", ticketWritingAreaHandler);
let ticketColorDiv = ticket.querySelector(".ticket-color");
ticketColorDiv.addEventListener("click", ticketColorHandler);
ticket.addEventListener("click", function (e) {
if (deleteState) {
let id = e.currentTarget
.querySelector(".ticket-id")
.innerText.split("#")[1];
let tasksArr = JSON.parse(localStorage.getItem("tasks"));
tasksArr = tasksArr.filter(function (el) {
return el.id != id;
});
localStorage.setItem("tasks", JSON.stringify(tasksArr));
e.currentTarget.remove();
}
});
grid.appendChild(ticket);
}
}
loadTasks();