forked from u-unknown-developers/frontend-goodies
/
main.js
136 lines (119 loc) · 3.98 KB
/
main.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
"use strict";
import "./assets/css/style.css";
import "./assets/css/queries.css";
import data from "./data/data.json";
const { goodies, topics, stacks } = data;
const goodiesContainer = document.querySelector(".cards-container ul");
const searchInput = document.querySelector("#search");
const topicSelect = document.querySelector("#topic");
const stackSelect = document.querySelector("#stack");
const navModeMoonIcon = document.querySelector(".dark");
const navModeSunIcon = document.querySelector(".light");
const navModeBtn = document.querySelector(".nav-mode");
window.addEventListener("DOMContentLoaded", displayGoodies);
window.addEventListener("DOMContentLoaded", addTopics);
window.addEventListener("DOMContentLoaded", addStacks);
window.addEventListener("DOMContentLoaded", setCurrentTheme);
searchInput.addEventListener("input", handleSearch);
topicSelect.addEventListener("change", handleTopicSelect);
stackSelect.addEventListener("change", handleStackSelect);
navModeBtn.addEventListener("click", toggleTheme);
navModeSunIcon.addEventListener("click", toggleTheme);
function displayGoodies() {
goodiesContainer.innerHTML = goodies
.map((item) => {
return `
<li class="card">
<a href="${item.link}" target="_blank" rel="noopener noreferrer">
<div class="card-img">
<img src="${item.img}" alt="${item.title} thumbnail" width="300px">
</div>
<div class="card-data">
<h2 class="card-title">${item.title}</h2>
<p class="card-description">${item.description}</p>
</div>
</a>
</li>
`;
})
.join("");
}
// creates option in specific select element
function createOption(value, parentSelect) {
const option = document.createElement("option");
option.value = value;
option.textContent = value;
parentSelect.appendChild(option);
}
function addTopics() {
topics.forEach((topic) => {
createOption(topic, topicSelect);
});
}
function addStacks() {
stacks.forEach((stack) => {
createOption(stack, stackSelect);
});
}
function handleSearch() {
const searchTerm = searchInput.value.trim().toLowerCase();
const goodiesCards = document.querySelectorAll(".card");
const goodiesSearchStrings = goodies.map((item) => {
return [
item.title,
item.description,
item.topic,
item.stack,
item.keywords.join(""),
]
.join("")
.toLowerCase();
});
goodiesSearchStrings.forEach((item, index) => {
if (item.includes(searchTerm)) {
goodiesCards[index].style.display = "";
} else {
goodiesCards[index].style.display = "none";
}
});
}
function handleTopicSelect() {
const goodiesTopic = goodies.map((item) => item.topic);
const goodiesCards = document.querySelectorAll(".card");
const topicSelectValue = topicSelect.value.toLowerCase();
goodiesTopic.forEach((item, index) => {
if (item === topicSelectValue || topicSelectValue === "all") {
goodiesCards[index].style.display = "";
} else {
goodiesCards[index].style.display = "none";
}
});
}
function handleStackSelect() {
const goodiesStack = goodies.map((item) => item.stack);
const goodiesCards = document.querySelectorAll(".card");
const stackSelectValue = stackSelect.value.toLowerCase();
goodiesStack.forEach((item, index) => {
if (item === stackSelectValue || stackSelectValue === "all") {
goodiesCards[index].style.display = "";
} else {
goodiesCards[index].style.display = "none";
}
});
}
function toggleTheme(e) {
const iconName = e.target.classList[0];
document.documentElement.setAttribute("data-theme", iconName);
localStorage.setItem("theme", iconName);
}
//sets the theme to the local storage theme or default system theme
function setCurrentTheme() {
const storedTheme =
localStorage.getItem("theme") ||
(window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light");
if (storedTheme === "dark") {
document.documentElement.setAttribute("data-theme", "dark");
}
}