MediaWiki:Common.js: Difference between revisions

From Another Chance Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
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').each(function () {
     $content.find('ul.gallery li.gallerybox').each(function () {
         var $gallery = $(this);
         var $box = $(this);
         var isWholeGallery = $gallery.hasClass('spoiler-image');
         var $caption = $box.find('.gallerytext');
       
        var captionText = $caption.text().trim().toLowerCase();
        $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 isIndividual = captionText.indexOf('spoiler: ') === 0


            if (isWholeGallery || isIndividual) {
        if (captionText.startsWith('spoiler:')) {
                $box.addClass('spoiler-image thumb');
            $box.addClass('spoiler-image thumb');
                if (isIndividual) {
            $caption.html($caption.html().replace('spoiler: ', ''));
                    $box.find('.gallerytext').html(function (i, html) {
         }
                        return html.replace('spoiler: ', '');
                    });
                }
            }
         });
     });
     });


Line 32: Line 22:
                 $current = $gallery.find('.gallerybox').first();
                 $current = $gallery.find('.gallerybox').first();
             }
             }
             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-image') && !$current.hasClass('revealed')) {
                 $container.addClass('spoiler-image');
                 $container.addClass('spoiler-image');
Line 42: Line 34:
         updateLargeImage();
         updateLargeImage();


         var observer = new MutationObserver((updateLargeImage);
         var observer = new MutationObserver(function () {
            updateLargeImage();
        });
 
         observer.observe($gallery[0], {
         observer.observe($gallery[0], {
             attributes: true,
             attributes: true,
Line 61: Line 56:


         var $container = $(this);
         var $container = $(this);
        var $gallery = $container.closest('ul.gallery');
 
         if (!$container.hasClass('revealed')) {
         if (!$container.hasClass('revealed')) {
             e.preventDefault();
             e.preventDefault();
Line 67: Line 62:


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


             if ($gallery.hasClass('spoiler-image')) {
             $container.closest('ul.gallery')
                $gallery.addClass('revealed');
                      .find('.gallerybox.slideshow-current')
                $gallery.find('.gallerybox.spoiler-image').addClass('revealed');
                      .addClass('revealed');
            }
         }
         }
    });
});
/* Toggles spoilered images between blurred and not blurred upon clicking */
$(function() {
    $('.spoiler-image').on('click', function() {
        $(this).toggleClass('revealed');
     });
     });
});
});

Revision as of 17:30, 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 li.gallerybox').each(function () {
        var $box = $(this);
        var $caption = $box.find('.gallerytext');
        var captionText = $caption.text().trim().toLowerCase();

        if (captionText.startsWith('spoiler:')) {
            $box.addClass('spoiler-image thumb');
            $caption.html($caption.html().replace('spoiler: ', ''));
        }
    });

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

        function updateLargeImage() {
            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-image') && !$current.hasClass('revealed')) {
                $container.addClass('spoiler-image');
            } else {
                $container.removeClass('spoiler-image revealed');
            }
        }

        updateLargeImage();

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

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

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

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

        var $container = $(this);

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

            $container.addClass('revealed');

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