User:Madminecrafter12/common.js

// Initialises stripes on all zebra tables after page load $(document).ready(function {	$('table.zebra tbody tr:nth-child(even)').addClass('even');	$('table.zebra tbody tr:nth-child(odd)').addClass('odd'); });

// Re do stripes on a table after sort finishes $(document).on('sortEnd.tablesorter', function(event) {	var table = $(event.target);	if(table.hasClass('zebra')) {		$('tbody tr:nth-child(even)',table).addClass('even').removeClass('odd');		$('tbody tr:nth-child(odd)',table).addClass('odd').removeClass('even');	} });

function getImageThumbnailURL(name, store, size) { var encodedName = encodeURIComponent(name.replace(/ /g, "_")); return "https://minecraft.gamepedia.com/media/minecraft.gamepedia.com/thumb/" + store + "/"		+ encodedName + "/"		+ size + "px-" + encodedName; }

function mimicIndicator(id, link, imgName, imgStore, title) { var encodedLink = encodeURIComponent(link.replace(/ /g, "_")); return $(" ") .attr("id", "mw-indicator-" + id) .addClass("mw-indicator") .append($("")			.attr({ "href": "/" + encodedLink, "title": title }).append($(" ") .attr({				"alt": title,				"src": getImageThumbnailURL(imgName, imgStore, 25),				"srcset": getImageThumbnailURL(imgName, imgStore, 38)					+ " 1.5x, "					+ getImageThumbnailURL(imgName, imgStore, 50)					+ " 2x",				"width": "25",				"height": "25"				}) )		); }

$(function {	var protectionLevelData = mw.config.get("wgRestrictionEdit");	if (protectionLevelData === null) {		// Null on nonexistent or special pages. Avoids a crash there.		return;	}	if (mw.config.get("wgAction") !== "view") {		// No need to display the indicator when viewing history or editing the page		return;	}	if (mw.config.get("wgPageName") === "Minecraft_Wiki") {		// The indicator lock breaks formatting on the main page due to the level 1 header being hidden		return;	} 	var protectionLevel = protectionLevelData[0];	if (protectionLevel === "autoconfirmed") {		mimicIndicator( "protection-semi", "Minecraft Wiki:Autoconfirmed users", "Semi-protected page lock.png", "9/9b", "This page is semi-protected so that only registered users can edit it." ).appendTo($(".mw-indicators"));	} else if (protectionLevel === "directoreditprotected") {		mimicIndicator( "protection-director", "Minecraft Wiki:Directors", "Director-protected page lock.png", "8/85",			"This page is directors-only protected so that only directors can edit it." ).appendTo($(".mw-indicators"));	} else if (protectionLevel === "sysop") {		mimicIndicator( "protection-full", "Minecraft Wiki:Administrators", "Fully-protected page lock.png", "4/49",			"This page is fully protected so that only administrators can edit it." ).appendTo($(".mw-indicators"));	} });

/** * A toggle script for hiding "patrol-requests". * This is basically just a function for hiding/redisplaying a heading with its underlying conversation. * * Created by User:Jack McKalling */ $(document).ready(function {	var objStatusClasses = {		"new": "patrol-request-status-new",		"wip":  "patrol-request-status-wip",		"done": "patrol-request-status-done"	};	/**	 * Check whether the current user wants to show (true) or hide (false) the specified patroller request status.	 * If the current user did not set these settings before, this method returns false by default.	 * This information is stored in the browser's localStorage (assuming there is one)	 * @return boolean	 */	function hasUserSetting(strName) {		var strRaw = localStorage.getItem("mw-patrol-requests-settings");		if (strName === null && strRaw === null) return null; // tell that user has no settings yet		// localStorage variable should be formatted as "status1 status2 statusX", e.g. "new wip"		// each value specifies a status of requests that should be shown		if (strRaw === null) return false;		if ((" " + strRaw + " ").indexOf(" " + strName + " ") < 0)  return false; return true; }	/**	 * Sets the user preference for the specified patroller request status. * If the current user wants the specified status to show, it will be appended to a space-separated multi value. * This information is acquired from the browser's localStorage (assuming there is one) * @param {string} strStatusKey The status key to set the value for * @param {boolean} booShow The value whether to show (true) or hide (false) */	function setUserSetting(strStatusKey, booShow) { var strSettings = localStorage.getItem("mw-patrol-requests-settings"); if (!objStatusClasses.hasOwnProperty(strStatusKey)) return; // cancel if status doesn't exist if (strSettings === null || strSettings === "") strSettings = ""; // new usersettings else if (strSettings == strStatusKey) strSettings = ""; // unset specified status else strSettings = (" " + strSettings + " ").replace(" " + strStatusKey + " ", " ").trim; // strip away specified status if (booShow) { strSettings += (strSettings !== "" ? " " : "") + strStatusKey; // add specified status }		localStorage.setItem("mw-patrol-requests-settings", strSettings); }	/**	 * Create a handler for toggling the display of "empty" headings. * The string argument specifies the status of the requests that are being displayed/hidden. * @param {boolean} booShow Whether to display or hide the "empty" headings * @param {string} strByStatus Specifies that requests with this status */	function toggleEmptyHeadings(booShow, strByStatus) { strByStatus = objStatusClasses.hasOwnProperty(strByStatus) ? strByStatus : ""; $("h2").nextAll("h3").each(function {			var headingContent = $(this).nextUntil("h3"), funcHasUnhideableRequests = function {				// detect requests without any status as unhideable				if ($(this).is(".patrol-request") && !$(this).is("[class*='patrol-request-status-']")) return true;				// (merged if with for-loop to only iterate over the above user defined properties)				for (var strStatusKey in objStatusClasses) if (objStatusClasses.hasOwnProperty(strStatusKey)) {					// detect requests with an unhideable status as unhideable					if (strStatusKey != strByStatus && $(this).is("." + objStatusClasses[strStatusKey])) {						if (hasUserSetting(strStatusKey)) return true; // but only if also set to display by user setting					}				}				return false; // only hideable stuff at this point			};			// check if heading contains only hideable conversations			if (headingContent.length > 0 // if heading is not empty && headingContent.is(function { return $(this).is("." + objStatusClasses[strByStatus]); }) // and contains hideable requests && !headingContent.is(funcHasUnhideableRequests)) { // but no unhideable ones				if (booShow) $(this).show;				else $(this).hide;			}		}); }	/**	 * Create handler for saving the display/hide state of patrol requests by their status * @param {jQuery.event} e The event that jQuery passes into the handler on trigger */	function saveNewDisplayState(e) { var booShowRequests = hasUserSetting(e.data.statusKey), // get current user setting objToggleLinks = $("." + objStatusClasses[e.data.statusKey].replace("-status-", "-toggle-")); // get the triggering jslinks booShowRequests = !booShowRequests; // invert toggle mode only on saving setUserSetting(e.data.statusKey, booShowRequests); // save user setting toggleDisplay(objToggleLinks, e.data.statusKey, booShowRequests); // activate toggled display }	/**	 * Toggle display of patrol requests by their status. * The togle may be triggered by a jslink which matches the status of the requests to toggle. * Multiple jslinks can trigger the same status. * @param {jQuery} objTrigger The jQuery object containing the jslink(s) that triggered the toggle * @param {string} strStatusKey The status key of patrol requests to toggle, must exist as key in the objStatusClasses global * @param {boolean} booShow Whether to show (true) or hide (false) the specified requests */	function toggleDisplay(objTrigger, strStatusKey, booShow) { $("." + objStatusClasses[strStatusKey]).next("dl").toggle(booShow); // toggle conversation $("." + objStatusClasses[strStatusKey]).toggle(booShow); // toggle request toggleEmptyHeadings(booShow, strStatusKey); // toggle grouped heading objTrigger.html(booShow ? "Hide" : "Show"); // toggle the triggering jslink }	/**	 * Attach handler to all relevant toggle jslinks and actuate the user setting state on load */	var objToggleLinks = null, booNoSettings = hasUserSetting(null) === null; for (var strStatusKey in objStatusClasses) if (objStatusClasses.hasOwnProperty(strStatusKey)) { if (booNoSettings) setUserSetting(strStatusKey, true); // enable all by default when user has no settings yet objToggleLinks = $("." + objStatusClasses[strStatusKey].replace("-status-", "-toggle-")); objToggleLinks.on("click", { statusKey: strStatusKey }, saveNewDisplayState); // add handler with autosave if (!hasUserSetting(strStatusKey)) toggleDisplay(objToggleLinks, strStatusKey, false); // hide on absence in user settings } });