[mediawiki.api] write mediawiki.api.watch module
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.api.watch.js
1 /**
2 * Additional mw.Api methods to assist with (un)watching wiki pages.
3 */
4 ( function( $, mw ) {
5
6 $.extend( mw.Api.prototype, {
7 /**
8 * Convinience method for 'action=watch'.
9 *
10 * @param page {String|mw.Title} Full page name or instance of mw.Title
11 * @param success {Function} callback to which the watch object will be passed
12 * watch object contains 'title' (full page name), 'watched' (boolean) and
13 * 'message' (parsed HTML of the 'addedwatchtext' message).
14 * @param err {Function} callback if error (optional)
15 * @param unwatch {Boolean} Internal variable, do not use. Used by unwatch() to
16 * reuse this function.
17 * @return {jqXHR}
18 */
19 watch: function( page, success, err, unwatch ) {
20 var params, ok;
21 params = {
22 action: 'watch',
23 title: String( page ),
24 token: mw.user.tokens.get( 'watchToken' ),
25 uselang: mw.config.get( 'wgUserLanguage' )
26 };
27 ok = function( data ) {
28 success( data.watch );
29 };
30 return this.post( params, { ok: ok, err: err } );
31 },
32 /**
33 * Convinience method for 'action=watch&unwatch='.
34 *
35 * @param page {String|mw.Title} Full page name or instance of mw.Title
36 * @param success {Function} callback to which the watch object will be passed
37 * watch object contains 'title' (full page name), 'unwatched' (boolean) and
38 * 'message' (parsed HTML of the 'removedwatchtext' message).
39 * @param err {Function} callback if error (optional)
40 * @return {jqXHR}
41 */
42 unwatch: function( page, success, err, unwatch ) {
43 var params, ok;
44 params = {
45 action: 'watch',
46 unwatch: 1,
47 title: String( page ),
48 token: mw.user.tokens.get( 'watchToken' ),
49 uselang: mw.config.get( 'wgUserLanguage' )
50 };
51 ok = function( data ) {
52 success( data.watch );
53 };
54 return this.post( params, { ok: ok, err: err } );
55 }
56
57 } );
58
59 } )( jQuery, mediaWiki );