MediaWiki:Vector.js: Difference between revisions

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




/**
(function($) {
* Surgical OOUI Sync for Image Fields
    /**
* Targets fields with autocompletesettings="File" to avoid clicking other fields.
    * OOUI Sync Fix: Performs a physical click into the image field
*/
    * exactly once after an upload is detected.
$(document).on('pfUploadComplete', function() {
    */
     // 1. Wait 1 second as requested
     var lastValue = "";
    setTimeout(function() {
        // 2. TARGET ONLY THE FILE FIELD:
        // We use the unique 'autocompletesettings' attribute you found.
        var $fileField = $('input[autocompletesettings="File"]');


         if ($fileField.length > 0 && $fileField.val() !== "") {
    var observer = new MutationObserver(function(mutations) {
            var el = $fileField[0]; // Get the raw HTML element
         mutations.forEach(function(mutation) {
            // Check for the value being inserted into an OOUI field
            if (mutation.type === "attributes" && mutation.attributeName === "value") {
                var target = mutation.target;
                var currentVal = target.value;


            // 3. THE PHYSICAL CLICK:
                // 1. Only act if the field has an image filename and it's NEW
            // This replicates the hardware interaction required by OOUI.
                if (currentVal && currentVal !== lastValue && target.getAttribute('autocompletesettings') === "File") {
            el.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
                   
            el.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
                    console.log("OOUI Sync: New value detected, performing one-shot click...");
            el.dispatchEvent(new MouseEvent('click', { bubbles: true }));


            // 4. FOCUS: Ensure the cursor enters the box
                    // 2. Perform a REAL physical click
            $fileField.focus();
                    // We dispatch mousedown/mouseup to satisfy OOUI's internal logic
                    target.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
                    target.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
                    target.dispatchEvent(new MouseEvent('click', { bubbles: true }));


            console.log("Surgical Sync: Clicked the image field successfully.");
                    // 3. Force focus and place cursor at the end
         }
                    target.focus();
     }, 1000);
                    var len = currentVal.length;
});
                    target.setSelectionRange(len, len);
 
                    // 4. Force data sync so the "Save" button recognizes it
                    target.dispatchEvent(new Event('input', { bubbles: true }));
                    $(target).trigger('change');
 
                    // 5. Update lastValue so we don't click again until the next change
                    lastValue = currentVal;
                }
            }
        });
    });
 
    // Start watching all OOUI inputs as soon as the document is ready
    $(document).ready(function() {
        var $oouiFields = $('.oo-ui-inputWidget-input');
        $oouiFields.each(function() {
            observer.observe(this, { attributes: true });
         });
     });
})(jQuery);

Revision as of 15:55, 25 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 */
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', '');
        }
    });
});



(function($) {
    /**
     * OOUI Sync Fix: Performs a physical click into the image field 
     * exactly once after an upload is detected.
     */
    var lastValue = "";

    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            // Check for the value being inserted into an OOUI field
            if (mutation.type === "attributes" && mutation.attributeName === "value") {
                var target = mutation.target;
                var currentVal = target.value;

                // 1. Only act if the field has an image filename and it's NEW
                if (currentVal && currentVal !== lastValue && target.getAttribute('autocompletesettings') === "File") {
                    
                    console.log("OOUI Sync: New value detected, performing one-shot click...");

                    // 2. Perform a REAL physical click
                    // We dispatch mousedown/mouseup to satisfy OOUI's internal logic
                    target.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
                    target.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
                    target.dispatchEvent(new MouseEvent('click', { bubbles: true }));

                    // 3. Force focus and place cursor at the end
                    target.focus();
                    var len = currentVal.length;
                    target.setSelectionRange(len, len);

                    // 4. Force data sync so the "Save" button recognizes it
                    target.dispatchEvent(new Event('input', { bubbles: true }));
                    $(target).trigger('change');

                    // 5. Update lastValue so we don't click again until the next change
                    lastValue = currentVal;
                }
            }
        });
    });

    // Start watching all OOUI inputs as soon as the document is ready
    $(document).ready(function() {
        var $oouiFields = $('.oo-ui-inputWidget-input');
        $oouiFields.each(function() {
            observer.observe(this, { attributes: true });
        });
    });
})(jQuery);