User:Bawolff/sandbox/opinion.js
From Wikinews, the free news source you can write!
< User:Bawolff | sandbox
Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror:: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins
/* Note this is not really for wikinews, as it has already has a working comment js system. This is for me to practise Javascript, because I want to learn how to do javascript stuff better (learn by doing...) */ /*extern wgPageName, wgArticlePath, wgNamespaceNumber, wgPageName, wgServer, addPortletLink wgScript wgScriptPath sajax_init_object*/ /*extern on_article_or_talk, on_comment_page, change_tab_link, change_tab_title, check_link, doesPageExist*/ var enableComments = (enableComments === undefined ? true : enableComments); if (enableComments && (wgPageName !== "Main_Page")) { (function () { //Variables for diff lang and options, etc var commentTabName = "Opinions2"; var page = wgPageName; //this is escaped /* commentPrefix and suffix should be escaped (aka [[foo bar]] -> [[foo_bar]] */ var commentPrefix = "Comments:"; var commentSuffix = ""; //If none, make empty string. Do not use null var ns = {}; ns[0] = ""; ns[1] = "Talk"; ns.main = ns[0]; ns.talk = ns[1]; ns.articleNumber = 0; ns.talkNumber = 1; ns.commentNumber = 102; ns.currentNumber = wgNamespaceNumber; var commentTabId = "ca-nstab-comments"; //probably shouldn't modify unless you really want to. if using custom ns for comments, MUST BE id used for tab var commentAccessKey = "o"; //Make sure it doesn't conflict with another accesskey var commentToolTip = "Express your personal opinion on the events described in this article"; var tabAfterComment = "ca-edit"; //id of next tab (or make null to be at end) var commentPortlet = "p-cactions"; //which part of nav bar (Rccomend p-cactions) var articleTabName = "Article"; var mainAccessKey = "c"; //Note: if you're using the talk:article/comments form, change this to something that does not conflict like "a" otherwise, should = MediaWiki:Accesskey-ca-nstab-main var mainToolTip = "View the article that this comment page is about"; //can be similiar to MediaWiki:Tooltip-ca-nstab-main var commentPreload = "Wikinews:Commentary_pages_on_news_events/body"; //default state of edit box for new page var commentIntro = "Wikinews:Commentary_pages_on_news_events/intro"; //Instructions //Variables that don't need to be modified when changing langs tabAfterComment = document.getElementById(tabAfterComment); var commentPrefixRegex = new RegExp("^"+commentPrefix); //hopefully no special chars in the commentPrefix var commentSuffixRegex = new RegExp(commentSuffix + "$"); // a / seems to be okay. var articleTabId = "ca-nstab-main"; var pCactions = "p-cactions"; //cactions portlet var wikiURL = function (page) { //If second argument, object for url arguments. if (arguments.length === 1) { return wgServer + wgArticlePath.replace("$1", page); } else { var URL = wgServer + wgScript + "?title=" + page; var argList = arguments[1]; for (var i in argList) { URL += "&" + i + "=" + argList[i]; } return URL; } } //Determine page we are on //Article, Talk: or Comment: if (ns.currentNumber === ns.commentNumber) { if ((page.indexOf(commentPrefix) === 0) && (page.lastIndexOf(commentSuffix) === page.length - commentSuffix.length)) on_comment_page(); } else if (ns.currentNumber === ns.talkNumber /*TALK*/) on_article_or_talk(); else if (ns.currentNumber === ns.articleNumber) on_article_or_talk(); function on_article_or_talk() { /*We're at an article and need to change its tabs*/ page = (ns.currentNumber === 1 ? page.replace(new RegExp("^"+ns.talk+"\:"), "") : page) //strip talk ns prefix if in talk ns. var newTab = addPortletLink(commentPortlet, wikiURL(commentPrefix + page + commentSuffix), commentTabName, commentTabId, commentToolTip, commentAccessKey, tabAfterComment); var newCommentPageURL = {preload: commentPreload, editintro: commentIntro, action: "edit"}; check_link(commentPrefix + page + commentSuffix, newTab, wikiURL(commentPrefix + page + commentSuffix, newCommentPageURL)); } function on_comment_page () { var articleTabId2 = articleTabId; //needed later var articleTitle = page.replace(commentPrefixRegex, "").replace(commentSuffixRegex, "");//get rid of commentness of page title var talkURL = wikiURL("Talk:" + articleTitle); change_tab_link("ca-talk", talkURL ); if (document.getElementById(articleTabId)) { //the comment page is somewhere in the main NS, Article -> Opinions change_tab_title(articleTabId, commentTabName); articleTabId2 = articleTabId + "-base"; //for later in function } //otherwise we assume that this has been taken care of server side and we do nothing //add link back to article var newArticleTab = addPortletLink(pCactions, wikiURL(articleTitle), articleTabName, articleTabId2, mainToolTip, mainAccessKey, document.getElementById(articleTabId2 === articleTabId ? commentTabId : articleTabId)); //Last bit of that function. If we're on comment ns, make article linkback before ca-nstab-comment, otherwise (in talk:article/comment) before ca-nstab-main. //Check if article still exists (one would hope it did) check_link(articleTitle, newArticleTab, wikiURL(articleTitle, {action: 'edit'})); } function change_tab_title (id, newTitle) { //This function should probably do some checking to make sure that the html is where it should be var tab = (typeof id === "string" ? id = document.getElementById(id) : id ); var label = id.firstChild.firstChild.data; label = newTitle; } function change_tab_link (id, newLink) { var tab = (typeof id === "string" ? id = document.getElementById(id) : id );//get node tab.firstChild.setAttribute("href", newLink); } function check_link (page /* to check. (wikipage, not URL)*/, element/*to mark new or does not exist*/, URL /* url to change link to if not exist*/) { /* Takes node of newly created tab (as node or as string of id), does bg check using ajax to see if it exists and applies appropriate css classes*/ var redden = function (exists) { if (!exists) { //Change link to an edit link, and put it to class new (make red) element = typeof element === "string" ? document.getElementById(element) : element; element.className = "new"; element.firstChild.href = URL; } } doesPageExist(page, redden); } })(); } //The following is bassed on Matt's checkCommentaryPageExist(pageName) function function doesPageExist(pageName, callback) { //Pagename can be a name as a string, or multiple pages seperated by a pipe. To check if it exists //Callback is a function. takes an object like {NameOfPageThatExists: true, NameOfNotAPage: false, ...} var mwAPI = "/api.php"; var x; x = sajax_init_object(); // do nothing if we can't check if (!x) { return false; } x.open("GET", wgServer + wgScriptPath + mwAPI + "?action=query&format=xml&titles=" + pageName, true); x.setRequestHeader("Pragma", "cache=yes"); x.setRequestHeader("Cache-Control", "no-transform"); x.onreadystatechange = function() { if (x.readyState === 4) { if (x.status === 200) { var xmldoc = x.responseXML; var xpage = xmldoc.getElementsByTagName('page'); if (xpage[0].hasAttribute("missing")) callback(false); //does not exist else callback(true); } } }; x.send(null); }
