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 1: Line 1:
/* =====================================
  RANDOM GIF PICKER - COMPLETE SCRIPT
  Independent bounce + blurred loading
===================================== */


(function () {
(function () {
Line 11: Line 6:
     var excluded = [24, 29, 35];
     var excluded = [24, 29, 35];


     /* ---------- GIF URL ---------- */
     /* =====================================
      GIF URL
    ===================================== */


     function getGifUrl(number) {
     function getGifUrl(number) {
Line 19: Line 16:
     }
     }


     /* ---------- Display GIF ---------- */
     /* =====================================
      DISPLAY GIF
    ===================================== */


     function setGif(picker, url) {
     function setGif(picker, url) {
Line 31: Line 30:
         if (image) {
         if (image) {
             image.src = url;
             image.src = url;
             return;
        } else {
             picker.style.backgroundImage =
                'url("' + url + '")';
         }
         }
        picker.style.backgroundImage = 'url("' + url + '")';
     }
     }


     /* ---------- Load random GIF ---------- */
     /* =====================================
      LOAD RANDOM GIF
    ===================================== */


     function loadRandomGif(picker) {
     function loadRandomGif(picker) {
        if (!picker || !picker.isConnected) return;
         var previous = parseInt(
         var previous = parseInt(
             picker.dataset.lastGifNumber,
             picker.dataset.lastGifNumber,
Line 59: Line 62:
         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));
             var temp = candidates[j];
             var temp = candidates[j];
             candidates[j] = candidates[k];
             candidates[j] = candidates[k];
Line 64: Line 68:
         }
         }


         // Cancel any previous loading attempt.
         // Invalidate previous loading attempts.
         picker._gifLoadToken =
         picker._gifLoadToken =
             (picker._gifLoadToken || 0) + 1;
             (picker._gifLoadToken || 0) + 1;
Line 85: Line 89:


                 console.warn(
                 console.warn(
                     '[GIF Picker] Could not find a working GIF.'
                     '[GIF Picker] No working GIFs found.'
                 );
                 );


Line 118: Line 122:
     }
     }


     /* ---------- Independent bounce ---------- */
     /* =====================================
      INDEPENDENT 3D-PRESERVING BOUNCE
    ===================================== */


     function bouncePicker(picker) {
     function bouncePicker(picker) {
        // Respect the visitor's reduced-motion preference.
         if (
         if (
             window.matchMedia &&
             window.matchMedia &&
Line 131: Line 136:
         }
         }


        // Cancel the previous bounce if clicked again.
         if (picker._gifBounceAnimation) {
         if (picker._gifBounceAnimation) {
             picker._gifBounceAnimation.cancel();
             picker._gifBounceAnimation.cancel();
Line 137: Line 141:
         }
         }


        // Use the separate CSS scale property.
         if (typeof picker.animate !== 'function') {
        // This preserves the existing 3D tilt transform.
             picker.classList.remove('is-gif-bouncing');
         if (typeof picker.animate === 'function') {
            void picker.offsetWidth;
             var animation = picker.animate(
            picker.classList.add('is-gif-bouncing');
                [
            return;
                    { scale: '1' },
        }
                    { scale: '.92', offset: 0.30 },
 
                    { scale: '1.07', offset: 0.60 },
        var tiltTransform =
                    { scale: '.97', offset: 0.80 },
            'perspective(850px) ' +
                    { scale: '1' }
            'rotateX(var(--tilt-y, 0deg)) ' +
                ],
            'rotateY(var(--tilt-x, 0deg)) ';
 
        var animation = picker.animate(
            [
                 {
                 {
                     duration: 420,
                     transform: tiltTransform + 'scale(1)'
                     easing: 'ease-out',
                },
                     fill: 'none'
                {
                    transform: tiltTransform + 'scale(.92)',
                    offset: 0.30
                },
                {
                    transform: tiltTransform + 'scale(1.09)',
                     offset: 0.60
                },
                {
                    transform: tiltTransform + 'scale(.96)',
                     offset: 0.82
                },
                {
                    transform: tiltTransform + 'scale(1)'
                 }
                 }
             );
             ],
            {
                duration: 550,
                easing: 'cubic-bezier(.34, 1.56, .64, 1)',
                fill: 'none'
            }
        );


            picker._gifBounceAnimation = animation;
        picker._gifBounceAnimation = animation;


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


         // Fallback for browsers without the Web Animations API.
         animation.onfinish = cleanup;
        picker.classList.remove('is-gif-bouncing');
         animation.oncancel = cleanup;
         void picker.offsetWidth;
        picker.classList.add('is-gif-bouncing');
     }
     }


     /* ---------- Initialize picker ---------- */
     /* =====================================
      CLICK HANDLER
    ===================================== */


    function handlePickerClick(event) {
        var picker = event.currentTarget;


/* ---------- Reliable bounce preserving 3D tilt ---------- */
        bouncePicker(picker);
 
         loadRandomGif(picker);
function bouncePicker(picker) {
    if (
        window.matchMedia &&
         window.matchMedia('(prefers-reduced-motion: reduce)').matches
    ) {
        return;
     }
     }


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


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


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


    var animation = picker.animate(
             // Prevent duplicate initialization.
        [
             if (picker.dataset.gifPickerInitialized === 'true') {
            { transform: tiltTransform + 'scale(1)' },
                return;
             { 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;
            picker.dataset.gifPickerInitialized = 'true';


    animation.onfinish = function () {
             picker.addEventListener(
        if (picker._gifBounceAnimation === animation) {
                'click',
             picker._gifBounceAnimation = null;
                handlePickerClick
        }
            );
    };
 
    animation.oncancel = function () {
        if (picker._gifBounceAnimation === animation) {
            picker._gifBounceAnimation = null;
        }
    };
}


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


     /* ---------- MediaWiki initialization ---------- */
     /* =====================================
      MEDIAWIKI INITIALIZATION
    ===================================== */


     if (window.mw && mw.hook) {
     if (window.mw && mw.hook) {

Revision as of 10:34, 3 December 2026


(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;
        } else {
            picker.style.backgroundImage =
                'url("' + url + '")';
        }
    }

    /* =====================================
       LOAD RANDOM GIF
    ===================================== */

    function loadRandomGif(picker) {
        if (!picker || !picker.isConnected) return;

        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;
        }

        // Invalidate previous loading attempts.
        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] No working GIFs found.'
                );

                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 3D-PRESERVING BOUNCE
    ===================================== */

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

        if (picker._gifBounceAnimation) {
            picker._gifBounceAnimation.cancel();
            picker._gifBounceAnimation = null;
        }

        if (typeof picker.animate !== 'function') {
            picker.classList.remove('is-gif-bouncing');
            void picker.offsetWidth;
            picker.classList.add('is-gif-bouncing');
            return;
        }

        var tiltTransform =
            'perspective(850px) ' +
            'rotateX(var(--tilt-y, 0deg)) ' +
            'rotateY(var(--tilt-x, 0deg)) ';

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

        picker._gifBounceAnimation = animation;

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

        animation.onfinish = cleanup;
        animation.oncancel = cleanup;
    }

    /* =====================================
       CLICK HANDLER
    ===================================== */

    function handlePickerClick(event) {
        var picker = event.currentTarget;

        bouncePicker(picker);
        loadRandomGif(picker);
    }

    /* =====================================
       INITIALIZE PICKER
    ===================================== */

    function initializePicker() {
        var pickers = document.querySelectorAll(selector);

        pickers.forEach(function (picker) {

            // Prevent duplicate initialization.
            if (picker.dataset.gifPickerInitialized === 'true') {
                return;
            }

            picker.dataset.gifPickerInitialized = 'true';

            picker.addEventListener(
                'click',
                handlePickerClick
            );

            // Load the first GIF automatically.
            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();
    }

})();