MediaWiki:Common.js: Difference between revisions
From M365Docs
No edit summary |
No edit summary |
||
| Line 27: | Line 27: | ||
$( '#ca-history' ).prependTo( $target ); | $( '#ca-history' ).prependTo( $target ); | ||
$( '#ca-edit, #ca-ve-edit, #ca-viewsource' ).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 ); | |||
} ); | |||
} ); | } ); | ||
Revision as of 10:19, 24 September 2026
/* 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 );
} );
} );