Merge "Remove $wgEnotifUseJobQ"
[mediawiki.git] / resources / src / mediawiki / page / patrol.ajax.js
bloba63202f56dec885746d5443ac39c9a8b23f78d0b
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 a' );
16                 $patrolLinks.on( 'click', function ( e ) {
17                         var $spinner, href, rcid, apiRequest;
19                         // Start preloading the notification module (normally loaded by mw.notify())
20                         mw.loader.load( 'mediawiki.notification' );
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 );
29                         href = $( this ).attr( 'href' );
30                         rcid = mw.util.getParamValue( 'rcid', href );
31                         apiRequest = new mw.Api();
33                         apiRequest.postWithToken( 'patrol', {
34                                 formatversion: 2,
35                                 action: 'patrol',
36                                 rcid: rcid
37                         } )
38                         .done( function ( data ) {
39                                 // Remove all patrollinks from the page (including any spinners inside).
40                                 $patrolLinks.closest( '.patrollink' ).remove();
41                                 if ( data.patrol !== undefined ) {
42                                         // Success
43                                         var title = new mw.Title( data.patrol.title );
44                                         mw.notify( mw.msg( 'markedaspatrollednotify', title.toText() ) );
45                                 } else {
46                                         // This should never happen as errors should trigger fail
47                                         mw.notify( mw.msg( 'markedaspatrollederrornotify' ), { type: 'error' } );
48                                 }
49                         } )
50                         .fail( function ( error ) {
51                                 $spinner.remove();
52                                 // Restore the patrol link. This allows the user to try again
53                                 // (or open it in a new window, bypassing this ajax module).
54                                 $patrolLinks.show();
55                                 if ( error === 'noautopatrol' ) {
56                                         // Can't patrol own
57                                         mw.notify( mw.msg( 'markedaspatrollederror-noautopatrol' ), { type: 'warn' } );
58                                 } else {
59                                         mw.notify( mw.msg( 'markedaspatrollederrornotify' ), { type: 'error' } );
60                                 }
61                         } );
63                         e.preventDefault();
64                 } );
65         } );
66 }( mediaWiki, jQuery ) );