MediaWiki:Vector.js: Difference between revisions

From Forklift Certified Video Games
Jump to navigation Jump to search
 
(117 intermediate revisions by the same user not shown)
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 */


mw.loader.using('jquery', function () {
/* Redirect ALL red links to the "Create Page" page */
     $('#addGalleryRowBtn').on('click', function () {
$('.new').on('click', function(e) {
         addGalleryRow();
    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 main categories */
if ( mw.config.get( 'wgPageName' ) === 'Category:Achievements' || mw.config.get( 'wgPageName' ) === 'Category:Forklifts' || mw.config.get( 'wgPageName' ) === 'Category:Video_Games' || mw.config.get( 'wgPageName' ) === 'Category:Items' || mw.config.get( 'wgPageName' ) === 'Category:Memes' || mw.config.get( 'wgPageName' ) === 'Category:Missions' || mw.config.get( 'wgPageName' ) === 'Category:Mods' ) {
    $( 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');
            }
         });
     });
     });
});
});


function addGalleryRow() {
 
     alert('Button works!');
/* 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);
});

Latest revision as of 01:50, 1 May 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 main categories */
if ( mw.config.get( 'wgPageName' ) === 'Category:Achievements' || mw.config.get( 'wgPageName' ) === 'Category:Forklifts' || mw.config.get( 'wgPageName' ) === 'Category:Video_Games' || mw.config.get( 'wgPageName' ) === 'Category:Items' || mw.config.get( 'wgPageName' ) === 'Category:Memes' || mw.config.get( 'wgPageName' ) === 'Category:Missions' || mw.config.get( 'wgPageName' ) === 'Category:Mods' ) {
    $( 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); 
});