MediaWiki:Vector.js: Difference between revisions

From Forklift Certified Video Games
Jump to navigation Jump to search
Line 1: Line 1:
/* All JavaScript here will be loaded for users of the Vector skin */
/* All JavaScript here will be loaded for users of the Vector skin */


/* --- BLOCK POPUPS ON IMAGES AT THE SOURCE --- */
/* --- THE ULTIMATE IMAGE POPUP KILLER --- */
mw.hook( 'pp.ready' ).add( function () {
(function() {
    // We hook into the extension's own 'ready' event
    function killImagePopups() {
    $('.mw-parser-output a').each( function () {
        // Target all image links and any links with images inside them
        var $this = $( this );
        var links = document.querySelectorAll('.mw-parser-output a.image, .mw-parser-output a.mw-file-description, .mw-parser-output a:has(img)');
         // If the link contains an image, we strip the trigger attributes
          
         if ( $this.find( 'img' ).length > 0 || $this.hasClass('image') ) {
         links.forEach(function(link) {
             $this.addClass( 'no-popup' );
             // 1. Add the official ignore classes
             $this.attr( 'data-dw-no-popup', 'true' );
            link.classList.add('no-popup');
             // Strip the 'title'—Popups cannot trigger without a title attribute
             link.classList.add('nopopups');
             $this.removeAttr( 'title' );
           
        }
             // 2. THE NUCLEAR OPTION: Remove the 'title' attribute.
    } );
            // The extension CANNOT trigger without a title attribute.
} );
             if (link.hasAttribute('title')) {
                var originalTitle = link.getAttribute('title');
                link.setAttribute('data-original-title', originalTitle);
                link.removeAttribute('title');
            }
        });
    }


// Secondary check for late-loading Cargo content
    // Run immediately and every time the content changes (for Cargo/Slideshows)
$(window).on('load', function() {
    killImagePopups();
    $('.mw-parser-output a.image, .mw-parser-output a.mw-file-description').removeAttr('title').addClass('no-popup');
    var observer = new MutationObserver(killImagePopups);
});
    observer.observe(document.body, { childList: true, subtree: true });
 
    // Optional: Restore a browser tooltip that doesn't trigger the popup
    $(document).on('mouseenter', 'a[data-original-title]', function() {
        $(this).attr('title', $(this).attr('data-original-title'));
    }).on('mouseleave', 'a[data-original-title]', function() {
        $(this).removeAttr('title');
    });
})();


/* Move subcategories to the top on Category:Forklifts and Category:Video_Games */
/* Move subcategories to the top on Category:Forklifts and Category:Video_Games */

Revision as of 01:03, 3 February 2026

/* All JavaScript here will be loaded for users of the Vector skin */

/* --- THE ULTIMATE IMAGE POPUP KILLER --- */
(function() {
    function killImagePopups() {
        // Target all image links and any links with images inside them
        var links = document.querySelectorAll('.mw-parser-output a.image, .mw-parser-output a.mw-file-description, .mw-parser-output a:has(img)');
        
        links.forEach(function(link) {
            // 1. Add the official ignore classes
            link.classList.add('no-popup');
            link.classList.add('nopopups');
            
            // 2. THE NUCLEAR OPTION: Remove the 'title' attribute. 
            // The extension CANNOT trigger without a title attribute.
            if (link.hasAttribute('title')) {
                var originalTitle = link.getAttribute('title');
                link.setAttribute('data-original-title', originalTitle);
                link.removeAttribute('title');
            }
        });
    }

    // Run immediately and every time the content changes (for Cargo/Slideshows)
    killImagePopups();
    var observer = new MutationObserver(killImagePopups);
    observer.observe(document.body, { childList: true, subtree: true });

    // Optional: Restore a browser tooltip that doesn't trigger the popup
    $(document).on('mouseenter', 'a[data-original-title]', function() {
        $(this).attr('title', $(this).attr('data-original-title'));
    }).on('mouseleave', 'a[data-original-title]', function() {
        $(this).removeAttr('title');
    });
})();

/* Move subcategories to the top on Category:Forklifts and Category:Video_Games */
if ( mw.config.get( 'wgPageName' ) === 'Category:Forklifts' || mw.config.get( 'wgPageName' ) === 'Category:Video_Games' ) {
    $( function() {
        var subcats = $( '#mw-subcategories' );
        var content = $( '.mw-parser-output' );
        if ( subcats.length && content.length ) {
            subcats.insertBefore( content );
            $( 'body' ).addClass( 'move-subcategories-up-active' );
            $( '#bodyContent' ).css( { 'display': 'flex', 'flex-direction': 'column' } );
            content.css( 'order', '2' );
            subcats.css( 'order', '1' );
        }
    } );
}

/* SHOW ALL BUTTON */
mw.loader.using(['mediawiki.util', 'jquery'], function() {
    $(function() {
        $('.cargo-show-all-btn').on('click', function() {
            var $this = $(this);
            var $grid = $this.siblings('.cargo-hidden-grid');

            // Toggle visibility
            $grid.toggleClass('grid-visible');
            
            // Dynamic text switching
            var isVisible = $grid.hasClass('grid-visible');
            var originalText = $this.text();
            
            if (isVisible) {
                $this.data('original-text', originalText);
                $this.text(originalText.replace('Show All', 'Hide')).addClass('btn-active');
            } else {
                $this.text($this.data('original-text')).removeClass('btn-active');
            }
        });
    });
});


/* NEWS SLIDESHOW */
mw.loader.using('mediawiki.util').then(function () {
    mw.hook('wikipage.content').add(function () {
        document.querySelectorAll('.news-slideshow-container').forEach(function (container) {
            var slides = container.querySelectorAll('.news-slideshow-slide');
            if (slides.length <= 1) return;

            var current = 0;
            var intervalId = null;
            var delay = 8000; // 8 seconds

            function showSlide(index) {
                slides.forEach(function (slide, i) {
                    slide.classList.toggle('active', i === index);
                });
            }

            function startSlideshow() {
                if (intervalId !== null) return;
                intervalId = setInterval(function () {
                    current = (current + 1) % slides.length;
                    showSlide(current);
                }, delay);
            }

            function stopSlideshow() {
                if (intervalId !== null) {
                    clearInterval(intervalId);
                    intervalId = null;
                }
            }

            // Initial state
            showSlide(current);
            startSlideshow();

            // Pause on hover
            container.addEventListener('mouseenter', stopSlideshow);
            container.addEventListener('mouseleave', startSlideshow);
        });
    });
});


/* FORM TABLE */
document.addEventListener('click', function (e) {
	const header = e.target.closest('.pf-collapsible');
	if (!header) return;

	const targetId = header.dataset.target;
	const section = document.getElementById(targetId);
	if (!section) return;

	const isOpen = section.style.display === 'block';
	section.style.display = isOpen ? 'none' : 'block';
});

/* CLICKABLE HOME BUTTON AT THE TOP IN MOBILE VIEW */
$(document).ready(function() {
    $('#left-navigation').on('click', function(e) {
        if (e.target === this || $(e.target).is('::before')) {
            window.location.href = mw.config.get('wgArticlePath').replace('$1', '');
        }
    });
});