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


/* --- THE INDESTRUCTIBLE IMAGE POPUP KILLER --- */
/* DISABLE PAGE PREVIEWS ON IMAGES ONLY */
$(document).ready(function() {
mw.hook( 'pp.ready' ).add( function () {
     // Target all image links specifically
     // Select every link that contains an image
     $('.mw-parser-output').on('mouseenter touchstart', 'a.image, a.mw-file-description, .no-popup', function() {
     $( '.mw-parser-output a' ).has( 'img' ).each( function () {
         var $this = $(this);
        // Adding 'no-popup' is the official way to tell the extension to ignore a link
        var realHref = $this.attr('href');
         $( this ).addClass( 'no-popup' );
          
          
         if (realHref && !realHref.includes('#')) {
        // As a nuclear backup, remove the title attribute which triggers the preview
            // 1. Move the real link to a data attribute
         if ( $( this ).attr( 'title' ) ) {
             $this.attr('data-real-href', realHref);
             $( this ).attr( 'data-title', $( this ).attr( 'title' ) );
            // 2. Change the href to just the hash - Popups ignores anchor-only links
             $( this ).removeAttr( 'title' );
             $this.attr('href', '#');
         }
         }
    }).on('mouseleave touchend', 'a.image, a.mw-file-description, .no-popup', function() {
     } );
        var $this = $(this);
} );
        var originalHref = $this.attr('data-real-href');
       
        if (originalHref) {
            // 3. Restore the link so it remains clickable
            $this.attr('href', originalHref);
        }
    });
 
    // Handle the click in case the 'mouseleave' hasn't fired yet
    $('.mw-parser-output').on('click', 'a[data-real-href]', function(e) {
        var url = $(this).attr('data-real-href');
        if (url) window.location.href = url;
     });
});


/* 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:19, 3 February 2026

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

/* DISABLE PAGE PREVIEWS ON IMAGES ONLY */
mw.hook( 'pp.ready' ).add( function () {
    // Select every link that contains an image
    $( '.mw-parser-output a' ).has( 'img' ).each( function () {
        // Adding 'no-popup' is the official way to tell the extension to ignore a link
        $( this ).addClass( 'no-popup' );
        
        // As a nuclear backup, remove the title attribute which triggers the preview
        if ( $( this ).attr( 'title' ) ) {
            $( this ).attr( 'data-title', $( this ).attr( 'title' ) );
            $( 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', '');
        }
    });
});