MediaWiki:Gadget-Accueil.stats.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.
( function ( mw ) {
'use strict';
var accueil = mw.libs && mw.libs.wikithionvilleAccueil;
if ( !accueil ) {
return;
}
function formatCounter( value ) {
try {
return value.toLocaleString( navigator.language || undefined, {
notation: 'compact',
compactDisplay: 'short',
maximumFractionDigits: 1
} );
} catch ( error ) {
return String( value );
}
}
function animateCounter( element, targetValue ) {
var target = Math.max( Number( targetValue ) || 0, 0 );
var duration = 1100;
var startTime = null;
function tick( timestamp ) {
var progress;
var value;
if ( startTime === null ) {
startTime = timestamp;
}
progress = Math.min( ( timestamp - startTime ) / duration, 1 );
value = Math.round( target * progress );
element.textContent = formatCounter( value );
if ( progress < 1 ) {
window.requestAnimationFrame( tick );
} else {
element.textContent = formatCounter( target );
}
}
window.requestAnimationFrame( tick );
}
function countWhenVisible( element, value ) {
var observer;
if ( !element ) {
return;
}
if ( accueil.reduceMotion || !( 'IntersectionObserver' in window ) ) {
animateCounter( element, value );
return;
}
observer = new IntersectionObserver( function ( entries ) {
if ( entries[ 0 ].isIntersecting ) {
observer.disconnect();
animateCounter( element, value );
}
}, { threshold: 0.2 } );
observer.observe( element );
}
accueil.initStats = function ( root ) {
var stats = root.querySelector( '.home-info__stats' );
var api;
if ( !stats ) {
return;
}
api = new mw.Api();
api.get( {
action: 'query',
meta: 'siteinfo',
siprop: 'statistics',
formatversion: 2
} ).done( function ( data ) {
var statistics = data.query && data.query.statistics;
if ( !statistics ) {
return;
}
stats.classList.add( 'is-visible' );
countWhenVisible( root.querySelector( '#counter_articles' ), statistics.articles );
countWhenVisible( root.querySelector( '#counter_pages' ), statistics.pages );
countWhenVisible( root.querySelector( '#counter_utilisateurs' ), statistics.users );
} );
};
}( mediaWiki ) );