MediaWiki:Gadget-ScriptoriumInfo.js
Page de l’interface de MediaWiki
Autres actions
Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
- Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
- Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
- Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
/**
* Gadget ScriptoriumInfo
* Affiche les propriétés Scriptorium (Statut, Classification, Bibliothécaire, Notes, Sources)
* sous le titre de chaque page wiki sous forme de chips.
*/
( function () {
'use strict';
var pageName = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
var namespace = mw.config.get( 'wgNamespaceNumber' );
var action = mw.config.get( 'wgAction' );
if ( action !== 'view' ) {
return;
}
if ( namespace !== 0 && namespace !== 14 ) {
return;
}
var STATUT_COLORS = {
'VALIDE': { bg: '#d4edda', color: '#27500a' },
'A RELIRE': { bg: '#e2d9f3', color: '#3C3489' },
'A COMPLETER': { bg: '#fff3cd', color: '#854f0b' },
'A CORRIGER': { bg: '#f8d7da', color: '#791f1f' },
'A DISCUTER': { bg: '#d6e9f8', color: '#0c447c' },
'OBSOLETE': { bg: '#e0e0e0', color: '#444441' },
'A SUPPRIMER': { bg: '#f8bbd0', color: '#72243e' },
'A CREER': { bg: '#4f586e', color: '#dbdeea' }
};
function buildChip( text, style ) {
var chip = document.createElement( 'span' );
chip.className = 'scrip-chip';
chip.textContent = text;
if ( style ) {
if ( style.bg ) chip.style.backgroundColor = style.bg;
if ( style.color ) chip.style.color = style.color;
chip.style.border = 'none';
}
return chip;
}
function buildNotesBox( text ) {
var box = document.createElement( 'div' );
box.className = 'scrip-notes';
var icon = document.createElement( 'i' );
icon.className = 'fas fa-scroll';
icon.style.marginRight = '8px';
box.appendChild( icon );
box.appendChild( document.createTextNode( text ) );
return box;
}
function insertScriptoriumBar( data ) {
var statut = data.Statut || '';
var classif = data.Classification || '';
var biblio = data[ 'Bibliothécaire' ] || data[ 'Bibliothecaire' ] || '';
var notes = data.Notes || '';
var sources = data.Sources || '';
if ( !statut && !classif && !biblio && !notes && !sources ) {
return;
}
var container = document.createElement( 'div' );
container.id = 'scrip-bar';
var chipsRow = document.createElement( 'div' );
chipsRow.className = 'scrip-chips';
if ( statut ) {
var statutStyle = STATUT_COLORS[ statut ] || { bg: '#f0f0f0', color: '#444' };
chipsRow.appendChild( buildChip( statut, statutStyle ) );
}
if ( classif && classif !== 'NON CLASSIFIE' ) {
var classifLink = document.createElement( 'a' );
classifLink.href = mw.util.getUrl( 'AC:Scriptorium' ) + '#' + classif.replace( / /g, '_' );
classifLink.className = 'scrip-chip';
classifLink.textContent = classif;
classifLink.title = 'Voir dans le Scriptorium';
chipsRow.appendChild( classifLink );
} else {
chipsRow.appendChild( buildChip( 'Classification manquante', { bg: '#ffcdd2', color: '#791f1f' } ) );
}
if ( biblio ) {
var biblioChip = buildChip( '', null );
var biblioIcon = document.createElement( 'i' );
biblioIcon.className = 'fa-solid fa-user';
biblioIcon.style.marginRight = '6px';
biblioChip.appendChild( biblioIcon );
biblioChip.appendChild( document.createTextNode( biblio ) );
chipsRow.appendChild( biblioChip );
}
container.appendChild( chipsRow );
if ( notes ) {
container.appendChild( buildNotesBox( notes ) );
}
var headingContainer = document.querySelector( '.firstHeading-container' ) ||
( document.getElementById( 'firstHeading' ) || {} ).parentNode;
if ( headingContainer ) {
headingContainer.appendChild( container );
}
}
function extractValue( printouts, prop ) {
if ( !printouts[ prop ] || !printouts[ prop ].length ) {
return '';
}
var values = [];
for ( var i = 0; i < printouts[ prop ].length; i++ ) {
var val = printouts[ prop ][ i ];
if ( typeof val === 'object' && val.fulltext ) {
values.push( val.fulltext );
} else if ( typeof val === 'string' ) {
values.push( val );
} else if ( typeof val === 'number' ) {
values.push( String( val ) );
}
}
return values.join( ', ' );
}
var apiUrl = mw.util.wikiScript( 'api' );
var queryName = ( namespace === 14 ) ? ':' + pageName : pageName;
var query = '[[' + queryName + ']]|?Statut|?Classification|?Bibliothécaire|?Notes|?Sources';
$.ajax( {
url: apiUrl,
data: {
action: 'ask',
query: query,
format: 'json'
},
dataType: 'json'
} ).done( function ( response ) {
if ( !response.query || !response.query.results ) {
return;
}
var results = response.query.results;
var pageData = null;
for ( var key in results ) {
if ( results.hasOwnProperty( key ) ) {
pageData = results[ key ];
break;
}
}
if ( !pageData || !pageData.printouts ) {
return;
}
var data = {
Statut: extractValue( pageData.printouts, 'Statut' ),
Classification: extractValue( pageData.printouts, 'Classification' ),
'Bibliothécaire': extractValue( pageData.printouts, 'Bibliothécaire' ),
Notes: extractValue( pageData.printouts, 'Notes' ),
Sources: extractValue( pageData.printouts, 'Sources' )
};
insertScriptoriumBar( data );
} );
}() );