MediaWiki:Gadget-togglePatrollerRequests.js

/** * 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 strStatusKey string The status key to set the value for * @param booShow boolean 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 booShow boolean Whether to display or hide the "empty" headings * @param strByStatus string 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])) return true;				}				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[strStatusKey]); }) // and contains hideable requests && !headingContent.is(funcHasUnhideableRequests)) { // but no unhideable ones				if (booShow) $(this).show;				else $(this).hide;			}		}); }	/**	 * Create handler for displaying/hiding patrol requests by their status */	function displayHandler(e) { var booShowRequests = hasUserSetting(e.data.statusKey); if (e.data.saveValue === true) booShowRequests = !booShowRequests; // invert toggle mode only on saving $("." + objStatusClasses[e.data.statusKey]).next("dl").toggle(booShowRequests); // toggle conversation $("." + objStatusClasses[e.data.statusKey]).toggle(booShowRequests); // toggle request toggleEmptyHeadings(booShowRequests, e.data.statusKey); // toggle grouped heading if (e.data.saveValue === true) setUserSetting(e.data.statusKey, booShowRequests); // toggle user setting if requested $(this).html(booShowRequests ? "Hide" : "Show"); // toggle link }	/**	 * Attach handler to all relevant toggle links */	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, saveValue: true }, displayHandler); // add handler with autosave if (!hasUserSetting(strStatusKey)) objToggleLinks.trigger({ type: "click", saveValue: false, statusKey: strStatusKey }); // hide on absence in usersettings } });