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..."
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
24 wgAjaxWatch
.iconMode
= false; // new icon driven functionality
25 wgAjaxWatch
.imgBasePath
= ""; // base img path derived from icons on load
27 wgAjaxWatch
.setLinkText = function( newText
) {
28 if( wgAjaxWatch
.iconMode
) {
29 for ( i
= 0; i
< wgAjaxWatch
.watchLinks
.length
; i
++ ) {
30 wgAjaxWatch
.watchLinks
[i
].firstChild
.alt
= newText
;
31 if ( newText
== wgAjaxWatch
.watchingMsg
|| newText
== wgAjaxWatch
.unwatchingMsg
) {
32 wgAjaxWatch
.watchLinks
[i
].className
+= ' loading';
33 } else if ( newText
== wgAjaxWatch
.watchMsg
|| newText
== wgAjaxWatch
.unwatchMsg
) {
34 wgAjaxWatch
.watchLinks
[i
].className
= wgAjaxWatch
.watchLinks
[i
].className
.replace( /loading/i, '' );
38 for ( i
= 0; i
< wgAjaxWatch
.watchLinks
.length
; i
++ ) {
39 changeText( wgAjaxWatch
.watchLinks
[i
], newText
);
44 wgAjaxWatch
.setLinkID = function( newId
) {
45 // We can only set the first one
46 wgAjaxWatch
.watchLinks
[0].parentNode
.setAttribute( 'id', newId
);
49 wgAjaxWatch
.setHref = function( string
) {
50 for( i
= 0; i
< wgAjaxWatch
.watchLinks
.length
; i
++ ) {
51 if( string
== 'watch' ) {
52 wgAjaxWatch
.watchLinks
[i
].href
= wgAjaxWatch
.watchLinks
[i
].href
53 .replace( /&action=unwatch/, '&action=watch' );
54 } else if( string
== 'unwatch' ) {
55 wgAjaxWatch
.watchLinks
[i
].href
= wgAjaxWatch
.watchLinks
[i
].href
56 .replace( /&action=watch/, '&action=unwatch' );
61 wgAjaxWatch
.ajaxCall = function() {
62 if(!wgAjaxWatch
.supported
) {
64 } else if (wgAjaxWatch
.inprogress
) {
67 if(!wfSupportsAjax()) {
68 // Lazy initialization so we don't toss up
69 // ActiveX warnings on initial page load
70 // for IE 6 users with security settings.
71 wgAjaxWatch
.supported
= false;
75 wgAjaxWatch
.inprogress
= true;
76 wgAjaxWatch
.setLinkText( wgAjaxWatch
.watching
77 ? wgAjaxWatch
.unwatchingMsg
: wgAjaxWatch
.watchingMsg
);
80 [wgPageName
, (wgAjaxWatch
.watching
? "u" : "w")],
81 wgAjaxWatch
.processResult
83 // if the request isn't done in 10 seconds, allow user to try again
84 wgAjaxWatch
.timeoutID
= window
.setTimeout(
85 function() { wgAjaxWatch
.inprogress
= false; },
91 wgAjaxWatch
.processResult = function(request
) {
92 if(!wgAjaxWatch
.supported
) {
95 var response
= request
.responseText
;
96 if( response
.match(/^<w#>/) ) {
97 wgAjaxWatch
.watching
= true;
98 wgAjaxWatch
.setLinkText(wgAjaxWatch
.unwatchMsg
);
99 wgAjaxWatch
.setLinkID("ca-unwatch");
100 wgAjaxWatch
.setHref( 'unwatch' );
101 } else if( response
.match(/^<u#>/) ) {
102 wgAjaxWatch
.watching
= false;
103 wgAjaxWatch
.setLinkText(wgAjaxWatch
.watchMsg
);
104 wgAjaxWatch
.setLinkID("ca-watch");
105 wgAjaxWatch
.setHref( 'watch' );
107 // Either we got a <err#> error code or it just plain broke.
108 window
.location
.href
= wgAjaxWatch
.watchLinks
[0].href
;
111 jsMsg( response
.substr(4), 'watch' );
112 wgAjaxWatch
.inprogress
= false;
113 if(wgAjaxWatch
.timeoutID
) {
114 window
.clearTimeout(wgAjaxWatch
.timeoutID
);
116 // Bug 12395 - avoid some watch link confusion on edit
117 var watchthis
= document
.getElementById("wpWatchthis");
118 if( watchthis
&& response
.match(/^<[uw]#>/) ) {
119 watchthis
.checked
= response
.match(/^<w#>/) ? "checked" : "";
124 wgAjaxWatch
.onLoad = function() {
125 // This document structure hardcoding sucks. We should make a class and
126 // toss all this out the window.
128 var el1
= document
.getElementById("ca-unwatch");
131 el1
= document
.getElementById("mw-unwatch-link1");
132 el2
= document
.getElementById("mw-unwatch-link2");
135 wgAjaxWatch
.watching
= true;
137 wgAjaxWatch
.watching
= false;
138 el1
= document
.getElementById("ca-watch");
140 el1
= document
.getElementById("mw-watch-link1");
141 el2
= document
.getElementById("mw-watch-link2");
144 wgAjaxWatch
.supported
= false;
149 // Detect if the watch/unwatch feature is in icon mode
150 if ( el1
.className
.match( /icon/i ) ) {
151 wgAjaxWatch
.iconMode
= true;
154 // The id can be either for the parent (Monobook-based) or the element
155 // itself (non-Monobook)
156 wgAjaxWatch
.watchLinks
.push( el1
.tagName
.toLowerCase() == "a"
157 ? el1
: el1
.firstChild
);
160 wgAjaxWatch
.watchLinks
.push( el2
);
163 // I couldn't get for (watchLink in wgAjaxWatch.watchLinks) to work, if
164 // you can be my guest.
165 for( i
= 0; i
< wgAjaxWatch
.watchLinks
.length
; i
++ ) {
166 wgAjaxWatch
.watchLinks
[i
].onclick
= wgAjaxWatch
.ajaxCall
;
171 hookEvent("load", wgAjaxWatch
.onLoad
);