« MediaWiki:Common.js » : différence entre les versions

De eMushpedia
Aller à la navigation Aller à la recherche
m missing semicolon
Aucun résumé des modifications
 
(15 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
/* Tout JavaScript présent ici sera exécuté par tous les utilisateurs à chaque chargement de page. */
/* Handle the theme switcher */
( function () {
    'use strict';


//override selectable languages for translation
var THEMES = [
wgULSLanguages = [];
    { id: 'wiki',    label: 'Wiki' },
wgULSLanguages.fr = 'french';
    { id: 'space',    label: 'Espace' },
wgULSLanguages.en = 'english';
    { id: 'terminal', label: 'Terminal' }
wgULSLanguages.es = 'spanish';
];
wgULSAcceptLanguageList = ["fr", "en", "es"];
var DEFAULT = 'space';
var KEY = 'emush-theme';
 
    // Read the stored theme, falling back to the first one
    function current() {
        try {
            var s = localStorage.getItem( KEY );
            for ( var i = 0; i < THEMES.length; i++ ) {
                if ( THEMES[ i ].id === s ) { return s; }
            }
        } catch ( e ) {}
        return THEMES[ 0 ].id;
    }
 
    // Swap the view-* class on <html> and persist the choice
    function apply( theme ) {
        var root = document.documentElement;
        THEMES.forEach( function ( t ) { root.classList.remove( 'view-' + t.id ); } );
        root.classList.add( 'view-' + theme );
        try { localStorage.setItem( KEY, theme ); } catch ( e ) {}
 
        // Notify other scripts that the layout may have changed
        mw.hook( 'emush.theme' ).fire( theme );
    }
 
    $( function () {
        var li = document.getElementById( 'pt-themetoggle' );
        if ( !li ) { return; }
        var trigger = li.querySelector( 'a' );
        if ( !trigger ) { return; }
 
        // Build the panel — hidden and absolutely positioned, so no layout shift
        var menu = document.createElement( 'ul' );
        menu.id = 'emush-theme-menu';
 
        THEMES.forEach( function ( t ) {
            var item = document.createElement( 'li' );
            var link = document.createElement( 'a' );
            link.href = '#';
            link.textContent = t.label;
            link.dataset.theme = t.id;
            item.appendChild( link );
            menu.appendChild( item );
        } );
        li.appendChild( menu );
 
        // Flag the entry matching the active theme
        function mark() {
            var now = current();
            menu.querySelectorAll( 'a' ).forEach( function ( a ) {
                a.classList.toggle( 'emush-active', a.dataset.theme === now );
            } );
        }
        mark();
 
        function close() {
            li.classList.remove( 'emush-open' );
            trigger.setAttribute( 'aria-expanded', 'false' );
        }
 
        trigger.setAttribute( 'aria-haspopup', 'true' );
        trigger.setAttribute( 'aria-expanded', 'false' );
 
        trigger.addEventListener( 'click', function ( e ) {
            e.preventDefault();
            var open = li.classList.toggle( 'emush-open' );
            trigger.setAttribute( 'aria-expanded', open ? 'true' : 'false' );
        } );
 
        menu.addEventListener( 'click', function ( e ) {
            var a = e.target.closest( 'a[data-theme]' );
            if ( !a ) { return; }
            e.preventDefault();
            apply( a.dataset.theme );
            mark();
            close();
        } );
 
        // Close on outside click or Escape
        document.addEventListener( 'click', function ( e ) {
            if ( !li.contains( e.target ) ) { close(); }
        } );
        document.addEventListener( 'keydown', function ( e ) {
            if ( e.key === 'Escape' ) { close(); }
        } );
    } );
}() );
 
 
/* Ensure the left panel is always the same height as #content */
$( function () {
    var panel = document.getElementById( 'mw-panel' );
    var content = document.getElementById( 'content' );
    if ( !panel || !content ) { return; }
 
    // Set an exact height; min-height would only act as a floor
    function sync() {
        panel.style.minHeight = '0';
 
        // Convert 1em to pixels using the panel's own font size
        var em = parseFloat( getComputedStyle( panel ).fontSize );
        panel.style.height = ( content.getBoundingClientRect().height - em ) + 'px';
    }
    sync();
 
    // Recompute when the content box changes size
    if ( window.ResizeObserver ) {
        new ResizeObserver( sync ).observe( content );
    } else {
        window.addEventListener( 'resize', sync );
    }
} );
 
 
/* Make the logo at the top center redirect to the home page */
$( function () {
    var sidebarLink = document.querySelector( '#p-logo a' );
    if ( !sidebarLink || document.getElementById( 'emush-logo-hotspot' ) ) { return; }
 
    var hotspot = document.createElement( 'a' );
    hotspot.id = 'emush-logo-hotspot';
    hotspot.href = sidebarLink.getAttribute( 'href' );
    hotspot.title = sidebarLink.getAttribute( 'title' ) || '';
    hotspot.setAttribute( 'aria-label', 'Accueil' );
    document.body.appendChild( hotspot );
} );

Dernière version du 5 août 2026 à 17:04

/* Handle the theme switcher */
( function () {
    'use strict';

	var THEMES = [
	    { id: 'wiki',     label: 'Wiki' },
	    { id: 'space',    label: 'Espace' },
	    { id: 'terminal', label: 'Terminal' }
	];
	var DEFAULT = 'space';
	var KEY = 'emush-theme';

    // Read the stored theme, falling back to the first one
    function current() {
        try {
            var s = localStorage.getItem( KEY );
            for ( var i = 0; i < THEMES.length; i++ ) {
                if ( THEMES[ i ].id === s ) { return s; }
            }
        } catch ( e ) {}
        return THEMES[ 0 ].id;
    }

    // Swap the view-* class on <html> and persist the choice
    function apply( theme ) {
        var root = document.documentElement;
        THEMES.forEach( function ( t ) { root.classList.remove( 'view-' + t.id ); } );
        root.classList.add( 'view-' + theme );
        try { localStorage.setItem( KEY, theme ); } catch ( e ) {}

        // Notify other scripts that the layout may have changed
        mw.hook( 'emush.theme' ).fire( theme );
    }

    $( function () {
        var li = document.getElementById( 'pt-themetoggle' );
        if ( !li ) { return; }
        var trigger = li.querySelector( 'a' );
        if ( !trigger ) { return; }

        // Build the panel — hidden and absolutely positioned, so no layout shift
        var menu = document.createElement( 'ul' );
        menu.id = 'emush-theme-menu';

        THEMES.forEach( function ( t ) {
            var item = document.createElement( 'li' );
            var link = document.createElement( 'a' );
            link.href = '#';
            link.textContent = t.label;
            link.dataset.theme = t.id;
            item.appendChild( link );
            menu.appendChild( item );
        } );
        li.appendChild( menu );

        // Flag the entry matching the active theme
        function mark() {
            var now = current();
            menu.querySelectorAll( 'a' ).forEach( function ( a ) {
                a.classList.toggle( 'emush-active', a.dataset.theme === now );
            } );
        }
        mark();

        function close() {
            li.classList.remove( 'emush-open' );
            trigger.setAttribute( 'aria-expanded', 'false' );
        }

        trigger.setAttribute( 'aria-haspopup', 'true' );
        trigger.setAttribute( 'aria-expanded', 'false' );

        trigger.addEventListener( 'click', function ( e ) {
            e.preventDefault();
            var open = li.classList.toggle( 'emush-open' );
            trigger.setAttribute( 'aria-expanded', open ? 'true' : 'false' );
        } );

        menu.addEventListener( 'click', function ( e ) {
            var a = e.target.closest( 'a[data-theme]' );
            if ( !a ) { return; }
            e.preventDefault();
            apply( a.dataset.theme );
            mark();
            close();
        } );

        // Close on outside click or Escape
        document.addEventListener( 'click', function ( e ) {
            if ( !li.contains( e.target ) ) { close(); }
        } );
        document.addEventListener( 'keydown', function ( e ) {
            if ( e.key === 'Escape' ) { close(); }
        } );
    } );
}() );


/* Ensure the left panel is always the same height as #content */
$( function () {
    var panel = document.getElementById( 'mw-panel' );
    var content = document.getElementById( 'content' );
    if ( !panel || !content ) { return; }

    // Set an exact height; min-height would only act as a floor
    function sync() {
        panel.style.minHeight = '0';

        // Convert 1em to pixels using the panel's own font size
        var em = parseFloat( getComputedStyle( panel ).fontSize );
        panel.style.height = ( content.getBoundingClientRect().height - em ) + 'px';
    }
    sync();

    // Recompute when the content box changes size
    if ( window.ResizeObserver ) {
        new ResizeObserver( sync ).observe( content );
    } else {
        window.addEventListener( 'resize', sync );
    }
} );


/* Make the logo at the top center redirect to the home page */
$( function () {
    var sidebarLink = document.querySelector( '#p-logo a' );
    if ( !sidebarLink || document.getElementById( 'emush-logo-hotspot' ) ) { return; }

    var hotspot = document.createElement( 'a' );
    hotspot.id = 'emush-logo-hotspot';
    hotspot.href = sidebarLink.getAttribute( 'href' );
    hotspot.title = sidebarLink.getAttribute( 'title' ) || '';
    hotspot.setAttribute( 'aria-label', 'Accueil' );
    document.body.appendChild( hotspot );
} );