Tooltips for HTMLCheckMatrix
[mediawiki.git] / resources / mediawiki.page / mediawiki.page.patrol.ajax.js
blobd7a07d7157d8db2669ebd952d9c4f2016c8c87f5
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         $( document ).ready( function () {
15                 var $patrolLinks = $( '.patrollink a' );
16                 $patrolLinks.on( 'click', function ( e ) {
17                         var $spinner, href, rcid, apiRequest;
19                         // Hide the link and create a spinner to show it inside the brackets.
20                         $spinner = $.createSpinner( {
21                                 size: 'small',
22                                 type: 'inline'
23                         } );
24                         $( this ).hide().after( $spinner );
26                         href = $( this ).attr( 'href' );
27                         rcid = mw.util.getParamValue( 'rcid', href );
28                         apiRequest = new mw.Api();
30                         apiRequest.post( {
31                                 action: 'patrol',
32                                 token: mw.user.tokens.get( 'patrolToken' ),
33                                 rcid: rcid
34                         } )
35                         .done( function ( data ) {
36                                 // Remove all patrollinks from the page (including any spinners inside).
37                                 $patrolLinks.closest( '.patrollink' ).remove();
38                                 if ( data.patrol !== undefined ) {
39                                         // Success
40                                         var title = new mw.Title( data.patrol.title );
41                                         mw.notify( mw.msg( 'markedaspatrollednotify', title.toText() ) );
42                                 } else {
43                                         // This should never happen as errors should trigger fail
44                                         mw.notify( mw.msg( 'markedaspatrollederrornotify' ) );
45                                 }
46                         } )
47                         .fail( function ( error ) {
48                                 $spinner.remove();
49                                 // Restore the patrol link. This allows the user to try again
50                                 // (or open it in a new window, bypassing this ajax module).
51                                 $patrolLinks.show();
52                                 if ( error === 'noautopatrol' ) {
53                                         // Can't patrol own
54                                         mw.notify( mw.msg( 'markedaspatrollederror-noautopatrol' ) );
55                                 } else {
56                                         mw.notify( mw.msg( 'markedaspatrollederrornotify' ) );
57                                 }
58                         } );
60                         e.preventDefault();
61                 } );
62         } );
63 }( mediaWiki, jQuery ) );