-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
110 lines (87 loc) · 3.23 KB
/
script.js
File metadata and controls
110 lines (87 loc) · 3.23 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
/* =========================
Portfolio JavaScript
Author: Trifon Stanchev
========================= */
// ===== Safe Smooth Scrolling (anchors only) =====
document.querySelectorAll('a[href^="#"]').forEach((link) => {
link.addEventListener("click", (e) => {
const targetId = link.getAttribute("href");
if (!targetId || targetId === "#") return;
const target = document.querySelector(targetId);
if (!target) return;
e.preventDefault();
target.scrollIntoView({ behavior: "smooth", block: "start" });
});
});
// ===== Theme Toggle (Light/Dark) =====
const themeToggleBtn = document.getElementById("themeToggle");
const themeIcon = document.getElementById("themeIcon");
function setTheme(mode) {
const isDark = mode === "dark";
document.body.classList.toggle("dark", isDark);
if (themeIcon) themeIcon.textContent = isDark ? "☀️" : "🌙";
localStorage.setItem("theme", mode);
}
(function initTheme() {
const saved = localStorage.getItem("theme");
if (saved === "dark" || saved === "light") {
setTheme(saved);
return;
}
const prefersDark =
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches;
setTheme(prefersDark ? "dark" : "light");
})();
themeToggleBtn?.addEventListener("click", () => {
const isDarkNow = document.body.classList.contains("dark");
setTheme(isDarkNow ? "light" : "dark");
});
// ===== Projects Search + Filter =====
const searchInput = document.getElementById("projectSearch");
const chipsContainer = document.getElementById("filterChips");
const projectCards = Array.from(document.querySelectorAll(".project-card"));
const noResults = document.getElementById("noResults");
let activeFilter = "all";
function normalize(str) {
return (str || "").toLowerCase().trim();
}
function matchesFilter(card, filter) {
if (filter === "all") return true;
const tags = normalize(card.getAttribute("data-tags"));
return tags.split(/\s+/).includes(filter);
}
function matchesSearch(card, query) {
if (!query) return true;
const text = normalize(card.innerText);
return text.includes(query);
}
function applyProjectsView({ scrollToFirstMatch = false } = {}) {
const q = normalize(searchInput?.value);
const visibleCards = [];
projectCards.forEach((card) => {
const ok = matchesFilter(card, activeFilter) && matchesSearch(card, q);
card.style.display = ok ? "block" : "none";
if (ok) visibleCards.push(card);
});
if (noResults) {
noResults.style.display = visibleCards.length === 0 ? "block" : "none";
}
if (scrollToFirstMatch && visibleCards.length > 0) {
visibleCards[0].scrollIntoView({ behavior: "smooth", block: "start" });
}
}
searchInput?.addEventListener("input", () => applyProjectsView());
chipsContainer?.addEventListener("click", (e) => {
const btn = e.target.closest(".chip");
if (!btn) return;
// Update active chip UI
chipsContainer.querySelectorAll(".chip").forEach((c) => c.classList.remove("active"));
btn.classList.add("active");
// Set filter
activeFilter = btn.dataset.filter || "all";
applyProjectsView({ scrollToFirstMatch: activeFilter !== "all" });
});
// Initial render (in case there is pre-filled search/filter)
applyProjectsView();
console.log("🚀 Portfolio script loaded successfully");