MediaWiki:Common.js: Difference between revisions

From Another Chance Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Reverted
Line 3: Line 3:
/* Allows images within galleries to be spoiled if "spoiler:" is put at the start of its caption*/
/* Allows images within galleries to be spoiled if "spoiler:" is put at the start of its caption*/
mw.hook('wikipage.content').add(function ($content) {
mw.hook('wikipage.content').add(function ($content) {
     $content.find('ul.gallery li.gallerybox').each(function () {
     $content.find('ul.gallery').each(function () {
        var $box = $(this);
        var $gallery = $(this);
        var $caption = $box.find('.gallerytext');
        var isWholeGallerySpoiler = $gallery.hasClass('spoiler-image');
         var captionText = $caption.text().trim().toLowerCase();
        var hasAnySpoiler = isWholeGallerySpoiler;
 
        $gallery.find('li.gallerybox').each(function () {
            var $box = $(this);
            var captionText = $box.find('.gallerytext').text().trim().toLowerCase();
            var imgSrc = ($box.find('img').attr('src') || '') + ' ' +
                        ($box.find('img').attr('alt') || '');
 
            var isIndividualSpoiler = captionText.indexOf('spoiler:') === 0;
 
            if (isWholeGallerySpoiler || isIndividualSpoiler) {
                $box.addClass('spoiler-image');
                hasAnySpoiler = true;
 
                if (isIndividualSpoiler) {
                    $box.find('.gallerytext').html(function (i, html) {
                        return html.replace('spoiler: ', '');
                    });
                }
            }
        });
         if (hasAnySpoiler && !$gallery.prev('.spoiler-reveal-all').length) {
            var $btn = $('<button type="button">')
                .text('Reveal all spoilers');
 
            $btn.on('click', function () {
                $gallery.addClass('spoiler-image revealed');
                $gallery.find('.gallerybox.spoiler').addClass('revealed');
                $gallery.find('.mw-gallery-slideshow-img-container')
                        .addClass('revealed')
                        .removeClass('spoiler-image');
                $btn.remove();
            });


        if (captionText.startsWith('spoiler:')) {
             $gallery.before($btn);
             $box.addClass('spoiler-image thumb');
            $caption.html($caption.html().replace('spoiler: ', ''));
         }
         }
     });
     });
     $content.find('ul.mw-gallery-slideshow').each(function () {
     $content.find('ul.mw-gallery-slideshow').each(function () {
         var $gallery = $(this);
         var $gallery = $(this);


         function updateLargeImage() {
         function updateImage() {
            if ($gallery.hasClass('spoiler-revealed')) {
                return;
            }
 
             var $current = $gallery.find('.gallerybox.slideshow-current');
             var $current = $gallery.find('.gallerybox.slideshow-current');
             if (!$current.length) {
             if (!$current.length) {
Line 25: Line 58:
             var $container = $gallery.find('.mw-gallery-slideshow-img-container');
             var $container = $gallery.find('.mw-gallery-slideshow-img-container');


             if ($current.hasClass('spoiler-image') && !$current.hasClass('revealed')) {
             if ($current.hasClass('spoiler') && !$current.hasClass('revealed')) {
                 $container.addClass('spoiler-image');
                 $container.addClass('spoiler-main');
             } else {
             } else {
                 $container.removeClass('spoiler-image revealed');
                 $container.removeClass('spoiler-main revealed');
             }
             }
         }
         }


         updateLargeImage();
         updateImage();
 
        var observer = new MutationObserver(function () {
            updateLargeImage();
        });


        var observer = new MutationObserver(updateImage);
         observer.observe($gallery[0], {
         observer.observe($gallery[0], {
             attributes: true,
             attributes: true,
Line 47: Line 77:


     $content.on('click', '.gallerybox.spoiler', function () {
     $content.on('click', '.gallerybox.spoiler', function () {
         $(this).addClass('spoiler-image revealed');
         $(this).addClass('revealed');
     });
     });


     $content.on('click', '.mw-gallery-slideshow-img-container.spoiler-image', function (e) {
     $content.on('click', '.mw-gallery-slideshow-img-container.spoiler-image', function (e) {
        // Ignore navigation buttons
         if ($(e.target).closest('.oo-ui-buttonElement, button, a.oo-ui-buttonElement-button').length) {
         if ($(e.target).closest('.oo-ui-buttonElement, button, a.oo-ui-buttonElement-button').length) {
             return;
             return;
Line 56: Line 87:


         var $container = $(this);
         var $container = $(this);
        var $gallery = $container.closest('ul.gallery');


         if (!$container.hasClass('revealed')) {
         if (!$container.hasClass('revealed')) {
Line 62: Line 94:


             $container.addClass('revealed');
             $container.addClass('revealed');
            $gallery.find('.gallerybox.slideshow-current').addClass('revealed');


             $container.closest('ul.gallery')
             // If the whole gallery was marked as spoiler, reveal everything
                      .find('.gallerybox.slideshow-current')
            if ($gallery.hasClass('spoiler-image')) {
                      .addClass('revealed');
                $gallery.addClass('spoiler-image revealed');
                $gallery.find('.gallerybox.spoiler-image').addClass('revealed');
            }
         }
         }
     });
     });
});
});

Revision as of 17:39, 20 August 2026

/* Any JavaScript here will be loaded for all users on every page load. */

/* Allows images within galleries to be spoiled if "spoiler:" is put at the start of its caption*/
mw.hook('wikipage.content').add(function ($content) {
    $content.find('ul.gallery').each(function () {
        var $gallery = $(this);
        var isWholeGallerySpoiler = $gallery.hasClass('spoiler-image');
        var hasAnySpoiler = isWholeGallerySpoiler;

        $gallery.find('li.gallerybox').each(function () {
            var $box = $(this);
            var captionText = $box.find('.gallerytext').text().trim().toLowerCase();
            var imgSrc = ($box.find('img').attr('src') || '') + ' ' +
                         ($box.find('img').attr('alt') || '');

            var isIndividualSpoiler = captionText.indexOf('spoiler:') === 0;

            if (isWholeGallerySpoiler || isIndividualSpoiler) {
                $box.addClass('spoiler-image');
                hasAnySpoiler = true;

                if (isIndividualSpoiler) {
                    $box.find('.gallerytext').html(function (i, html) {
                        return html.replace('spoiler: ', '');
                    });
                }
            }
        });
        if (hasAnySpoiler && !$gallery.prev('.spoiler-reveal-all').length) {
            var $btn = $('<button type="button">')
                .text('Reveal all spoilers');

            $btn.on('click', function () {
                $gallery.addClass('spoiler-image revealed');
                $gallery.find('.gallerybox.spoiler').addClass('revealed');
                $gallery.find('.mw-gallery-slideshow-img-container')
                        .addClass('revealed')
                        .removeClass('spoiler-image');
                $btn.remove();
            });

            $gallery.before($btn);
        }
    });
    $content.find('ul.mw-gallery-slideshow').each(function () {
        var $gallery = $(this);

        function updateImage() {
            if ($gallery.hasClass('spoiler-revealed')) {
                return;
            }

            var $current = $gallery.find('.gallerybox.slideshow-current');
            if (!$current.length) {
                $current = $gallery.find('.gallerybox').first();
            }

            var $container = $gallery.find('.mw-gallery-slideshow-img-container');

            if ($current.hasClass('spoiler') && !$current.hasClass('revealed')) {
                $container.addClass('spoiler-main');
            } else {
                $container.removeClass('spoiler-main revealed');
            }
        }

        updateImage();

        var observer = new MutationObserver(updateImage);
        observer.observe($gallery[0], {
            attributes: true,
            attributeFilter: ['class'],
            subtree: true,
            childList: true
        });
    });

    $content.on('click', '.gallerybox.spoiler', function () {
        $(this).addClass('revealed');
    });

    $content.on('click', '.mw-gallery-slideshow-img-container.spoiler-image', function (e) {
        // Ignore navigation buttons
        if ($(e.target).closest('.oo-ui-buttonElement, button, a.oo-ui-buttonElement-button').length) {
            return;
        }

        var $container = $(this);
        var $gallery = $container.closest('ul.gallery');

        if (!$container.hasClass('revealed')) {
            e.preventDefault();
            e.stopPropagation();

            $container.addClass('revealed');
            $gallery.find('.gallerybox.slideshow-current').addClass('revealed');

            // If the whole gallery was marked as spoiler, reveal everything
            if ($gallery.hasClass('spoiler-image')) {
                $gallery.addClass('spoiler-image revealed');
                $gallery.find('.gallerybox.spoiler-image').addClass('revealed');
            }
        }
    });
});