|
| 1 | +/*global finna*/ |
| 2 | +finna.scrollableList = (function finnaScrollableList() { |
| 3 | + /** |
| 4 | + * Initialize scrollable list |
| 5 | + */ |
| 6 | + function initScrollableList() { |
| 7 | + document.querySelectorAll(".list-scrollable").forEach((scrollable, index) => { |
| 8 | + |
| 9 | + // Identify DOM elements |
| 10 | + const list = scrollable.querySelector(".list"); |
| 11 | + const items = list.querySelectorAll(".list-item"); |
| 12 | + const links = list.querySelectorAll(".list-link"); |
| 13 | + |
| 14 | + list.querySelectorAll('.list-link img').forEach(el => { |
| 15 | + el.onload = function onCarouselImageLoad() { |
| 16 | + if (this.naturalWidth !== 10 && this.naturalHeight !== 10) { |
| 17 | + el.nextElementSibling.classList.add('hidden'); |
| 18 | + } |
| 19 | + }; |
| 20 | + }); |
| 21 | + |
| 22 | + // Create next/prev buttons dynamically |
| 23 | + const prevBtn = document.createElement("button"); |
| 24 | + prevBtn.className = "arrow-btn hidden"; |
| 25 | + prevBtn.setAttribute("aria-label", "Scroll backward"); |
| 26 | + prevBtn.innerHTML = "❮"; |
| 27 | + |
| 28 | + const nextBtn = document.createElement("button"); |
| 29 | + nextBtn.className = "arrow-btn"; |
| 30 | + nextBtn.setAttribute("aria-label", "Scroll forward"); |
| 31 | + nextBtn.innerHTML = "❯"; |
| 32 | + |
| 33 | + // Positioning classes |
| 34 | + prevBtn.classList.add("scroll-prev-btn"); |
| 35 | + prevBtn.classList.add("scroll-prev-btn-" + index); |
| 36 | + nextBtn.classList.add("scroll-next-btn"); |
| 37 | + nextBtn.classList.add("scroll-next-btn-" + index); |
| 38 | + |
| 39 | + // Insert buttons into DOM |
| 40 | + scrollable.prepend(prevBtn); |
| 41 | + scrollable.append(nextBtn); |
| 42 | + |
| 43 | + /** |
| 44 | + * Initialize tabindex |
| 45 | + */ |
| 46 | + function initTabIndexes() { |
| 47 | + links.forEach(link => link.setAttribute("tabindex", "-1")); |
| 48 | + |
| 49 | + // Active item from HTML or fallback to the first one |
| 50 | + const activeItem = |
| 51 | + list.querySelector(".list-item.active .list-link") || |
| 52 | + links[0]; |
| 53 | + activeItem.setAttribute("tabindex", "0"); |
| 54 | + } |
| 55 | + initTabIndexes(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Ensure only active element is tabbable |
| 59 | + * @param {object} activeLink active link |
| 60 | + */ |
| 61 | + function updateTabIndexes(activeLink) { |
| 62 | + links.forEach(link => link.setAttribute("tabindex", "-1")); |
| 63 | + if (activeLink) activeLink.setAttribute("tabindex", "0"); |
| 64 | + } |
| 65 | + // Initialize tabindex (first active or first item) |
| 66 | + const initialActive = list.querySelector(".list-item.active .list-link") || links[0]; |
| 67 | + updateTabIndexes(initialActive); |
| 68 | + |
| 69 | + /** |
| 70 | + * Activate an item |
| 71 | + * @param {object} link link |
| 72 | + */ |
| 73 | + function activate(link) { |
| 74 | + items.forEach(i => i.classList.remove("active")); |
| 75 | + link.parentElement.classList.add("active"); |
| 76 | + updateTabIndexes(link); |
| 77 | + |
| 78 | + link.scrollIntoView({ behavior: "smooth", inline: "center" }); |
| 79 | + link.focus({ preventScroll: true }); |
| 80 | + } |
| 81 | + |
| 82 | + // Intersection Observer for prev/next buttons |
| 83 | + const observer = new IntersectionObserver( |
| 84 | + (entries) => { |
| 85 | + entries.forEach((entry) => { |
| 86 | + if (entry.target === items[0]) { |
| 87 | + prevBtn.classList.toggle("hidden", entry.isIntersecting); |
| 88 | + } |
| 89 | + if (entry.target === items[items.length - 1]) { |
| 90 | + nextBtn.classList.toggle("hidden", entry.isIntersecting); |
| 91 | + } |
| 92 | + }); |
| 93 | + }, |
| 94 | + { root: list, threshold: 0.9 } |
| 95 | + ); |
| 96 | + |
| 97 | + observer.observe(items[0]); |
| 98 | + observer.observe(items[items.length - 1]); |
| 99 | + |
| 100 | + // Keyboard navigation |
| 101 | + list.addEventListener("keydown", (e) => { |
| 102 | + const idx = Array.from(links).indexOf(document.activeElement); |
| 103 | + if (e.key === "ArrowRight" && links[idx + 1]) { |
| 104 | + activate(links[idx + 1]); |
| 105 | + } |
| 106 | + if (e.key === "ArrowLeft" && links[idx - 1]) { |
| 107 | + activate(links[idx - 1]); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + /** |
| 112 | + * Button scrolling |
| 113 | + * @param {*} dir direction |
| 114 | + */ |
| 115 | + function scrollByDir(dir) { |
| 116 | + list.scrollBy({ |
| 117 | + left: (list.clientWidth * 0.5) * dir, |
| 118 | + behavior: "smooth", |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + prevBtn.addEventListener("click", () => scrollByDir(-1)); |
| 123 | + nextBtn.addEventListener("click", () => scrollByDir(1)); |
| 124 | + |
| 125 | + // Make sure to start at left |
| 126 | + list.scrollTo({ left: 0, behavior: "instant" }); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + /** |
| 132 | + * Initialize |
| 133 | + */ |
| 134 | + function init() { |
| 135 | + initScrollableList(); |
| 136 | + } |
| 137 | + |
| 138 | + var my = { |
| 139 | + init: init |
| 140 | + }; |
| 141 | + |
| 142 | + return my; |
| 143 | +})(); |
0 commit comments