Difference between revisions of "MediaWiki:Common.js"

Line 1: Line 1:
//
+
/* Any JavaScript here will be loaded for all users on every page load. */
// TEXT SWAP-OUT ANIMATION
 
//
 
// Swappers have these two attributes.
 
var nexus = $( "input[data-swapper][data-swap-interval]" );
 
if(nexus.length) {
 
// Check each swapper separately.
 
nexus.each(function() {
 
// Define variables inside here to hold stuff for the setInterval loop.
 
var nex = $(this);
 
var overlord = $( "#swap-override" );  // A checkbox to pause ALL swapping on a page
 
var inverter = $( "#swap-inverter" );  // A checkbox to invert ALL animations (check to animate)
 
var ident = nex.attr( "data-swapper" );    // Name
 
var rep = nex.attr( "data-swap-interval" ); // Duration (in milliseconds)
 
setInterval(function(){
 
var x = nex.prop( "checked" );
 
if(!overlord.prop( "checked" ) && (inverter.prop( "checked" ) ? x : !x) ) { // Pause when checked
 
$( "[data-swap-nexus=" + ident + "]" ).each(function() {
 
// Rotate out the text!
 
var swap = $(this).attr( "data-swap" ).split( " " ),
 
newText = swap.shift();
 
swap.push( newText );
 
$(this).text(newText).attr( "data-swap" , swap.join( " " ));
 
});
 
}
 
}, rep);
 
});
 
}
 
 
 
 
 
//
 
// OVERRIDE LINK TITLES (in certain circumstances)
 
//
 
$( "span[title][data-title-override] a[title]" ).removeAttr( "title" );
 
  
 +
// Function to streamline element creation
 
var $e = function (tag, text, classes, id) {
 
var $e = function (tag, text, classes, id) {
 
var d = document.createElement(tag);
 
var d = document.createElement(tag);
Line 77: Line 45:
 
document.documentElement.style.setProperty(variable, value);
 
document.documentElement.style.setProperty(variable, value);
 
};
 
};
window.console.log("v1");
+
 
 +
// Look for a Div that indicates we're a lexicon page
 +
var lexiconInfo = $i("lexiconInfo");
 +
// Swappers have these two attributes.
 +
var swapperNexus = $a("input[data-swapper][data-swap-interval]");
 +
 
 +
//
 +
// TEXT SWAP-OUT ANIMATION
 +
//
 +
if(swapperNexus.length) {
 +
// Check each swapper separately.
 +
swapperNexus.forEach(function(nex) {
 +
// Define variables inside here to hold stuff for the setInterval loop.
 +
var overlord = $i("swap-override");  // A checkbox to pause ALL swapping on a page
 +
var inverter = $i("swap-inverter");  // A checkbox to invert ALL animations (check to animate)
 +
var ds = nex.dataset;
 +
var ident = ds.swapper;    // Name
 +
var rep = parseInt(ds.swapInterval); // Duration (in milliseconds)
 +
setInterval(function(){
 +
var x = nex.checked;
 +
if(!overlord.checked && (inverter.checked ? x : !x) ) { // Pause when checked
 +
$a("[data-swap-nexus=\"" + ident + "\"]").forEach(function(d) {
 +
// Rotate out the text!
 +
var swap = d.dataset.swap.split(" ");
 +
var newText = swap.shift();
 +
swap.push(newText);
 +
d.textContent = newText;
 +
d.dataset.swap = swap.join(" ");
 +
});
 +
}
 +
}, rep);
 +
});
 +
}

Revision as of 05:33, 28 November 2019

/* Any JavaScript here will be loaded for all users on every page load. */

// Function to streamline element creation
var $e = function (tag, text, classes, id) {
	var d = document.createElement(tag);
	var cl = d.classList;
	if (classes !== undefined) {
		classes.forEach( function(c) {
			cl.add(c);
		});
	}
	if(id) {
		d.id = id;
	}
	d.textContent = (text || "");
	return d;
};
// Function to streamline getElementById
var $i = function (id, top) {
	if(top === undefined) {
		top = document;
	}
	return top.getElementById(id);
};
// Function to streamline querySelector
var $q = function (query, top) {
	if(top === undefined) {
		top = document;
	}
	return top.querySelector(query);
};
// Function to streamline querySelectorAll
var $a = function (query, top) {
	if(top === undefined) {
		top = document;
	}
	return top.querySelectorAll(query);
};
// Function to look up a CSS variable (not needed)
/*var $v = function (variable) {
	return window.getComputedStyle(document.documentElement).getPropertyValue(variable).trim();
};*/
// Function to set CSS variables
var $sv = function (variable, value) {
	document.documentElement.style.setProperty(variable, value);
};

// Look for a Div that indicates we're a lexicon page
var lexiconInfo = $i("lexiconInfo");
// Swappers have these two attributes.
var swapperNexus = $a("input[data-swapper][data-swap-interval]");

//
// TEXT SWAP-OUT ANIMATION
//
if(swapperNexus.length) {
	// Check each swapper separately.
	swapperNexus.forEach(function(nex) {
		// Define variables inside here to hold stuff for the setInterval loop.
		var overlord = $i("swap-override");  // A checkbox to pause ALL swapping on a page
		var inverter = $i("swap-inverter");  // A checkbox to invert ALL animations (check to animate)
		var ds = nex.dataset;
		var ident = ds.swapper;     // Name
		var rep = parseInt(ds.swapInterval); // Duration (in milliseconds)
		setInterval(function(){
			var x = nex.checked;
			if(!overlord.checked &&	(inverter.checked ? x : !x) ) { // Pause when checked
				$a("[data-swap-nexus=\"" + ident + "\"]").forEach(function(d) {
					// Rotate out the text!
					var swap = d.dataset.swap.split(" ");
					var newText = swap.shift();
					swap.push(newText);
					d.textContent = newText;
					d.dataset.swap = swap.join(" ");
				});
			}
		}, rep);
	});
}