Commit | Line | Data |
---|---|---|
64a19c3d ML |
1 | // dependencies: |
2 | // * ajax.js: | |
3 | /*extern sajax_init_object, sajax_do_call */ | |
4 | // * wikibits.js: | |
5 | /*extern changeText, akeytt, hookEvent, jsMsg */ | |
6 | ||
7 | // These should have been initialized in the generated js | |
8 | /*extern wgAjaxWatch, wgPageName */ | |
9 | ||
10 | if(typeof wgAjaxWatch === "undefined" || !wgAjaxWatch) { | |
11 | var wgAjaxWatch = { | |
12 | watchMsg: "Watch", | |
13 | unwatchMsg: "Unwatch", | |
14 | watchingMsg: "Watching...", | |
15 | unwatchingMsg: "Unwatching..." | |
16 | }; | |
17 | } | |
18 | ||
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 | ||
25 | wgAjaxWatch.setLinkText = function(newText) { | |
26 | for (i = 0; i < wgAjaxWatch.watchLinks.length; i++) { | |
27 | changeText(wgAjaxWatch.watchLinks[i], newText); | |
28 | } | |
29 | }; | |
30 | ||
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 | |
35 | }; | |
36 | ||
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' ); | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | wgAjaxWatch.ajaxCall = function() { | |
50 | if(!wgAjaxWatch.supported) { | |
51 | return true; | |
52 | } else if (wgAjaxWatch.inprogress) { | |
53 | return false; | |
54 | } | |
55 | if(!wfSupportsAjax()) { | |
56 | // Lazy initialization so we don't toss up | |
57 | // ActiveX warnings on initial page load | |
58 | // for IE 6 users with security settings. | |
59 | wgAjaxWatch.supported = false; | |
60 | return true; | |
61 | } | |
62 | ||
63 | wgAjaxWatch.inprogress = true; | |
64 | wgAjaxWatch.setLinkText( wgAjaxWatch.watching | |
65 | ? wgAjaxWatch.unwatchingMsg : wgAjaxWatch.watchingMsg); | |
66 | sajax_do_call( | |
67 | "wfAjaxWatch", | |
68 | [wgPageName, (wgAjaxWatch.watching ? "u" : "w")], | |
69 | wgAjaxWatch.processResult | |
70 | ); | |
71 | // if the request isn't done in 10 seconds, allow user to try again | |
72 | wgAjaxWatch.timeoutID = window.setTimeout( | |
73 | function() { wgAjaxWatch.inprogress = false; }, | |
74 | 10000 | |
75 | ); | |
76 | return false; | |
77 | }; | |
78 | ||
79 | wgAjaxWatch.processResult = function(request) { | |
80 | if(!wgAjaxWatch.supported) { | |
81 | return; | |
82 | } | |
83 | var response = request.responseText; | |
84 | if( response.match(/^<w#>/) ) { | |
85 | wgAjaxWatch.watching = true; | |
86 | wgAjaxWatch.setLinkText(wgAjaxWatch.unwatchMsg); | |
87 | wgAjaxWatch.setLinkID("ca-unwatch"); | |
88 | wgAjaxWatch.setHref( 'unwatch' ); | |
89 | } else if( response.match(/^<u#>/) ) { | |
90 | wgAjaxWatch.watching = false; | |
91 | wgAjaxWatch.setLinkText(wgAjaxWatch.watchMsg); | |
92 | wgAjaxWatch.setLinkID("ca-watch"); | |
93 | wgAjaxWatch.setHref( 'watch' ); | |
94 | } else { | |
95 | // Either we got a <err#> error code or it just plain broke. | |
96 | window.location.href = wgAjaxWatch.watchLinks[0].href; | |
97 | return; | |
98 | } | |
99 | jsMsg( response.substr(4), 'watch' ); | |
100 | wgAjaxWatch.inprogress = false; | |
101 | if(wgAjaxWatch.timeoutID) { | |
102 | window.clearTimeout(wgAjaxWatch.timeoutID); | |
103 | } | |
104 | // Bug 12395 - avoid some watch link confusion on edit | |
105 | var watchthis = document.getElementById("wpWatchthis"); | |
106 | if( watchthis && response.match(/^<[uw]#>/) ) { | |
107 | watchthis.checked = response.match(/^<w#>/) ? "checked" : ""; | |
108 | } | |
109 | return; | |
110 | }; | |
111 | ||
112 | wgAjaxWatch.onLoad = function() { | |
113 | // This document structure hardcoding sucks. We should make a class and | |
114 | // toss all this out the window. | |
115 | var el1 = document.getElementById("ca-unwatch"); | |
116 | var el2 = null; | |
117 | if (!el1) { | |
118 | el1 = document.getElementById("mw-unwatch-link1"); | |
119 | el2 = document.getElementById("mw-unwatch-link2"); | |
120 | } | |
121 | if(el1) { | |
122 | wgAjaxWatch.watching = true; | |
123 | } else { | |
124 | wgAjaxWatch.watching = false; | |
125 | el1 = document.getElementById("ca-watch"); | |
126 | if (!el1) { | |
127 | el1 = document.getElementById("mw-watch-link1"); | |
128 | el2 = document.getElementById("mw-watch-link2"); | |
129 | } | |
130 | if(!el1) { | |
131 | wgAjaxWatch.supported = false; | |
132 | return; | |
133 | } | |
134 | } | |
135 | ||
136 | // The id can be either for the parent (Monobook-based) or the element | |
137 | // itself (non-Monobook) | |
138 | wgAjaxWatch.watchLinks.push( el1.tagName.toLowerCase() == "a" | |
139 | ? el1 : el1.firstChild ); | |
140 | ||
141 | if( el2 ) { | |
142 | wgAjaxWatch.watchLinks.push( el2 ); | |
143 | } | |
144 | ||
145 | // I couldn't get for (watchLink in wgAjaxWatch.watchLinks) to work, if | |
146 | // you can be my guest. | |
147 | for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) { | |
148 | wgAjaxWatch.watchLinks[i].onclick = wgAjaxWatch.ajaxCall; | |
149 | } | |
150 | return; | |
151 | }; | |
152 | ||
153 | hookEvent("load", wgAjaxWatch.onLoad); |