File size: 6,062 Bytes
aa09365 | 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 | // 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();
})();
|