/* THE FOUR SERVICE CARDS, AND WHAT DROPS OPEN UNDER THEM. * * Taufik: cards like "The latest" on a chapter page, and pressing one drops * down to reveal the content. The cards ARE those cards — same markup, same * stylesheet, same rail script for the arrows. This file is only the reveal. * * A CARD IS A BUTTON THAT SAYS WHAT IT IS. `aria-expanded` on the card and * `aria-controls` pointing at the panel is the whole of the accessibility * story here, and it is also how the stylesheet knows which card is open — * one source of truth rather than a class that has to be kept in step with an * attribute. * * MORE THAN ONE MAY BE OPEN. The first version closed the others, and it was * wrong: four services described in the same terms are meant to be compared, * and a reader who opens Traffic Vector to check it against Traffic Watch * should not have Traffic Watch taken away. Pressing an open card closes it. * * If this file never arrives the page still works: the panels are `hidden`, * but every card's address is a real fragment id and the headings are on the * page, so nothing is lost that was not decoration. */ (function () { /* THE GUARD IS THE BUTTONS, NOT THE RAIL. It used to be `[data-mobrail]`, which was true while this only ever ran on one page. The Sovereign page now uses the same drop-downs from a row of chips and has no rail at all, and gating on the rail would have loaded the script and done nothing — the most confusing kind of nothing, because everything looks present. */ var cards = [].slice.call(document.querySelectorAll('[data-mobopen]')); if (!cards.length) return; var panelOf = function (id) { return document.querySelector('[data-mobpanel="' + id + '"]'); }; /* THE PILL ON THE CARD SAYS WHAT PRESSING IT WILL DO. The caret is flipped in CSS from `aria-expanded`, but the WORDS are set here rather than with `content:` in a pseudo-element: generated text is not reliably read out by a screen reader and cannot be translated. */ function label(card, words) { var pill = card.querySelector('.mobcard__more'); if (pill) pill.textContent = words; } function open(id) { var panel = panelOf(id); var card = document.querySelector('[data-mobopen="' + id + '"]'); if (!panel || !card) return; panel.hidden = false; card.setAttribute('aria-expanded', 'true'); label(card, 'Close'); /* MEASURED, NOT GUESSED. A height animation needs a number, and the number is whatever the content came to — so it is read once, animated to, and then released to `auto` so a browser zoom or a wrapped line later cannot leave the panel clipped at yesterday's height. */ var to = panel.scrollHeight; panel.style.maxHeight = '0px'; requestAnimationFrame(function () { panel.style.maxHeight = to + 'px'; setTimeout(function () { if (!panel.hidden) panel.style.maxHeight = 'none'; }, 380); }); /* THE REST OF THE ROW FALLS BACK. Their matte covers the window; ours covers the row, because these are drop-downs rather than modals and the panel underneath must stay readable. See `.is-picking` in mobility.css. */ var rail = card.closest('.mobrail'); if (rail) rail.classList.add('is-picking'); } function shut(id) { var panel = panelOf(id); var card = document.querySelector('[data-mobopen="' + id + '"]'); if (!panel || !card) return; panel.style.maxHeight = panel.scrollHeight + 'px'; requestAnimationFrame(function () { panel.style.maxHeight = '0px'; setTimeout(function () { panel.hidden = true; panel.style.maxHeight = ''; }, 320); }); card.setAttribute('aria-expanded', 'false'); label(card, 'Read it'); /* the row comes back up only when nothing at all is open — closing one of two open panels must not un-dim the row while the other is still there */ var rail = card.closest('.mobrail'); if (rail && !rail.parentNode.querySelector('.mobcard[aria-expanded="true"]')) { rail.classList.remove('is-picking'); } } function isOpen(card) { return card.getAttribute('aria-expanded') === 'true'; } cards.forEach(function (card) { card.addEventListener('click', function () { var id = card.getAttribute('data-mobopen'); if (isOpen(card)) { shut(id); return; } open(id); /* BRING IT INTO VIEW, but only if it is not already there. Scrolling a panel the reader can see is the animation nobody asked for. */ var panel = panelOf(id); setTimeout(function () { var r = panel.getBoundingClientRect(); if (r.top < 60 || r.top > innerHeight - 120) { panel.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }, 60); }); }); document.querySelectorAll('[data-mobshut]').forEach(function (x) { x.addEventListener('click', function () { var id = x.getAttribute('data-mobshut'); shut(id); var card = document.querySelector('[data-mobopen="' + id + '"]'); if (card) card.focus(); }); }); /* THE FOUR NAMES ON THE TITLE SLIDE open their panel rather than jumping to a heading inside something that is closed — which is what an ordinary fragment link would have done, landing the reader nowhere. */ document.querySelectorAll('[data-mobjump]').forEach(function (a) { a.addEventListener('click', function (e) { var id = a.getAttribute('data-mobjump'); var card = document.querySelector('[data-mobopen="' + id + '"]'); if (!card) return; // let the link do its normal thing e.preventDefault(); if (!isOpen(card)) open(id); var panel = panelOf(id); if (panel) panel.scrollIntoView({ behavior: 'smooth', block: 'start' }); }); }); /* ARRIVING ON A DEEP LINK. /smart-mobility#greenlight should open Greenlight, not scroll to a heading in a panel that is shut. */ (function () { var want = (location.hash || '').replace('#', ''); if (!want) return; var card = document.querySelector('[data-mobopen="' + want + '"]'); if (!card) return; open(want); setTimeout(function () { var panel = panelOf(want); if (panel) panel.scrollIntoView({ block: 'start' }); }, 120); })(); })();