MediaWiki:Common.js
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.
/* Tout JavaScript présent ici sera exécuté par tous les utilisateurs à chaque chargement de page. */
/**
* Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
* loaded for all users on every wiki page. If possible create a gadget that is
* enabled by default instead of adding it here (since gadgets are fully
* optimized ResourceLoader modules with possibility to add dependencies etc.)
*
* Since Common.js isn't a gadget, there is no place to declare its
* dependencies, so we have to lazy load them with mw.loader.using on demand and
* then execute the rest in the callback. In most cases these dependencies will
* be loaded (or loading) already and the callback will not be delayed. In case a
* dependency hasn't arrived yet it'll make sure those are loaded before this.
*/
/* global mw, $ */
/* jshint strict:false, browser:true */
// Modern ES6+ syntax is standard across all MediaWiki supported browsers
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
/* Begin of mw.loader.using callback */
/**
* Map addPortletLink to mw.util
* @deprecated
*/
mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );
/**
* Load JS and CSS by URL
*/
var extraCSS = mw.util.getParamValue( 'withCSS' ),
extraJS = mw.util.getParamValue( 'withJS' );
if ( extraCSS ) {
if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
}
}
if ( extraJS ) {
if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
}
}
/**
* Collapsible tables (shim for legacy .collapsible)
*/
mw.hook( 'wikipage.content' ).add( function ( $content ) {
var $tables = $content.find( 'table.collapsible:not(.mw-collapsible)' );
if ( !$tables.length ) { return; }
$tables.addClass( 'mw-collapsible' ).each( function () {
if ( $( this ).hasClass( 'collapsed' ) ) {
$( this ).addClass( 'mw-collapsed' );
}
} );
mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
$tables.makeCollapsible();
} );
} );
/**
* Support autocollapse, innercollapse, outercollapse
*/
mw.hook( 'wikipage.collapsibleContent' ).add( function ( $collapsibleContent ) {
var autoCollapseThreshold = 2,
hasOuter = $collapsibleContent.parents( '.outercollapse' ).length > 0;
$collapsibleContent.each( function () {
var $element = $( this );
if ( $element.hasClass( 'collapsible' ) ) {
$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
}
if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
$element.data( 'mw-collapsible' ).collapse();
} else if ( $element.hasClass( 'innercollapse' ) && hasOuter ) {
$element.data( 'mw-collapsible' ).collapse();
}
var $toggle = $element.find( '.mw-collapsible-toggle' );
if ( $toggle.length && $toggle.parent()[0].style.color ) {
$toggle.css( 'color', 'inherit' ).find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
}
} );
} );
/* End of mw.loader.using callback */
/**
* Wikidata SPARQL tables rendering
*/
var WIKIDATA_REGEX = /https?:\/\/www\.wikidata\.org\/entity\/(Q\d+)/g,
COMMONS_REGEX = /^https?:\/\/commons\.wikimedia\.org\/wiki\/Special:FilePath\//i;
mw.hook( 'wikipage.content' ).add( function ( $content ) {
var tables = ($content ? $content[0] : document).querySelectorAll( '.wikidata-sparql-table:not([data-loaded])' );
if ( !tables.length ) { return; }
tables.forEach( function ( el ) {
el.setAttribute( 'data-loaded', '1' );
var sparql = el.getAttribute( 'data-sparql' ),
title = el.getAttribute( 'data-title' );
if ( !sparql ) { return; }
el.textContent = '⏳ Loading…';
fetch( 'https://query.wikidata.org/sparql?format=json&query=' + encodeURIComponent( sparql ), {
headers: { Accept: 'application/json' }
} )
.then( function ( r ) { return r.json(); } )
.then( function ( data ) {
var vars = data.head.vars,
rows = data.results.bindings,
html = [],
vLen = vars.length;
if ( title ) {
html.push( '<h3>', title, '</h3>' );
}
html.push( '<table class="wikitable sortable"><thead><tr>' );
for ( var i = 0; i < vLen; i++ ) {
html.push( '<th>', vars[i].replace( /_/g, ' ' ), '<th>' );
}
html.push( '</tr></thead><tbody>' );
for ( var r = 0, rLen = rows.length; r < rLen; r++ ) {
var row = rows[r];
html.push( '<tr>' );
for ( var c = 0; c < vLen; c++ ) {
var v = vars[c],
val = row[v] ? row[v].value : '';
if ( COMMONS_REGEX.test( val ) ) {
var thumb = val.replace( /^http:\/\//, 'https://' );
html.push( '<td><img src="', thumb, '?width=100" style="max-height:80px;" /></td>' );
} else {
val = val.replace( WIKIDATA_REGEX, '<a href="https://www.wikidata.org/wiki/$1" target="_blank" rel="noopener">$1</a>' );
html.push( '<td>', val, '</td>' );
}
}
html.push( '</tr>' );
}
html.push( '</tbody></table>' );
el.innerHTML = html.join('');
// Trigger MediaWiki table sorting module if loaded
mw.hook( 'wikipage.content' ).fire( $( el ) );
} )
.catch( function () {
el.textContent = '⚠️ Failed to load.';
} );
} );
} );
} );
/* DO NOT ADD CODE BELOW THIS LINE */