MediaWiki:Vector-2022.js

Revision as of 23:17, 13 October 2026 by KurthBemis (talk | contribs)

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* UVLists: show sidebar sections initially, then remember visitor choices. */
(function () {
    if (mw.config.get('wgUserName')) {
        return;
    }

    var root = document.documentElement;
    var sections = ['vector-main-menu', 'vector-toc'];
    var prefix = 'uvlists.sidebar.';
    var observer;

    function readChoice(section) {
        try {
            return localStorage.getItem(prefix + section);
        } catch (e) {
            return null;
        }
    }

    function saveChoice(section, pinned) {
        try {
            localStorage.setItem(prefix + section, pinned ? '1' : '0');
        } catch (e) {
            // Storage may be unavailable in some browser configurations.
        }
    }

    // Record actual visitor clicks, ignoring our initialization clicks.
    document.addEventListener('click', function (event) {
        if (!event.isTrusted || !(event.target instanceof Element)) {
            return;
        }

        var button = event.target.closest('[data-event-name]');
        if (!button) {
            return;
        }

        var name = button.getAttribute('data-event-name');

        sections.forEach(function (section) {
            if (name === 'pinnable-header.' + section + '.pin') {
                saveChoice(section, true);
            } else if (name === 'pinnable-header.' + section + '.unpin') {
                saveChoice(section, false);
            }
        });
    }, true);

    function restoreChoices() {
        if (!root.classList.contains('vector-animations-ready')) {
            return false;
        }

        sections.forEach(function (section) {
            // No saved choice means show the section initially.
            var action = readChoice(section) === '0' ? 'unpin' : 'pin';
            var button = document.querySelector(
                '[data-event-name="pinnable-header.' +
                section + '.' + action + '"]'
            );

            if (button) {
                button.click();
            }
        });

        return true;
    }

    if (!restoreChoices()) {
        observer = new MutationObserver(function () {
            if (root.classList.contains('vector-animations-ready')) {
                observer.disconnect();
                restoreChoices();
            }
        });

        observer.observe(root, {
            attributes: true,
            attributeFilter: ['class']
        });

        window.setTimeout(function () {
            observer.disconnect();
        }, 15000);
    }
}());