MediaWiki:Gadget-DeferredDisplay.js: Difference between revisions

From Little Tail Wiki
Jump to navigationJump to search
No edit summary
Tag: Reverted
Undo revision 3000 by Zoltier (talk)
Tag: Undo
 
Line 1: Line 1:
/**
/**
  * Deferred display of {{tl|nsfw}}-tagged images.
  * Deferred display of {{tl|nsfw}}-tagged images.
* jshint validation
  * @author Vish, 2024
  * @author Dschwen, 2013
  */
  */


(function ($, mw) {
$(document).ready(function() {
// nothing should be tagged outside these pages yet
    // Hide images immediately
var fpc = "Featured picture candidate";
    $('img[alt*=NSFWTAG]').css('opacity', '0');
var ti = mw.config.get("wgTitle");
    $('.image[title*=NSFWTAG]').find('img').css('opacity', '0');
if ($.inArray(mw.config.get('wgNamespaceNumber'), [4, 5, 10, 11, 14, 15]) === -1 || (ti.indexOf(fpc) === -1 && ti !== "Nsfw/sandbox"))
   
return;
    // Create overlay div and apply styles
// wrap non thumb images first
    function createOverlay(parent) {
$('img[alt*=NSFWTAG]').each(function (i, e) {
        parent.css({
$(e).wrap($('<div>').css({
            display: 'block'
display: 'inline-block',
        });
width: $(e).width() + 'px',
        var image = parent.find('img');
height: $(e).height() + 'px',
 
backgroundColor: '#eee'
        // Use the `height` attribute or fallback to CSS
}));
        var overlayHeight = image.attr('height') || image.css('height') || '100%';
}).parent().add($('.thumbinner,.gallerybox').has('.nsfwelement').find('.image, .mw-file-description').parent())
       
.one('click', function (e) {
        var overlay = $('<div>').css({
$(this).css('background', 'none').find('img').css({
            position: 'absolute',
visibility: ''
            top: 0,
});
            left: 0,
e.preventDefault();
            width: '100%',
})
            height: overlayHeight,
.css({
            backgroundImage: 'url("https://static.jojowiki.com/images/customizations/Dialog-warning-yellow.png")',
backgroundImage: 'url("https://static.jojowiki.com/images/customizations/Dialog-warning-yellow.png")',
            backgroundRepeat: 'no-repeat',
backgroundRepeat: 'no-repeat',
            backgroundPosition: 'center',
backgroundPosition: 'center'
            backgroundSize: 'contain',
})
            backgroundColor: 'black',
.find('img').css({
            cursor: 'pointer',
visibility: 'hidden'
            zIndex: 1 // Ensure overlay is on top
});
        }).appendTo(parent);
}(jQuery, mediaWiki));
 
        overlay.one('click', function(e) {
            e.preventDefault();
            $(this).remove();
            parent.css({
                backgroundImage: '',
                backgroundRepeat: '',
                backgroundPosition: '',
                backgroundSize: '',
                display: ''
            }).find('img').css({
                visibility: '',
                opacity: '100%'
            });
        });
 
        console.log('Overlay created with height:', overlayHeight);
    }
 
    // Wrap non-thumb images and add overlay
    $('img[alt*=NSFWTAG]').parent().add($('.thumb')
        .has('.nsfwelement').find('.image').parent())
        .each(function() {
            var parent = $(this);
            parent.css({
                position: 'relative', // Ensure parent is positioned
            }).find('img').css({
                visibility: 'hidden'
            });
 
            createOverlay(parent);
        });
 
    // Add overlay to elements with NSFWTAG in title attribute within .image
    $('.image[title*=NSFWTAG]').each(function() {
        var parent = $(this).closest('.thumb');
        if (parent.find('img[alt*=NSFWTAG]').length === 0) { // Only apply if no img with NSFWTAG is found
            parent.css({
                position: 'relative', // Ensure parent is positioned
            });
 
            createOverlay(parent);
        }
    });
});

Latest revision as of 17:48, 11 March 2025

/**
 * Deferred display of {{tl|nsfw}}-tagged images.
 * @author Vish, 2024
 */

$(document).ready(function() {
    // Hide images immediately
    $('img[alt*=NSFWTAG]').css('opacity', '0');
    $('.image[title*=NSFWTAG]').find('img').css('opacity', '0');
    
    // Create overlay div and apply styles
    function createOverlay(parent) {
        parent.css({
            display: 'block'
        });
        var image = parent.find('img');

        // Use the `height` attribute or fallback to CSS
        var overlayHeight = image.attr('height') || image.css('height') || '100%';
        
        var overlay = $('<div>').css({
            position: 'absolute',
            top: 0,
            left: 0,
            width: '100%',
            height: overlayHeight,
            backgroundImage: 'url("https://static.jojowiki.com/images/customizations/Dialog-warning-yellow.png")',
            backgroundRepeat: 'no-repeat',
            backgroundPosition: 'center',
            backgroundSize: 'contain',
            backgroundColor: 'black',
            cursor: 'pointer',
            zIndex: 1 // Ensure overlay is on top
        }).appendTo(parent);

        overlay.one('click', function(e) {
            e.preventDefault();
            $(this).remove();
            parent.css({
                backgroundImage: '',
                backgroundRepeat: '',
                backgroundPosition: '',
                backgroundSize: '',
                display: ''
            }).find('img').css({
                visibility: '',
                opacity: '100%'
            });
        });

        console.log('Overlay created with height:', overlayHeight);
    }

    // Wrap non-thumb images and add overlay
    $('img[alt*=NSFWTAG]').parent().add($('.thumb')
        .has('.nsfwelement').find('.image').parent())
        .each(function() {
            var parent = $(this);
            parent.css({
                position: 'relative', // Ensure parent is positioned
            }).find('img').css({
                visibility: 'hidden'
            });

            createOverlay(parent);
        });

    // Add overlay to elements with NSFWTAG in title attribute within .image
    $('.image[title*=NSFWTAG]').each(function() {
        var parent = $(this).closest('.thumb');
        if (parent.find('img[alt*=NSFWTAG]').length === 0) { // Only apply if no img with NSFWTAG is found
            parent.css({
                position: 'relative', // Ensure parent is positioned
            });

            createOverlay(parent);
        }
    });
});