-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
176 lines (159 loc) · 5.14 KB
/
main.js
File metadata and controls
176 lines (159 loc) · 5.14 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
let main = document.querySelector('main');
let body = document.querySelector('body');
let themeToggle = document.querySelector('.themeToggle');
let btn = document.querySelectorAll('.btn');
let scrollToTop = document.querySelector('.scrollToTop');
let id = 0;
const modal = document.querySelector('.modal');
const dim = document.querySelector('.dim');
const xBtn = modal.querySelector('svg');
const noBtn = document.querySelector('.noBtn');
const yesBtn = document.querySelector('.yesBtn');
fetch('data.json')
.then(response => response.json())
.then(data => {
data.forEach(tool => {
let container = document.createElement('div');
container.innerHTML = `
<div class='pic'>
<img alt='${tool.name} logo' src='${tool.logo}'/>
</div>
<div class='data'>
<h3>${tool.name}</h3>
<p style='font-size: 14px;' class='pa'>${tool.description}</p>
</div>
<button class="removeBtn">Remove</button>
<label class="switch">
<input aria-label="checkbox" type="checkbox" class='checkboxBtn'>
<span class="slider round"></span>
</label>
`
container.className = 'container'
container.id = id;
id++;
container.setAttribute('data-toggled', false)
main.appendChild(container)
});
})
.catch(error => console.error('Error loading JSON:', error));
function changeTheme() {
const current = document.body.getAttribute('data-theme');
const next = current === 'dark' ? 'light' : 'dark';
document.body.setAttribute('data-theme', next);
localStorage.setItem('theme', next)
}
function hideModal() {
modal.classList.remove('modalShown');
dim.classList.remove('modalShown');
}
dim.addEventListener('click', () => {
hideModal();
})
xBtn.addEventListener('click', () => {
hideModal();
})
noBtn.addEventListener('click', () => {
hideModal();
})
document.addEventListener('click', (e) => {
const btn = e.target.closest('.removeBtn');
if (btn) {
modal.classList.add('modalShown');
dim.classList.add('modalShown');
yesBtn.addEventListener('click', () => {
btn.parentElement.remove();
const container = document.querySelectorAll('.container');
handleEmptyMessage(container);
hideModal();
})
}
});
function handleEmptyMessage(container) {
const visibleContainers = [...container].filter(cont => !cont.classList.contains('hidden'));
const message = document.getElementById('emptyMessage');
if (visibleContainers.length === 0) {
message.classList.remove('hidden');
} else {
message.classList.add('hidden');
}
}
document.addEventListener('click', (e) => {
const btn = e.target.closest('.checkboxBtn');
if (btn) {
const label = btn.closest('label');
const container = label.closest('.container');
if(container) {
const current = container.getAttribute('data-toggled');
container.setAttribute('data-toggled', current === 'true' ? 'false' : 'true');
setLocalStorageToggle();
}
}
});
function handleToggle(cont, op) {
cont.getAttribute('data-toggled') === op ? cont.classList.add('hidden') : cont.classList.remove('hidden');
}
btn.forEach((but) => {
but.addEventListener('click', () => {
const container = document.querySelectorAll('.container');
let filter = but.getAttribute('data-filter');
if(but.classList.contains('toggled')) {
return
} else {
btn.forEach(b => b.classList.remove('toggled'))
but.classList.add('toggled')
container.forEach(cont => {
switch(filter) {
case 'active':
handleToggle(cont, 'false');
break;
case 'inactive':
handleToggle(cont, 'true');
break;
case 'all':
cont.classList.remove('hidden')
break;
}
})
handleEmptyMessage(container)
}
})
})
function setLocalStorageToggle() {
const container = document.querySelectorAll('.container');
container.forEach(cont => {
localStorage.setItem(`container-${cont.id}`, cont.getAttribute('data-toggled'));
})
}
function getLocalStorageToggle() {
const container = document.querySelectorAll('.container');
container.forEach(cont => {
const savedVisibility = localStorage.getItem(`container-${cont.id}`) || 'false';
cont.setAttribute('data-toggled', savedVisibility);
if(cont.getAttribute('data-toggled') === 'true') {
cont.querySelector('.checkboxBtn').checked = true;
} else {
cont.querySelector('.checkboxBtn').checked = false;
}
})
}
window.onscroll = () => {
if (document.body.scrollTop > 140 || document.documentElement.scrollTop > 140) {
scrollToTop.style.display = "flex";
} else {
scrollToTop.style.display = "none";
}
}
function scrollTopFunc() {
window.scrollTo({top: 0, behavior: 'smooth'});
}
window.addEventListener('DOMContentLoaded', () => {
const savedTheme = localStorage.getItem('theme') || 'light';
document.body.setAttribute('data-theme', savedTheme);
const observer = new MutationObserver(() => {
if (document.querySelector('.container')) {
getLocalStorageToggle();
observer.disconnect();
}
});
observer.observe(document.body, { childList: true, subtree: true });
})