-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase-study.js
More file actions
96 lines (85 loc) · 3.65 KB
/
Copy pathcase-study.js
File metadata and controls
96 lines (85 loc) · 3.65 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
(function () {
const pageData = window.caseStudyPageData || {};
function language() {
return typeof getCurrentLocale === "function" ? getCurrentLocale() : (document.documentElement.lang || "en");
}
function applyTranslations() {
const activeLanguage = language();
const translations = pageData.translations;
/* The page id opts this case study into the shipped locale pack, so a
* locale beyond the inline EN/TR pair is not silently reset to English. */
const namespace = pageData.id ? `case-studies:${pageData.id}` : null;
const active = typeof getLocalizedCollection === "function"
? getLocalizedCollection(translations, activeLanguage, namespace)
: (translations?.[activeLanguage] || translations?.en);
if (active) {
document.querySelectorAll("[data-case-i18n]").forEach((element) => {
const value = active[element.dataset.caseI18n];
if (value) element.textContent = value;
});
document.querySelectorAll("[data-case-i18n-alt]").forEach((element) => {
const value = active[element.dataset.caseI18nAlt];
if (value) element.alt = value;
});
document.querySelectorAll("[data-case-i18n-aria-label]").forEach((element) => {
const value = active[element.dataset.caseI18nAriaLabel];
if (value) element.setAttribute("aria-label", value);
});
}
const title = active?.__title || pageData.titles?.[activeLanguage] || pageData.titles?.en;
if (title) document.title = title;
const openModalImage = document.querySelector("[data-case-modal].is-open [data-case-modal-image]");
const openModalTrigger = document.querySelector("[data-case-gallery][aria-expanded='true']");
if (openModalImage && openModalTrigger) {
openModalImage.alt = openModalTrigger.querySelector("img")?.alt || "";
}
}
if (typeof subscribeSiteLocale === "function") subscribeSiteLocale(() => applyTranslations());
const modal = document.querySelector("[data-case-modal]");
const modalImage = modal?.querySelector("[data-case-modal-image]");
const modalClose = modal?.querySelector("[data-case-modal-close]");
let modalTrigger = null;
if (modal) modal.inert = true;
function closeModal() {
if (!modal?.classList.contains("is-open")) return;
modal.classList.remove("is-open");
modal.setAttribute("aria-hidden", "true");
modal.inert = true;
document.body.classList.remove("case-modal-open");
modalTrigger?.setAttribute("aria-expanded", "false");
if (modalImage) {
modalImage.removeAttribute("src");
modalImage.alt = "";
}
modalTrigger?.focus();
}
document.querySelectorAll("[data-case-gallery]").forEach((button) => {
button.setAttribute("aria-expanded", "false");
button.addEventListener("click", () => {
if (!modal || !modalImage) return;
modalTrigger = button;
const image = button.querySelector("img");
modalImage.src = button.dataset.caseGallery;
modalImage.alt = image?.alt || "";
modal.classList.add("is-open");
modal.inert = false;
modal.setAttribute("aria-hidden", "false");
document.body.classList.add("case-modal-open");
button.setAttribute("aria-expanded", "true");
modalClose?.focus();
});
});
modalClose?.addEventListener("click", closeModal);
modal?.addEventListener("click", (event) => {
if (event.target === modal) closeModal();
});
document.addEventListener("keydown", (event) => {
if (!modal?.classList.contains("is-open")) return;
if (event.key === "Escape") closeModal();
if (event.key === "Tab" && modalClose) {
event.preventDefault();
modalClose.focus();
}
});
applyTranslations();
})();