-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.js
396 lines (334 loc) · 11.3 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
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
// import projectsData from "./assets/json/projects.json" assert { type: "json" };
const reveal = {
distance: "30px",
easing: "ease-in",
duration: 500,
delay: 300,
origin: "top",
};
ScrollReveal().reveal(".main", reveal);
/*---------------- MENU SHOW Y HIDDEN ----------------*/
const navMenu = document.getElementById("nav-menu"),
navToggle = document.getElementById("nav-toggle"),
navClose = document.getElementById("nav-close");
const themesMenu = document.getElementById("themes-menu"),
themesToggle = document.getElementById("themes-menu-toggle"),
themesClose = document.getElementById("themes-close");
/*---- MENU SHOW ----*/
/* Validate if constant exists */
if (navToggle) {
navToggle.addEventListener("click", () => {
navMenu.classList.add("show-menu");
themesMenu.classList.remove("show-menu");
});
}
if (themesToggle) {
themesToggle.addEventListener("click", () => {
themesMenu.classList.add("show-menu");
navMenu.classList.remove("show-menu");
});
}
/*---- MENU HIDDEN ----*/
/* Validate if constant exists */
if (navClose) {
navClose.addEventListener("click", () => {
navMenu.classList.remove("show-menu");
});
}
if (themesClose) {
themesClose.addEventListener("click", () => {
themesMenu.classList.remove("show-menu");
});
}
/*---------------- REMOVE MENU MOBILE ----------------*/
const navLink = document.querySelectorAll(".nav_link");
function linkAction() {
const navMenu = document.getElementById("nav-menu");
//when we click on each nav_link, we remove the show-menu class
navMenu.classList.remove("show-menu");
}
navLink.forEach((n) => n.addEventListener("click", linkAction));
/*---------------- SKILLS ----------------*/
const skillsContent = document.getElementsByClassName("skills_content"),
skillsHeader = document.querySelectorAll(".skills_header");
function toggleSkills() {
let itemClass = this.parentNode.className;
for (let i = 0; i < skillsContent.length; i++) {
skillsContent[i].className = "skills_content skills_close";
}
if (itemClass === "skills_content skills_close") {
this.parentNode.className = "skills_content skills_open";
}
}
skillsHeader.forEach((el) => {
el.addEventListener("click", toggleSkills);
});
/*---------------- SERVICES MODAL ----------------*/
/*-------------------------PROJECTS-------------------- */
await fetch("./assets/json/projects.json")
.then((response) => {
return response.json();
})
.then((projectsData) => {
console.log(projectsData);
document.getElementById("project-cards").innerHTML = `
${projectsData
.map(function (project) {
return `
<!-----------Project Card--------------->
<div class="project_card card swiper-slide" >
<img class="project_image" src="${project.image}"/>
<div class="project_data">
<h3 class="project_title">${project.title}</h3>
<p class="project_description">
${project.description}
</p>
<a href="${project.sourceLink}" target="_blank" class="button button-flex button-small project_button">Checkout<i class="uil uil-external-link-alt button_icon checkout_button_icon"></i></a>
</div>
</div>
`;
})
.join("")}
`;
});
/*-------------------- PROJECTS SWIPER ----------------*/
var swiper = new Swiper(".projects_content", {
slidesPerView: 2,
spaceBetween: 30,
slidesPerGroup: 1,
loop: true,
centerSlide: true,
loop: true,
fade: true,
grabCursor: true,
pagination: {
el: ".swiper-pagination",
clickable: true,
dynamicBullets: true,
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
autoplay: {
delay: 2000,
disableOnInteraction: false,
},
breakpoints: {
0: {
slidesPerView: 1,
},
568: {
slidesPerView: 2,
},
1024: {
slidesPerView: 3,
},
1366: {
slidesPerView: 5,
},
},
});
/*-------------------------- BLOGS ----------------------------*/
function readingTime(text) {
const wpm = 225;
const words = text.trim().split(/\s+/).length;
const time = Math.ceil(words / wpm);
return time;
}
// Fetch function
async function gql(query, variables = {}) {
const data = await fetch("https://gql.hashnode.com/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
variables,
}),
});
return data.json();
}
//old query query GetUserArticles($page: Int!) {
// user(username: "sp-xd") {
// publication {
// posts(page: $page) {
// title
// brief
// slug
// coverImage
// dateAdded
// contentMarkdown
// }
// }
// }
// }
// Query
const GET_USER_ARTICLES = `
query GetUserArticles($numOfPosts: Int!) {
user(username: "sp-xd") {
publications(first: 1) {
edges {
node {
posts(first: $numOfPosts) {
edges {
node {
title
brief
slug
coverImage {
url
}
publishedAt
readTimeInMinutes
}
}
}
}
}
}
}
}
`;
// making the call and injecting the articles
gql(GET_USER_ARTICLES, { numOfPosts: 6 }).then((result) => {
const articles = result.data.user.publications.edges[0].node.posts.edges;
let container = document.createElement("div");
container.classList.add("blog_items");
articles.forEach((article) => {
article = article.node;
let blog_item = document.createElement("div");
blog_item.classList.add("blog_item", "card");
let blog_content = document.createElement("div");
blog_content.classList.add("blog_content");
let article_link = `https://blogs.somnathpaul.in/${article.slug}`;
let title = document.createElement("h3");
title.classList.add("blog_title");
let title_link = document.createElement("a");
title_link.target = "_blank";
title_link.href = article_link;
title_link.innerText = article.title;
title.appendChild(title_link);
let brief = document.createElement("p");
brief.classList.add("blog_brief");
brief.innerText = article.brief;
let read_more_link = document.createElement("a");
read_more_link.target = "_blank";
read_more_link.href = article_link;
read_more_link.classList.add("read_more_button"); //, 'button', 'button-flex');
read_more_link.innerHTML = `Read more <i class="uil uil-external-link-alt"></i>`;
let date = new Date(Date.parse(article.publishedAt));
let options = { day: "numeric", month: "long", year: "numeric" };
let date_added = document.createElement("span");
date_added.innerHTML = `<i class="uil uil-calender"></i> ${date.toLocaleString(
"en-US",
options
)} `;
let reading_time = document.createElement("span");
reading_time.innerHTML = `  <i class="uil uil-book-open"></i> ${article.readTimeInMinutes} min read`;
let meta_data = document.createElement("span");
meta_data.appendChild(date_added);
meta_data.appendChild(reading_time);
let coverImage = document.createElement("img");
coverImage.src = article.coverImage.url;
coverImage.classList.add("blog_img");
brief.appendChild(read_more_link);
blog_item.appendChild(coverImage);
blog_content.appendChild(title);
// blog_item.appendChild(link);
blog_content.appendChild(meta_data);
blog_content.appendChild(brief);
blog_item.appendChild(blog_content);
container.appendChild(blog_item);
});
document.querySelector(".blogs_container").appendChild(container);
});
/*---------------- SCROLL SECTIONS ACTIVE LINK ----------------*/
const sections = document.querySelectorAll("section[id]");
function scrollActive() {
const scrollY = window.pageYOffset;
sections.forEach((current) => {
const sectionHeight = current.offsetHeight;
const sectionTop = current.offsetTop - 50;
let sectionId = current.getAttribute("id");
if (scrollY > sectionTop && scrollY <= sectionTop + sectionHeight) {
document
.querySelector(".nav_menu a[href*=" + sectionId + "]")
.classList.add("active-link");
} else {
document
.querySelector(".nav_menu a[href*=" + sectionId + "]")
.classList.remove("active-link");
}
});
}
window.addEventListener("scroll", scrollActive);
/*---------------- CHANGE BACKGROUND HEADER ----------------*/
function scrollHeader() {
const nav = document.getElementById("header");
if (this.scrollY >= 80) nav.classList.add("scroll-header");
else nav.classList.remove("scroll-header");
}
window.addEventListener("scroll", scrollHeader);
/*---------------- SHOW SCROLL UP ----------------*/
function scrollUp() {
const scrollUpButton = document.getElementById("scroll-up");
if (this.scrollY >= 560) scrollUpButton.classList.add("scroll-show");
else scrollUpButton.classList.remove("scroll-show");
}
window.addEventListener("scroll", scrollUp);
/*---------------- DARK LIGHT themes ----------------*/
const darkLightThemeButton = document.getElementById("dark_light-theme-button");
const darkTheme = "dark-theme";
const iconTheme = "uil-sun";
// localStorage theme is being retrieved if the site was previously visited
const selectedTheme = localStorage.getItem("selected-theme");
const selectedIcon = localStorage.getItem("selected-icon");
const getCurrentTheme = () =>
document.body.classList.contains(darkTheme) ? "dark" : "light";
const getCurrentIcon = () =>
darkLightThemeButton.classList.contains(iconTheme) ? "uil-moon" : "uil-sun";
if (selectedTheme) {
document.body.classList[selectedTheme === "dark" ? "add" : "remove"](
darkTheme
);
darkLightThemeButton.classList[
selectedIcon === "uil-moon" ? "add" : "remove"
](iconTheme);
}
darkLightThemeButton.addEventListener("click", () => {
document.body.classList.toggle(darkTheme);
darkLightThemeButton.classList.toggle(iconTheme);
// storing current theme to local storage
localStorage.setItem("selected-theme", getCurrentTheme());
localStorage.setItem("selected-icon", getCurrentIcon());
});
/*------------------- Color Themes --------------------- */
const gradientThemesButtons = document.querySelectorAll(".theme_item");
const selectedGradientThemeIndexNumber = localStorage.getItem(
"selectedGradientThemeIndexNumber"
);
if (selectedGradientThemeIndexNumber) {
gradientThemesButtons.forEach((el, id) => {
if (selectedGradientThemeIndexNumber == id) {
toggleColorTheme(el, id);
}
});
}
function toggleColorTheme(element, indexNumber) {
let itemId = element.id;
for (let i = 0; i < gradientThemesButtons.length; i++) {
gradientThemesButtons[i].classList.remove("theme-selected");
document.body.classList.remove("gradient-theme-" + (i + 1));
}
document.body.classList.add(itemId);
element.classList.add("theme-selected");
//storing current gradient theme to local storage
localStorage.setItem("selectedGradientThemeIndexNumber", indexNumber);
}
gradientThemesButtons.forEach((el, id) => {
el.addEventListener("click", () => {
toggleColorTheme(el, id);
});
});