Jump to content

MediaWiki:Vector-2022.js: Difference between revisions

From UVLists
No edit summary
mNo edit summary
 
Line 1: Line 1:
/* UVLists: show sidebar sections initially, then remember visitor choices. */
/* UVLists: show the main menu in the sidebar for logged-out visitors unless
  they have hidden it. Vector keeps the table of contents pinned and
  remembers that choice itself; logged-in users have a saved preference. */
(function () {
(function () {
    var key = 'uvlists.sidebar.vector-main-menu';
     if (mw.config.get('wgUserName')) {
     if (mw.config.get('wgUserName')) {
         return;
         return;
     }
     }


    var root = document.documentElement;
     function hidden() {
    var sections = ['vector-main-menu', 'vector-toc'];
    var prefix = 'uvlists.sidebar.';
    var observer;
 
     function readChoice(section) {
         try {
         try {
             return localStorage.getItem(prefix + section);
             return localStorage.getItem(key) === '0';
         } catch (e) {
         } catch (e) {
             return null;
             return false;
        }
    }
 
    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.
     // Remember the visitor's own hide and show clicks.
     document.addEventListener('click', function (event) {
     document.addEventListener('click', function (event) {
         if (!event.isTrusted || !(event.target instanceof Element)) {
         var button = event.isTrusted && event.target instanceof Element &&
             return;
             event.target.closest('[data-event-name^="pinnable-header.vector-main-menu."]');
        }
         if (button) {
 
             try {
        var button = event.target.closest('[data-event-name]');
                localStorage.setItem(key, /\.pin$/.test(button.getAttribute('data-event-name')) ? '1' : '0');
         if (!button) {
            } catch (e) {
             return;
                // Storage may be unavailable; the default applies next time.
            }
         }
         }
        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);
     }, true);


     function restoreChoices() {
     mw.loader.using('skins.vector.js').then(function () {
        if (!root.classList.contains('vector-animations-ready')) {
         $(function () {
            return false;
             // The sidebar column exists only at 1120px and wider. Pinning
         }
             // there lets Vector unpin and repin as the window is resized.
 
             var pin = document.querySelector('[data-event-name="pinnable-header.vector-main-menu.pin"]');
        sections.forEach(function (section) {
             if (pin && !hidden() && window.matchMedia('(min-width: 1120px)').matches &&
             // No saved choice means show the section initially.
                !document.documentElement.classList.contains('vector-feature-main-menu-pinned-enabled')) {
             var action = readChoice(section) === '0' ? 'unpin' : 'pin';
                 pin.click();
             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);
    }
}());
}());

Latest revision as of 23:52, 13 October 2026

/* UVLists: show the main menu in the sidebar for logged-out visitors unless
   they have hidden it. Vector keeps the table of contents pinned and
   remembers that choice itself; logged-in users have a saved preference. */
(function () {
    var key = 'uvlists.sidebar.vector-main-menu';

    if (mw.config.get('wgUserName')) {
        return;
    }

    function hidden() {
        try {
            return localStorage.getItem(key) === '0';
        } catch (e) {
            return false;
        }
    }

    // Remember the visitor's own hide and show clicks.
    document.addEventListener('click', function (event) {
        var button = event.isTrusted && event.target instanceof Element &&
            event.target.closest('[data-event-name^="pinnable-header.vector-main-menu."]');
        if (button) {
            try {
                localStorage.setItem(key, /\.pin$/.test(button.getAttribute('data-event-name')) ? '1' : '0');
            } catch (e) {
                // Storage may be unavailable; the default applies next time.
            }
        }
    }, true);

    mw.loader.using('skins.vector.js').then(function () {
        $(function () {
            // The sidebar column exists only at 1120px and wider. Pinning
            // there lets Vector unpin and repin as the window is resized.
            var pin = document.querySelector('[data-event-name="pinnable-header.vector-main-menu.pin"]');
            if (pin && !hidden() && window.matchMedia('(min-width: 1120px)').matches &&
                !document.documentElement.classList.contains('vector-feature-main-menu-pinned-enabled')) {
                pin.click();
            }
        });
    });
}());