summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki/page/rollback.js
blob: 6db518db1e9414d26941a08659f7efe1e14bd70f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*!
 * Enhance rollback links by using asynchronous API requests,
 * rather than navigating to an action page.
 *
 * @since 1.28
 * @author Timo Tijhof
 */
( function ( mw, $ ) {

	$( function () {
		$( '.mw-rollback-link' ).on( 'click', 'a[data-mw="interface"]', function ( e ) {
			var api, $spinner,
				$link = $( this ),
				url = this.href,
				page = mw.util.getParamValue( 'title', url ),
				user = mw.util.getParamValue( 'from', url );

			if ( !page || user === null ) {
				// Let native browsing handle the link
				return true;
			}

			// Preload the notification module for mw.notify
			mw.loader.load( 'mediawiki.notification' );

			// Remove event handler so that next click (re-try) uses server action
			$( e.delegateTarget ).off( 'click' );

			// Hide the link and create a spinner to show it inside the brackets.
			$spinner = $.createSpinner( { size: 'small', type: 'inline' } );
			$link.hide().after( $spinner );

			// @todo: data.messageHtml is no more. Convert to using errorformat=html.
			api = new mw.Api();
			api.rollback( page, user )
				.then( function ( data ) {
					mw.notify( $.parseHTML( data.messageHtml ), {
						title: mw.msg( 'actioncomplete' )
					} );

					// Remove link container and the subsequent text node containing " | ".
					if ( e.delegateTarget.nextSibling && e.delegateTarget.nextSibling.nodeType === Node.TEXT_NODE ) {
						$( e.delegateTarget.nextSibling ).remove();
					}
					$( e.delegateTarget ).remove();
				}, function ( errorCode, data ) {
					var message = data && data.error && data.error.messageHtml ?
							$.parseHTML( data.error.messageHtml ) :
							mw.msg( 'rollbackfailed' ),
						type = errorCode === 'alreadyrolled' ? 'warn' : 'error';

					mw.notify( message, {
						type: type,
						title: mw.msg( 'rollbackfailed' ),
						autoHide: false
					} );

					// Restore the link (enables user to try again)
					$spinner.remove();
					$link.show();
				} );

			e.preventDefault();
		} );
	} );

}( mediaWiki, jQuery ) );