3 /*extern sajax_init_object, sajax_do_call */
5 /*extern changeText, akeytt, hookEvent */
7 // These should have been initialized in the generated js
8 /*extern wgAjaxWatch, wgArticleId */
10 if(typeof wgAjaxWatch
=== "undefined" || !wgAjaxWatch
) {
13 unwatchMsg
: "Unwatch",
14 watchingMsg
: "Watching...",
15 unwatchingMsg
: "Unwatching..."
19 wgAjaxWatch
.supported
= true; // supported on current page and by browser
20 wgAjaxWatch
.watching
= false; // currently watching page
21 wgAjaxWatch
.inprogress
= false; // ajax request in progress
22 wgAjaxWatch
.timeoutID
= null; // see wgAjaxWatch.ajaxCall
23 wgAjaxWatch
.watchLink1
= null; // "watch"/"unwatch" link
24 wgAjaxWatch
.watchLink2
= null; // second one, for (some?) non-Monobook-based
25 wgAjaxWatch
.oldHref
= null; // url for action=watch/action=unwatch
27 wgAjaxWatch
.setLinkText = function(newText
) {
28 changeText(wgAjaxWatch
.watchLink1
, newText
);
29 if (wgAjaxWatch
.watchLink2
) {
30 changeText(wgAjaxWatch
.watchLink2
, newText
);
34 wgAjaxWatch
.setLinkID = function(newId
) {
35 wgAjaxWatch
.watchLink1
.id
= newId
;
36 akeytt(newId
); // update tooltips for Monobook
39 wgAjaxWatch
.ajaxCall = function() {
40 if(!wgAjaxWatch
.supported
|| wgAjaxWatch
.inprogress
) {
43 wgAjaxWatch
.inprogress
= true;
44 wgAjaxWatch
.setLinkText(wgAjaxWatch
.watching
? wgAjaxWatch
.unwatchingMsg
: wgAjaxWatch
.watchingMsg
);
45 sajax_do_call("wfAjaxWatch", [wgArticleId
, (wgAjaxWatch
.watching
? "u" : "w")], wgAjaxWatch
.processResult
);
46 // if the request isn't done in 10 seconds, allow user to try again
47 wgAjaxWatch
.timeoutID
= window
.setTimeout(function() { wgAjaxWatch
.inprogress
= false; }, 10000);
51 wgAjaxWatch
.processResult = function(request
) {
52 if(!wgAjaxWatch
.supported
) {
55 var response
= request
.responseText
;
56 if(response
== "<err#>") {
57 window
.location
.href
= wgAjaxWatch
.oldHref
;
59 } else if(response
== "<w#>") {
60 wgAjaxWatch
.watching
= true;
61 wgAjaxWatch
.setLinkText(wgAjaxWatch
.unwatchMsg
);
62 wgAjaxWatch
.setLinkID("ca-unwatch");
63 wgAjaxWatch
.oldHref
= wgAjaxWatch
.oldHref
.replace(/action=watch/, "action=unwatch");
64 } else if(response
== "<u#>") {
65 wgAjaxWatch
.watching
= false;
66 wgAjaxWatch
.setLinkText(wgAjaxWatch
.watchMsg
);
67 wgAjaxWatch
.setLinkID("ca-watch");
68 wgAjaxWatch
.oldHref
= wgAjaxWatch
.oldHref
.replace(/action=unwatch/, "action=watch");
70 wgAjaxWatch
.inprogress
= false;
71 if(wgAjaxWatch
.timeoutID
) {
72 window
.clearTimeout(wgAjaxWatch
.timeoutID
);
77 wgAjaxWatch
.onLoad = function() {
78 var el1
= document
.getElementById("ca-unwatch");
81 el1
= document
.getElementById("mw-unwatch-link1");
82 el2
= document
.getElementById("mw-unwatch-link2");
85 wgAjaxWatch
.watching
= true;
87 wgAjaxWatch
.watching
= false;
88 el1
= document
.getElementById("ca-watch");
90 el1
= document
.getElementById("mw-watch-link1");
91 el2
= document
.getElementById("mw-watch-link2");
94 wgAjaxWatch
.supported
= false;
99 if(!wfSupportsAjax()) {
100 wgAjaxWatch
.supported
= false;
104 // The id can be either for the parent (Monobook-based) or the element
105 // itself (non-Monobook)
106 wgAjaxWatch
.watchLink1
= el1
.tagName
.toLowerCase() == "a" ? el1
: el1
.firstChild
;
107 wgAjaxWatch
.watchLink2
= el2
? el2
: null;
109 wgAjaxWatch
.oldHref
= wgAjaxWatch
.watchLink1
.getAttribute("href");
110 wgAjaxWatch
.watchLink1
.setAttribute("href", "javascript:wgAjaxWatch.ajaxCall()");
111 if (wgAjaxWatch
.watchLink2
) {
112 wgAjaxWatch
.watchLink2
.setAttribute("href", "javascript:wgAjaxWatch.ajaxCall()");
117 hookEvent("load", wgAjaxWatch
.onLoad
);
120 * @return boolean whether the browser supports XMLHttpRequest
122 function wfSupportsAjax() {
123 var request
= sajax_init_object();
124 var supportsAjax
= request
? true : false;