Jump to content

MediaWiki:Minerva.js

From Artemis Archive
Revision as of 12:36, 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 */
$.when(mw.loader.using('mediawiki.util'), $.ready).then(function() {

    var menu = 'p-personal'; // Default menu location for personal links
    // If logged in, attempt to add to the interaction or navigation menu
    if (mw.config.get('wgUserId')) {
        menu = document.getElementById('p-interaction') ? 'p-interaction' : 'p-navigation';
        // Preferences link adding code removed as previously requested
    }

    // --- Function to generate safe URL ---
    function getSafeUrl(pageName) {
        var url = '#error-generating-url'; // Default fallback 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] mw.util.getUrl not available, using basic fallback.');
                var prefix = '/wiki/'; // Default to /wiki/
                if (typeof mw !== 'undefined' && typeof mw.config !== 'undefined' && typeof mw.config.get === 'function') {
                    prefix = mw.config.get('wgArticlePath', '/wiki/$1').replace('$1', '');
                } else {
                    console.warn('[Minerva.js] mw.config.get not available for wgArticlePath.');
                }
                url = prefix + encodeURIComponent(pageName.replace(/ /g, '_')); // Encode the page name
            }
        } catch (e) {
            console.error('[Minerva.js] Error within getSafeUrl for "' + pageName + '":', e);
        }
        return url;
    }

    // --- Helper function to correctly add icon and label structure ---
    // This function takes the anchor element (<a>) created by addPortletLink
    // and the desired icon class, then restructures the anchor's content.
    function addIconAndLabelStructure(anchorElement, iconClass) {
        if (!anchorElement) {
            console.warn('[Minerva.js] addIconAndLabelStructure: anchorElement is invalid.');
            return; // Safety check
        }
        var originalText = anchorElement.textContent; // Get the original link text
        anchorElement.innerHTML = ''; // Clear the anchor's existing content (the text node)

        // 1. Create and append the icon span
        var iconSpan = document.createElement('span');
        // Add classes based on inspection: mw-ui-icon, mw-ui-icon-element, and the specific icon class
        iconSpan.classList.add('mw-ui-icon', 'mw-ui-icon-element', iconClass);
        anchorElement.appendChild(iconSpan);

        // 2. Create and append the label span
        var labelSpan = document.createElement('span');
        // Add the class found on the text span in the screenshot
        labelSpan.classList.add('toggle-list-item__label');
        labelSpan.textContent = originalText; // Put the original text inside this span
        anchorElement.appendChild(labelSpan);
    }

    // --- Add Categories section header ---
    var categoriesHeader = document.createElement('li');
    categoriesHeader.classList.add('menu__item', 'menu__item--category', 'menu-header');
    categoriesHeader.innerHTML = '<span class="menu-header-text">— CATEGORIES —</span>'; // Keep using <span> for non-link header
    document.getElementById(menu).appendChild(categoriesHeader);

    // --- Add links for Categories with CORRECT icon structure ---
    // Get the anchor element returned by addPortletLink, then pass it to the helper function
    var anchorLocation = mw.util.addPortletLink(menu, getSafeUrl('Animal_cruelty_cases_by_location'), 'By Location/地点').getElementsByTagName('a')[0];
    addIconAndLabelStructure(anchorLocation, 'mw-ui-icon-minerva-mapPin'); // Use mapPin as requested

    var anchorSpecies = mw.util.addPortletLink(menu, getSafeUrl('Animal_cruelty_cases_by_species'), 'By Species/物种').getElementsByTagName('a')[0];
    addIconAndLabelStructure(anchorSpecies, 'mw-ui-icon-minerva-mapPin'); // Use mapPin as requested

    var anchorYear = mw.util.addPortletLink(menu, getSafeUrl('Animal_cruelty_cases_by_year'), 'By Year/年份').getElementsByTagName('a')[0];
    addIconAndLabelStructure(anchorYear, 'mw-ui-icon-minerva-settings'); // Use mapPin as requested

    // --- Add Resources section header ---
    var resourcesHeader = document.createElement('li');
    resourcesHeader.classList.add('menu__item', 'menu__item--category', 'menu-header');
    resourcesHeader.innerHTML = '<span class="menu-header-text">— RESOURCES —</span>'; // Keep using <span> for non-link header
    document.getElementById(menu).appendChild(resourcesHeader);

    // --- Add links for Resources with CORRECT icon structure ---
    var anchorUseful = mw.util.addPortletLink(menu, getSafeUrl('Useful_links'), 'Useful links/实用链接').getElementsByTagName('a')[0];
    addIconAndLabelStructure(anchorUseful, 'mw-ui-icon-minerva-link'); // Keep using 'link' icon

    var anchorDocs = mw.util.addPortletLink(menu, getSafeUrl('Documentaries'), 'Documentaries/纪录片').getElementsByTagName('a')[0];
    addIconAndLabelStructure(anchorDocs, 'mw-ui-icon-minerva-userContributions'); // Use userContributions as requested

    var anchorStudies = mw.util.addPortletLink(menu, getSafeUrl('Studies'), 'Studies/研究').getElementsByTagName('a')[0];
    addIconAndLabelStructure(anchorStudies, 'mw-ui-icon-minerva-speechBubbles'); // Use speechBubbles as requested

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