forked from amyyalex/simple-contribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
232 lines (211 loc) · 7.39 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
window.addEventListener('DOMContentLoaded', () => {
let scrollPos = 0;
const mainNav = document.getElementById('navbar');
const headerHeight = mainNav.clientHeight;
window.addEventListener('scroll', function() {
const currentTop = document.body.getBoundingClientRect().top * -1;
if ( currentTop < scrollPos) {
// Scrolling Up
if (currentTop > 0 && mainNav.classList.contains('is-fixed')) {
mainNav.classList.add('is-visible');
} else {
console.log(123);
mainNav.classList.remove('is-visible', 'is-fixed');
}
} else {
// Scrolling Down
mainNav.classList.remove(['is-visible']);
if (currentTop > headerHeight && !mainNav.classList.contains('is-fixed')) {
mainNav.classList.add('is-fixed');
}
}
scrollPos = currentTop;
});
})
// initialization of cardDetailsArray
let cardDetailsArray;
const cardsPerPage = 10;
let noOfCards;
function createCard(details) {
const cardTemplate = document.getElementById("contributor-card");
const cardClone = cardTemplate.content.cloneNode(true);
cardClone.querySelector(".card-details h1").textContent = details.name;
cardClone.querySelector(".card-details h5").textContent = details.profession;
cardClone.querySelector(".card-details p").innerHTML = details.quote;
const socialIcons = cardClone.querySelector(".social-icon");
// Define the social media platforms and their corresponding icons
//if you have any other in mind add it here and test it out for icons
const socialMedia = [
{ key: "twitter", icon: "uil uil-twitter" },
{ key: "github", icon: "uil uil-github" },
{ key: "linkedin", icon: "uil uil-linkedin" },
{ key: "dribbble", icon: "uil uil-dribbble" },
{ key: "behance", icon: "uil uil-behance" },
{ key: "email", icon: "uil uil-envelope" },
{ key: "instagram", icon: "uil uil-instagram" },
];
// Iterate through the social media platforms and add icons if links are provided
for (const platform of socialMedia) {
if (details[platform.key]) {
socialIcons.innerHTML += `<a href="${
details[platform.key]
}" target="_blank"><i class="${platform.icon}"></i></a>`;
}
}
return cardClone;
}
function removeCard() {
const cards = document.querySelectorAll(".card");
cards.forEach((element) => {
cardsContainer.removeChild(element);
});
}
function research() {
removeCard();
const searchValue = inputSearch.value.trim().toLowerCase();
if (searchValue.length <= 0) {
cardDetailsArray.forEach((details) => {
const card = createCard(details);
cardsContainer.appendChild(card);
});
inputSearch.value = "";
} else {
const resultArray = [...cardDetailsArray];
const noResultsParagraph = cardsContainer.querySelector(
".no-results-message"
);
resultArray.sort((a, b) => a.name.localeCompare(b.name));
const filteredResults = resultArray.filter((details) =>
details.name.toLowerCase().startsWith(searchValue.toLowerCase())
);
if (filteredResults.length > 0) {
filteredResults.forEach((details) => {
const card = createCard(details);
cardsContainer.appendChild(card);
});
if (noResultsParagraph) {
cardsContainer.removeChild(noResultsParagraph);
}
} else {
if (!noResultsParagraph) {
const noResultsParagraph = document.createElement("p");
noResultsParagraph.className = "no-results-message";
noResultsParagraph.textContent = "Name Not Found.";
cardsContainer.appendChild(noResultsParagraph);
}
}
}
}
const cardsContainer = document.querySelector(".cards");
const paginationContainer = document.querySelector(".pagination");
const inputSearch = document.getElementById("search");
inputSearch.addEventListener("input", research);
fetch("./cardDetails.json")
.then((response) => response.json())
.then((data) => {
cardDetailsArray = data.cardDetails;
noOfCards = cardDetailsArray.length;
let noOfPages = Math.ceil(noOfCards / cardsPerPage);
cardsContainer.style.display = "none";
displayPages(1);
for (let j = 1; j <= noOfPages; j++) {
const pageTemplate = document.getElementById("pages");
const pageClone = pageTemplate.content.cloneNode(true);
pageClone.querySelector(".page-details p").textContent = j;
pageClone
.querySelector(".page-details")
.addEventListener("click", function () {
cardsContainer.innerHTML = "";
displayPages(j);
setPageStyle(j);
window.scrollTo({ top: 560, behavior: "smooth" });
});
paginationContainer.appendChild(pageClone);
}
})
.catch((error) => console.error("Error fetching JSON:", error));
function displayPages(j) {
for (
let i = (j - 1) * cardsPerPage;
i < j * cardsPerPage && i < noOfCards;
i++
) {
let details = cardDetailsArray[i];
const card = createCard(details);
cardsContainer.appendChild(card);
}
cardsContainer.style.display = "flex";
}
function setPageStyle(selectedPage) {
const allPages = document.querySelectorAll(".page-details p");
allPages.forEach((page, index) => {
const isSelected = index + 1 === selectedPage;
if (page && page.classList && page.style) {
if (isSelected) {
page.classList.add("selected-page");
} else {
page.classList.remove("selected-page");
}
}
});
}
//
//
// Toggle mobile navbar menu
const attatchMobileMenuToggleFunction = () => {
try {
const menuIcon = document.getElementById("navbar-menu-icon-1");
const linkListContainer = document.getElementById(
"navbar-link-list-container"
);
const navbar_social_media_container = document.getElementById(
"navbar-social-media-container"
);
const google_translate_element = document.getElementById(
"google_translate_element"
);
// validate
if (menuIcon === null || menuIcon === undefined) {
console.log("Element with ID 'navbar-menu-icon-1' not found");
return;
}
if (linkListContainer === null || linkListContainer === undefined) {
console.log("Element with ID 'navbar-link-list-container' not found");
return;
}
if (
navbar_social_media_container === null ||
navbar_social_media_container === undefined
) {
console.log("Element with ID 'navbar-social-media-container' not found");
return;
}
if (
google_translate_element === null ||
google_translate_element === undefined
) {
console.log("Element with ID 'google_translate_element' not found");
return;
}
// Attach a function to the click event of menuIcon
menuIcon.addEventListener("click", function () {
// Toggle the display property of linkListContainer
if (linkListContainer.style.display === "flex") {
linkListContainer.style.display = "none";
google_translate_element.style.display = "none";
navbar_social_media_container.style.display = "none";
menuIcon.style.opacity = 1;
} else {
linkListContainer.style.display = "flex";
google_translate_element.style.display = "flex";
navbar_social_media_container.style.display = "flex";
menuIcon.style.opacity = 0.2;
}
});
} catch (error) {
console.error(error);
}
};
document.addEventListener("DOMContentLoaded", function () {
attatchMobileMenuToggleFunction();
});