AgPerry's picture
Add files using upload-large-folder tool
aa09365 verified
Raw History Blame Contribute Delete
6.06 kB
// Global-nav flyouts and the home gallery.
//
// Progressive enhancement only: every flyout destination is a real anchor and
// every gallery slide is in the document, so both work with JavaScript off.
(function () {
"use strict";
var OPEN_DELAY = 120;
var CLOSE_DELAY = 260;
function initNav() {
var nav = document.querySelector(".globalnav");
if (!nav) return;
var items = Array.prototype.slice.call(nav.querySelectorAll(".globalnav-item"));
var openTimer = null;
var closeTimer = null;
function setOpen(item, open) {
item.setAttribute("data-open", open ? "true" : "false");
var trigger = item.querySelector(".globalnav-trigger");
if (trigger) trigger.setAttribute("aria-expanded", open ? "true" : "false");
if (open) nav.setAttribute("data-flyout", "open");
}
function closeAll(except) {
var anyOpen = false;
items.forEach(function (item) {
if (item === except) {
anyOpen = true;
return;
}
setOpen(item, false);
});
if (!anyOpen) nav.setAttribute("data-flyout", "closed");
}
function scheduleOpen(item) {
window.clearTimeout(closeTimer);
window.clearTimeout(openTimer);
// An already-open menu swaps instantly, the way the source nav does.
var immediate = nav.getAttribute("data-flyout") === "open";
openTimer = window.setTimeout(
function () {
closeAll(item);
setOpen(item, true);
},
immediate ? 0 : OPEN_DELAY
);
}
function scheduleClose() {
window.clearTimeout(openTimer);
window.clearTimeout(closeTimer);
closeTimer = window.setTimeout(function () {
closeAll(null);
}, CLOSE_DELAY);
}
items.forEach(function (item) {
var trigger = item.querySelector(".globalnav-trigger");
if (!trigger) return;
item.addEventListener("mouseenter", function () {
if (window.matchMedia("(hover: none)").matches) return;
scheduleOpen(item);
});
item.addEventListener("mouseleave", function () {
if (window.matchMedia("(hover: none)").matches) return;
scheduleClose();
});
// Keyboard and touch still need an explicit activation.
trigger.addEventListener("click", function (event) {
event.preventDefault();
window.clearTimeout(openTimer);
window.clearTimeout(closeTimer);
var open = item.getAttribute("data-open") === "true";
closeAll(open ? null : item);
setOpen(item, !open);
if (open) nav.setAttribute("data-flyout", "closed");
});
trigger.addEventListener("focus", function () {
if (nav.getAttribute("data-flyout") === "open") {
closeAll(item);
setOpen(item, true);
}
});
});
document.addEventListener("keydown", function (event) {
if (event.key !== "Escape") return;
closeAll(null);
nav.setAttribute("data-flyout", "closed");
});
document.addEventListener("click", function (event) {
if (nav.contains(event.target)) return;
closeAll(null);
nav.setAttribute("data-flyout", "closed");
});
var toggle = nav.querySelector(".menu-toggle");
if (toggle) {
toggle.addEventListener("click", function () {
var open = nav.getAttribute("data-menu") === "open";
nav.setAttribute("data-menu", open ? "closed" : "open");
toggle.setAttribute("aria-expanded", open ? "false" : "true");
if (open) closeAll(null);
});
}
}
function initGallery() {
var gallery = document.querySelector("[data-gallery]");
if (!gallery) return;
var slides = Array.prototype.slice.call(gallery.querySelectorAll(".gallery-item"));
var dots = Array.prototype.slice.call(gallery.querySelectorAll(".gallery-dot"));
if (slides.length < 2) return;
var index = 0;
var timer = null;
var interval = 5000;
function show(next) {
index = (next + slides.length) % slides.length;
slides.forEach(function (slide, position) {
slide.setAttribute("data-active", position === index ? "true" : "false");
slide.setAttribute("aria-hidden", position === index ? "false" : "true");
});
dots.forEach(function (dot, position) {
dot.setAttribute("aria-selected", position === index ? "true" : "false");
});
}
function play() {
stop();
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
timer = window.setInterval(function () {
show(index + 1);
}, interval);
gallery.setAttribute("data-playing", "true");
}
function stop() {
window.clearInterval(timer);
timer = null;
gallery.setAttribute("data-playing", "false");
}
dots.forEach(function (dot, position) {
dot.addEventListener("click", function () {
show(position);
play();
});
});
var previous = gallery.querySelector("[data-gallery-previous]");
var next = gallery.querySelector("[data-gallery-next]");
if (previous) {
previous.addEventListener("click", function () {
show(index - 1);
play();
});
}
if (next) {
next.addEventListener("click", function () {
show(index + 1);
play();
});
}
var toggle = gallery.querySelector("[data-gallery-toggle]");
if (toggle) {
toggle.addEventListener("click", function () {
if (timer) {
stop();
toggle.textContent = "Play";
toggle.setAttribute("aria-label", "Play endless entertainment gallery");
} else {
play();
toggle.textContent = "Pause";
toggle.setAttribute("aria-label", "Pause endless entertainment gallery");
}
});
}
gallery.addEventListener("mouseenter", stop);
gallery.addEventListener("mouseleave", play);
gallery.addEventListener("focusin", stop);
show(0);
play();
}
initNav();
initGallery();
})();