|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var THEME_KEY = 'foundry-toolkit-changelog-theme'; |
| 5 | + var root = document.documentElement; |
| 6 | + var toggle = document.getElementById('theme-toggle'); |
| 7 | + var icon = toggle ? toggle.querySelector('[data-theme-icon]') : null; |
| 8 | + var search = document.getElementById('search'); |
| 9 | + var results = document.getElementById('results'); |
| 10 | + var navEmpty = document.getElementById('nav-empty'); |
| 11 | + var contentEmpty = document.getElementById('content-empty'); |
| 12 | + var releases = Array.prototype.slice.call(document.querySelectorAll('.release')); |
| 13 | + var navItems = Array.prototype.slice.call(document.querySelectorAll('.nav__item')); |
| 14 | + |
| 15 | + // Theme |
| 16 | + |
| 17 | + function applyTheme(theme) { |
| 18 | + root.setAttribute('data-theme', theme); |
| 19 | + if (icon) { |
| 20 | + icon.textContent = theme === 'dark' ? '☀' : '☾'; |
| 21 | + } |
| 22 | + if (toggle) { |
| 23 | + toggle.setAttribute('aria-label', theme === 'dark' ? 'Switch to light theme' : 'Switch to dark theme'); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + function readStoredTheme() { |
| 28 | + try { |
| 29 | + return window.localStorage.getItem(THEME_KEY); |
| 30 | + } catch (error) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + var stored = readStoredTheme(); |
| 36 | + var prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 37 | + applyTheme(stored || (prefersDark ? 'dark' : 'light')); |
| 38 | + |
| 39 | + if (toggle) { |
| 40 | + toggle.addEventListener('click', function () { |
| 41 | + var next = root.getAttribute('data-theme') === 'dark' ? 'light' : 'dark'; |
| 42 | + applyTheme(next); |
| 43 | + try { |
| 44 | + window.localStorage.setItem(THEME_KEY, next); |
| 45 | + } catch (error) { |
| 46 | + /* Storage can be blocked; the theme still applies for this page view. */ |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + // Filtering |
| 52 | + |
| 53 | + var searchIndex = releases.map(function (release) { |
| 54 | + return { |
| 55 | + element: release, |
| 56 | + id: release.id, |
| 57 | + text: (release.textContent || '').toLowerCase(), |
| 58 | + }; |
| 59 | + }); |
| 60 | + |
| 61 | + function filter(query) { |
| 62 | + var normalized = query.trim().toLowerCase(); |
| 63 | + var matchedIds = {}; |
| 64 | + var matches = 0; |
| 65 | + |
| 66 | + searchIndex.forEach(function (entry) { |
| 67 | + var isMatch = normalized === '' || entry.text.indexOf(normalized) !== -1; |
| 68 | + entry.element.hidden = !isMatch; |
| 69 | + if (isMatch) { |
| 70 | + matchedIds[entry.id] = true; |
| 71 | + matches += 1; |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + navItems.forEach(function (item) { |
| 76 | + item.hidden = !matchedIds[item.getAttribute('data-target')]; |
| 77 | + }); |
| 78 | + |
| 79 | + document.querySelectorAll('.nav__group').forEach(function (group) { |
| 80 | + var visible = group.querySelectorAll('.nav__item:not([hidden])').length; |
| 81 | + group.hidden = visible === 0; |
| 82 | + }); |
| 83 | + |
| 84 | + if (navEmpty) { |
| 85 | + navEmpty.hidden = matches !== 0; |
| 86 | + } |
| 87 | + if (contentEmpty) { |
| 88 | + contentEmpty.hidden = matches !== 0; |
| 89 | + } |
| 90 | + if (results) { |
| 91 | + results.hidden = normalized === ''; |
| 92 | + results.textContent = |
| 93 | + matches === 1 ? '1 matching release' : matches + ' matching releases'; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (search) { |
| 98 | + search.addEventListener('input', function () { |
| 99 | + filter(search.value); |
| 100 | + }); |
| 101 | + |
| 102 | + // Restore a filter that survived a reload (e.g. browser form restoration). |
| 103 | + if (search.value) { |
| 104 | + filter(search.value); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + document.addEventListener('keydown', function (event) { |
| 109 | + if (event.key === '/' && search && document.activeElement !== search) { |
| 110 | + event.preventDefault(); |
| 111 | + search.focus(); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + // Active release highlighting |
| 116 | + |
| 117 | + function setActive(id) { |
| 118 | + navItems.forEach(function (item) { |
| 119 | + item.classList.toggle('is-active', item.getAttribute('data-target') === id); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + if ('IntersectionObserver' in window && releases.length > 0) { |
| 124 | + var visible = new Set(); |
| 125 | + var observer = new IntersectionObserver( |
| 126 | + function (entries) { |
| 127 | + entries.forEach(function (entry) { |
| 128 | + if (entry.isIntersecting) { |
| 129 | + visible.add(entry.target.id); |
| 130 | + } else { |
| 131 | + visible.delete(entry.target.id); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + for (var i = 0; i < releases.length; i += 1) { |
| 136 | + if (visible.has(releases[i].id)) { |
| 137 | + setActive(releases[i].id); |
| 138 | + return; |
| 139 | + } |
| 140 | + } |
| 141 | + }, |
| 142 | + { rootMargin: '-96px 0px -60% 0px', threshold: 0 }, |
| 143 | + ); |
| 144 | + |
| 145 | + releases.forEach(function (release) { |
| 146 | + observer.observe(release); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + if (window.location.hash) { |
| 151 | + setActive(window.location.hash.slice(1)); |
| 152 | + } else if (releases.length > 0) { |
| 153 | + setActive(releases[0].id); |
| 154 | + } |
| 155 | +})(); |
0 commit comments