summaryrefslogtreecommitdiff
path: root/resources/mediawiki.api/mediawiki.api.watch.js
blob: d32344212e35bcafa9ddfa832acdd309139029da (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
/**
 * Additional mw.Api methods to assist with (un)watching wiki pages.
 * @since 1.19
 */
( function ( mw, $ ) {

	/**
	 * @context {mw.Api}
	 */
	function doWatchInternal( page, success, err, addParams ) {
		var params = {
			action: 'watch',
			title: String( page ),
			token: mw.user.tokens.get( 'watchToken' ),
			uselang: mw.config.get( 'wgUserLanguage' )
		};
		function ok( data ) {
			success( data.watch );
		}
		if ( addParams ) {
			$.extend( params, addParams );
		}
		return this.post( params, { ok: ok, err: err } );
	}

	$.extend( mw.Api.prototype, {
		/**
		 * Convinience method for 'action=watch'.
		 *
		 * @param page {String|mw.Title} Full page name or instance of mw.Title
		 * @param success {Function} Callback to which the watch object will be passed.
		 * Watch object contains properties 'title' (full pagename), 'watched' (boolean) and
		 * 'message' (parsed HTML of the 'addedwatchtext' message).
		 * @param err {Function} Error callback (optional)
		 * @return {jqXHR}
		 */
		watch: function ( page, success, err ) {
			return doWatchInternal.call( this, page, success, err );
		},
		/**
		 * Convinience method for 'action=watch&unwatch=1'.
		 *
		 * @param page {String|mw.Title} Full page name or instance of mw.Title
		 * @param success {Function} Callback to which the watch object will be passed.
		 * Watch object contains properties 'title' (full pagename), 'watched' (boolean) and
		 * 'message' (parsed HTML of the 'removedwatchtext' message).
		 * @param err {Function} Error callback (optional)
		 * @return {jqXHR}
		 */
		unwatch: function ( page, success, err ) {
			return doWatchInternal.call( this, page, success, err, { unwatch: 1 } );
		}

	} );

}( mediaWiki, jQuery ) );