3 /*extern sajax_init_object, sajax_do_call */
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) {
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"
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'] )
47 for ( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
48 changeText( wgAjaxWatch.watchLinks[i], newText );
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' );
70 wgAjaxWatch.ajaxCall = function() {
71 if(!wgAjaxWatch.supported) {
73 } else if (wgAjaxWatch.inprogress) {
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;
84 wgAjaxWatch.inprogress = true;
85 wgAjaxWatch.setLinkText( wgAjaxWatch.watching
86 ? wgAjaxWatch.unwatchingMsg : wgAjaxWatch.watchingMsg);
89 [wgPageName, (wgAjaxWatch.watching ? "u" : "w")],
90 wgAjaxWatch.processResult
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; },
100 wgAjaxWatch.processResult = function(request) {
101 if(!wgAjaxWatch.supported) {
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' );
116 // Either we got a <err#> error code or it just plain broke.
117 window.location.href = wgAjaxWatch.watchLinks[0].href;
120 jsMsg( response.substr(4), 'watch' );
121 wgAjaxWatch.inprogress = false;
122 if(wgAjaxWatch.timeoutID) {
123 window.clearTimeout(wgAjaxWatch.timeoutID);
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" : "";
133 wgAjaxWatch.onLoad = function() {
134 // This document structure hardcoding sucks. We should make a class and
135 // toss all this out the window.
137 var el1 = document.getElementById("ca-unwatch");
140 el1 = document.getElementById("mw-unwatch-link1");
141 el2 = document.getElementById("mw-unwatch-link2");
144 wgAjaxWatch.watching = true;
146 wgAjaxWatch.watching = false;
147 el1 = document.getElementById("ca-watch");
149 el1 = document.getElementById("mw-watch-link1");
150 el2 = document.getElementById("mw-watch-link2");
153 wgAjaxWatch.supported = false;
158 // Detect if the watch/unwatch feature is in icon mode
159 if ( el1.className.match( /icon/i ) ) {
160 wgAjaxWatch.iconMode = true;
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 );
169 wgAjaxWatch.watchLinks.push( el2 );
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;
180 hookEvent("load", wgAjaxWatch.onLoad);