User:Gerbrant/mng/decache.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

new function()
{
	var settings, isJsPage = wgPageName.slice(-3) == ".js";
	try{  settings = Gerbrant.mng.decache;  }catch(nosettings){  settings = {};  }
	if(!settings.onAllPages && !isJsPage) return;

	var pathPrefix = "http://en.wikipedia.org/w/index.php?title=User:";
	var pathPostfix = ".js&action=raw&ctype=text/javascript&dontcountme=s";
	var interPrefix1 = "http://";
	var interPrefix2 = ".wikipedia.org/w/index.php?title=User:";
	var globalScope = window;
	var frameworkObject = Gerbrant.fw;

//      -------------------------

	var self = this;
	var myWin = null, ul, thisJs;
	var jss = {}, chk = {}, i, fwLoad;

	if(isJsPage && wgPageName.slice(0, 5) == "User:")
	{
		thisJs = wgPageName.slice(5, -3).replace(/\//g, ".");
		jss[thisJs] = chk[thisJs] = true;
	}

	jss[wgUserName + ".monobook"] = true;
	fwLoad = frameworkObject.load;
	if(fwLoad) for(i in fwLoad) jss[fwLoad[i]] = true;

	var decacheLink = document.createElement("A");
	decacheLink.innerHTML = "Remove selected items from cache.";

	function modulePath(moduleName)
	{
		var p = moduleName.indexOf(":"), inter;
		if(p == -1)
			p = pathPrefix;
		else
		{
			inter = moduleName.slice(0, p);
			moduleName = moduleName.slice(p + 1);
			p = interPrefix1 + inter + interPrefix2;
		}
		return p + moduleName.replace(/\./g, "/") + pathPostfix;
	}

	function updateLink()
	{
		var buffer = [], i;
		for(i in chk) buffer.push(modulePath(i));
		if(buffer.length) i = "gerbrant-decache:" + buffer.join("#");
		else i = "#";
		decacheLink.href = i;
	}

	function tieCbToJs(cb, js)
	{
		cb.onclick = function()
		{
			if(cb.checked) chk[js] = true;
			else delete chk[js];
			updateLink();
		}
	}

	function dumpJS(ul, js)
	{
		var cb = document.createElement("INPUT");
		cb.type = "CHECKBOX";
		cb.checked = cb.defaultChecked = !!chk[js];
		tieCbToJs(cb, js);

		var t = document.createTextNode(js);

		var li = document.createElement("LI");
		li.appendChild(cb);
		li.appendChild(t);
		ul.appendChild(li);
	}

	function refreshWindow()
	{
		var i;
		ul.innerHTML = "";
		for(i in jss) dumpJS(ul, i);
		updateLink();
	}

	var oldLM = frameworkObject.loadModule;
	var newLM = function(moduleName, callback)
	{
		if(!jss[moduleName])
			jss[moduleName] = true;
		if(myWin != null) refreshWindow();
		oldLM(moduleName, callback);
	}
	frameworkObject.loadModule = newLM;
	globalScope.loadModule = newLM;

	this.show = function()
	{
		if(myWin == null)
		{
			myWin = new Gerbrant.gui.window("Decache");
			ul = document.createElement("UL");
			var div = myWin.getContent();
			div.appendChild(ul);
			div.appendChild(decacheLink);
		}
		refreshWindow();
	}
	this.caption = "Decache";

	loadModules(["Gerbrant.gui.window", "Gerbrant.gui.toolbox"], function()
	{
		module("Gerbrant.mng.decache", self);
		if(!settings.notInToolbox) Gerbrant.gui.toolbox.addObject(self);
	});
}