« MediaWiki:Gadget-Accueil.stats.js » : différence entre les versions
Page de l’interface de MediaWiki
Autres actions
Sync homepage from repository (commit b06f0a0) |
Sync homepage from repository (commit 0e34c54) |
||
| Ligne 1 : | Ligne 1 : | ||
( function ( mw ) { | ( function ( mw ) { | ||
"use strict"; | |||
const accueil = mw.libs && mw.libs.wikithionvilleAccueil; | |||
if ( !accueil ) | if (!accueil) return; | ||
function formatCounter( value ) { | function formatCounter( value ) { | ||
try { | try { | ||
return value.toLocaleString( navigator.language || undefined, { | return value.toLocaleString( | ||
navigator.language || undefined, | |||
{ | |||
notation: "compact", | |||
compactDisplay: "short", | |||
maximumFractionDigits: 1 | |||
} | |||
); | |||
} catch ( error ) { | } catch ( error ) { | ||
return String( value ); | return String( value ); | ||
| Ligne 20 : | Ligne 21 : | ||
} | } | ||
function animateCounter( element, | function animateCounter(element, target) { | ||
const DURATION = 1100; | |||
let startTime = null; | |||
function tick( timestamp ) { | function tick(timestamp) { | ||
if (startTime === null) | |||
startTime = timestamp; | |||
const progress = Math.min((timestamp - startTime) / DURATION, 1); | |||
const value = Math.round(target * progress); | |||
element.textContent = formatCounter( value ); | element.textContent = formatCounter( value ); | ||
if ( progress < 1 ) { | if ( progress < 1 ) { | ||
window.requestAnimationFrame( tick ); | window.requestAnimationFrame( tick ); | ||
return; | |||
} | } | ||
element.textContent = formatCounter(target); | |||
} | } | ||
window.requestAnimationFrame( tick ); | window.requestAnimationFrame(tick); | ||
} | } | ||
function countWhenVisible( element, value ) { | function countWhenVisible(element, value) { | ||
if (!element) return; | |||
value = Number(value); | |||
if ( | if (isNaN(value)) value = 0; | ||
value = Math.max(value, 0); | |||
if ( accueil.reduceMotion || !( | if ( | ||
accueil.reduceMotion | |||
|| !("IntersectionObserver" in window) | |||
) { | |||
element.textContent = formatCounter(value); | |||
return; | return; | ||
} | } | ||
observer = new IntersectionObserver( | const observer = new IntersectionObserver( | ||
entries => { | |||
if (!entries[0].isIntersecting) return; | |||
observer.disconnect(); | observer.disconnect(); | ||
animateCounter( element, value ); | animateCounter( element, value ); | ||
} | }, | ||
{ threshold: 0.2 } | |||
); | |||
observer.observe( element ); | observer.observe(element); | ||
} | } | ||
accueil.initStats = | accueil.initStats = root => { | ||
const stats = root.querySelector(".home-info__stats"); | |||
if (!stats) return; | |||
const api = new mw.Api(); | |||
return; | api.get({ | ||
action: "query", | |||
meta: "siteinfo", | |||
siprop: "statistics", | |||
formatversion: 2 | |||
}) | |||
.then(data => { | |||
const statistics = | |||
data | |||
&& 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 | |||
); | |||
} ); | }) | ||
.catch(error => { | |||
console.error("Impossible de charger les statistiques", error); | |||
}); | |||
}; | }; | ||
}( mediaWiki ) ); | }(mediaWiki)); | ||
Dernière version du 12 juin 2026 à 13:00
( function ( mw ) {
"use strict";
const 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, target) {
const DURATION = 1100;
let startTime = null;
function tick(timestamp) {
if (startTime === null)
startTime = timestamp;
const progress = Math.min((timestamp - startTime) / DURATION, 1);
const value = Math.round(target * progress);
element.textContent = formatCounter( value );
if ( progress < 1 ) {
window.requestAnimationFrame( tick );
return;
}
element.textContent = formatCounter(target);
}
window.requestAnimationFrame(tick);
}
function countWhenVisible(element, value) {
if (!element) return;
value = Number(value);
if (isNaN(value)) value = 0;
value = Math.max(value, 0);
if (
accueil.reduceMotion
|| !("IntersectionObserver" in window)
) {
element.textContent = formatCounter(value);
return;
}
const observer = new IntersectionObserver(
entries => {
if (!entries[0].isIntersecting) return;
observer.disconnect();
animateCounter( element, value );
},
{ threshold: 0.2 }
);
observer.observe(element);
}
accueil.initStats = root => {
const stats = root.querySelector(".home-info__stats");
if (!stats) return;
const api = new mw.Api();
api.get({
action: "query",
meta: "siteinfo",
siprop: "statistics",
formatversion: 2
})
.then(data => {
const statistics =
data
&& 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
);
})
.catch(error => {
console.error("Impossible de charger les statistiques", error);
});
};
}(mediaWiki));