Merge "selenium: invoke jobs to enforce eventual consistency"
[lhc/web/wiklou.git] / resources / src / mediawiki.page.rollback.js
1 /*!
2 * Enhance rollback links by using asynchronous API requests,
3 * rather than navigating to an action page.
4 *
5 * @since 1.28
6 * @author Timo Tijhof
7 */
8 ( function () {
9
10 $( function () {
11 $( '.mw-rollback-link' ).on( 'click', 'a[data-mw="interface"]', function ( e ) {
12 var api, $spinner,
13 $link = $( this ),
14 url = this.href,
15 page = mw.util.getParamValue( 'title', url ),
16 user = mw.util.getParamValue( 'from', url );
17
18 if ( !page || user === null ) {
19 // Let native browsing handle the link
20 return true;
21 }
22
23 // Preload the notification module for mw.notify
24 mw.loader.load( 'mediawiki.notification' );
25
26 // Remove event handler so that next click (re-try) uses server action
27 $( e.delegateTarget ).off( 'click' );
28
29 // Hide the link and create a spinner to show it inside the brackets.
30 $spinner = $.createSpinner( { size: 'small', type: 'inline' } );
31 $link.hide().after( $spinner );
32
33 // @todo: data.messageHtml is no more. Convert to using errorformat=html.
34 api = new mw.Api();
35 api.rollback( page, user )
36 .then( function ( data ) {
37 mw.notify( $.parseHTML( data.messageHtml ), {
38 title: mw.msg( 'actioncomplete' )
39 } );
40
41 // Remove link container and the subsequent text node containing " | ".
42 if ( e.delegateTarget.nextSibling && e.delegateTarget.nextSibling.nodeType === Node.TEXT_NODE ) {
43 $( e.delegateTarget.nextSibling ).remove();
44 }
45 $( e.delegateTarget ).remove();
46 }, function ( errorCode, data ) {
47 var message = data && data.error && data.error.messageHtml ?
48 $.parseHTML( data.error.messageHtml ) :
49 mw.msg( 'rollbackfailed' ),
50 type = errorCode === 'alreadyrolled' ? 'warn' : 'error';
51
52 mw.notify( message, {
53 type: type,
54 title: mw.msg( 'rollbackfailed' ),
55 autoHide: false
56 } );
57
58 // Restore the link (enables user to try again)
59 $spinner.remove();
60 $link.show();
61 } );
62
63 e.preventDefault();
64 } );
65 } );
66
67 }() );