User:Weberma/monobook.js

//=================================================================================== //globals that should be defined //===================================================================================

//a list containing the path to the js-file of the navigation-popup-gadget var navpopupScripts = ['MediaWiki:Gadget-navpop.css','MediaWiki:Gadget-popups.js'];;

//the text in the help-alert-box var collectionHelpText = "To inactivate this function click 'Clear Book' on the left.";

//the face of the link to the help-alert-box (can contain html-code) var collectionHelpLink = "?";

//the face of the link that adds articles and categories var collectionAddText = "add to your book";

//the face of the link that deletes article var collectionDeleteText = "delete from your book";

//a text that is displayed while processing the task var collectionProcessingText = "processing...";

//a text that is displayed after processing is done var collectionDoneText = "done";

//=================================================================================== //configuration is complete, dont touch anything below this line //===================================================================================

//=================================================================================== //import the Navpopup-gadget-script(s) //=================================================================================== for (var i = 0; i < navpopupScripts.length; i++) { if (navpopupScripts[i].match(/.js$/)) { importScript(navpopupScripts[i]); } else if (navpopupScripts[i].match(/.css$/)) { importStylesheet(navpopupScripts[i]); } }

//=================================================================================== //further globals //=================================================================================== var createBookMode; //list of articles in the collection var collectionArticleList = []; //pointer to the popupDiv var collectionPopup;

//=================================================================================== //functions //===================================================================================

//edited Navpopup.prototype.unhide-methods function collectionPopupUnhide { this.runHooks('unhide', 'before'); if (typeof this.visible != 'undefined' && !this.visible) { if (!isBadLinkForCollection) { this.mainDiv.innerHTML = getPediapressNavigation(this.mainDiv); this.mainDiv.style.display='inline'; this.visible=true; }	}	this.runHooks('unhide', 'after'); }

//edited Navpopup.prototype.setInnerHTML-method function collectionPopupSetInnerHTML { if (!isBadLinkForCollection) { this.mainDiv.innerHTML = getPediapressNavigation(this.mainDiv); } }

//a function to replace the content of the popupDiv //it receives a pointer to the current popupDiv function getPediapressNavigation(popup) { collectionPopup = popup;//save the pointer //collectionCall2('GetCollection', []); //refresh the articlelist

//investigate name and namespace of the article var href = pg.current.link.href.split('/'); var nsStr = pg.current.article.namespace; var article = getArticlenameForCollection(href[href.length-1], nsStr); var ns = getNamespaceNumber(nsStr)

//initialise the output var popupContent = ''+collectionDeleteText+'';	} else {		if (ns != 14) {			popupContent += 'collectionCall3(\'AddArticle\',['+ns+',\+article+'\',0]);return false;';		} else {			popupContent += 'collectionCall3(\'AddCategory\',[\+article+'\']);return false;';		}		popupContent += '" href="">'+collectionAddText+''; }	popupContent+= ' '+collectionHelpLink+'';

//print out the output return popupContent; }

//function to deqal with xmlhttprequests function collectionCall2(func, args) { sajax_request_type = 'POST'; sajax_do_call('wfAjax' + func, args, function(xhr) {		collectionArticleList = getCollectionArticleList(xhr.responseText);		if (collectionArticleList.length == 0) {		   stopCollectionPopups;		} else {		    startCollectionPopups;		}	}); }

//a function, that executes a xmlhttprequest, refreshes the collection and closes the popupDiv function collectionCall3(func, args) { args[1] = unescape(args[1]); //the articlename has to be edited collectionPopup.innerHTML = collectionProcessingText;

//xmlhttprequest sajax_request_type = 'POST'; sajax_do_call('wfAjaxCollection' + func, args, function(xhr) {		collectionCall2('GetCollection', []); //refresh the articlelist		sajax_request_type = 'GET';		sajax_do_call('wfAjaxCollectionGetPortlet', [func], function(xhr) { document.getElementById('collectionPortletList').parentNode.innerHTML = xhr.responseText; });		collectionPopup.innerHTML = collectionDoneText; //output		window.setTimeout("hideCollectionPopup", 500); //hide the popupDiv	}); }

//hide the popupDiv function hideCollectionPopup(popup) { collectionPopup.style.display = 'none'; collectionPopup.visble = false; }

//stop popups function stopCollectionPopups { if (createBookMode) { var bodyDiv = document.getElementById("bodyContent"); var links = bodyDiv.getElementsByTagName('a'); for (var i = 0; i < links.length; i++) { removeTooltip(links[i]); links[i].onmousedown = null; }	createBookMode = false; } }

//start popups function startCollectionPopups { if (!createBookMode) { var bodyDiv = document.getElementById("bodyContent"); var links = bodyDiv.getElementsByTagName('a'); for (var i = 0; i < links.length; i++) { addTooltip(links[i]); }	createBookMode = true; } }

//checks if an article is in the collection function isInCollection(article) { for (var i = 0; i < collectionArticleList.length; i++) { //alert(collectionArticleList[i] + ' ' + article); if (collectionArticleList[i] == unescape(article)) return true; if (pg.current.article.namespace) { if (collectionArticleList[i] == pg.current.article.namespace + ":" + article) return true; }	}	return false; }

//example of a line of the pg.ns.list-array, from the navpopup-gadget //"de": [m, 1"Spezial", 2"Diskussion", 3"Benutzer", 4"Benutzer Diskussion", 5"Wikipedia", 6"Wikipedia Diskussion", 7"Bild", 8"Bild Diskussion", 9"MediaWiki", 10"MediaWiki Diskussion", 11"Vorlage", 12"Vorlage Diskussion", 13"Hilfe", 14"Hilfe Diskussion", 15"Kategorie", 16"Kategorie Diskussion", 17"Portal", 18"Portal Diskussion"],

//some links should'nt create popups function isBadLinkForCollection { var link = pg.current.link.href; if (link.match(/#/)) return true; if (link.match(/redlink=1$/)) return true; if (link.match(/action=edit/)) return true; if (link.match(/.gif$/)) return true; if (link.match(/.jpg$/)) return true; if (link.match(/.png$/)) return true; if (link.match(/.svg$/)) return true; if (pg.current.article.namespace) { var ns = pg.current.article.namespace; if (ns != pg.ns.list[9] && ns != pg.ns.list[5] && ns != pg.ns.list[15]) return true; }	return false; }

//a function to get the number of an namespacestring function getNamespaceNumber(nsStr) { var nsInt = 0; if (pg.current.article.namespace) { if (nsStr == pg.ns.list[0]) nsInt = -2; else if (nsStr == pg.ns.list[1]) nsInt = -1; else if (nsStr == pg.ns.list[2]) nsInt = 1; else if (nsStr == pg.ns.list[3]) nsInt = 2; else if (nsStr == pg.ns.list[4]) nsInt = 3; else if (nsStr == pg.ns.list[5]) nsInt = 4; else if (nsStr == pg.ns.list[6]) nsInt = 5; else if (nsStr == pg.ns.list[7]) nsInt = 6; else if (nsStr == pg.ns.list[8]) nsInt = 7; else if (nsStr == pg.ns.list[9]) nsInt = 8; else if (nsStr == pg.ns.list[10]) nsInt = 9; else if (nsStr == pg.ns.list[11]) nsInt = 10; else if (nsStr == pg.ns.list[12]) nsInt = 11; else if (nsStr == pg.ns.list[13]) nsInt = 12; else if (nsStr == pg.ns.list[14]) nsInt = 13; else if (nsStr == pg.ns.list[15]) nsInt = 14; else if (nsStr == pg.ns.list[16]) nsInt = 15; }	return nsInt; }

//extracts the articlename from a url function getArticlenameForCollection(url, ns) { var parts = url.split(":"); if (parts[0] != ns) return url; var newUrl = ""; for (var i = 1; i < parts.length; i++) { newUrl += parts[i] + ":"; }	return newUrl.substring(0, newUrl.length-1); }

//extract a List of articles from a collection-String function getCollectionArticleList(coll) { var articles = coll.split('{'); var out = []; var len; for (var i = 3; i < articles.length; i++) { len = out.push(articles[i].split('\",\"')[2]); out[len-1] = out[len-1].match(/title\":\"(.*)$/)[1].replace(/ /g, "_"); }	return out; }

//================================================================================= //start the program //================================================================================= $(function {	collectionCall2('GetCollection', []);//refresh the collection	//replace two Methods from the Navpopup-Object, to have an interface	Navpopup.prototype.unhide = collectionPopupUnhide;	Navpopup.prototype.setInnerHTML = collectionPopupSetInnerHTML; });