Jump to content

MediaWiki:Minerva.js

From Artemis Archive
Revision as of 13:47, 2 May 2025 by Bxuwd (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.
/* All JavaScript here will be loaded for users of the MinervaNeue skin */

/* likeMarioWiki: Custom menu - Version 3 (Manual Structure Mimicking Working Icons) */
// This version manually creates the LI > A > SPAN structure based on observed working icons.
$.when(mw.loader.using(['mediawiki.util']), $.ready).then(function() {

    var menuId = 'p-personal'; // Default menu ID
    // For logged-in users, try 'p-interaction' first, then fallback to 'p-navigation'
    if (mw.config.get('wgUserId')) {
        menuId = document.getElementById('p-interaction') ? 'p-interaction' : 'p-navigation';
    }
    // Final fallback if the chosen menu doesn't exist
    if (!document.getElementById(menuId)) {
         menuId = 'p-personal';
    }

    var targetMenuUl = document.getElementById(menuId);
    if (!targetMenuUl) {
        console.error('[Minerva.js V3] Could not find target menu UL element with ID:', menuId);
        return; // Stop if menu doesn't exist
    }

    // Function to safely get page URL (same as before)
    function getSafeUrl(pageName) {
        var url = '#error-generating-url';
        try {
            if (typeof mw !== 'undefined' && typeof mw.util !== 'undefined' && typeof mw.util.getUrl === 'function') {
                url = mw.util.getUrl(pageName);
            } else {
                console.warn('[Minerva.js V3] mw.util.getUrl not available.');
                var prefix = mw.config.get('wgArticlePath', '/wiki/$1').replace('$1', '');
                url = prefix + encodeURIComponent(pageName.replace(/ /g, '_'));
            }
        } catch (e) {
            console.error('[Minerva.js V3] Error in getSafeUrl for "' + pageName + '":', e);
        }
        return url;
    }

    // --- Helper function: Manually creates LI > A > SPAN(icon) + SPAN(label) structure ---
    function addMenuItem(targetUlElement, pageName, linkText, iconName) {
        try {
            // 1. Create list item (LI)
            var listItem = document.createElement('li');
            listItem.classList.add('mw-list-item', 'toggle-list-item'); // Add base classes for list item

            // 2. Create anchor (A)
            var anchor = document.createElement('a');
            anchor.href = getSafeUrl(pageName);
            anchor.classList.add('mw-list-item__label', 'toggle-list-item__anchor'); // Base classes for link

            // 3. Create icon span (SPAN) - INSIDE the anchor
            var iconSpan = document.createElement('span');
            iconSpan.classList.add('mw-ui-icon', 'mw-ui-icon-element', 'mw-ui-icon-minerva-' + iconName);
            // Add the simpler class too, just in case (like .minerva-icon--die from example)
            // Though mw-ui-icon-minerva-* should be the standard one targeted by CSS
            iconSpan.classList.add('minerva-icon--' + iconName);
            anchor.appendChild(iconSpan); // Append icon span to anchor

            // 4. Create label span (SPAN) - INSIDE the anchor, after icon
            var labelSpan = document.createElement('span');
            labelSpan.classList.add('toggle-list-item__label-text'); // Use a class for the text if needed, or just text node
            labelSpan.textContent = linkText;
            anchor.appendChild(labelSpan); // Append label span to anchor

            // 5. Append anchor to list item, and list item to menu
            listItem.appendChild(anchor);
            targetUlElement.appendChild(listItem);

            console.log('[Minerva.js V3] Added menu item:', linkText, 'with icon:', iconName);

        } catch (e) {
            console.error('[Minerva.js V3] Error adding menu item "' + linkText + '":', e);
        }
    }

    // --- Helper function: Adds a header item ---
    function addMenuHeader(targetUlElement, headerText) {
         try {
            var headerItem = document.createElement('li');
            // Note: Check if these classes are correct for headers in Minerva
            headerItem.classList.add('menu__item', 'menu__item--category', 'menu-header');
            headerItem.innerHTML = '<span class="menu-header-text">' + headerText + '</span>';
            targetUlElement.appendChild(headerItem);
            console.log('[Minerva.js V3] Added header:', headerText);
         } catch (e) {
             console.error('[Minerva.js V3] Error adding header "' + headerText + '":', e);
         }
    }

    // --- Add Categories section ---
    addMenuHeader(targetMenuUl, '— CATEGORIES —');
    addMenuItem(targetMenuUl, 'Animal_cruelty_cases_by_location', 'By Location/地点', 'mapPin'); // Use mapPin icon
    addMenuItem(targetMenuUl, 'Animal_cruelty_cases_by_species', 'By Species/物种', 'mapPin'); // Use mapPin icon
    addMenuItem(targetMenuUl, 'Animal_cruelty_cases_by_year', 'By Year/年份', 'mapPin'); // Use mapPin icon

    // --- Add Resources section ---
    addMenuHeader(targetMenuUl, '— RESOURCES —');
    addMenuItem(targetMenuUl, 'Useful_links', 'Useful links/实用链接', 'link'); // Use link icon
    addMenuItem(targetMenuUl, 'Documentaries', 'Documentaries/纪录片', 'userContributions'); // Use userContributions icon
    addMenuItem(targetMenuUl, 'Studies', 'Studies/研究', 'speechBubbles'); // Use speechBubbles icon

    console.log('[Minerva.js V3] Script finished.');

}); // End of $.when()