]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - resources/src/mediawiki/page/patrol.ajax.js
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / resources / src / mediawiki / page / patrol.ajax.js
1 /*!
2  * Animate patrol links to use asynchronous API requests to
3  * patrol pages, rather than navigating to a different URI.
4  *
5  * @since 1.21
6  * @author Marius Hoch <hoo@online.de>
7  */
8 ( function ( mw, $ ) {
9         if ( !mw.user.tokens.exists( 'patrolToken' ) ) {
10                 // Current user has no patrol right, or an old cached version of user.tokens
11                 // that didn't have patrolToken yet.
12                 return;
13         }
14         $( function () {
15                 var $patrolLinks = $( '.patrollink[data-mw="interface"] a' );
16                 $patrolLinks.on( 'click', function ( e ) {
17                         var $spinner, rcid, apiRequest;
18
19                         // Preload the notification module for mw.notify
20                         mw.loader.load( 'mediawiki.notification' );
21
22                         // Hide the link and create a spinner to show it inside the brackets.
23                         $spinner = $.createSpinner( {
24                                 size: 'small',
25                                 type: 'inline'
26                         } );
27                         $( this ).hide().after( $spinner );
28
29                         rcid = mw.util.getParamValue( 'rcid', this.href );
30                         apiRequest = new mw.Api();
31
32                         apiRequest.postWithToken( 'patrol', {
33                                 formatversion: 2,
34                                 action: 'patrol',
35                                 rcid: rcid
36                         } ).done( function ( data ) {
37                                 var title;
38                                 // Remove all patrollinks from the page (including any spinners inside).
39                                 $patrolLinks.closest( '.patrollink' ).remove();
40                                 if ( data.patrol !== undefined ) {
41                                         // Success
42                                         title = new mw.Title( data.patrol.title );
43                                         mw.notify( mw.msg( 'markedaspatrollednotify', title.toText() ) );
44                                 } else {
45                                         // This should never happen as errors should trigger fail
46                                         mw.notify( mw.msg( 'markedaspatrollederrornotify' ), { type: 'error' } );
47                                 }
48                         } ).fail( function ( error ) {
49                                 $spinner.remove();
50                                 // Restore the patrol link. This allows the user to try again
51                                 // (or open it in a new window, bypassing this ajax module).
52                                 $patrolLinks.show();
53                                 if ( error === 'noautopatrol' ) {
54                                         // Can't patrol own
55                                         mw.notify( mw.msg( 'markedaspatrollederror-noautopatrol' ), { type: 'warn' } );
56                                 } else {
57                                         mw.notify( mw.msg( 'markedaspatrollederrornotify' ), { type: 'error' } );
58                                 }
59                         } );
60
61                         e.preventDefault();
62                 } );
63         } );
64 }( mediaWiki, jQuery ) );