MediaWiki:Vector.js: Difference between revisions
Jump to navigation
Jump to search
| Line 173: | Line 173: | ||
/* | /* | ||
Targeting: Content Achievement[creator] | |||
*/ | */ | ||
(function () { | (function () { | ||
console.log("Cargo Dashboard: Script initialized."); | |||
if ( | function stamp() { | ||
// | // We use a CSS attribute selector that is very reliable for names with brackets | ||
if (! | var field = document.querySelector('[name="Content Achievement[creator]"]'); | ||
var user = mw.config.get('wgUserName'); | |||
console.log(" | |||
if (field) { | |||
// Only fill if the field is genuinely empty or currently "UNKNOWN" | |||
if (!field.value || field.value.trim() === "" || field.value === "UNKNOWN") { | |||
field.value = user; | |||
console.log("Cargo Dashboard: SUCCESS! Creator set to: " + user); | |||
} | } | ||
} else { | |||
console.log("Cargo Dashboard: Searching for field..."); | |||
} | } | ||
} | } | ||
// | // Check frequently for 10 seconds to catch the "Preview" redraw | ||
var | var interval = setInterval(stamp, 500); | ||
setTimeout(function() { clearInterval(interval); }, 10000); | |||
// | // Standard MediaWiki hooks | ||
mw.hook('wikipage.content').add( | mw.hook('wikipage.content').add(stamp); | ||
})(); | })(); | ||
Revision as of 23:34, 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);
});
/*
Targeting: Content Achievement[creator]
*/
(function () {
console.log("Cargo Dashboard: Script initialized.");
function stamp() {
// We use a CSS attribute selector that is very reliable for names with brackets
var field = document.querySelector('[name="Content Achievement[creator]"]');
var user = mw.config.get('wgUserName');
if (field) {
// Only fill if the field is genuinely empty or currently "UNKNOWN"
if (!field.value || field.value.trim() === "" || field.value === "UNKNOWN") {
field.value = user;
console.log("Cargo Dashboard: SUCCESS! Creator set to: " + user);
}
} else {
console.log("Cargo Dashboard: Searching for field...");
}
}
// Check frequently for 10 seconds to catch the "Preview" redraw
var interval = setInterval(stamp, 500);
setTimeout(function() { clearInterval(interval); }, 10000);
// Standard MediaWiki hooks
mw.hook('wikipage.content').add(stamp);
})();