3 /*extern sajax_init_object, sajax_do_call */
5 /*extern changeText, akeytt, hookEvent, jsMsg */
7 // These should have been initialized in the generated js
8 /*extern wgAjaxWatch, wgPageName */
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
.watchLinks
= []; // "watch"/"unwatch" links
25 wgAjaxWatch
.setLinkText = function(newText
) {
26 for (i
= 0; i
< wgAjaxWatch
.watchLinks
.length
; i
++) {
27 changeText(wgAjaxWatch
.watchLinks
[i
], newText
);
31 wgAjaxWatch
.setLinkID = function(newId
) {
32 // We can only set the first one
33 wgAjaxWatch
.watchLinks
[0].setAttribute( 'id', newId
);
34 akeytt(newId
); // update tooltips for Monobook
37 wgAjaxWatch
.setHref = function( string
) {
38 for( i
= 0; i
< wgAjaxWatch
.watchLinks
.length
; i
++ ) {
39 if( string
== 'watch' ) {
40 wgAjaxWatch
.watchLinks
[i
].href
= wgAjaxWatch
.watchLinks
[i
].href
41 .replace( /&action=unwatch/, '&action=watch' );
42 } else if( string
== 'unwatch' ) {
43 wgAjaxWatch
.watchLinks
[i
].href
= wgAjaxWatch
.watchLinks
[i
].href
44 .replace( /&action=watch/, '&action=unwatch' );
49 wgAjaxWatch
.ajaxCall = function() {
50 if(!wgAjaxWatch
.supported
) {
52 } else if (wgAjaxWatch
.inprogress
) {
55 wgAjaxWatch
.inprogress
= true;
56 wgAjaxWatch
.setLinkText( wgAjaxWatch
.watching
57 ? wgAjaxWatch
.unwatchingMsg
: wgAjaxWatch
.watchingMsg
);
60 [wgPageName
, (wgAjaxWatch
.watching
? "u" : "w")],
61 wgAjaxWatch
.processResult
63 // if the request isn't done in 10 seconds, allow user to try again
64 wgAjaxWatch
.timeoutID
= window
.setTimeout(
65 function() { wgAjaxWatch
.inprogress
= false; },
71 wgAjaxWatch
.processResult = function(request
) {
72 if(!wgAjaxWatch
.supported
) {
75 var response
= request
.responseText
;
76 if( response
.match(/^<err#>/) ) {
77 window
.location
.href
= wgAjaxWatch
.watchLink1
.href
;
79 } else if( response
.match(/^<w#>/) ) {
80 wgAjaxWatch
.watching
= true;
81 wgAjaxWatch
.setLinkText(wgAjaxWatch
.unwatchMsg
);
82 wgAjaxWatch
.setLinkID("ca-unwatch");
83 wgAjaxWatch
.setHref( 'unwatch' );
84 } else if( response
.match(/^<u#>/) ) {
85 wgAjaxWatch
.watching
= false;
86 wgAjaxWatch
.setLinkText(wgAjaxWatch
.watchMsg
);
87 wgAjaxWatch
.setLinkID("ca-watch");
88 wgAjaxWatch
.setHref( 'watch' );
90 jsMsg( response
.substr(4), 'watch' );
91 wgAjaxWatch
.inprogress
= false;
92 if(wgAjaxWatch
.timeoutID
) {
93 window
.clearTimeout(wgAjaxWatch
.timeoutID
);
98 wgAjaxWatch
.onLoad = function() {
99 // This document structure hardcoding sucks. We should make a class and
100 // toss all this out the window.
101 var el1
= document
.getElementById("ca-unwatch");
104 el1
= document
.getElementById("mw-unwatch-link1");
105 el2
= document
.getElementById("mw-unwatch-link2");
108 wgAjaxWatch
.watching
= true;
110 wgAjaxWatch
.watching
= false;
111 el1
= document
.getElementById("ca-watch");
113 el1
= document
.getElementById("mw-watch-link1");
114 el2
= document
.getElementById("mw-watch-link2");
117 wgAjaxWatch
.supported
= false;
122 if(!wfSupportsAjax()) {
123 wgAjaxWatch
.supported
= false;
127 // The id can be either for the parent (Monobook-based) or the element
128 // itself (non-Monobook)
129 wgAjaxWatch
.watchLinks
.push( el1
.tagName
.toLowerCase() == "a"
130 ? el1
: el1
.firstChild
);
133 wgAjaxWatch
.watchLinks
.push( el2
);
136 // I couldn't get for (watchLink in wgAjaxWatch.watchLinks) to work, if
137 // you can be my guest.
138 for( i
= 0; i
< wgAjaxWatch
.watchLinks
.length
; i
++ ) {
139 wgAjaxWatch
.watchLinks
[i
].onclick
= wgAjaxWatch
.ajaxCall
;
144 hookEvent("load", wgAjaxWatch
.onLoad
);
147 * @return boolean whether the browser supports XMLHttpRequest
149 function wfSupportsAjax() {
150 var request
= sajax_init_object();
151 var supportsAjax
= request
? true : false;