Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
417 lines (374 loc) · 12.2 KB
/
Copy pathscript.js
File metadata and controls
417 lines (374 loc) · 12.2 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* global Chart */
// Ensure the footer year stays current
document.addEventListener("DOMContentLoaded", () => {
const yearSpan = document.getElementById("year");
if (yearSpan) {
yearSpan.textContent = new Date().getFullYear();
}
initThemeToggle();
initMobileNav();
initRadarChart();
initHoverCards();
initRoadmap();
});
/**
* THEME TOGGLE — cycles between light and dark.
*
* Shows the current state as an emoji (🌙 for dark, ☀️ for light).
* Clicking toggles. The chosen theme is stored in localStorage and
* applied to <html data-theme=...>. Chart.js re-syncs via a
* MutationObserver on the data-theme attribute.
*/
function initThemeToggle() {
const button = document.getElementById("theme-toggle");
if (!button) return;
const root = document.documentElement;
const icon = button.querySelector(".theme-icon");
const stored = localStorage.getItem("theme-preference");
function applyTheme(theme) {
root.setAttribute("data-theme", theme);
localStorage.setItem("theme-preference", theme);
if (icon) {
icon.textContent = theme === "dark" ? "🌙" : "☀️";
}
button.setAttribute(
"aria-label",
`Toggle colour theme — currently ${theme} mode`
);
}
// On load: apply stored preference or default to dark.
// Normalise any legacy "system" value to "dark".
const initialTheme = stored === "light" ? "light" : "dark";
applyTheme(initialTheme);
button.addEventListener("click", () => {
const current = root.getAttribute("data-theme");
const next = current === "dark" ? "light" : "dark";
applyTheme(next);
});
}
/**
* MOBILE NAV / HAMBURGER
*/
function initMobileNav() {
const menuToggle = document.querySelector(".menu-toggle");
const mainNav = document.querySelector(".main-nav");
if (!menuToggle || !mainNav) return;
// Toggle on button click
menuToggle.addEventListener("click", (event) => {
event.stopPropagation(); // don't trigger outside-click handler
const isOpen = mainNav.classList.toggle("open");
menuToggle.setAttribute("aria-expanded", String(isOpen));
menuToggle.classList.toggle("open", isOpen);
});
// Close when clicking outside the nav / button
document.addEventListener("click", (event) => {
const isOpen = mainNav.classList.contains("open");
if (!isOpen) return;
const clickedInsideNav = mainNav.contains(event.target);
const clickedToggle = menuToggle.contains(event.target);
if (!clickedInsideNav && !clickedToggle) {
mainNav.classList.remove("open");
menuToggle.classList.remove("open");
menuToggle.setAttribute("aria-expanded", "false");
}
});
// Close menu when resizing back to desktop width
window.addEventListener("resize", () => {
if (window.innerWidth > 768) {
mainNav.classList.remove("open");
menuToggle.classList.remove("open");
menuToggle.setAttribute("aria-expanded", "false");
}
});
}
/**
* RADAR CHART
*
* Two profiles (Cyber / General), smooth data transitions on mode switch,
* theme-reactive colours, combined tooltip, keyboard-navigable tablist.
* Respects prefers-reduced-motion.
*/
function initRadarChart() {
const canvas = document.getElementById("skillsRadar");
if (!canvas || typeof Chart === "undefined") return;
const ctx = canvas.getContext("2d");
const reducedMotionQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
const modes = {
cyber: {
title: "Cyber",
labels: [
["Core", "Technical"],
["OSINT &", "Recon"],
["Network", "Assessment"],
["Windows", "Assessment"],
["Unix", "Assessment"],
["Web App", "Testing"]
],
current: [40, 25, 20, 30, 20, 25],
target: [75, 65, 60, 65, 60, 60]
},
general: {
title: "General",
labels: [
["Leadership", " "],
["Engineering", "Governance"],
["Quality", "& Audit"],
["Process", "Transformation"],
["Defence", "Acquisition"],
["Operational", "Delivery"]
],
current: [88, 88, 75, 82, 80, 85],
target: [90, 88, 88, 85, 80, 85]
}
};
// Read live theme colours from CSS custom properties so the chart
// follows the active light / dark theme.
function getThemeColors() {
const styles = getComputedStyle(document.documentElement);
const read = (name, fallback) =>
(styles.getPropertyValue(name) || "").trim() || fallback;
return {
accent: read("--accent", "#4ade80"),
accent2: read("--accent-2", "#38bdf8"),
textMain: read("--text-main", "#e5e7eb"),
textMuted: read("--text-muted", "#9ca3af")
};
}
// Convert a hex colour string to rgba() with the given alpha. Any value
// that isn't a valid hex is returned untouched.
function hexToRgba(hex, alpha) {
if (typeof hex !== "string" || !hex.startsWith("#")) return hex;
const h = hex.replace("#", "");
const full =
h.length === 3
? h
.split("")
.map((c) => c + c)
.join("")
: h;
if (full.length !== 6) return hex;
const r = parseInt(full.slice(0, 2), 16);
const g = parseInt(full.slice(2, 4), 16);
const b = parseInt(full.slice(4, 6), 16);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
function updateCanvasLabel(modeKey) {
const title = modes[modeKey].title;
canvas.setAttribute(
"aria-label",
`${title} skills radar chart showing current levels and 12-month targets across six axes`
);
}
let currentMode = "cyber";
let radarChart = null;
function buildConfig() {
const mode = modes[currentMode];
const colors = getThemeColors();
return {
type: "radar",
data: {
labels: mode.labels,
datasets: [
{
label: "Current level",
data: mode.current,
fill: true,
backgroundColor: hexToRgba(colors.accent, 0.25),
borderColor: colors.accent,
pointBackgroundColor: colors.accent,
pointRadius: 3,
borderWidth: 2
},
{
label: "Target (12 months)",
data: mode.target,
fill: true,
backgroundColor: hexToRgba(colors.accent2, 0.18),
borderColor: colors.accent2,
pointBackgroundColor: colors.accent2,
pointRadius: 3,
borderWidth: 2
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
// Tight padding so the polygon fills the available wrapper.
// Sized for the compact 280px chart wrapper.
layout: {
padding: { top: 5, right: 10, bottom: 5, left: 10 }
},
animation: {
duration: reducedMotionQuery.matches ? 0 : 600,
easing: "easeOutQuart"
},
plugins: {
legend: { display: false },
tooltip: { enabled: false }
},
scales: {
r: {
// Hard min/max so scale is identical across modes —
// no visual resize when switching Cyber / General.
min: 0,
max: 95,
ticks: {
stepSize: 25,
showLabelBackdrop: false,
color: "rgba(0, 0, 0, 0)"
},
grid: { color: colors.textMuted, lineWidth: 1 },
angleLines: { color: colors.textMuted, lineWidth: 1 },
pointLabels: {
color: colors.textMain,
padding: 6,
font: {
size: 10,
family: "system-ui, -apple-system, sans-serif"
}
}
}
}
}
};
}
// Update the chart data + theme colours in place. Chart.js animates
// between states natively, giving a smooth morph between Cyber / General
// and when the user switches theme.
function updateRadar() {
if (!radarChart) return;
const mode = modes[currentMode];
const colors = getThemeColors();
radarChart.data.labels = mode.labels;
const [currentDs, targetDs] = radarChart.data.datasets;
currentDs.data = mode.current;
currentDs.backgroundColor = hexToRgba(colors.accent, 0.25);
currentDs.borderColor = colors.accent;
currentDs.pointBackgroundColor = colors.accent;
targetDs.data = mode.target;
targetDs.backgroundColor = hexToRgba(colors.accent2, 0.18);
targetDs.borderColor = colors.accent2;
targetDs.pointBackgroundColor = colors.accent2;
const r = radarChart.options.scales.r;
r.grid.color = colors.textMuted;
r.angleLines.color = colors.textMuted;
r.pointLabels.color = colors.textMain;
radarChart.update();
updateCanvasLabel(currentMode);
}
function setMode(modeKey, focusButton) {
if (!modes[modeKey]) return;
currentMode = modeKey;
const toggleButtons = document.querySelectorAll(".toggle-button");
toggleButtons.forEach((btn) => {
const isActive = btn.getAttribute("data-mode") === modeKey;
btn.classList.toggle("active", isActive);
btn.setAttribute("aria-selected", String(isActive));
btn.setAttribute("tabindex", isActive ? "0" : "-1");
});
if (focusButton) focusButton.focus();
updateRadar();
}
// Initial render
radarChart = new Chart(ctx, buildConfig());
updateCanvasLabel(currentMode);
// Toggle buttons: click + keyboard arrow navigation (WAI-ARIA tab pattern)
const toggleButtons = Array.from(document.querySelectorAll(".toggle-button"));
toggleButtons.forEach((btn, index) => {
btn.addEventListener("click", () => {
const mode = btn.getAttribute("data-mode");
if (mode && mode !== currentMode) setMode(mode);
});
btn.addEventListener("keydown", (e) => {
let nextIndex = null;
if (e.key === "ArrowRight" || e.key === "ArrowDown") {
nextIndex = (index + 1) % toggleButtons.length;
} else if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
nextIndex = (index - 1 + toggleButtons.length) % toggleButtons.length;
} else if (e.key === "Home") {
nextIndex = 0;
} else if (e.key === "End") {
nextIndex = toggleButtons.length - 1;
}
if (nextIndex !== null) {
e.preventDefault();
const targetBtn = toggleButtons[nextIndex];
const targetMode = targetBtn.getAttribute("data-mode");
setMode(targetMode, targetBtn);
}
});
});
// Re-sync chart colours whenever <html data-theme> changes.
// MutationObserver covers every possible trigger (toggle button click,
// DevTools edit, future programmatic changes) without coupling the chart
// to any specific control.
const themeObserver = new MutationObserver(() => {
requestAnimationFrame(updateRadar);
});
themeObserver.observe(document.documentElement, {
attributes: true,
attributeFilter: ["data-theme"]
});
}
/**
* ROADMAP — node card popup tap-toggle.
*
* Desktop hover/focus is handled in CSS (`:hover ~ .node-popup`, `:focus-visible ~`).
* This function adds explicit click/tap toggling so:
* - touch users can open popups (no hover state),
* - any user can pin a popup open by clicking,
* - only one popup is open at a time,
* - clicking outside or pressing Escape closes any open popup.
*/
function initRoadmap() {
const cards = document.querySelectorAll(".roadmap-track .node-card");
if (!cards.length) return;
function closeAll(except) {
cards.forEach((c) => {
if (c !== except) c.setAttribute("aria-expanded", "false");
});
}
cards.forEach((card) => {
card.addEventListener("click", (e) => {
e.stopPropagation();
const isOpen = card.getAttribute("aria-expanded") === "true";
closeAll(card);
card.setAttribute("aria-expanded", isOpen ? "false" : "true");
});
});
// Click outside any node-card closes all
document.addEventListener("click", (e) => {
if (!e.target.closest(".roadmap-track .node-card")) {
closeAll(null);
}
});
// Escape closes all and restores focus to the active card if any
document.addEventListener("keydown", (e) => {
if (e.key !== "Escape") return;
const open = document.querySelector(
'.roadmap-track .node-card[aria-expanded="true"]'
);
if (open) {
closeAll(null);
open.focus();
}
});
}
/**
* HOVER CARD MOUSE-EFFECT
*/
function initHoverCards() {
const container = document.getElementById("cards-matrix");
if (!container) return;
container.addEventListener("mousemove", (e) => {
const cards = container.getElementsByClassName("hover-card");
for (const card of cards) {
const rect = card.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
card.style.setProperty("--mouse-x", `${x}px`);
card.style.setProperty("--mouse-y", `${y}px`);
}
});
}