// Links that only change this page's query (a time range, a tab, a resident) // swap the page's main in place instead of loading a new page, so whatever // runs behind it (the garden skin's scene) keeps going. The links stay real // links: without fetch, or when the fetch fails, they navigate as before. (function () { var main = document.querySelector('main'); if (!main || !window.fetch || !window.DOMParser || !history.pushState) return; var pending = null; // Runtime marks a skin adds to the page's own elements (the garden's // g-sheet) survive the swap; everything else follows the server's markup. var keep = function (c) { return c.indexOf('g-') === 0; }; function attrs(a, b) { for (var i = a.attributes.length - 1; i >= 0; i--) { var n = a.attributes[i].name; if (n !== 'class' && !b.hasAttribute(n)) a.removeAttribute(n); } for (var j = 0; j < b.attributes.length; j++) { var at = b.attributes[j]; if (at.name !== 'class' && a.getAttribute(at.name) !== at.value) a.setAttribute(at.name, at.value); } var cls = (b.getAttribute('class') || '').split(/\s+/).filter(Boolean); Array.prototype.forEach.call(a.classList, function (c) { if (keep(c) && cls.indexOf(c) < 0) cls.push(c); }); if ((a.getAttribute('class') || '') !== cls.join(' ')) { if (cls.length) a.setAttribute('class', cls.join(' ')); else a.removeAttribute('class'); } } // Patch a to look like b, keeping a's nodes wherever the shape matches. function morph(a, b) { if (a.nodeType !== 1) { if (a.nodeValue !== b.nodeValue) a.nodeValue = b.nodeValue; return; } attrs(a, b); var ak = a.childNodes, bk = Array.prototype.slice.call(b.childNodes); if (ak.length !== bk.length) { a.replaceChildren.apply(a, bk.map(function (n) { return document.importNode(n, true); })); return; } for (var i = 0; i < bk.length; i++) { var x = ak[i], y = bk[i]; if (x.nodeType !== y.nodeType || x.nodeName !== y.nodeName) a.replaceChild(document.importNode(y, true), x); else if (!x.isEqualNode(y)) morph(x, y); } } function go(url, push) { if (pending) pending.abort(); var ctl = pending = new AbortController(); main.setAttribute('aria-busy', 'true'); fetch(url, { signal: ctl.signal, credentials: 'same-origin', headers: { accept: 'text/html' } }) .then(function (r) { if (!r.ok) throw new Error(String(r.status)); return r.text(); }) .then(function (html) { var doc = new DOMParser().parseFromString(html, 'text/html'); var next = doc.querySelector('main'); if (!next) throw new Error('no main'); morph(main, next); main.removeAttribute('aria-busy'); if (doc.title) document.title = doc.title; if (push) history.pushState({ swap: 1 }, '', url); pending = null; document.dispatchEvent(new CustomEvent('zds:swap')); }) .catch(function (e) { if (e && e.name === 'AbortError') return; location.href = url; }); } document.addEventListener('click', function (e) { if (e.defaultPrevented || e.button || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return; var a = e.target.closest && e.target.closest('main a[href]'); if (!a || a.target || a.hasAttribute('download')) return; var u = new URL(a.href, location.href); if (u.origin !== location.origin || u.pathname !== location.pathname || u.hash || u.search === location.search) return; e.preventDefault(); go(u.href, true); }); addEventListener('popstate', function () { go(location.href, false); }); })();