summaryrefslogtreecommitdiff
path: root/skins/common/ajaxwatch.js
blob: 7f546014fe6cc55bbb76eb744c26204ebe773952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// dependencies:
// * ajax.js:
  /*extern sajax_init_object, sajax_do_call */
// * wikibits.js:
  /*extern changeText, hookEvent, jsMsg */

// These should have been initialized in the generated js
/*extern wgAjaxWatch, wgPageName */

if(typeof wgAjaxWatch === "undefined" || !wgAjaxWatch) {
	var wgAjaxWatch = {
		watchMsg: "Watch",
		unwatchMsg: "Unwatch",
		watchingMsg: "Watching...",
		unwatchingMsg: "Unwatching...",
		'tooltip-ca-watchMsg': "Add this page to your watchlist",
		'tooltip-ca-unwatchMsg': "Remove this page from your watchlist"
	};
}

wgAjaxWatch.supported = true; // supported on current page and by browser
wgAjaxWatch.watching = false; // currently watching page
wgAjaxWatch.inprogress = false; // ajax request in progress
wgAjaxWatch.timeoutID = null; // see wgAjaxWatch.ajaxCall
wgAjaxWatch.watchLinks = []; // "watch"/"unwatch" links
wgAjaxWatch.iconMode = false; // new icon driven functionality 
wgAjaxWatch.imgBasePath = ""; // base img path derived from icons on load

wgAjaxWatch.setLinkText = function( newText ) {
	if( wgAjaxWatch.iconMode ) {
		for ( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
			wgAjaxWatch.watchLinks[i].firstChild.alt = newText;
			if ( newText == wgAjaxWatch.watchingMsg || newText == wgAjaxWatch.unwatchingMsg ) {
				wgAjaxWatch.watchLinks[i].className += ' loading';
			} else if ( newText == wgAjaxWatch.watchMsg || newText == wgAjaxWatch.unwatchMsg ) {
				wgAjaxWatch.watchLinks[i].className = 
					wgAjaxWatch.watchLinks[i].className.replace( /loading/i, '' );
				// update the title text on the link
				var keyCommand = wgAjaxWatch.watchLinks[i].title.match( /\[.*?\]$/ ) ? 
					wgAjaxWatch.watchLinks[i].title.match( /\[.*?\]$/ )[0] : "";
				wgAjaxWatch.watchLinks[i].title = ( newText == wgAjaxWatch.watchMsg ? 
					wgAjaxWatch['tooltip-ca-watchMsg'] : wgAjaxWatch['tooltip-ca-unwatchMsg'] )
					+ " " + keyCommand;
			}
		}
	} else {
		for ( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
			changeText( wgAjaxWatch.watchLinks[i], newText );
		}
	}
};

wgAjaxWatch.setLinkID = function( newId ) {
	// We can only set the first one
	wgAjaxWatch.watchLinks[0].parentNode.setAttribute( 'id', newId );
};

wgAjaxWatch.setHref = function( string ) {
	for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
		if( string == 'watch' ) {
			wgAjaxWatch.watchLinks[i].href = wgAjaxWatch.watchLinks[i].href
				.replace( /&action=unwatch/, '&action=watch' );
		} else if( string == 'unwatch' ) {
			wgAjaxWatch.watchLinks[i].href = wgAjaxWatch.watchLinks[i].href
				.replace( /&action=watch/, '&action=unwatch' );
		}
	}
}

wgAjaxWatch.ajaxCall = function() {
	if(!wgAjaxWatch.supported) {
		return true;
	} else if (wgAjaxWatch.inprogress) {
		return false;
	}
	if(!wfSupportsAjax()) {
		// Lazy initialization so we don't toss up
		// ActiveX warnings on initial page load
		// for IE 6 users with security settings.
		wgAjaxWatch.supported = false;
		return true;
	}

	wgAjaxWatch.inprogress = true;
	wgAjaxWatch.setLinkText( wgAjaxWatch.watching
		? wgAjaxWatch.unwatchingMsg : wgAjaxWatch.watchingMsg);
	sajax_do_call(
		"wfAjaxWatch",
		[wgPageName, (wgAjaxWatch.watching ? "u" : "w")], 
		wgAjaxWatch.processResult
	);
	// if the request isn't done in 10 seconds, allow user to try again
	wgAjaxWatch.timeoutID = window.setTimeout(
		function() { wgAjaxWatch.inprogress = false; },
		10000
	);
	return false;
};

wgAjaxWatch.processResult = function(request) {
	if(!wgAjaxWatch.supported) {
		return;
	}
	var response = request.responseText;
	if( response.match(/^<w#>/) ) {
		wgAjaxWatch.watching = true;
		wgAjaxWatch.setLinkText(wgAjaxWatch.unwatchMsg);
		wgAjaxWatch.setLinkID("ca-unwatch");
		wgAjaxWatch.setHref( 'unwatch' );
	} else if( response.match(/^<u#>/) ) {
		wgAjaxWatch.watching = false;
		wgAjaxWatch.setLinkText(wgAjaxWatch.watchMsg);
		wgAjaxWatch.setLinkID("ca-watch");
		wgAjaxWatch.setHref( 'watch' );
	} else {
		// Either we got a <err#> error code or it just plain broke.
		window.location.href = wgAjaxWatch.watchLinks[0].href;
		return;
	}
	jsMsg( response.substr(4), 'watch' );
	wgAjaxWatch.inprogress = false;
	if(wgAjaxWatch.timeoutID) {
		window.clearTimeout(wgAjaxWatch.timeoutID);
	}
	// Bug 12395 - avoid some watch link confusion on edit
	var watchthis = document.getElementById("wpWatchthis");
	if( watchthis && response.match(/^<[uw]#>/) ) {
		watchthis.checked = response.match(/^<w#>/) ? "checked" : "";
	}
	return;
};

wgAjaxWatch.onLoad = function() {
	// This document structure hardcoding sucks.  We should make a class and
	// toss all this out the window.
	
	var el1 = document.getElementById("ca-unwatch");
	var el2 = null;
	if ( !el1 ) {
		el1 = document.getElementById("mw-unwatch-link1");
		el2 = document.getElementById("mw-unwatch-link2");
	}
	if( el1 ) {
		wgAjaxWatch.watching = true;
	} else {
		wgAjaxWatch.watching = false;
		el1 = document.getElementById("ca-watch");
		if ( !el1 ) {
			el1 = document.getElementById("mw-watch-link1");
			el2 = document.getElementById("mw-watch-link2");
		}
		if( !el1 ) {
			wgAjaxWatch.supported = false;
			return;
		}
	}
	
	// Detect if the watch/unwatch feature is in icon mode
	if ( el1.className.match( /icon/i ) ) {
		wgAjaxWatch.iconMode = true;
	}
	
	// The id can be either for the parent (Monobook-based) or the element
	// itself (non-Monobook)
	wgAjaxWatch.watchLinks.push( el1.tagName.toLowerCase() == "a"
		? el1 : el1.firstChild );

	if( el2 ) {
		wgAjaxWatch.watchLinks.push( el2 );
	}

	// I couldn't get for (watchLink in wgAjaxWatch.watchLinks) to work, if
	// you can be my guest.
	for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
		wgAjaxWatch.watchLinks[i].onclick = wgAjaxWatch.ajaxCall;
	}
	return;
};

hookEvent("load", wgAjaxWatch.onLoad);