MediaWiki:Vector.js: Difference between revisions

From Forklift Certified Video Games
Jump to navigation Jump to search
Line 174: Line 174:


/*  
/*  
   PREVIEW-PROOF CREATOR LOCK
   Forces the username into the 'creator' field and  
  Grabs the 'baked' username from the preload and locks it
   prevents the Preview button from wiping it.
   so the Preview button cannot wipe it.
*/
*/
$(function() {
(function () {
     function lockCreatorField() {
     function forceCreatorStamp() {
         var creatorField = document.getElementById('permanent-creator');
         var creatorField = document.getElementById('permanent-creator');
          
         var currentUser = mw.config.get('wgUserName');
         if (creatorField) {
 
             // 1. If the field has a baked name, lock it to 'Read Only'
         if (creatorField && currentUser) {
             if (creatorField.value !== "" && creatorField.value !== null) {
             // Only fill if it's currently empty (handles the Preview wipe)
                 creatorField.readOnly = true;
             if (!creatorField.value || creatorField.value.trim() === "") {
                 // 2. Extra Insurance: Store it in the browser's session memory
                 creatorField.value = currentUser;
                // so it survives the Preview refresh.
                 console.log("Creator field recovered: " + currentUser);
                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)
     // Run every 500ms for 5 seconds after the page loads
     mw.hook('pf.formLoad').add(lockCreatorField);
    // This catches the 'Preview' redraw perfectly.
     mw.hook('wikipage.content').add(lockCreatorField);
    var stampInterval = setInterval(forceCreatorStamp, 500);
});
    setTimeout(function() { clearInterval(stampInterval); }, 5000);
 
    // Also run on every possible MediaWiki event
     mw.hook('pf.formLoad').add(forceCreatorStamp);
     mw.hook('wikipage.content').add(forceCreatorStamp);
    $(forceCreatorStamp);
})();

Revision as of 23:22, 28 April 2026

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




/* 
   Forces the username into the 'creator' field and 
   prevents the Preview button from wiping it.
*/
(function () {
    function forceCreatorStamp() {
        var creatorField = document.getElementById('permanent-creator');
        var currentUser = mw.config.get('wgUserName');

        if (creatorField && currentUser) {
            // Only fill if it's currently empty (handles the Preview wipe)
            if (!creatorField.value || creatorField.value.trim() === "") {
                creatorField.value = currentUser;
                console.log("Creator field recovered: " + currentUser);
            }
        }
    }

    // Run every 500ms for 5 seconds after the page loads 
    // This catches the 'Preview' redraw perfectly.
    var stampInterval = setInterval(forceCreatorStamp, 500);
    setTimeout(function() { clearInterval(stampInterval); }, 5000);

    // Also run on every possible MediaWiki event
    mw.hook('pf.formLoad').add(forceCreatorStamp);
    mw.hook('wikipage.content').add(forceCreatorStamp);
    $(forceCreatorStamp);
})();