|
| 1 | +/* Forge nav — "N NEW" news chip. |
| 2 | + * |
| 3 | + * Data source: files/news-meta.json (built by site/blog/build_blog.py). |
| 4 | + * Storage: localStorage["daslang:news:lastSeen"] = "YYYY-MM-DD". |
| 5 | + * |
| 6 | + * Semantics (mirrors blog-counter.js): |
| 7 | + * - First-time visitor (no key) → seeded from news-meta.json baseline_date, |
| 8 | + * which is the 2nd-newest entry's date. |
| 9 | + * So count = 1 (just the newest). |
| 10 | + * - Landing with hash "#news" → key set to newest_date; chip clears. |
| 11 | + * - Click on the news nav link → key set to newest_date. |
| 12 | + * - count > 9 → render "9+ NEW". |
| 13 | + * - count === 0 → chip not rendered. |
| 14 | + * |
| 15 | + * Nav anchors are matched by link.hash === "#news" so the same script works |
| 16 | + * from any page that links to "#news" / "index.html#news" / "../index.html#news". |
| 17 | + */ |
| 18 | +(function () { |
| 19 | + 'use strict'; |
| 20 | + |
| 21 | + var STORAGE_KEY = 'daslang:news:lastSeen'; |
| 22 | + |
| 23 | + function readLastSeen() { |
| 24 | + try { return window.localStorage.getItem(STORAGE_KEY); } |
| 25 | + catch (_) { return null; } |
| 26 | + } |
| 27 | + |
| 28 | + function writeLastSeen(date) { |
| 29 | + try { window.localStorage.setItem(STORAGE_KEY, date); } |
| 30 | + catch (_) { /* private mode / quota — silent */ } |
| 31 | + } |
| 32 | + |
| 33 | + function newsJsonUrl() { |
| 34 | + // Locate our own <script> tag and derive news-meta.json next to it. |
| 35 | + var scripts = document.getElementsByTagName('script'); |
| 36 | + for (var i = 0; i < scripts.length; i++) { |
| 37 | + var src = scripts[i].getAttribute('src') || ''; |
| 38 | + if (src.indexOf('news-counter.js') !== -1) { |
| 39 | + return src.replace(/news-counter\.js(\?.*)?$/, 'news-meta.json'); |
| 40 | + } |
| 41 | + } |
| 42 | + return 'files/news-meta.json'; |
| 43 | + } |
| 44 | + |
| 45 | + function findNewsLinks() { |
| 46 | + // Match any nav link whose URL hash is "#news" — works for |
| 47 | + // "#news", "index.html#news", "../index.html#news". |
| 48 | + var links = document.querySelectorAll('.forge-nav__links a[href]'); |
| 49 | + var out = []; |
| 50 | + for (var i = 0; i < links.length; i++) { |
| 51 | + if (links[i].hash === '#news') out.push(links[i]); |
| 52 | + } |
| 53 | + return out; |
| 54 | + } |
| 55 | + |
| 56 | + function renderChip(anchor, count) { |
| 57 | + if (anchor.querySelector('.forge-blog-chip')) return; |
| 58 | + var chip = document.createElement('span'); |
| 59 | + chip.className = 'forge-blog-chip'; |
| 60 | + chip.textContent = (count > 9 ? '9+' : count) + ' NEW'; |
| 61 | + anchor.appendChild(chip); |
| 62 | + anchor.classList.add('forge-blog-link'); |
| 63 | + } |
| 64 | + |
| 65 | + function clearChips() { |
| 66 | + var chips = document.querySelectorAll('.forge-nav__links a .forge-blog-chip'); |
| 67 | + for (var i = 0; i < chips.length; i++) { |
| 68 | + var parent = chips[i].parentNode; |
| 69 | + if (parent && parent.hash === '#news') { |
| 70 | + chips[i].remove(); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + function update(data) { |
| 76 | + // Landing-with-#news: mark seen and render nothing. Early-return so |
| 77 | + // that a silently-failed writeLastSeen (private mode / quota) cannot |
| 78 | + // still cause the chip to render — we are already on the destination. |
| 79 | + if (window.location.hash === '#news') { |
| 80 | + writeLastSeen(data.newest_date); |
| 81 | + return; |
| 82 | + } |
| 83 | + var anchors = findNewsLinks(); |
| 84 | + for (var k = 0; k < anchors.length; k++) { |
| 85 | + (function (a) { |
| 86 | + a.addEventListener('click', function () { |
| 87 | + writeLastSeen(data.newest_date); |
| 88 | + clearChips(); |
| 89 | + }); |
| 90 | + })(anchors[k]); |
| 91 | + } |
| 92 | + var lastSeen = readLastSeen() || data.baseline_date; |
| 93 | + var count = 0; |
| 94 | + for (var i = 0; i < data.news.length; i++) { |
| 95 | + if (data.news[i].date > lastSeen) count++; |
| 96 | + } |
| 97 | + if (count <= 0) return; |
| 98 | + for (var j = 0; j < anchors.length; j++) { |
| 99 | + renderChip(anchors[j], count); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + function start() { |
| 104 | + fetch(newsJsonUrl(), { cache: 'no-cache' }) |
| 105 | + .then(function (r) { return r.ok ? r.json() : null; }) |
| 106 | + .then(function (data) { if (data) update(data); }) |
| 107 | + .catch(function () { /* offline / missing — silent */ }); |
| 108 | + } |
| 109 | + |
| 110 | + if (document.readyState === 'loading') { |
| 111 | + document.addEventListener('DOMContentLoaded', start); |
| 112 | + } else { |
| 113 | + start(); |
| 114 | + } |
| 115 | +})(); |
0 commit comments