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 */


/* --- IMAGE POPUP GATEKEEPER --- */
/**
* SURGICAL IMAGE POPUP KILLER
* Intercepts events before the Popups extension can see them.
*/
(function() {
(function() {
     function cloakImages() {
     function handleImageHover(e) {
         // Targets standard images and new 1.43 file descriptions
         // Find if we are hovering an image link
         var imgs = document.querySelectorAll('a.image, a.mw-file-description');
         var link = e.target.closest('a.image, a.mw-file-description, .no-popup');
         for (var i = 0; i < imgs.length; i++) {
          
             if (!imgs[i].classList.contains('no-popup')) {
        if (link) {
                 // Extension:Popups explicitly ignores links with these classes
            // 1. Temporarily move the title to a safe data attribute
                imgs[i].classList.add('no-popup');
             if (link.hasAttribute('title')) {
                 imgs[i].classList.add('nopopups');
                 link.setAttribute('data-ignored-title', link.getAttribute('title'));
                 link.removeAttribute('title');
             }
             }
            // 2. Stop the event from reaching the Popups extension logic
            e.stopImmediatePropagation();
         }
         }
     }
     }


     // Run every 50ms for the first 2 seconds to beat the extension's scan
     // Capture phase (true) is the 'Cleanest Trick' to beat extensions
     var interval = setInterval(cloakImages, 50);
     window.addEventListener('mouseover', handleImageHover, true);
     setTimeout(function() { clearInterval(interval); }, 2000);
     window.addEventListener('touchstart', handleImageHover, true);


     // Watch for dynamic content (Cargo, etc.)
     // Restore title on mouseout so browser tooltips still work
    var observer = new MutationObserver(cloakImages);
    window.addEventListener('mouseout', function(e) {
    observer.observe(document.body, { childList: true, subtree: true });
        var link = e.target.closest('a.image, a.mw-file-description, .no-popup');
        if (link && link.hasAttribute('data-ignored-title')) {
            link.setAttribute('title', link.getAttribute('data-ignored-title'));
            link.removeAttribute('data-ignored-title');
        }
    }, true);
})();
})();



Revision as of 01:15, 3 February 2026

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

/** 
 * SURGICAL IMAGE POPUP KILLER 
 * Intercepts events before the Popups extension can see them.
 */
(function() {
    function handleImageHover(e) {
        // Find if we are hovering an image link
        var link = e.target.closest('a.image, a.mw-file-description, .no-popup');
        
        if (link) {
            // 1. Temporarily move the title to a safe data attribute
            if (link.hasAttribute('title')) {
                link.setAttribute('data-ignored-title', link.getAttribute('title'));
                link.removeAttribute('title');
            }
            // 2. Stop the event from reaching the Popups extension logic
            e.stopImmediatePropagation();
        }
    }

    // Capture phase (true) is the 'Cleanest Trick' to beat extensions
    window.addEventListener('mouseover', handleImageHover, true);
    window.addEventListener('touchstart', handleImageHover, true);

    // Restore title on mouseout so browser tooltips still work
    window.addEventListener('mouseout', function(e) {
        var link = e.target.closest('a.image, a.mw-file-description, .no-popup');
        if (link && link.hasAttribute('data-ignored-title')) {
            link.setAttribute('title', link.getAttribute('data-ignored-title'));
            link.removeAttribute('data-ignored-title');
        }
    }, true);
})();

/* 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', '');
        }
    });
});