Jump to content

MediaWiki:Common.js: Difference between revisions

From Fedoverse Wiki, the free encyclopedia
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 174: Line 174:
     /* ---------- Initialize picker ---------- */
     /* ---------- Initialize picker ---------- */


    function initializePicker() {
        document.querySelectorAll(selector).forEach(function (picker) {


            if (picker.dataset.gifPickerInit === '1') {
/* ---------- Reliable bounce preserving 3D tilt ---------- */
                return;
            }


            picker.dataset.gifPickerInit = '1';
function bouncePicker(picker) {
    if (
        window.matchMedia &&
        window.matchMedia('(prefers-reduced-motion: reduce)').matches
    ) {
        return;
    }
 
    // Stop any previous bounce.
    if (picker._gifBounceAnimation) {
        picker._gifBounceAnimation.cancel();
        picker._gifBounceAnimation = null;
    }
 
    if (typeof picker.animate !== 'function') {
        console.warn('[GIF Picker] Web Animations API unavailable.');
        return;
    }


            // Click handler.
    // Keep the existing 3D tilt throughout the bounce.
            picker.addEventListener('click', function (event) {
    var tiltTransform =
        'perspective(850px) ' +
        'rotateX(var(--tilt-y)) ' +
        'rotateY(var(--tilt-x)) ';


                if (picker.closest('a')) {
    var animation = picker.animate(
                    event.preventDefault();
        [
                }
            { transform: tiltTransform + 'scale(1)' },
            { transform: tiltTransform + 'scale(.92)' },
            { transform: tiltTransform + 'scale(1.09)' },
            { transform: tiltTransform + 'scale(.96)' },
            { transform: tiltTransform + 'scale(1)' }
        ],
        {
            duration: 550,
            easing: 'cubic-bezier(.34, 1.56, .64, 1)',
            fill: 'none'
        }
    );


                console.log('[GIF Picker] Click detected');
    picker._gifBounceAnimation = animation;


                // Start bounce immediately.
    animation.onfinish = function () {
                bouncePicker(picker);
        if (picker._gifBounceAnimation === animation) {
            picker._gifBounceAnimation = null;
        }
    };


                // Load a different GIF independently.
    animation.oncancel = function () {
                loadRandomGif(picker);
        if (picker._gifBounceAnimation === animation) {
            });
            picker._gifBounceAnimation = null;
        }
    };
}


            // Load the first GIF.
            loadRandomGif(picker);
        });
    }


     /* ---------- MediaWiki initialization ---------- */
     /* ---------- MediaWiki initialization ---------- */

Revision as of 10:26, 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 ---------- */


/* ---------- Reliable bounce preserving 3D tilt ---------- */

function bouncePicker(picker) {
    if (
        window.matchMedia &&
        window.matchMedia('(prefers-reduced-motion: reduce)').matches
    ) {
        return;
    }

    // Stop any previous bounce.
    if (picker._gifBounceAnimation) {
        picker._gifBounceAnimation.cancel();
        picker._gifBounceAnimation = null;
    }

    if (typeof picker.animate !== 'function') {
        console.warn('[GIF Picker] Web Animations API unavailable.');
        return;
    }

    // Keep the existing 3D tilt throughout the bounce.
    var tiltTransform =
        'perspective(850px) ' +
        'rotateX(var(--tilt-y)) ' +
        'rotateY(var(--tilt-x)) ';

    var animation = picker.animate(
        [
            { transform: tiltTransform + 'scale(1)' },
            { transform: tiltTransform + 'scale(.92)' },
            { transform: tiltTransform + 'scale(1.09)' },
            { transform: tiltTransform + 'scale(.96)' },
            { transform: tiltTransform + 'scale(1)' }
        ],
        {
            duration: 550,
            easing: 'cubic-bezier(.34, 1.56, .64, 1)',
            fill: 'none'
        }
    );

    picker._gifBounceAnimation = animation;

    animation.onfinish = function () {
        if (picker._gifBounceAnimation === animation) {
            picker._gifBounceAnimation = null;
        }
    };

    animation.oncancel = function () {
        if (picker._gifBounceAnimation === animation) {
            picker._gifBounceAnimation = null;
        }
    };
}


    /* ---------- MediaWiki initialization ---------- */

    if (window.mw && mw.hook) {
        mw.hook('wikipage.content').add(initializePicker);
    }

    if (document.readyState === 'loading') {
        document.addEventListener(
            'DOMContentLoaded',
            initializePicker
        );
    } else {
        initializePicker();
    }

})();