Jump to content

MediaWiki:Minerva.js

From Artemis Archive
Revision as of 13:34, 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 1 (Uncommented JS Style Attempt) */
// WARNING: This method uses JavaScript to generate icon URLs and apply styles.
// It might produce console warnings or fail if URL generation is incorrect on your setup.
$.when(mw.loader.using(['mediawiki.util']), $.ready).then(function() {

    var menu = 'p-personal';
    // For logged-in users, try 'p-interaction' first, then fallback to 'p-navigation'
    if (mw.config.get('wgUserId')) {
        menu = document.getElementById('p-interaction') ? 'p-interaction' : 'p-navigation';
    }

    // Function to safely get page URL
    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 V1] 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 V1] mw.config.get not available for wgArticlePath.');
                }
                url = prefix + encodeURIComponent(pageName.replace(/ /g, '_'));
            }
        } catch (e) {
            console.error('[Minerva.js V1] Error within getSafeUrl for "' + pageName + '":', e);
        }
        return url;
    }

    // --- Helper function: Creates structure AND applies manual style via JS ---
    function addIconWithJSStyle(anchorElement, iconName) { // Renamed for clarity
        if (!anchorElement) { console.warn('[Minerva.js V1] addIconWithJSStyle: anchorElement is invalid.'); return; }
        var originalText = anchorElement.textContent;
        anchorElement.innerHTML = ''; // Clear existing content

        // 1. Create icon span and add base classes
        var iconSpan = document.createElement('span');
        iconSpan.classList.add('mw-ui-icon', 'mw-ui-icon-element');
        // Add specific icon class for potential CSS targeting (though style is applied below)
        iconSpan.classList.add('mw-ui-icon-minerva-' + iconName);

        // --- Icon URL generation and style application (UNCOMMENTED) ---
        var iconPath = '#error-generating-icon-url';
        try {
            // Attempt to generate the icon URL using mw.util.getUrl
            iconPath = mw.util.getUrl(null, {
                'title': 'Special:Redirect/skin', // Placeholder title for URL generation
                'modules': 'skins.minerva.icons.images',
                'image': iconName,
                'format': 'original',
                'skin': mw.config.get('skin', 'minerva'), // Use current skin context
                'lang': mw.config.get('wgUserLanguage', 'en'), // Use user language
            });
            // Basic check if the generated path looks like a valid load.php URL
            if (!iconPath || !iconPath.includes('load.php')) {
                console.warn('[Minerva.js V1] Generated iconPath might be incorrect:', iconPath);
            }
        } catch(e) {
            console.error('[Minerva.js V1] Error generating icon URL for', iconName, e);
        }

        // Apply the generated icon path as a mask-image style if generation didn't fail
        if (iconPath !== '#error-generating-icon-url') {
            iconSpan.style.maskImage = 'url("' + iconPath + '")';
            iconSpan.style.webkitMaskImage = 'url("' + iconPath + '")'; // For compatibility
             console.log('[Minerva.js V1] Applied maskImage for icon:', iconName);
        } else {
            console.warn('[Minerva.js V1] Skipping maskImage style for', iconName, 'due to URL generation error.');
        }
        // --- End of style application section ---

        anchorElement.appendChild(iconSpan); // Add the icon span

        // 4. Create and add label span
        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>';
    var targetMenuUl = document.getElementById(menu);
    if (targetMenuUl) {
        targetMenuUl.appendChild(categoriesHeader);
        console.log('[Minerva.js V1] Added Categories header to menu:', menu);
    } else {
        console.error('[Minerva.js V1] Could not find menu element with ID:', menu);
    }

    // --- Add links for Categories ---
    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');
            addIconWithJSStyle(anchorLocation, 'mapPin'); // Use JS style function
        }
    }
    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');
            addIconWithJSStyle(anchorSpecies, 'mapPin'); // Use JS style function
        }
    }
    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');
            addIconWithJSStyle(anchorYear, 'mapPin'); // Use JS style function
        }
    }

    // --- 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>';
    if (targetMenuUl) { // Append to the same menu UL
        targetMenuUl.appendChild(resourcesHeader);
        console.log('[Minerva.js V1] Added Resources header.');
    }

    // --- Add links for Resources ---
    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');
            addIconWithJSStyle(anchorUseful, 'link'); // Use JS style function
        }
    }
    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');
            addIconWithJSStyle(anchorDocs, 'userContributions'); // Use JS style function
        }
    }
    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');
            addIconWithJSStyle(anchorStudies, 'speechBubbles'); // Use JS style function
        }
    }

    console.log('[Minerva.js V1] Script finished (Attempting manual style via JS).');

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