-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
716 lines (584 loc) · 20.8 KB
/
Copy pathapp.js
File metadata and controls
716 lines (584 loc) · 20.8 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import { rowData } from "./data.js";
const parseRowData = JSON.parse(rowData);
const loadingPage = document.querySelector(".startPage");
const hideOverlay = document.querySelector("#hide-overlay");
const hideWrapper = document.querySelector("#hide-wrapper");
const puzzleClose = document.querySelector("#puzzle-close");
const puzzleMod2 = document.querySelector("#puzzle-mod2");
const titlesBtn = document.querySelector("#titles-buttons");
const containerItem = document.querySelector(".container-item");
const modeTheme = document.querySelector(".toggleLight");
const darkTheme = document.querySelector("#darkness");
const lightTheme = document.querySelector("#lighter");
const searchMenu = document.querySelector(".searchBar");
const enigmaItem = document.querySelector(".puzzle-enigma");
const wraperTables = document.querySelector(".wrapperTable");
const tableContent = document.querySelector(".mainTableContainer");
const puzzleMod = document.querySelector("#puzzle-mod");
const mainTable = document.querySelector(".firstTable");
const deleteBtn = document.querySelector(".deleteChecked");
const nav = document.querySelector(".navigation");
const secondTable = document.querySelector("#second-table");
const audioEnigma = document.querySelector("#audio-enigma");
const displaySecondTable = document.querySelector("#display-second-table");
const title = "starwars";
let row = [];
let bodyItem = [];
let tableBodyItems = [];
let arrayOfName = [];
let lengthOfTableBody = 0;
let nameItem = [];
let currentPage = 1;
let startIndex = 0;
let lastIndex = 0;
let selectValue = 10;
let countPages = 0;
let cheatCodes = "";
function startMainPage() {
const titleArray = title.split("");
const titleList = document.createElement("ul");
titleList.className = "loadingText";
titleArray.forEach((item) => {
const rowItem = document.createElement("li");
rowItem.innerHTML = item;
titleList.appendChild(rowItem);
});
loadingPage.appendChild(titleList);
}
startMainPage();
setTimeout(() => {
loadingPage.style.opacity = "0";
}, 3000);
setTimeout(() => {
loadingPage.style.display = "none";
}, 5500);
function createTitleBtn() {
Object.keys(parseRowData).forEach((title) => {
const titleBtn = document.createElement("button");
titleBtn.className = "mainBtn";
titleBtn.innerHTML = title;
titlesBtn.appendChild(titleBtn);
});
}
createTitleBtn();
function createMainTable(e) {
tableContent.style.display = "flex";
wraperTables.style.flexDirection = "column";
if (!e.target.classList.contains("mainBtn")) return;
mainTable.innerHTML = "";
const titleHeadTable = e.target.innerHTML;
const titlesTableHeader = findTitleForHeader(titleHeadTable);
const valueOfObject = parseRowData[titleHeadTable];
secondTable.style.display = "none";
createTableHead(titlesTableHeader);
createTableBody(titlesTableHeader, titleHeadTable, valueOfObject);
}
function findTitleForHeader(titleHeadTable) {
const arrayOfTitleItem = Object.keys(parseRowData[titleHeadTable][0]);
const titlesForMainTable = [
"id",
...arrayOfTitleItem.slice(0, 3),
"created",
"actions",
];
secondTable.innerHTML = "";
return titlesForMainTable;
}
function createTableHead(titlesTableHeader) {
const headTable = document.createElement("thead");
const rowThead = document.createElement("tr");
rowThead.className = "headTable";
headTable.appendChild(rowThead);
mainTable.appendChild(headTable);
titlesTableHeader.forEach((titleItems) => {
const rowTheadItems = document.createElement("th");
rowTheadItems.textContent = titleItems;
rowThead.appendChild(rowTheadItems);
});
}
function createTableBody(titlesTableHeader, titleHeadTable, valueOfObject) {
const tableBody = document.createElement("tbody");
tableBody.id = "table-body";
mainTable.appendChild(tableBody);
nameItem = [];
const [id, title1, title2, title3, created, actions] = titlesTableHeader;
bodyItem = valueOfObject.map((valueItems, index) => {
index += 1;
nameItem.push(valueItems[title1]);
return `<tr class='itemTable' id=${index} data-name='${titleHeadTable}'>
<td>${index}</td>
<td data-name='${title1}'>${valueItems[title1]}</td>
<td>${valueItems[title2]}</td>
<td>${valueItems[title3]}</td>
<td>${createDate(valueItems[created])}</td>
<td>
<button class='addButton' data-title='${titleHeadTable}'>+</button>
<button class='removeButton'>
<img class='deleteIcon' src="./images/eraser-solid.svg" alt="Delete button">
<input type='checkbox'></button>
</td>
</tr>
`;
});
tableBodyItems = [...bodyItem];
searchMenu.innerHTML = "";
containerItem.style.display = "flex";
clear();
createFooterNav();
updateIndex();
createSearchMenu();
newPage();
}
function createDate(date) {
const now = new Date(date);
return `${now.getDate()}-${now.getMonth() + 1}-${now.getFullYear()}`;
}
function deleteButton(e) {
row.forEach((item) => item.remove());
deleteBtn.innerHTML = "";
row = [];
deleteBtn.style.display = "none";
}
function modifyItemsTable(e) {
if (
e.target.className === "removeButton" ||
e.target.className === "deleteIcon"
) {
const itemToRemove = e.target.closest("tr");
itemToRemove.remove();
}
if (e.target.tagName === "INPUT") {
createArrayOfDeleteItems(e.target);
}
createSecondTable(e);
}
function createArrayOfDeleteItems(tagName) {
const itemToRemove = tagName.closest("tr");
if (tagName.checked === true) {
row.push(itemToRemove);
} else {
const check = row.indexOf(itemToRemove);
if (check !== -1) {
row.splice(check, 1);
}
}
if (row.length > 0) {
deleteBtn.style.display = "flex";
deleteBtn.textContent = `Delete marked items?`;
} else {
deleteBtn.style.display = "none";
deleteBtn.textContent = "";
}
}
function createSecondTable(e) {
displaySecondTable.style.display = "flex";
if (e.target.tagName === "BUTTON" && e.target.dataset.title) {
secondTable.innerHTML = "";
closeTab(e);
const idItem = Number(e.target.closest("tr").id);
const secondeHeadKeys = ["Key", "Values"];
const headTable = document.createElement("thead");
const rowThead = document.createElement("tr");
rowThead.className = "secondTableRow";
headTable.appendChild(rowThead);
secondTable.appendChild(headTable);
secondeHeadKeys.forEach((titleKeys) => {
const rowTheadItems = document.createElement("th");
rowTheadItems.className = "secondThead";
rowTheadItems.textContent = titleKeys;
rowThead.appendChild(rowTheadItems);
});
const mainKey = e.target.dataset.title;
const singleValueOfItems = parseRowData[mainKey][idItem - 1];
const arraysOfKeyValues = Object.entries(singleValueOfItems);
arraysOfKeyValues.forEach((item) => {
let unpackArray;
const [name, value] = item;
if (value.length === 0) {
unpackArray = `NO DATA`;
} else if (typeof value === "object") {
unpackArray = value.join(" ");
} else {
unpackArray = value;
}
const tableBody = document.createElement("tbody");
secondTable.appendChild(tableBody);
tableBody.innerHTML += `
<tr class='secondTbody'>
<td>${name}</td>
<td>${unpackArray}</td>
</tr>
`;
});
const windowWith = window.innerWidth;
if (windowWith > 1100) {
wraperTables.style.flexDirection = "row";
}
secondTable.style.display = "block";
handleResize();
}
}
function closeTab() {
const close = document.createElement("div");
close.innerHTML = `<img src='./images/x-mark-256.png'>`;
close.className = "closeTab";
secondTable.appendChild(close);
const closeItem = document.querySelector(".closeTab");
closeItem.addEventListener("click", () => {
secondTable.style.display = "none";
secondTable.innerHTML = "";
displaySecondTable.classList.remove("overlaySecondTable");
displaySecondTable.style.display = "none";
document.body.style.overflow = "visible";
wraperTables.style.flexDirection = "column";
});
}
function handleResize() {
const windowWith = window.innerWidth;
if (windowWith <= 540) {
displaySecondTable.classList.add("overlaySecondTable", "flex");
document.body.style.overflow = "hidden";
} else {
displaySecondTable.classList.remove("overlaySecondTable", "flex");
document.body.style.overflow = "visible";
}
}
function clear() {
currentPage = 1;
startIndex = 0;
lastIndex = 0;
selectValue = 10;
countPages = 0;
}
function createFooterNav() {
const navItem = document.querySelector(".navigation");
navItem.innerHTML = "";
const arrowLeftBtn = document.createElement("button");
arrowLeftBtn.className = "arrowLeft";
arrowLeftBtn.id = "arrow-left";
arrowLeftBtn.innerHTML = `<img class='left' src='./images/arrow-left-long-solid.svg'>`;
navItem.appendChild(arrowLeftBtn);
const page = document.createElement("input");
page.id = "input-page";
page.value = "1";
page.min = "1";
page.type = "number";
navItem.appendChild(page);
const textInput = document.createElement("span");
textInput.id = "text-page";
textInput.innerHTML = "from 1";
navItem.appendChild(textInput);
const arrowRightBtn = document.createElement("button");
arrowRightBtn.className = "arrowRight";
arrowRightBtn.id = "arrow-right";
arrowRightBtn.innerHTML =
"<img class='right' src='./images/arrow-right-long-solid.svg'>";
navItem.appendChild(arrowRightBtn);
const selectItems = document.createElement("select");
selectItems.id = "select-countity";
navItem.appendChild(selectItems);
const option10 = document.createElement("option");
option10.innerHTML = "10";
selectItems.appendChild(option10);
const option20 = document.createElement("option");
option20.innerHTML = "20";
selectItems.appendChild(option20);
const arrowRight = document.querySelector(".right");
const arrowLeft = document.querySelector(".left");
const countityItems = document.querySelector("#select-countity");
arrowRight.addEventListener("click", handleArrowClick);
arrowLeft.addEventListener("click", handleArrowClick);
countityItems.addEventListener("change", changeCountity);
page.addEventListener("blur", changePage);
}
function handleArrowClick(e) {
e.stopPropagation();
pages();
const arrowRight = document.querySelector(".right");
const arrowLeft = document.querySelector(".left");
if (currentPage > 1 && e.target === arrowLeft) {
currentPage--;
}
if (e.target === arrowRight && currentPage < countPages) {
currentPage++;
}
deleteBtn.style.display = "none";
updateIndex();
newPage();
}
function changeCountity() {
const countityItems = document.querySelector("#select-countity");
const prevSelectValue = selectValue;
const currentValue = +countityItems.value;
if (currentValue === 20 && currentPage % 2 === 0) {
startIndex = (currentPage - 1 - 1) * prevSelectValue;
} else {
startIndex = (currentPage - 1) * prevSelectValue;
}
lastIndex = startIndex + currentValue;
selectValue = currentValue;
currentPage = Math.ceil((startIndex + 1) / selectValue);
displayTableBody();
newPage();
pages();
}
function changePage(e) {
const inputPage = document.querySelector("#input-page");
if (+inputPage.value > countPages) {
inputPage.value = countPages;
}
currentPage = +inputPage.value;
updateIndex();
}
function pages(itemlength = 0) {
const textPages = document.querySelector("#text-page");
if (itemlength > 0) {
lengthOfTableBody = itemlength;
} else {
lengthOfTableBody = tableBodyItems.length;
}
countPages = Math.ceil(lengthOfTableBody / selectValue);
if (countPages === 1) {
nav.classList.add("navigationDisabled");
} else {
nav.classList.remove("navigationDisabled");
}
textPages.innerHTML = `from ${countPages}`;
}
function updateIndex() {
startIndex = (currentPage - 1) * selectValue;
lastIndex = startIndex + selectValue;
displayTableBody();
pages();
}
function newPage() {
const inputPage = document.querySelector("#input-page");
inputPage.value = currentPage;
}
function displayTableBody() {
const mainTableBody = document.querySelector("#table-body");
mainTableBody.innerHTML = tableBodyItems
.slice(startIndex, lastIndex)
.join("");
}
function createSearchMenu() {
const divId = document.createElement("div");
divId.className = "searchId";
searchMenu.appendChild(divId);
const inputId = document.createElement("input");
inputId.type = "number";
inputId.dataset.input = "searchItem";
inputId.min = `1`;
inputId.max = `${tableBodyItems.length}`;
inputId.placeholder = `Search ID from 0 to ${tableBodyItems.length}`;
divId.appendChild(inputId);
const searchButtonOfItems = document.createElement("button");
searchButtonOfItems.id = "search-id";
searchButtonOfItems.innerHTML = "Search";
divId.appendChild(searchButtonOfItems);
const divName = document.createElement("div");
divName.className = "searchName";
searchMenu.appendChild(divName);
const valueOfName = document.querySelector("td[data-name]").dataset.name;
const findItemsByName = document.createElement("input");
findItemsByName.dataset.input = "searchName";
findItemsByName.placeholder = `Search by ${valueOfName}`;
divName.appendChild(findItemsByName);
const searchButtonOfName = document.createElement("button");
searchButtonOfName.id = "search-name";
searchButtonOfName.innerHTML = "Search";
divName.appendChild(searchButtonOfName);
modeTheme.style.display = "block";
}
function solvePuzzle(e) {
createPuzzle();
enigmaItem.style.opacity = "0";
const puzzleArea = document.querySelector("#puzzle-Input");
const puzzleText = document.querySelector("#puzzle-cod");
const puzzleBtn = document.querySelector("#puzzle-button");
if (e.target === enigmaItem || e.target === puzzleMod) {
if (!hideWrapper.classList.contains("activeTab")) {
puzzleText.innerHTML =
`<p class="alertSolve">Solve the puzzle in JavaScript and feel the power</p>
<p class="solve">"b" + "a" + + "a" + "a"</p>
`;
puzzleArea.style.display = "block";
puzzleBtn.style.display = "block";
puzzleBtn.innerHTML = "CHECK";
puzzleClose.style.display = "block";
puzzleMod2.style.display = "block";
hideOverlay.classList.add("overlay", "flex", "fixed");
hideWrapper.classList.add("activeTab", "flex");
puzzleBtn.style.opacity = "1";
puzzleArea.style.opacity = "1";
}
}
}
function createPuzzle() {
const puzzleItem = document.querySelector("#puzzle-cod");
if (!puzzleItem) {
createAlert();
const puzzleInput = document.createElement("input");
puzzleInput.type = "text";
puzzleInput.placeholder = "Your solution";
puzzleInput.id = "puzzle-Input";
puzzleInput.className = "puzzleInputClass";
hideWrapper.appendChild(puzzleInput);
const puzzleButton = document.createElement("button");
puzzleButton.id = "puzzle-button";
puzzleButton.className = "puzzleCheck";
hideWrapper.appendChild(puzzleButton);
const puzzleBtn = document.querySelector("#puzzle-button");
puzzleBtn.addEventListener("click", checkSolution);
}
}
function alertErrors() {
createAlert();
const puzzleText = document.querySelector("#puzzle-cod");
if (!hideWrapper.classList.contains("activeTab")) {
puzzleText.innerHTML = "<p class='alertData'>Submit correct data</p>";
puzzleClose.style.display = "block";
hideOverlay.classList.add("overlay", "flex", "fixed");
hideWrapper.classList.add("activeTab", "flex");
}
}
function createAlert() {
const textAlert = document.createElement("div");
textAlert.id = "puzzle-cod";
hideWrapper.appendChild(textAlert);
}
function checkSolution() {
const puzzleArea = document.querySelector("#puzzle-Input");
const puzzleText = document.querySelector("#puzzle-cod");
const puzzleBtn = document.querySelector("#puzzle-button");
if (puzzleArea.value.toLowerCase() === "banana") {
puzzleText.innerHTML = `<p class="alertSolve">WELL DONE !!!<br>Type on your keyboard:</p><p class='solve'>VADER</p>`;
puzzleArea.style.display = "none";
puzzleBtn.style.opacity = "0";
puzzleArea.style.opacity = "0";
window.addEventListener("keydown", checkCheatCodes);
} else {
puzzleText.innerHTML = "";
puzzleText.innerHTML = `<p class="alertSolve">WRONG ANSWER</p><p class='solve'>"b" + "a" + + "a" + "a"</p>`;
puzzleBtn.style.opacity = "1";
puzzleArea.style.opacity = "1";
}
}
function checkCheatCodes(e) {
cheatCodes += e.key.toLowerCase();
if (cheatCodes.includes("vader")) {
cheatCodes = "";
playAudio(audioEnigma);
}
}
function playAudio(sound) {
sound.currentTime = 0;
sound.play();
}
function closePuzzle() {
const puzzleArea = document.querySelector("#puzzle-Input");
const puzzleText = document.querySelector("#puzzle-cod");
const puzzleBtn = document.querySelector("#puzzle-button");
hideWrapper.classList.remove("activeTab", "flex");
hideOverlay.classList.remove("overlay", "flex", "fixed");
puzzleClose.style.display = "none";
puzzleText.remove();
if (enigmaItem.style.opacity === "0") {
enigmaItem.style.opacity = "1";
puzzleText.innerHTML = "";
puzzleMod2.style.display = "none";
puzzleArea.value = "";
puzzleBtn.remove();
puzzleArea.remove();
}
}
function findItems(e) {
const mainTableBody = document.querySelector("#table-body");
const itemOfsearchById = document.querySelector("#search-id");
const inputOfId = document.querySelector('input[data-input="searchItem"]');
const itemOfSearchByName = document.querySelector("#search-name");
const inputOfName = document.querySelector('input[data-input="searchName"]');
if (e.target === itemOfsearchById) {
if (inputOfId.value === "") return;
if (+inputOfId.value > bodyItem.length || +inputOfId.value < 1) {
alertErrors();
return;
}
mainTableBody.innerHTML = findItemsOfId(inputOfId);
secondTable.style.display = "none";
}
if (e.target === itemOfSearchByName) {
if (inputOfName.value === "") return;
const content = findItemsOfName(inputOfName);
if (content.length === 0) {
alertErrors();
return;
}
secondTable.style.display = "none";
}
}
function findItemsOfId(inputOfId) {
const valueOfId = +inputOfId.value;
const findItem = [bodyItem[valueOfId - 1]];
pages(findItem.length);
clear();
newPage();
inputOfId.value = "";
return findItem;
}
function findItemsOfName(inputOfName) {
const valueOfName = inputOfName.value.toLowerCase();
const findName = nameItem.filter((nameElement) => {
return nameElement.toLowerCase().includes(valueOfName);
});
arrayOfName = [];
for (const item of findName) {
for (const name of bodyItem) {
if (name.toLowerCase().includes(item.toLowerCase())) {
arrayOfName.push(name);
}
}
}
if (arrayOfName.length >= 1) {
tableBodyItems = arrayOfName;
}
clear();
changeCountity();
inputOfName.value = "";
return arrayOfName.join("");
}
const bodyTheme = document.querySelector("body");
let theme = localStorage.getItem("theme");
modeTheme.addEventListener("click", () => {
if (theme === "dark") {
bodyTheme.classList.remove("dark");
lightTheme.style.display = "none";
darkTheme.style.display = "flex";
theme = "light";
} else {
bodyTheme.classList.add("dark");
theme = "dark";
lightTheme.style.display = "flex";
darkTheme.style.display = "none";
}
localStorage.setItem("theme", theme);
});
window.addEventListener("resize", () => {
const windowWith = window.innerWidth;
if (windowWith < 1100) {
wraperTables.style.flexDirection = "column";
} else {
wraperTables.style.flexDirection = "row";
}
});
if (theme === "dark") {
bodyTheme.classList.add("dark");
lightTheme.style.display = "flex";
darkTheme.style.display = "none";
}
titlesBtn.addEventListener("click", createMainTable);
mainTable.addEventListener("click", modifyItemsTable);
deleteBtn.addEventListener("click", deleteButton);
searchMenu.addEventListener("click", findItems);
enigmaItem.addEventListener("click", solvePuzzle);
puzzleClose.addEventListener("click", closePuzzle);