MediaWiki:Common.js

From M365Docs
Revision as of 08:55, 25 September 2026 by Tisuiss (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.
/* Marque les visiteurs non connectés */
if ( mw.config.get( 'wgUserName' ) === null ) {
    document.documentElement.classList.add( 'user-anon' );
}

/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using( 'mediawiki.util' ).then( function () {
    if ( mw.config.get( 'wgUserGroups' ).indexOf( 'sysop' ) !== -1 &&
         mw.config.get( 'wgArticleId' ) > 0 ) {
        mw.util.addPortletLink(
            'p-tb',
            mw.util.getUrl( 'Special:ChangeContentModel/' + mw.config.get( 'wgPageName' ) ),
            'Changer le modèle de contenu'
        );
    }
} );

/* Déplacer "Edit source" et "History" dans le menu Page tools */
$( function () {
    // Liste du bloc "Page tools" (repérée via Move / Delete / Protect)
    var $target = $( '#ca-move, #ca-delete, #ca-protect' ).first().closest( 'ul' );

    // Si l'utilisateur n'a pas ces droits, on utilise le bloc "More"
    if ( !$target.length ) {
        $target = $( '#t-info, #t-whatlinkshere' ).first().closest( 'ul' );
    }
    if ( !$target.length ) {
        return;
    }

    // Placés en tête de liste : Edit source, puis History
    $( '#ca-history' ).prependTo( $target );
    $( '#ca-edit, #ca-ve-edit, #ca-viewsource' ).prependTo( $target );
} );

/* Cadre "Arborescence" dans la colonne de droite */
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( function () {
    var ns = mw.config.get( 'wgNamespaceNumber' );
    if ( mw.config.get( 'wgAction' ) !== 'view' || ns < 0 ) {
        return;
    }

    var title = mw.config.get( 'wgTitle' );               // ex. "TenantTool/App"
    var nsText = mw.config.get( 'wgFormattedNamespace' );  // ex. "Interne"
    var prefix = nsText ? nsText + ':' : '';
    var root = title.split( '/' )[ 0 ];                    // ex. "TenantTool"

    new mw.Api().get( {
        action: 'query',
        list: 'allpages',
        apnamespace: ns,
        apprefix: root + '/',
        apfilterredir: 'nonredirects',
        aplimit: 'max',
        formatversion: 2
    } ).then( function ( data ) {
        var pages = data.query.allpages;
        if ( !pages.length ) {
            return; // pas de sous-pages : pas de cadre
        }

        // Construction de l'arbre
        var tree = {};
        pages.forEach( function ( p ) {
            var rel = p.title.slice( prefix.length + root.length + 1 );
            var node = tree;
            rel.split( '/' ).forEach( function ( part ) {
                node[ part ] = node[ part ] || {};
                node = node[ part ];
            } );
        } );

        function item( fullTitle, label ) {
            var $li = $( '<li>' ).append(
                $( '<a>' ).attr( 'href', mw.util.getUrl( prefix + fullTitle ) ).text( label )
            );
            if ( fullTitle === title ) {
                $li.addClass( 'arbo-courant' );
            }
            return $li;
        }

        function build( node, path ) {
            var $ul = $( '<ul>' );
            Object.keys( node ).sort().forEach( function ( key ) {
                var full = path + '/' + key;
                var $li = item( full, key );
                if ( Object.keys( node[ key ] ).length ) {
                    $li.append( build( node[ key ], full ) );
                }
                $ul.append( $li );
            } );
            return $ul;
        }

        var $rootItem = item( root, root ).append( build( tree, root ) );

        // On copie le bloc "More" pour garder le style du thème
        var $model = $( '#t-whatlinkshere' ).closest( '[id^="p-"]' );
        if ( !$model.length ) {
            return;
        }
        var $box = $model.clone();
        $box.attr( 'id', 'p-arborescence' );
        $box.find( '[id]' ).removeAttr( 'id' );
        $box.find( 'h3' ).first().text( 'Arborescence' );
        $box.find( 'ul' ).first().empty().append( $rootItem );
        $box.insertBefore( $model );
    } );
} );

/* Bandeaux colorés selon le mot en gras : Info, Attention, Astuce... */
mw.hook( 'wikipage.content' ).add( function ( $content ) {
    var types = {
        'info': 'info',
        'note': 'info',
        'attention': 'warning',
        'avertissement': 'warning',
        'astuce': 'tip',
        'important': 'danger',
        'danger': 'danger'
    };
    $content.find( 'blockquote' ).each( function () {
        var mot = $( this ).find( 'p' ).first().find( 'strong' ).first().text()
            .toLowerCase().replace( /\s*:.*$/, '' ).trim();
        if ( types[ mot ] ) {
            $( this ).addClass( 'bandeau-' + types[ mot ] );
        }
    } );
} );

/* Bouton "Copier" sur les blocs de code */
mw.hook( 'wikipage.content' ).add( function ( $content ) {
    $content.find( 'pre' ).each( function () {
        var $pre = $( this );
        if ( $pre.parent().hasClass( 'code-copie' ) ) {
            return; // bouton déjà ajouté
        }
        $pre.wrap( '<div class="code-copie"></div>' );
        var $bouton = $( '<button type="button" class="code-copie-bouton">Copier</button>' );
        $bouton.on( 'click', function () {
            navigator.clipboard.writeText( $pre.text() ).then( function () {
                $bouton.text( 'Copié ✓' );
                setTimeout( function () {
                    $bouton.text( 'Copier' );
                }, 1500 );
            } );
        } );
        $pre.before( $bouton );
    } );
} );

/* ===== 7. Bouton "Bandeau" dans la barre d'édition (pages Markdown) ===== */
mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
    if ( mw.config.get( 'wgPageContentModel' ) !== 'markdown' ) {
        return; // uniquement sur les pages Markdown
    }

    function bandeau( texte ) {
        return {
            type: 'encapsulate',
            options: {
                pre: texte,
                peri: 'Texte',
                post: '',
                ownline: true
            }
        };
    }

    $textarea.wikiEditor( 'addToToolbar', {
        section: 'main',
        groups: {
            bandeaux: {
                tools: {
                    bandeau: {
                        label: 'Bandeau',
                        type: 'select',
                        list: {
                            info: {
                                label: 'ℹ️ Info',
                                action: bandeau( '> ℹ️ **Info :** ' )
                            },
                            attention: {
                                label: '⚠️ Attention',
                                action: bandeau( '> ⚠️ **Attention :** ' )
                            },
                            astuce: {
                                label: '✅ Astuce',
                                action: bandeau( '> ✅ **Astuce :** ' )
                            },
                            important: {
                                label: '⛔ Important',
                                action: bandeau( '> ⛔ **Important :** ' )
                            }
                        }
                    }
                }
            }
        }
    } );
} );