Basculer le menu
Changer de menu des préférences
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.

MediaWiki:Gadget-ScriptoriumInfo.js

Page de l’interface de MediaWiki

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: '#c8e6c9', color: '#27500a' },
        'A RELIRE':     { bg: '#EEEDFE', color: '#3C3489' },
        'A COMPLETER':  { bg: '#ffe0b2', color: '#854f0b' },
        'A CORRIGER':   { bg: '#ffcdd2', color: '#791f1f' },
        'A DISCUTER':   { bg: '#bbdefb', color: '#0c447c' },
        'OBSOLETE':     { bg: '#e0e0e0', color: '#444441' },
        'A SUPPRIMER':  { bg: '#f8bbd0', color: '#72243e' },
        'A CREER':      { bg: '#f0f0f0', color: '#222220' }
    };

    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';
        box.textContent = 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' ) {
            chipsRow.appendChild( buildChip( classif, null ) );
        } else {
            chipsRow.appendChild( buildChip( 'Classification manquante', { bg: '#ffcdd2', color: '#791f1f' } ) );
        }

        if ( biblio ) {
            chipsRow.appendChild( buildChip( biblio, null ) );
        }

        if ( sources ) {
            var srcList = sources.split( ',' );
            for ( var i = 0; i < srcList.length; i++ ) {
                var s = srcList[ i ].trim();
                if ( s ) {
                    chipsRow.appendChild( buildChip( s, null ) );
                }
            }
        }

        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 query = '[[' + pageName + ']]|?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 );
    } );

}() );
Les témoins (''cookies'') nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de témoins.