File size: 9,689 Bytes
9f517f2 | 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 | /* Etsy storefront interactions. */
(function () {
"use strict";
function ready(fn) {
if (document.readyState !== "loading") { fn(); return; }
document.addEventListener("DOMContentLoaded", fn);
}
ready(function () {
// Source-faithful category menu. It is a real button/overlay on every
// viewport, closes on outside click or Escape, and leaves each menu item
// as a plain link when JavaScript is unavailable.
var toggle = document.querySelector(".cat-toggle");
var nav = document.getElementById("category-menu");
var scrim = document.querySelector("[data-category-scrim]");
if (toggle && nav) {
var setCategoryOpen = function (open) {
toggle.setAttribute("aria-expanded", String(open));
nav.hidden = !open;
if (scrim) { scrim.hidden = !open; }
};
toggle.addEventListener("click", function (event) {
event.stopPropagation();
setCategoryOpen(toggle.getAttribute("aria-expanded") !== "true");
});
if (scrim) { scrim.addEventListener("click", function () { setCategoryOpen(false); }); }
document.addEventListener("click", function (event) {
if (!nav.hidden && !nav.contains(event.target)) { setCategoryOpen(false); }
});
document.addEventListener("keydown", function (event) {
if (event.key === "Escape" && !nav.hidden) {
setCategoryOpen(false);
toggle.focus();
}
});
}
// Header search band: an ARIA combobox layered over the real GET form.
// Debounced, single-flight via AbortController, with a monotonic sequence
// guard so a slow early response cannot overwrite a newer one. Every
// suggestion is a query this database actually resolves, so choosing one
// always lands on a real results page. With scripting off the form
// submits to /search exactly as before.
var band = document.querySelector("[data-search-band]");
if (band) {
var input = band.querySelector("[data-search-input]");
var list = band.querySelector("[data-search-list]");
var status = band.querySelector("[data-search-status]");
var endpoint = band.getAttribute("data-suggest-endpoint");
var timer = null;
var controller = null;
var sequence = 0;
var settled = 0;
var active = -1;
var closeList = function () {
list.hidden = true;
list.innerHTML = "";
input.setAttribute("aria-expanded", "false");
input.removeAttribute("aria-activedescendant");
active = -1;
};
var highlight = function (next) {
var options = list.querySelectorAll("[role=option]");
if (!options.length) { return; }
if (next < 0) { next = options.length - 1; }
if (next >= options.length) { next = 0; }
active = next;
Array.prototype.forEach.call(options, function (option, index) {
var on = index === active;
option.setAttribute("aria-selected", String(on));
option.classList.toggle("active", on);
if (on) { input.setAttribute("aria-activedescendant", option.id); }
});
};
var draw = function (results) {
if (!results.length) {
closeList();
status.textContent = "No matching searches.";
return;
}
list.innerHTML = "";
results.forEach(function (result, index) {
var item = document.createElement("li");
item.id = "global-search-option-" + index;
item.setAttribute("role", "option");
item.setAttribute("aria-selected", "false");
var link = document.createElement("a");
link.href = "/search?q=" + encodeURIComponent(result.query);
link.textContent = result.query;
var count = document.createElement("span");
count.className = "suggestion-count";
count.textContent = result.results + " results";
link.appendChild(count);
item.appendChild(link);
list.appendChild(item);
});
list.hidden = false;
input.setAttribute("aria-expanded", "true");
active = -1;
status.textContent =
results.length + (results.length === 1 ? " search" : " searches") + " suggested.";
};
var fetchSuggestions = function (query) {
if (controller) { controller.abort(); }
controller = new AbortController();
var ticket = ++sequence;
fetch(endpoint + "?q=" + encodeURIComponent(query), {
signal: controller.signal,
headers: { "Accept": "application/json" }
})
.then(function (response) {
if (!response.ok) { throw new Error("suggest failed"); }
return response.json();
})
.then(function (payload) {
if (ticket <= settled) { return; }
settled = ticket;
draw((payload && payload.results) || []);
})
.catch(function (error) {
if (error && error.name === "AbortError") { return; }
closeList();
status.textContent = "Suggestions unavailable. Press Enter to search.";
});
};
input.addEventListener("input", function () {
var query = input.value.trim();
if (timer) { window.clearTimeout(timer); }
if (query.length < 2) {
if (controller) { controller.abort(); }
closeList();
status.textContent = "";
return;
}
timer = window.setTimeout(function () { fetchSuggestions(query); }, 180);
});
input.addEventListener("keydown", function (event) {
if (list.hidden) { return; }
if (event.key === "ArrowDown") { event.preventDefault(); highlight(active + 1); }
else if (event.key === "ArrowUp") { event.preventDefault(); highlight(active - 1); }
else if (event.key === "Escape") { closeList(); }
else if (event.key === "Enter" && active >= 0) {
var chosen = list.querySelectorAll("[role=option] a")[active];
if (chosen) { event.preventDefault(); window.location.assign(chosen.href); }
}
});
document.addEventListener("click", function (event) {
if (!band.contains(event.target)) { closeList(); }
});
closeList();
}
// Homepage feature carousel. The source controls are buttons that cycle
// through distinct image/copy/CTA panels without navigating away. There
// is deliberately no autoplay, so the page remains stable for keyboard,
// touch, and assistive-technology users.
var carousel = document.querySelector("[data-hero-carousel]");
if (carousel) {
var slides = Array.prototype.slice.call(
carousel.querySelectorAll("[data-hero-slide]")
);
var previous = carousel.querySelector('[data-carousel-direction="previous"]');
var next = carousel.querySelector('[data-carousel-direction="next"]');
var carouselStatus = carousel.querySelector("[data-carousel-status]");
var activeSlide = 0;
var showSlide = function (index) {
if (!slides.length) { return; }
activeSlide = (index + slides.length) % slides.length;
slides.forEach(function (slide, slideIndex) {
var active = slideIndex === activeSlide;
slide.hidden = !active;
slide.setAttribute("aria-hidden", String(!active));
});
if (carouselStatus) {
carouselStatus.textContent =
"Feature " + (activeSlide + 1) + " of " + slides.length;
}
};
if (previous) {
previous.addEventListener("click", function () { showSlide(activeSlide - 1); });
}
if (next) {
next.addEventListener("click", function () { showSlide(activeSlide + 1); });
}
showSlide(0);
}
// Favourite controls stay real POST forms — this only announces the
// outcome, which a full page navigation would otherwise leave silent for
// a screen-reader user, and marks the toggle state for assistive tech.
document.querySelectorAll(".favorite-form").forEach(function (form) {
var button = form.querySelector("button[type=submit]");
if (!button) { return; }
var action = form.querySelector("input[name=action]");
var adding = !action || action.value === "add";
button.setAttribute("aria-pressed", String(!adding));
form.addEventListener("submit", function () {
button.setAttribute("data-submitting", "true");
var live = form.querySelector("[data-favorite-status]");
if (live) {
live.textContent = adding ? "Adding to favorites…" : "Removing from favorites…";
}
});
});
// First-visit cookie-consent overlay reproduction. Session-scoped
// (sessionStorage) disposable UI state only; Escape and Okay dismiss it.
var overlay = document.getElementById("cookie-overlay");
if (overlay) {
var seen = false;
try { seen = window.sessionStorage.getItem("cookie-overlay") === "1"; }
catch (error) { seen = true; }
if (!seen) {
overlay.hidden = false;
var okay = overlay.querySelector(".cookie-okay");
var dismiss = function () {
overlay.hidden = true;
try { window.sessionStorage.setItem("cookie-overlay", "1"); }
catch (error) { /* disposable */ }
};
if (okay) {
okay.addEventListener("click", dismiss);
okay.focus();
}
document.addEventListener("keydown", function (event) {
if (event.key === "Escape" && !overlay.hidden) { dismiss(); }
});
}
}
});
})();
|