| Line 131: |
Line 131: |
| | // </nowiki></pre> | | // </nowiki></pre> |
| | | | |
| − | */
| |
| | | | |
| − | addOnloadHook(function () {
| + | // [[User:Quarl/watchlist.js]] - adds buttons to watchlist: "unwatch", "diff |
| | + | // since" |
| | | | |
| − | // Check if we're on the watchlist
| + | // UNWATCH button asynchronously unwatches and crosses the entry off the |
| − | if (!wgCanonicalSpecialPageName || wgCanonicalSpecialPageName != "Watchlist") return; | + | // watchlist. |
| − | if (!document.forms[0] || !document.forms[0].namespace) return; | + | // |
| | + | // DIFF SINCE button shows differences since last edited. |
| | + | |
| | + | // quarl 2006-01-09 added asynchronous feature. |
| | + | // quarl 2006-02-03 factored; added diff since. |
| | + | |
| | + | // requires: wikipage.js, util.js, wikiwatch.js, diffsince.js |
| | + | |
| | + | // originally based on http://en.wikipedia.org/wiki/User:Omegatron/monobook.js |
| | + | // see also http://en.wikipedia.org/wiki/User:Matthewmayer/monobook.js |
| | + | |
| | + | // see also Bug 424 http://bugzilla.wikipedia.org/show_bug.cgi?id=424 |
| | + | |
| | + | // <pre><nowiki> |
| | + | |
| | + | var watchlist = new Object(); |
| | + | watchlist.wp = {}; |
| | + | |
| | + | watchlist.unwatchAsync = function(pagename) { |
| | + | var wp = watchlist.wp[pagename]; |
| | + | if (!wp) { alert("## internal error 72192d74-ab57-4a98-917f-8c6ca03b0559"); return; } |
| | + | wikiwatch.unwatchAsync(wp, watchlist._unwatchSuccess, wp.unwatchSpan); |
| | + | return false; |
| | + | } |
| | + | |
| | + | watchlist.watchAsync = function(pagename) { |
| | + | var wp = watchlist.wp[pagename]; |
| | + | if (!wp) { alert("## internal error 72192d74-ab57-4a98-917f-8c6ca03b0559"); return; } |
| | + | wikiwatch.watchAsync(wp, watchlist._watchSuccess, wp.unwatchSpan); |
| | + | return false; |
| | + | } |
| | + | |
| | + | watchlist.diffSince = function(pagename) { |
| | + | var wp = watchlist.wp[pagename]; |
| | + | if (!wp) { alert("## internal error 72192d74-ab57-4a98-917f-8c6ca03b0559"); return; } |
| | + | return diffsince.diffPageAsync(wp, wp.diffsinceSpan); |
| | + | } |
| | + | |
| | + | watchlist._addStrikeThrough = function(node) { |
| | + | // return node && insertNode(node, document.createElement('s')); |
| | + | if (!node) return 0; |
| | + | |
| | + | addClass(ensureSpan(node), 'history-deleted'); |
| | + | return 1; |
| | + | } |
| | | | |
| − | // Unwatch links go back to watchlist with "Removing requested items from watchlist..." message
| + | watchlist._removeStrikeThrough = function(node) { |
| − | var query_prefix = "title="+encodeURIComponent(wgPageName)+"&action=submit&remove=1&id[]="; | + | if (!node) return 0; |
| | | | |
| − | // ...or... | + | removeClass(ensureSpan(node), 'history-deleted'); |
| − | // Unwatch links go to "Removed from watchlist" page | + | return 1; |
| − | //var query_prefix = "action=unwatch&title=";
| + | } |
| | | | |
| − | // get list of all links in content:
| + | watchlist._unwatchSuccess = function(wp) { |
| − | var links = document.getElementById('content').getElementsByTagName('a'); | + | var wpNT = wp.notalkPage(); |
| | + | var wpT = wp.talkPage(); |
| | | | |
| − | // make a static copy of the nodelist and lose the original for speed | + | if (0 == (watchlist._addStrikeThrough(findHref(wpNT.url)) + |
| − | // while we're at it, prune the uninteresting links from the list
| + | watchlist._addStrikeThrough(findHref(wpT.url)))) |
| − | var linksCopy = new Array ();
| + | { |
| − | for (var i = 0; i < links.length; i++) {
| + | alert("Unwatched article '"+wp.page+"', but couldn't annotate current page."); |
| − | if (/[?&]action=history([&#]|$)/.test(links[i].href)) linksCopy.push(links[i]); | + | return; |
| | } | | } |
| − | links = linksCopy;
| |
| | | | |
| − | for (var i = 0; i < links.length; i++) { | + | watchlist._updateWuwLink(wpT, 'watch'); |
| − | // create unwatch link and append it after history link
| + | watchlist._updateWuwLink(wpNT, 'watch'); |
| − | var unwatch = document.createElement('a');
| + | } |
| − | unwatch.href = wgServer + wgScriptPath + "/index.php?" + query_prefix + encodeURIComponent(links[i].title);
| |
| − | unwatch.title = "Unwatch "+links[i].title;
| |
| − | unwatch.appendChild(document.createTextNode("unwatch"));
| |
| − | links[i].parentNode.insertBefore(unwatch, links[i].nextSibling);
| |
| | | | |
| − | // insert a delimiter between the two links
| + | watchlist._watchSuccess = function(wp) { |
| − | var delim = links[i].previousSibling;
| + | var wpNT = wp.notalkPage(); |
| − | delimText = (delim.nodeType == 3 ? delim.nodeValue : ""); // kluge to handle case where "diff" is unlinked
| + | var wpT = wp.talkPage(); |
| − | delim = document.createTextNode(delimText.replace(/^.*diff/, ""));
| + | |
| − | links[i].parentNode.insertBefore(delim, unwatch); | + | if (0 == (watchlist._removeStrikeThrough(findHref(wpNT.url)) + |
| | + | watchlist._removeStrikeThrough(findHref(wpT.url)))) |
| | + | { |
| | + | alert("Watched article '"+wp.page+"', but couldn't annotate current page."); |
| | + | return; |
| | } | | } |
| − | });
| |
| | | | |
| | + | watchlist._updateWuwLink(wpT, 'unwatch'); |
| | + | watchlist._updateWuwLink(wpNT, 'unwatch'); |
| | + | } |
| | + | |
| | + | // Update a watch/unwatch link. Returns 1 on success, 0 if not found. |
| | // | | // |
| | + | // wuw must be 'watch' or 'unwatch' |
| | + | watchlist._updateWuwLink = function(wpX, wuw) { |
| | + | wp = watchlist.wp[wpX.page]; |
| | + | if (!wp) return 0; |
| | + | var link = "javascript:return watchlist."+wuw+"Async("+string_quote_escape(wp.page)+")"; |
| | + | var url = wp.qurl+'&action='+wuw; |
| | + | wp.unwatchSpan.innerHTML = '<a onclick="'+link+'" href="' + url + '">'+wuw+'</a>'; |
| | + | return 1; |
| | + | } |
| | + | |
| | + | |
| | + | watchlist._load = function() |
| | + | { |
| | + | if (wikiPage.page == "Special:Watchlist") watchlist._annotatePage(); |
| | + | } |
| | + | |
| | + | watchlist._annotatePage = function() { |
| | + | var links = copyArray(document.getElementById('bodyContent').getElementsByTagName('a')); |
| | + | for (i in links) { |
| | + | var link = links[i]; |
| | + | if (link.href && link.href.match(/action=history$/)) { |
| | + | var wp = new WikiPage(link.href); |
| | + | watchlist.wp[wp.page] = wp; |
| | + | |
| | + | wp.unwatchSpan = document.createElement('span'); |
| | + | var unwatchLink = "javascript:return watchlist.unwatchAsync("+string_quote_escape(wp.page)+")"; |
| | + | var unwatchUrl = wp.qurl+'&action=unwatch'; |
| | + | wp.unwatchSpan.innerHTML = '<a onclick="'+unwatchLink+'" href="' + unwatchUrl + '">unwatch</a>'; |
| | + | add_after(link, wp.unwatchSpan); |
| | + | add_after(link, document.createTextNode('; ')); |
| | + | |
| | + | wp.diffsinceSpan = document.createElement('span'); |
| | + | diffsinceLink = "javascript:return watchlist.diffSince("+string_quote_escape(wp.page)+")"; |
| | + | var diffsinceUrl = diffsince.makeUrl(wp); |
| | + | wp.diffsinceSpan.innerHTML = '<a onclick="'+diffsinceLink+'" href="' + diffsinceUrl + '">since</a>'; |
| | + | add_before(link, wp.diffsinceSpan); |
| | + | add_before(link, document.createTextNode('; ')); |
| | + | } |
| | + | } |
| | + | } |
| | + | |
| | + | addOnloadHook(watchlist._load); |
| | + | |
| | + | // </nowiki></pre> |