Fixed as per Tim's comments on r60665:
[mediawiki.git] / skins / common / ajaxwatch.js
blob7f546014fe6cc55bbb76eb744c26204ebe773952
1 // dependencies:
2 // * ajax.js:
3   /*extern sajax_init_object, sajax_do_call */
4 // * wikibits.js:
5   /*extern changeText, hookEvent, jsMsg */
7 // These should have been initialized in the generated js
8 /*extern wgAjaxWatch, wgPageName */
10 if(typeof wgAjaxWatch === "undefined" || !wgAjaxWatch) {
11         var wgAjaxWatch = {
12                 watchMsg: "Watch",
13                 unwatchMsg: "Unwatch",
14                 watchingMsg: "Watching...",
15                 unwatchingMsg: "Unwatching...",
16                 'tooltip-ca-watchMsg': "Add this page to your watchlist",
17                 'tooltip-ca-unwatchMsg': "Remove this page from your watchlist"
18         };
21 wgAjaxWatch.supported = true; // supported on current page and by browser
22 wgAjaxWatch.watching = false; // currently watching page
23 wgAjaxWatch.inprogress = false; // ajax request in progress
24 wgAjaxWatch.timeoutID = null; // see wgAjaxWatch.ajaxCall
25 wgAjaxWatch.watchLinks = []; // "watch"/"unwatch" links
26 wgAjaxWatch.iconMode = false; // new icon driven functionality 
27 wgAjaxWatch.imgBasePath = ""; // base img path derived from icons on load
29 wgAjaxWatch.setLinkText = function( newText ) {
30         if( wgAjaxWatch.iconMode ) {
31                 for ( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
32                         wgAjaxWatch.watchLinks[i].firstChild.alt = newText;
33                         if ( newText == wgAjaxWatch.watchingMsg || newText == wgAjaxWatch.unwatchingMsg ) {
34                                 wgAjaxWatch.watchLinks[i].className += ' loading';
35                         } else if ( newText == wgAjaxWatch.watchMsg || newText == wgAjaxWatch.unwatchMsg ) {
36                                 wgAjaxWatch.watchLinks[i].className = 
37                                         wgAjaxWatch.watchLinks[i].className.replace( /loading/i, '' );
38                                 // update the title text on the link
39                                 var keyCommand = wgAjaxWatch.watchLinks[i].title.match( /\[.*?\]$/ ) ? 
40                                         wgAjaxWatch.watchLinks[i].title.match( /\[.*?\]$/ )[0] : "";
41                                 wgAjaxWatch.watchLinks[i].title = ( newText == wgAjaxWatch.watchMsg ? 
42                                         wgAjaxWatch['tooltip-ca-watchMsg'] : wgAjaxWatch['tooltip-ca-unwatchMsg'] )
43                                         + " " + keyCommand;
44                         }
45                 }
46         } else {
47                 for ( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
48                         changeText( wgAjaxWatch.watchLinks[i], newText );
49                 }
50         }
53 wgAjaxWatch.setLinkID = function( newId ) {
54         // We can only set the first one
55         wgAjaxWatch.watchLinks[0].parentNode.setAttribute( 'id', newId );
58 wgAjaxWatch.setHref = function( string ) {
59         for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
60                 if( string == 'watch' ) {
61                         wgAjaxWatch.watchLinks[i].href = wgAjaxWatch.watchLinks[i].href
62                                 .replace( /&action=unwatch/, '&action=watch' );
63                 } else if( string == 'unwatch' ) {
64                         wgAjaxWatch.watchLinks[i].href = wgAjaxWatch.watchLinks[i].href
65                                 .replace( /&action=watch/, '&action=unwatch' );
66                 }
67         }
70 wgAjaxWatch.ajaxCall = function() {
71         if(!wgAjaxWatch.supported) {
72                 return true;
73         } else if (wgAjaxWatch.inprogress) {
74                 return false;
75         }
76         if(!wfSupportsAjax()) {
77                 // Lazy initialization so we don't toss up
78                 // ActiveX warnings on initial page load
79                 // for IE 6 users with security settings.
80                 wgAjaxWatch.supported = false;
81                 return true;
82         }
84         wgAjaxWatch.inprogress = true;
85         wgAjaxWatch.setLinkText( wgAjaxWatch.watching
86                 ? wgAjaxWatch.unwatchingMsg : wgAjaxWatch.watchingMsg);
87         sajax_do_call(
88                 "wfAjaxWatch",
89                 [wgPageName, (wgAjaxWatch.watching ? "u" : "w")], 
90                 wgAjaxWatch.processResult
91         );
92         // if the request isn't done in 10 seconds, allow user to try again
93         wgAjaxWatch.timeoutID = window.setTimeout(
94                 function() { wgAjaxWatch.inprogress = false; },
95                 10000
96         );
97         return false;
100 wgAjaxWatch.processResult = function(request) {
101         if(!wgAjaxWatch.supported) {
102                 return;
103         }
104         var response = request.responseText;
105         if( response.match(/^<w#>/) ) {
106                 wgAjaxWatch.watching = true;
107                 wgAjaxWatch.setLinkText(wgAjaxWatch.unwatchMsg);
108                 wgAjaxWatch.setLinkID("ca-unwatch");
109                 wgAjaxWatch.setHref( 'unwatch' );
110         } else if( response.match(/^<u#>/) ) {
111                 wgAjaxWatch.watching = false;
112                 wgAjaxWatch.setLinkText(wgAjaxWatch.watchMsg);
113                 wgAjaxWatch.setLinkID("ca-watch");
114                 wgAjaxWatch.setHref( 'watch' );
115         } else {
116                 // Either we got a <err#> error code or it just plain broke.
117                 window.location.href = wgAjaxWatch.watchLinks[0].href;
118                 return;
119         }
120         jsMsg( response.substr(4), 'watch' );
121         wgAjaxWatch.inprogress = false;
122         if(wgAjaxWatch.timeoutID) {
123                 window.clearTimeout(wgAjaxWatch.timeoutID);
124         }
125         // Bug 12395 - avoid some watch link confusion on edit
126         var watchthis = document.getElementById("wpWatchthis");
127         if( watchthis && response.match(/^<[uw]#>/) ) {
128                 watchthis.checked = response.match(/^<w#>/) ? "checked" : "";
129         }
130         return;
133 wgAjaxWatch.onLoad = function() {
134         // This document structure hardcoding sucks.  We should make a class and
135         // toss all this out the window.
136         
137         var el1 = document.getElementById("ca-unwatch");
138         var el2 = null;
139         if ( !el1 ) {
140                 el1 = document.getElementById("mw-unwatch-link1");
141                 el2 = document.getElementById("mw-unwatch-link2");
142         }
143         if( el1 ) {
144                 wgAjaxWatch.watching = true;
145         } else {
146                 wgAjaxWatch.watching = false;
147                 el1 = document.getElementById("ca-watch");
148                 if ( !el1 ) {
149                         el1 = document.getElementById("mw-watch-link1");
150                         el2 = document.getElementById("mw-watch-link2");
151                 }
152                 if( !el1 ) {
153                         wgAjaxWatch.supported = false;
154                         return;
155                 }
156         }
157         
158         // Detect if the watch/unwatch feature is in icon mode
159         if ( el1.className.match( /icon/i ) ) {
160                 wgAjaxWatch.iconMode = true;
161         }
162         
163         // The id can be either for the parent (Monobook-based) or the element
164         // itself (non-Monobook)
165         wgAjaxWatch.watchLinks.push( el1.tagName.toLowerCase() == "a"
166                 ? el1 : el1.firstChild );
168         if( el2 ) {
169                 wgAjaxWatch.watchLinks.push( el2 );
170         }
172         // I couldn't get for (watchLink in wgAjaxWatch.watchLinks) to work, if
173         // you can be my guest.
174         for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
175                 wgAjaxWatch.watchLinks[i].onclick = wgAjaxWatch.ajaxCall;
176         }
177         return;
180 hookEvent("load", wgAjaxWatch.onLoad);