* API: Allow for query extensions
[mediawiki.git] / skins / common / ajaxwatch.js
blob16e4fdc455d9de20772ca81366bb4f166ccde856
1 // dependencies:
2 // * ajax.js:
3   /*extern sajax_init_object, sajax_do_call */
4 // * wikibits.js:
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) {
11         var wgAjaxWatch = {
12                 watchMsg: "Watch",
13                 unwatchMsg: "Unwatch",
14                 watchingMsg: "Watching...",
15                 unwatchingMsg: "Unwatching..."
16         };
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);
31         }
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) {
41                 return;
42         }
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);
48         return;
51 wgAjaxWatch.processResult = function(request) {
52         if(!wgAjaxWatch.supported) {
53                 return;
54         }
55         var response = request.responseText;
56         if(response == "<err#>") {
57                 window.location.href = wgAjaxWatch.oldHref;
58                 return;
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");
69         }
70         wgAjaxWatch.inprogress = false;
71         if(wgAjaxWatch.timeoutID) {
72                 window.clearTimeout(wgAjaxWatch.timeoutID);
73         }
74         return;
77 wgAjaxWatch.onLoad = function() {
78         var el1 = document.getElementById("ca-unwatch");
79         var el2 = null;
80         if (!el1) {
81                 el1 = document.getElementById("mw-unwatch-link1");
82                 el2 = document.getElementById("mw-unwatch-link2");
83         }
84         if(el1) {
85                 wgAjaxWatch.watching = true;
86         } else {
87                 wgAjaxWatch.watching = false;
88                 el1 = document.getElementById("ca-watch");
89                 if (!el1) {
90                         el1 = document.getElementById("mw-watch-link1");
91                         el2 = document.getElementById("mw-watch-link2");
92                 }
93                 if(!el1) {
94                         wgAjaxWatch.supported = false;
95                         return;
96                 }
97         }
99         if(!wfSupportsAjax()) {
100                 wgAjaxWatch.supported = false;
101                 return;
102         }
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()");
113         }
114         return;
117 hookEvent("load", wgAjaxWatch.onLoad);
120  * @return boolean whether the browser supports XMLHttpRequest
121  */
122 function wfSupportsAjax() {
123         var request = sajax_init_object();
124         var supportsAjax = request ? true : false;
125         delete request;
126         return supportsAjax;