MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 1: | Line 1: | ||
/* ===================================== | /* ===================================== | ||
RANDOM GIF PICKER - | RANDOM GIF PICKER - COMPLETE SCRIPT | ||
Independent bounce + blurred loading | |||
===================================== */ | ===================================== */ | ||
(function () { | (function () { | ||
'use strict'; | |||
var selector = '#random-gif-picker'; | var selector = '#random-gif-picker'; | ||
var excluded = [24, 29, 35]; | var excluded = [24, 29, 35]; | ||
/* ---------- GIF URL ---------- */ | |||
function getGifUrl(number) { | function getGifUrl(number) { | ||
| Line 789: | Line 18: | ||
); | ); | ||
} | } | ||
/* ---------- Display GIF ---------- */ | |||
function setGif(picker, url) { | function setGif(picker, url) { | ||
| Line 805: | Line 36: | ||
picker.style.backgroundImage = 'url("' + url + '")'; | picker.style.backgroundImage = 'url("' + url + '")'; | ||
} | } | ||
/* ---------- Load random GIF ---------- */ | |||
function loadRandomGif(picker) { | function loadRandomGif(picker) { | ||
| Line 823: | Line 56: | ||
} | } | ||
// Shuffle candidates. | |||
for (var j = candidates.length - 1; j > 0; j--) { | for (var j = candidates.length - 1; j > 0; j--) { | ||
var k = Math.floor(Math.random() * (j + 1)); | var k = Math.floor(Math.random() * (j + 1)); | ||
| Line 830: | Line 64: | ||
} | } | ||
// Cancel any previous loading attempt. | |||
picker._gifLoadToken = | picker._gifLoadToken = | ||
(picker._gifLoadToken || 0) + 1; | (picker._gifLoadToken || 0) + 1; | ||
| Line 836: | Line 71: | ||
var index = 0; | var index = 0; | ||
picker.classList.add('is-gif-loading'); | picker.classList.add('is-gif-loading'); | ||
| Line 853: | Line 87: | ||
'[GIF Picker] Could not find a working GIF.' | '[GIF Picker] Could not find a working GIF.' | ||
); | ); | ||
return; | return; | ||
} | } | ||
| Line 872: | Line 107: | ||
picker.dataset.lastGifNumber = number; | picker.dataset.lastGifNumber = number; | ||
picker.classList.remove('is-gif-loading'); | picker.classList.remove('is-gif-loading'); | ||
}; | }; | ||
probe.onerror = tryNext; | probe.onerror = tryNext; | ||
probe.src = url; | probe.src = url; | ||
} | } | ||
| Line 883: | Line 118: | ||
} | } | ||
/* ---------- Independent bounce ---------- */ | |||
if ( | function bouncePicker(picker) { | ||
// Respect the visitor's reduced-motion preference. | |||
if ( | |||
window.matchMedia && | |||
window.matchMedia( | |||
'(prefers-reduced-motion: reduce)' | |||
).matches | |||
) { | |||
return; | return; | ||
} | } | ||
picker. | // Cancel the previous bounce if clicked again. | ||
if (picker._gifBounceAnimation) { | |||
picker._gifBounceAnimation.cancel(); | |||
picker._gifBounceAnimation = null; | |||
} | |||
// Use the separate CSS scale property. | |||
// This preserves the existing 3D tilt transform. | |||
if (typeof picker.animate === 'function') { | |||
var animation = picker.animate( | |||
[ | |||
{ scale: '1' }, | |||
{ scale: '.92', offset: 0.30 }, | |||
{ scale: '1.07', offset: 0.60 }, | |||
{ scale: '.97', offset: 0.80 }, | |||
{ scale: '1' } | |||
], | |||
{ | |||
duration: 420, | |||
easing: 'ease-out', | |||
fill: 'none' | |||
} | |||
); | |||
picker._gifBounceAnimation = animation; | |||
animation.onfinish = function () { | |||
if (picker._gifBounceAnimation === animation) { | |||
picker._gifBounceAnimation = null; | |||
} | |||
}; | |||
return; | |||
} | |||
// Fallback for browsers without the Web Animations API. | |||
picker.classList.remove('is-gif-bouncing'); | |||
void picker.offsetWidth; | |||
picker.classList.add('is-gif-bouncing'); | |||
} | |||
/* ---------- Initialize picker ---------- */ | |||
function initializePicker() { | |||
document.querySelectorAll(selector).forEach(function (picker) { | |||
if (picker.dataset.gifPickerInit === '1') { | |||
return; | |||
} | } | ||
picker.dataset.gifPickerInit = '1'; | |||
// Click handler. | |||
picker.addEventListener('click', function (event) { | |||
if (picker.closest('a')) { | |||
event.preventDefault(); | |||
} | |||
console.log('[GIF Picker] Click detected'); | |||
// Start bounce immediately. | |||
bouncePicker(picker); | |||
// Load a different GIF independently. | |||
// | loadRandomGif(picker); | ||
picker | }); | ||
// Load the first GIF. | |||
loadRandomGif(picker); | |||
}); | |||
} | } | ||
/ | /* ---------- MediaWiki initialization ---------- */ | ||
if ( | if (window.mw && mw.hook) { | ||
mw.hook('wikipage.content').add(initializePicker); | |||
} | } | ||
if (document.readyState === 'loading') { | |||
document.addEventListener( | |||
'DOMContentLoaded', | |||
initializePicker | |||
); | |||
} else { | |||
initializePicker(); | |||
} | |||
}); | })(); | ||
Revision as of 10:23, 3 December 2026
/* =====================================
RANDOM GIF PICKER - COMPLETE SCRIPT
Independent bounce + blurred loading
===================================== */
(function () {
'use strict';
var selector = '#random-gif-picker';
var excluded = [24, 29, 35];
/* ---------- GIF URL ---------- */
function getGifUrl(number) {
return mw.util.getUrl(
'Special:Redirect/file/' + number + '.gif'
);
}
/* ---------- Display GIF ---------- */
function setGif(picker, url) {
if (picker.tagName === 'IMG') {
picker.src = url;
return;
}
var image = picker.querySelector('img');
if (image) {
image.src = url;
return;
}
picker.style.backgroundImage = 'url("' + url + '")';
}
/* ---------- Load random GIF ---------- */
function loadRandomGif(picker) {
var previous = parseInt(
picker.dataset.lastGifNumber,
10
);
var candidates = [];
for (var i = 1; i <= 100; i++) {
if (
excluded.indexOf(i) === -1 &&
i !== previous
) {
candidates.push(i);
}
}
// Shuffle candidates.
for (var j = candidates.length - 1; j > 0; j--) {
var k = Math.floor(Math.random() * (j + 1));
var temp = candidates[j];
candidates[j] = candidates[k];
candidates[k] = temp;
}
// Cancel any previous loading attempt.
picker._gifLoadToken =
(picker._gifLoadToken || 0) + 1;
var token = picker._gifLoadToken;
var index = 0;
picker.classList.add('is-gif-loading');
function tryNext() {
if (
token !== picker._gifLoadToken ||
!picker.isConnected
) {
return;
}
if (index >= candidates.length) {
picker.classList.remove('is-gif-loading');
console.warn(
'[GIF Picker] Could not find a working GIF.'
);
return;
}
var number = candidates[index++];
var url = getGifUrl(number);
var probe = new Image();
probe.onload = function () {
if (
token !== picker._gifLoadToken ||
!picker.isConnected
) {
return;
}
setGif(picker, url);
picker.dataset.lastGifNumber = number;
picker.classList.remove('is-gif-loading');
};
probe.onerror = tryNext;
probe.src = url;
}
tryNext();
}
/* ---------- Independent bounce ---------- */
function bouncePicker(picker) {
// Respect the visitor's reduced-motion preference.
if (
window.matchMedia &&
window.matchMedia(
'(prefers-reduced-motion: reduce)'
).matches
) {
return;
}
// Cancel the previous bounce if clicked again.
if (picker._gifBounceAnimation) {
picker._gifBounceAnimation.cancel();
picker._gifBounceAnimation = null;
}
// Use the separate CSS scale property.
// This preserves the existing 3D tilt transform.
if (typeof picker.animate === 'function') {
var animation = picker.animate(
[
{ scale: '1' },
{ scale: '.92', offset: 0.30 },
{ scale: '1.07', offset: 0.60 },
{ scale: '.97', offset: 0.80 },
{ scale: '1' }
],
{
duration: 420,
easing: 'ease-out',
fill: 'none'
}
);
picker._gifBounceAnimation = animation;
animation.onfinish = function () {
if (picker._gifBounceAnimation === animation) {
picker._gifBounceAnimation = null;
}
};
return;
}
// Fallback for browsers without the Web Animations API.
picker.classList.remove('is-gif-bouncing');
void picker.offsetWidth;
picker.classList.add('is-gif-bouncing');
}
/* ---------- Initialize picker ---------- */
function initializePicker() {
document.querySelectorAll(selector).forEach(function (picker) {
if (picker.dataset.gifPickerInit === '1') {
return;
}
picker.dataset.gifPickerInit = '1';
// Click handler.
picker.addEventListener('click', function (event) {
if (picker.closest('a')) {
event.preventDefault();
}
console.log('[GIF Picker] Click detected');
// Start bounce immediately.
bouncePicker(picker);
// Load a different GIF independently.
loadRandomGif(picker);
});
// Load the first GIF.
loadRandomGif(picker);
});
}
/* ---------- MediaWiki initialization ---------- */
if (window.mw && mw.hook) {
mw.hook('wikipage.content').add(initializePicker);
}
if (document.readyState === 'loading') {
document.addEventListener(
'DOMContentLoaded',
initializePicker
);
} else {
initializePicker();
}
})();