(bug 20847) Remove akeytt() function, but leave dummy. Contributed by Derk-Jan Hartman
[lhc/web/wiklou.git] / skins / common / ajaxwatch.js
1 // dependencies:
2 // * ajax.js:
3 /*extern sajax_init_object, sajax_do_call */
4 // * wikibits.js:
5 /*extern changeText, 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 wgAjaxWatch.iconMode = false; // new icon driven functionality
25 wgAjaxWatch.imgBasePath = ""; // base img path derived from icons on load
26
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, '' );
35 }
36 }
37 } else {
38 for ( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
39 changeText( wgAjaxWatch.watchLinks[i], newText );
40 }
41 }
42 };
43
44 wgAjaxWatch.setLinkID = function( newId ) {
45 // We can only set the first one
46 wgAjaxWatch.watchLinks[0].parentNode.setAttribute( 'id', newId );
47 };
48
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' );
57 }
58 }
59 }
60
61 wgAjaxWatch.ajaxCall = function() {
62 if(!wgAjaxWatch.supported) {
63 return true;
64 } else if (wgAjaxWatch.inprogress) {
65 return false;
66 }
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;
72 return true;
73 }
74
75 wgAjaxWatch.inprogress = true;
76 wgAjaxWatch.setLinkText( wgAjaxWatch.watching
77 ? wgAjaxWatch.unwatchingMsg : wgAjaxWatch.watchingMsg);
78 sajax_do_call(
79 "wfAjaxWatch",
80 [wgPageName, (wgAjaxWatch.watching ? "u" : "w")],
81 wgAjaxWatch.processResult
82 );
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; },
86 10000
87 );
88 return false;
89 };
90
91 wgAjaxWatch.processResult = function(request) {
92 if(!wgAjaxWatch.supported) {
93 return;
94 }
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' );
106 } else {
107 // Either we got a <err#> error code or it just plain broke.
108 window.location.href = wgAjaxWatch.watchLinks[0].href;
109 return;
110 }
111 jsMsg( response.substr(4), 'watch' );
112 wgAjaxWatch.inprogress = false;
113 if(wgAjaxWatch.timeoutID) {
114 window.clearTimeout(wgAjaxWatch.timeoutID);
115 }
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" : "";
120 }
121 return;
122 };
123
124 wgAjaxWatch.onLoad = function() {
125 // This document structure hardcoding sucks. We should make a class and
126 // toss all this out the window.
127
128 var el1 = document.getElementById("ca-unwatch");
129 var el2 = null;
130 if ( !el1 ) {
131 el1 = document.getElementById("mw-unwatch-link1");
132 el2 = document.getElementById("mw-unwatch-link2");
133 }
134 if( el1 ) {
135 wgAjaxWatch.watching = true;
136 } else {
137 wgAjaxWatch.watching = false;
138 el1 = document.getElementById("ca-watch");
139 if ( !el1 ) {
140 el1 = document.getElementById("mw-watch-link1");
141 el2 = document.getElementById("mw-watch-link2");
142 }
143 if( !el1 ) {
144 wgAjaxWatch.supported = false;
145 return;
146 }
147 }
148
149 // Detect if the watch/unwatch feature is in icon mode
150 if ( el1.className.match( /icon/i ) ) {
151 wgAjaxWatch.iconMode = true;
152 }
153
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 );
158
159 if( el2 ) {
160 wgAjaxWatch.watchLinks.push( el2 );
161 }
162
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;
167 }
168 return;
169 };
170
171 hookEvent("load", wgAjaxWatch.onLoad);