MediaWiki:Vector.js

From Forklift Certified Video Games
Revision as of 23:19, 28 April 2026 by Admin (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* All JavaScript here will be loaded for users of the Vector skin */

/* Redirect ALL red links to the "Create Page" page */
$('.new').on('click', function(e) {
    e.preventDefault();
    window.location.href = mw.util.getUrl('Fork:Create_Page');
});

/* Accelerate native Vector collapsible tabs */
( function () {
    var triggerNativeTabs = function () {
        if ( mw.config.get( 'skin' ) === 'vector' ) {
            // Forces the native script to recalculate and move links into the "More" menu
            $( window ).trigger( 'resize' );
        }
    };
    // Run as soon as the basic page structure is ready
    $( triggerNativeTabs );
}() );

// Force immediate check for collapsible tabs in Vector Legacy
mw.loader.using( 'mediawiki.util' ).done( function () {
    $( function () {
        if ( mw.config.get( 'skin' ) === 'vector' ) {
            // Trigger the resize event immediately on load
            $( window ).trigger( 'resize' );
        }
    } );
} );

/* 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 */
$(function () {
    $('.news-slideshow-container').each(function () {
        var $container = $(this);
        var $slides = $container.find('.news-slideshow-slide');
        if ($slides.length <= 1) return;

        var current = 0;
        var timer;
        var delay = 6000;

        function nextSlide() {
            $slides.eq(current).removeClass('active');
            current = (current + 1) % $slides.length;
            $slides.eq(current).addClass('active');
        }

        function start() {
            timer = setInterval(nextSlide, delay);
        }

        function stop() {
            clearInterval(timer);
        }

        // Initialize: Show first slide immediately
        $slides.eq(0).addClass('active');
        start();

        // Interaction: Pause on hover for better accessibility
        $container.on('mouseenter', stop).on('mouseleave', start);
    });
});



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


/* PageForms autofocus/autoclick uploaded image input field */
$(function() {
    // This script will keep track of which specific fields it has already "woken up"
    var syncedFields = new Set();

    var syncInterval = setInterval(function() {
        // Target ONLY fields meant for File uploads (ignoring dates, text, etc.)
        $('input[autocompletesettings="File"]').each(function() {
            var $field = $(this);
            var el = this;
            var val = $field.val();

            // Only act if:
            // 1. The field has a filename
            // 2. We haven't already performed the "One-Shot" click for this specific field
            if (val !== "" && !syncedFields.has(el)) {
                
                // 1. PERFORM THE PHYSICAL CLICK
                // Replicates your manual interaction exactly
                el.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
                el.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
                el.dispatchEvent(new MouseEvent('click', { bubbles: true }));

                // 2. ACTIVATE CURSOR
                $field.focus();
                
                // 3. COMMIT DATA
                el.dispatchEvent(new Event('input', { bubbles: true }));
                $field.trigger('change');

                // 4. LOCK THIS FIELD
                // Adds this specific HTML element to our "already done" list
                syncedFields.add(el);
                
                console.log("Surgical Sync: Activated field for: " + val);
            }
        });
        
        // Stop the timer once ALL current file fields are synced, but keep it running allows it to catch new gallery rows if added.
    }, 500); 
});




/* 
   PREVIEW-PROOF CREATOR LOCK
   Grabs the 'baked' username from the preload and locks it 
   so the Preview button cannot wipe it.
*/
$(function() {
    function lockCreatorField() {
        var creatorField = document.getElementById('permanent-creator');
        
        if (creatorField) {
            // 1. If the field has a baked name, lock it to 'Read Only'
            if (creatorField.value !== "" && creatorField.value !== null) {
                creatorField.readOnly = true;
                // 2. Extra Insurance: Store it in the browser's session memory 
                // so it survives the Preview refresh.
                sessionStorage.setItem('baked_creator', creatorField.value);
            } 
            // 3. If Preview wiped the field, pull it back from session memory
            else if (sessionStorage.getItem('baked_creator')) {
                creatorField.value = sessionStorage.getItem('baked_creator');
            }
        }
    }

    // Run on load and every time the form updates (like after a Preview)
    mw.hook('pf.formLoad').add(lockCreatorField);
    mw.hook('wikipage.content').add(lockCreatorField);
});