Jump to content

MediaWiki:Vector-2022.js: Difference between revisions

From UVLists
Created page with "→UVLists: pin the main menu for public visitors.: (function () { if (mw.config.get('wgUserName')) { return; } var root = document.documentElement; function pinMainMenu() { if (!root.classList.contains('vector-animations-ready')) { return false; } var button = document.querySelector( '[data-event-name="pinnable-header.vector-main-menu.pin"]' ); if (!button) { return fal..."
 
No edit summary
Line 1: Line 1:
/* UVLists: pin the main menu for public visitors. */
/* UVLists: show sidebar sections initially, then remember visitor choices. */
(function () {
(function () {
     if (mw.config.get('wgUserName')) {
     if (mw.config.get('wgUserName')) {
         return;
         return;
     }
     }
     var root = document.documentElement;
     var root = document.documentElement;
     function pinMainMenu() {
    var sections = ['vector-main-menu', 'vector-toc'];
         if (!root.classList.contains('vector-animations-ready')) {
    var prefix = 'uvlists.sidebar.';
             return false;
    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 = document.querySelector(
 
            '[data-event-name="pinnable-header.vector-main-menu.pin"]'
         var button = event.target.closest('[data-event-name]');
        );
         if (!button) {
         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;
             return false;
         }
         }
         button.click();
 
         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;
         return true;
     }
     }
     if (pinMainMenu()) {
 
         return;
     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);
     }
     }
    var observer = new MutationObserver(function () {
        if (pinMainMenu()) {
            observer.disconnect();
        }
    });
    observer.observe(root, {
        attributes: true,
        attributeFilter: ['class']
    });
    // Stop watching if the expected controls never become available.
    window.setTimeout(function () {
        observer.disconnect();
    }, 15000);
}());
}());

Revision as of 23:17, 13 October 2026

/* 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);
    }
}());