-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
380 lines (362 loc) · 13.6 KB
/
app.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
const promise = document.querySelector(".promise");
const input = document.querySelector("#input");
const submit = document.querySelector("#add");
const mainDiv = document.querySelector(".promisesCont");
const svgIcon = document.querySelector(".icon");
const removeAll = document.querySelector(".removeAll");
const aboutMe = document.querySelector(".aboutMe");
const settings = document.querySelector(".settings");
const gearHolder = document.querySelector(".gearIcon");
const gearIcon = document.querySelector(".gearIcon i");
const appThemes = document.querySelector(".settings .themes");
const themes = document.querySelectorAll("ul li");
const themeBox = document.querySelector("ul");
//register the service worker
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker.register("./serviceWorker.js").then((res) => console.log(`service worker register`)).catch((err) => {
console.log(`service worker is not registered:${err}`)
})
})
}
//empty array to store the promise
let arrOfPromises = [];
//check if there is theme in LS , if it's exist then call handelThemes()
if (localStorage.getItem("theme")) {
handelThemes(JSON.parse(localStorage.getItem("theme")));
}
// if there is themeName in Ls then call the handelActive()
if (localStorage.getItem("themeName")) {
handelActive();
}
//check if there is data from local storage
// if there is data then assign the data to the arr Of promises
// then call showRemove() , which
if (localStorage.getItem("promise")) {
arrOfPromises = JSON.parse(localStorage.getItem("promise"));
showRemove();
}
// get the data form local storge
getDataFromLs();
//when click on "add" button
submit.addEventListener("click", (e) => {
if (input.value !== "") {
addPromiseToTheArr(input.value);
addPromiseToPage();
}
input.value = "";
// minpulate remove All button
showRemove();
mainDiv.style.display = "block";
});
//add the input value to the array of objects
function addPromiseToTheArr(inputValue) {
const promise = {
id: Date.now(),
title: inputValue,
checked: false,
};
arrOfPromises.push(promise);
addPromiseToLocalStorge(arrOfPromises);
}
//add the promise to local storge
function addPromiseToLocalStorge(data) {
localStorage.setItem("promise", JSON.stringify(data));
}
//when click on promise div to remove the task or to toggle it as done
mainDiv.addEventListener("click", (e) => {
if (e.target.parentElement.classList.contains("remove")) {
removePWith(
e.target.parentElement.parentElement.parentElement.getAttribute("p-id")
);
e.target.parentElement.parentElement.parentElement.remove();
}
if (
e.target.parentElement.parentElement.classList.contains("promiseFromInput")
) {
togglePromiseWith(
e.target.parentElement.parentElement.getAttribute("p-id")
);
e.target.parentElement.parentElement.classList.toggle("done");
}
});
// show the promise at the main Div
function addPromiseToPage() {
mainDiv.innerHTML = "";
arrOfPromises.forEach((promise) => {
//create div with class ".promise" and contain the input.value and set att with the id
let pName = document.createElement("div");
pName.className = "promiseFromInput";
pName.setAttribute("p-id", promise.id);
//create pragraph to hold the input value
let prg = document.createElement("p");
let pCont = document.createTextNode(promise.title);
prg.appendChild(pCont);
pName.appendChild(prg);
mainDiv.appendChild(pName);
//create remove button
let remove = document.createElement("span");
remove.className = "remove";
let icon = document.createElement("i");
icon.className = "fa-solid fa-trash";
icon.setAttribute("role", "button");
remove.appendChild(icon);
//create checkbox for done
let checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.className = "checkbox";
checkbox.setAttribute("role", "button");
if (promise.checked == true) {
checkbox.setAttribute("checked", "checked");
pName.classList.add("done");
}
//create div to hold trash and checkbox
let holder = document.createElement("div");
holder.className = "holder";
holder.appendChild(remove);
holder.appendChild(checkbox);
pName.appendChild(holder);
//create custom checkbox
let customCheckbox = document.createElement("span");
customCheckbox.className = "checkmark";
holder.appendChild(customCheckbox);
//create date to show it when user add a promise
let dateHolder = document.createElement("div");
dateHolder.className = "date";
let date = new Date(promise.id);
date = date.toString();
// the format would be like : 8-3-2023
// the date output as : Thu Aug 03 2023 14:26
let day = date.substring(8, 10); //03
let month = date.substring(4, 7); // aug
let year = date.substring(11, 15); // 2023
let time = date.substring(16, 21); // 14:26
dateHolder.appendChild(
document.createTextNode(`${day}-${month}-${year}- ${time}`)
);
pName.appendChild(dateHolder);
});
}
//remove promise with id
function removePWith(promiseId) {
arrOfPromises = arrOfPromises.filter((e) => {
return e.id != promiseId;
});
addPromiseToLocalStorge(arrOfPromises);
showRemove();
}
//toggle promis With Promise id
function togglePromiseWith(promiseId) {
for (let i = 0; i < arrOfPromises.length; i++) {
if (arrOfPromises[i].id == promiseId) {
if (arrOfPromises[i].checked == false) {
arrOfPromises[i].checked = true;
} else {
arrOfPromises[i].checked = false;
}
}
}
addPromiseToLocalStorge(arrOfPromises);
}
// get Data from local Storge
// important : till now i dont see reason to use this function while you can replace it with the line 9 & 10;
function getDataFromLs() {
let data = localStorage.getItem("promise");
if (data) {
let parseData = JSON.parse(data);
addPromiseToPage(parseData);
}
}
// show and hide the input field and the add button when click on the svgIcon
svgIcon.onclick = function () {
if (!promise.classList.contains("show")) {
promise.classList.add("show");
} else promise.classList.remove("show");
};
// show and hide remove based on the array of promises length
function showRemove() {
if (arrOfPromises.length >= 2) {
removeAll.classList.add("showRemove");
removeAll.classList.add("rotate");
} else {
removeAll.classList.remove("showRemove");
}
}
//when click on remove All button
removeAll.addEventListener("click", (e) => {
//minpulate remove all
e.target.classList.remove("showRemove");
//create Overlay
let overlay = document.createElement("div");
overlay.className = "overlay";
document.body.appendChild(overlay);
//create popup to set yes or not to remove locacl storge
let popup = document.createElement("div");
popup.className = "popup";
let span = document.createElement("span");
span.className = "popupTitle";
span.appendChild(document.createTextNode("Are You Sure?"));
popup.appendChild(span);
let yes = document.createElement("span");
yes.className = "yes";
yes.appendChild(document.createTextNode("Yes"));
let no = document.createElement("span");
no.className = "no";
no.appendChild(document.createTextNode("No"));
//create holder for yes and no
let holderYandNo = document.createElement("div");
holderYandNo.className = "holderYandNo";
holderYandNo.appendChild(yes);
holderYandNo.appendChild(no);
popup.appendChild(holderYandNo);
document.body.appendChild(popup);
holderYandNo.addEventListener("click", (e) => {
if (e.target.classList.contains("yes")) {
localStorage.removeItem("promise");
arrOfPromises = [];
mainDiv.style.display = "none";
popup.style.display = "none";
overlay.style.display = "none";
} else {
if (e.target.classList.contains("no")) {
popup.style.display = "none";
overlay.style.display = "none";
removeAll.classList.add("showRemove");
}
}
});
});
// show year on about Me section
function showYearOnAboutSec() {
let aboutYear = document.createElement("span");
aboutYear.className = "year";
let date = new Date().getFullYear();
date = date.toString();
aboutYear.appendChild(document.createTextNode(date));
aboutMe.appendChild(aboutYear);
}
showYearOnAboutSec();
//add theme to local storge
function addThemeToLs(colorone, colortwo, threeColor, forColor) {
const theme = {
mainColor: colorone,
secColro: colortwo,
thrColor: threeColor,
forColor: forColor,
};
localStorage.setItem("theme", JSON.stringify(theme));
}
//handel Themes
themes.forEach((theme) => {
//handle class active
theme.addEventListener("click", (e) => {
themes.forEach((theme) => {
theme.classList.remove("active");
});
e.currentTarget.classList.add("active");
});
//change root value when click
theme.addEventListener("click", (e) => {
if (e.target.classList.contains("themeOne")) {
document.documentElement.style.setProperty("--main-color", "#ececec");
document.documentElement.style.setProperty("--sec-color", "#9fd3c7");
document.documentElement.style.setProperty("--thr-color", "#385170");
document.documentElement.style.setProperty("--for-color", "#142d4c");
addThemeToLs(
document.documentElement.style.getPropertyValue("--main-color"),
document.documentElement.style.getPropertyValue("--sec-color"),
document.documentElement.style.getPropertyValue("--thr-color"),
document.documentElement.style.getPropertyValue("--for-color")
);
setDatAtrrTols(e.currentTarget.dataset.themename);
// handelThemes(JSON.parse(localStorage.getItem("theme")));
}
if (e.target.classList.contains("themeTwo")) {
document.documentElement.style.setProperty("--main-color", "#c4bbf0");
document.documentElement.style.setProperty("--sec-color", "#927fbf");
document.documentElement.style.setProperty("--thr-color", "#4f3b78");
document.documentElement.style.setProperty("--for-color", "#363b4e");
// handelThemes(JSON.parse(localStorage.getItem("theme")));
addThemeToLs(
document.documentElement.style.getPropertyValue("--main-color"),
document.documentElement.style.getPropertyValue("--sec-color"),
document.documentElement.style.getPropertyValue("--thr-color"),
document.documentElement.style.getPropertyValue("--for-color")
);
setDatAtrrTols(e.currentTarget.dataset.themename);
}
if (e.target.classList.contains("themeThree")) {
document.documentElement.style.setProperty("--main-color", "#fdf3f3");
document.documentElement.style.setProperty("--sec-color", "#f8e7e7");
document.documentElement.style.setProperty("--thr-color", "#a070a1");
document.documentElement.style.setProperty("--for-color", "#724060");
addThemeToLs(
document.documentElement.style.getPropertyValue("--main-color"),
document.documentElement.style.getPropertyValue("--sec-color"),
document.documentElement.style.getPropertyValue("--thr-color"),
document.documentElement.style.getPropertyValue("--for-color")
);
setDatAtrrTols(e.currentTarget.dataset.themename);
}
if (e.target.classList.contains("themeFour")) {
document.documentElement.style.setProperty("--main-color", "#f2f2f2");
document.documentElement.style.setProperty("--sec-color", "#ebd5d5");
document.documentElement.style.setProperty("--thr-color", "#ea8a8a");
document.documentElement.style.setProperty("--for-color", "#685454");
addThemeToLs(
document.documentElement.style.getPropertyValue("--main-color"),
document.documentElement.style.getPropertyValue("--sec-color"),
document.documentElement.style.getPropertyValue("--thr-color"),
document.documentElement.style.getPropertyValue("--for-color")
);
setDatAtrrTols(e.currentTarget.dataset.themename);
}
if (e.target.classList.contains("themeFive")) {
document.documentElement.style.setProperty("--main-color", "#a3f7bf");
document.documentElement.style.setProperty("--sec-color", "#393e46");
document.documentElement.style.setProperty("--thr-color", "#222831");
document.documentElement.style.setProperty("--for-color", "#29a19c");
addThemeToLs(
document.documentElement.style.getPropertyValue("--main-color"),
document.documentElement.style.getPropertyValue("--sec-color"),
document.documentElement.style.getPropertyValue("--thr-color"),
document.documentElement.style.getPropertyValue("--for-color")
);
setDatAtrrTols(e.currentTarget.dataset.themename);
}
//handle class active
});
});
//handel gear icon
gearIcon.onclick = function () {
this.classList.toggle("fa-spin");
settings.classList.toggle("showSettings");
gearHolder.classList.toggle("show");
};
//handel themes
function handelThemes(theme) {
//change root values
document.documentElement.style.setProperty("--main-color", theme.mainColor);
document.documentElement.style.setProperty("--sec-color", theme.secColro);
document.documentElement.style.setProperty("--thr-color", theme.thrColor);
document.documentElement.style.setProperty("--for-color", theme.forColor);
}
//get theme form local storge
function getThemeFromls() {
if (true) {
JSON.parse(localStorage.getItem("theme"));
}
}
// set themeName to local storge
function setDatAtrrTols(data) {
localStorage.setItem("themeName", data);
}
// handel class active
function handelActive() {
themes.forEach((th) => {
th.classList.remove("active");
if (th.dataset.themename === localStorage.getItem("themeName")) {
th.classList.add("active");
}
});
}