Jump to content

MediaWiki:Minerva.js

From Artemis Archive
Revision as of 13:09, 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 - Attempt 4b (Trying different require path for IconLoader) */
$.when(mw.loader.using(['mediawiki.util', 'mobile.icons']), $.ready).then(function() {

    var IconLoader = null; // Initialize IconLoader as null
    try {
        // Try accessing IconLoader as a property of the main module export
        var mobileIconsModule = require('mobile.icons');
        if (mobileIconsModule && typeof mobileIconsModule.IconLoader === 'object' && typeof mobileIconsModule.IconLoader.add === 'function') {
             IconLoader = mobileIconsModule.IconLoader;
             console.log('[Minerva.js] Successfully retrieved IconLoader from mobileIconsModule.IconLoader');
        } else if (mobileIconsModule && typeof mobileIconsModule.add === 'function') {
             // Maybe the module itself has the add method? Less likely based on name.
             IconLoader = mobileIconsModule; // Treat the module itself as the loader
             console.log('[Minerva.js] Retrieved IconLoader assuming module itself has .add method.');
        } else {
             console.warn('[Minerva.js] Could not find IconLoader.add function via mobileIconsModule.IconLoader or directly on module.');
             // Attempt the original path as a fallback, just in case
             try {
                 IconLoader = require('mobile.icons/IconLoader');
                 console.log('[Minerva.js] Successfully retrieved IconLoader using fallback path mobile.icons/IconLoader');
             } catch (e_fallback) {
                  console.error('[Minerva.js] Failed to get IconLoader using both primary and fallback methods.', e_fallback);
             }
        }
    } catch (e) {
        console.error('[Minerva.js] Error during require("mobile.icons") or accessing IconLoader:', e);
    }


    var menu = 'p-personal';
    if (mw.config.get('wgUserId')) {
        menu = document.getElementById('p-interaction') ? 'p-interaction' : 'p-navigation';
    }

    function getSafeUrl(pageName) { /* ... same as before ... */ 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] mw.util.getUrl not available, using basic fallback.'); var prefix = '/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, '_')); } } catch (e) { console.error('[Minerva.js] Error within getSafeUrl for "' + pageName + '":', e); } return url; }
    function addIconAndLabelStructure(anchorElement, iconClass) { /* ... same as before ... */ if (!anchorElement) { console.warn('[Minerva.js] addIconAndLabelStructure: anchorElement is invalid.'); return; } var originalText = anchorElement.textContent; anchorElement.innerHTML = ''; var iconSpan = document.createElement('span'); iconSpan.classList.add('mw-ui-icon', 'mw-ui-icon-element', iconClass); anchorElement.appendChild(iconSpan); var labelSpan = document.createElement('span'); labelSpan.classList.add('toggle-list-item__label'); labelSpan.textContent = originalText; 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>'; document.getElementById(menu).appendChild(categoriesHeader); console.log('[Minerva.js] Added Categories header.');

    // --- Add links for Categories (structure + classes) ---
    var liLocation = mw.util.addPortletLink(menu, getSafeUrl('Animal_cruelty_cases_by_location'), 'By Location/地点'); if (liLocation) { liLocation.classList.add('toggle-list-item'); var anchorLocation = liLocation.getElementsByTagName('a')[0]; if (anchorLocation) { anchorLocation.classList.add('toggle-list-item__anchor'); addIconAndLabelStructure(anchorLocation, 'mw-ui-icon-minerva-mapPin'); console.log('[Minerva.js] Added By Location link.'); } }
    var liSpecies = mw.util.addPortletLink(menu, getSafeUrl('Animal_cruelty_cases_by_species'), 'By Species/物种'); if (liSpecies) { liSpecies.classList.add('toggle-list-item'); var anchorSpecies = liSpecies.getElementsByTagName('a')[0]; if (anchorSpecies) { anchorSpecies.classList.add('toggle-list-item__anchor'); addIconAndLabelStructure(anchorSpecies, 'mw-ui-icon-minerva-mapPin'); console.log('[Minerva.js] Added By Species link.'); } }
    var liYear = mw.util.addPortletLink(menu, getSafeUrl('Animal_cruelty_cases_by_year'), 'By Year/年份'); if (liYear) { liYear.classList.add('toggle-list-item'); var anchorYear = liYear.getElementsByTagName('a')[0]; if (anchorYear) { anchorYear.classList.add('toggle-list-item__anchor'); addIconAndLabelStructure(anchorYear, 'mw-ui-icon-minerva-mapPin'); console.log('[Minerva.js] Added By Year link.'); } }

    // --- 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>'; document.getElementById(menu).appendChild(resourcesHeader); console.log('[Minerva.js] Added Resources header.');

    // --- Add links for Resources (structure + classes) ---
    var liUseful = mw.util.addPortletLink(menu, getSafeUrl('Useful_links'), 'Useful links/实用链接'); if (liUseful) { liUseful.classList.add('toggle-list-item'); var anchorUseful = liUseful.getElementsByTagName('a')[0]; if (anchorUseful) { anchorUseful.classList.add('toggle-list-item__anchor'); addIconAndLabelStructure(anchorUseful, 'mw-ui-icon-minerva-link'); console.log('[Minerva.js] Added Useful links.'); } }
    var liDocs = mw.util.addPortletLink(menu, getSafeUrl('Documentaries'), 'Documentaries/纪录片'); if (liDocs) { liDocs.classList.add('toggle-list-item'); var anchorDocs = liDocs.getElementsByTagName('a')[0]; if (anchorDocs) { anchorDocs.classList.add('toggle-list-item__anchor'); addIconAndLabelStructure(anchorDocs, 'mw-ui-icon-minerva-userContributions'); console.log('[Minerva.js] Added Documentaries link.'); } }
    var liStudies = mw.util.addPortletLink(menu, getSafeUrl('Studies'), 'Studies/研究'); if (liStudies) { liStudies.classList.add('toggle-list-item'); var anchorStudies = liStudies.getElementsByTagName('a')[0]; if (anchorStudies) { anchorStudies.classList.add('toggle-list-item__anchor'); addIconAndLabelStructure(anchorStudies, 'mw-ui-icon-minerva-speechBubbles'); console.log('[Minerva.js] Added Studies link.'); } }

    // --- Trigger IconLoader only if it was successfully loaded ---
    if (IconLoader && typeof IconLoader.add === 'function') { // Ensure IconLoader is valid and has the 'add' method
        var menuSelector = '#' + menu + ' .mw-ui-icon-element';
        var newIcons = document.querySelectorAll(menuSelector);
        if (newIcons.length > 0) {
            console.log('[Minerva.js] Found', newIcons.length, 'new icons to process. Attempting IconLoader.add().');
            try {
                IconLoader.add(newIcons); // <<< Try using the retrieved IconLoader
                console.log('[Minerva.js] IconLoader.add() called successfully.');
            } catch (e) {
                console.error('[Minerva.js] Error calling IconLoader.add():', e);
            }
        } else {
             console.log('[Minerva.js] No new icons found to process with selector:', menuSelector);
        }
    } else {
        console.warn('[Minerva.js] Skipping IconLoader.add() call because IconLoader object or .add method was not found.');
    }

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

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