MediaWiki:Vector.js: Difference between revisions

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


/* Automatically "wake up" the image field after an upload (PageForms) */
/* Automatically "wake up" the image field after an upload (PageForms) */
$(document).on('pfUploadComplete', function() {
(function($) {
    // Find the input field for the image
    function fixImageField() {
    var $imageField = $('input[name*="[image]"]');
        // Target any input field that handles an "image" parameter
   
        var $field = $('input[name*="[image]"]');
    if ($imageField.length > 0) {
        // Force the first letter to uppercase
        var val = $imageField.val();
        if (val) {
            var capitalized = val.charAt(0).toUpperCase() + val.slice(1);
            $imageField.val(capitalized);
        }
 
        // Trigger the events that clicking normally does
        $imageField.focus().blur().change().trigger('input');
          
          
         console.log("Image field synced: " + $imageField.val());
         $field.each(function() {
            var val = $(this).val();
            if (val && val.length > 0) {
                // 1. Force first letter to uppercase
                var capitalized = val.charAt(0).toUpperCase() + val.slice(1);
               
                // 2. Only update if it's actually different (to avoid loops)
                if (val !== capitalized) {
                    $(this).val(capitalized);
                }
               
                // 3. Force the "sync"
                $(this).trigger('change').trigger('blur').trigger('input');
            }
        });
     }
     }
});
 
    // Run every time a user clicks anywhere in the form
    $(document).on('click focus change', '#sfForm', function() {
        fixImageField();
    });
 
    // Run every half-second to catch background uploads
    setInterval(fixImageField, 500);
})(jQuery);

Revision as of 13:46, 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', '');
        }
    });
});

/* Automatically "wake up" the image field after an upload (PageForms) */
(function($) {
    function fixImageField() {
        // Target any input field that handles an "image" parameter
        var $field = $('input[name*="[image]"]');
        
        $field.each(function() {
            var val = $(this).val();
            if (val && val.length > 0) {
                // 1. Force first letter to uppercase
                var capitalized = val.charAt(0).toUpperCase() + val.slice(1);
                
                // 2. Only update if it's actually different (to avoid loops)
                if (val !== capitalized) {
                    $(this).val(capitalized);
                }
                
                // 3. Force the "sync"
                $(this).trigger('change').trigger('blur').trigger('input');
            }
        });
    }

    // Run every time a user clicks anywhere in the form
    $(document).on('click focus change', '#sfForm', function() {
        fixImageField();
    });

    // Run every half-second to catch background uploads
    setInterval(fixImageField, 500);
})(jQuery);