MediaWiki:Citizen.js

MediaWiki interface page

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Sinfar resource pages: close Citizen's mobile Contents after a local jump.
 * Compatibility fix for nested labels in Citizen 2.8.5 / checkboxHack.
 * Keeps native fragment navigation, desktop Contents, and other menus intact.
 */
(function () {
    'use strict';

    function bindResourceContents() {
        var toc = document.getElementById('mw-panel-toc');
        var checkbox = document.getElementById('citizen-toc__checkbox');
        if (!document.body.classList.contains('skin-citizen') ||
            !document.querySelector('.mw-parser-output .sf-resource-page') ||
            !toc || !checkbox || toc.hasAttribute('data-sf-close-on-jump')) {
            return;
        }
        toc.setAttribute('data-sf-close-on-jump', '1');

        toc.addEventListener('click', function (event) {
            if (!checkbox.checked || event.defaultPrevented || event.button !== 0 ||
                event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) {
                return;
            }
            var target = event.target;
            if (target && target.nodeType !== 1) {
                target = target.parentElement;
            }
            var link = target && target.closest('a[href^="#"]');
            if (!link || !toc.contains(link) ||
                (link.target && link.target !== '_self') || link.hasAttribute('download')) {
                return;
            }
            var id;
            try {
                id = decodeURIComponent(link.getAttribute('href').slice(1));
            } catch (error) {
                return;
            }
            if (!id || (id !== 'top' && !document.getElementById(id))) {
                return;
            }

            checkbox.checked = false;
            checkbox.setAttribute('aria-expanded', 'false');
            checkbox.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
            // No preventDefault or manual scrolling: the browser owns the jump.
        });
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', bindResourceContents);
    } else {
        bindResourceContents();
    }
}());

/* BEGIN SINFAR GETTING STARTED JOURNEY v1 */
/* REVIEW ONLY: additive Citizen.js component, not a replacement.
 * Wiki content is complete without JS. Only .sf-setup is enhanced.
 * Keep Citizen_Resource_Contents_v1.js unchanged. No network/storage use. */
(function () {
    'use strict';
    function init() {
        document.querySelectorAll('.mw-parser-output .sf-setup:not([data-setup-bound])').forEach(function (root) {
            var routes = Array.from(root.querySelectorAll('.sf-setup-route'));
            var choices = Array.from(root.querySelectorAll('.sf-setup-choice a[href]'));
            if (routes.length !== 3 || choices.length !== 3) { return; }
            var records = [];
            root.querySelectorAll('.sf-setup-step').forEach(function (step, i) {
                var headline = step.querySelector('h3 .mw-headline');
                var body = step.querySelector('.sf-setup-body');
                if (!headline || !body) { return; }
                headline.tabIndex = -1;
                var button = document.createElement('button');
                button.type = 'button';
                button.className = 'sf-setup-toggle';
                button.textContent = headline.textContent;
                body.id = 'sf-setup-content-' + i;
                button.setAttribute('aria-controls', body.id);
                // Keep the original mw-headline ID and heading for native Contents.
                headline.textContent = '';
                headline.appendChild(button);
                var record = { step:step, button:button, body:body, route:step.closest('.sf-setup-route') };
                function setOpen(open) {
                    body.hidden = !open;
                    button.setAttribute('aria-expanded', String(open));
                }
                record.setOpen = setOpen;
                var first = record.route && record.route.querySelector('.sf-setup-step') === step;
                setOpen(!!first);
                button.addEventListener('click', function () { setOpen(body.hidden); });
                records.push(record);
            });
            if (!records.length) { return; }
            root.setAttribute('data-setup-bound', '1');
            root.classList.add('sf-setup-enhanced');
            routes.forEach(function (route) {
                var headline = route.querySelector('h2 .mw-headline');
                if (headline) { headline.tabIndex = -1; }
            });
            var tools = document.createElement('div');
            tools.className = 'sf-setup-tools';
            ['Expand all steps', 'Collapse all steps'].forEach(function (label, index) {
                var button = document.createElement('button');
                button.type = 'button'; button.textContent = label;
                button.addEventListener('click', function () {
                    records.filter(function (r) { return r.route && !r.route.hidden; }).forEach(function (r) { r.setOpen(index === 0); });
                });
                tools.appendChild(button);
            });
            routes[0].before(tools);
            function select(route) {
                routes.forEach(function (r) { r.hidden = r !== route; });
                choices.forEach(function (a) {
                    var selected = a.hash === '#' + route.getAttribute('data-setup-route');
                    if (selected) { a.setAttribute('aria-current', 'true'); }
                    else { a.removeAttribute('aria-current'); }
                });
            }
            function reveal(hash) {
                var id;
                try { id = decodeURIComponent(hash.slice(1)); } catch (error) { return; }
                var target = document.getElementById(id);
                if (!target || !root.contains(target)) { return; }
                var route = target.closest('.sf-setup-route');
                if (route) { select(route); }
                records.forEach(function (r) {
                    if (r.step.contains(target)) { r.setOpen(true); }
                });
            }
            // Capture reveals hidden destinations BEFORE native fragment navigation.
            // No preventDefault; Citizen still owns menu dismissal and the browser owns scrolling.
            document.addEventListener('click', function (event) {
                if (!root.isConnected || event.defaultPrevented || event.button !== 0 ||
                    event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) { return; }
                var node = event.target.nodeType === 1 ? event.target : event.target.parentElement;
                var a = node && node.closest('a[href]');
                if (!a || a.hasAttribute('download') || (a.target && a.target !== '_self')) { return; }
                var url = new URL(a.href, location.href);
                if (url.origin === location.origin && url.pathname === location.pathname && url.search === location.search && url.hash) {
                    reveal(url.hash);
                }
            }, true);
            window.addEventListener('hashchange', function () {
                if (root.isConnected) { reveal(location.hash); }
            });
            select(routes[0]);
            reveal(location.hash);
        });
    }
    if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); }
    else { init(); }
    if (window.mw && mw.hook) { mw.hook('wikipage.content').add(init); }
}());

/* END SINFAR GETTING STARTED JOURNEY v1 */