summaryrefslogtreecommitdiff
blob: c05466920efb9c2d98e3123c936a4068b12519dd (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
( function ( $, mw, OO ) {
	'use strict';

	function reloadThankedState() {
		$( 'a.mw-thanks-thank-link' ).each( function ( idx, el ) {
			var $thankLink = $( el );
			if ( mw.thanks.thanked.contains( $thankLink ) ) {
				$thankLink.before( mw.message( 'thanks-thanked', mw.user, $thankLink.data( 'recipient-gender' ) ).escaped() );
				$thankLink.remove();
			}
		} );
	}

	// $thankLink is the element with the data-revision-id attribute
	// $thankElement is the element to be removed on success
	function sendThanks( $thankLink, $thankElement ) {
		var source, apiParams;

		if ( $thankLink.data( 'clickDisabled' ) ) {
			// Prevent double clicks while we haven't received a response from API request
			return false;
		}
		$thankLink.data( 'clickDisabled', true );

		// Determine the thank source (history, diff, or log).
		if ( mw.config.get( 'wgAction' ) === 'history' ) {
			source = 'history';
		} else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Log' ) {
			source = 'log';
		} else {
			source = 'diff';
		}

		// Construct the API parameters.
		apiParams = {
			action: 'thank',
			source: source
		};
		if ( $thankLink.data( 'log-id' ) ) {
			apiParams.log = $thankLink.data( 'log-id' );
		} else {
			apiParams.rev = $thankLink.data( 'revision-id' );
		}

		// Send the API request.
		( new mw.Api() ).postWithToken( 'csrf', apiParams )
			.then(
				// Success
				function () {
					$thankElement.before( mw.message( 'thanks-thanked', mw.user, $thankLink.data( 'recipient-gender' ) ).escaped() );
					$thankElement.remove();
					mw.thanks.thanked.push( $thankLink );
				},
				// Fail
				function ( errorCode ) {
					// If error occured, enable attempting to thank again
					$thankLink.data( 'clickDisabled', false );
					switch ( errorCode ) {
						case 'invalidrevision':
							OO.ui.alert( mw.msg( 'thanks-error-invalidrevision' ) );
							break;
						case 'ratelimited':
							OO.ui.alert( mw.msg( 'thanks-error-ratelimited', mw.user ) );
							break;
						case 'revdeleted':
							OO.ui.alert( mw.msg( 'thanks-error-revdeleted' ) );
							break;
						default:
							OO.ui.alert( mw.msg( 'thanks-error-undefined', errorCode ) );
					}
				}
			);
	}

	/**
	 * Add interactive handlers to all 'thank' links in $content
	 *
	 * @param {jQuery} $content
	 */
	function addActionToLinks( $content ) {
		var $thankLinks = $content.find( 'a.mw-thanks-thank-link' );
		if ( mw.config.get( 'thanks-confirmation-required' ) ) {
			$thankLinks.each( function () {
				var $thankLink = $( this );
				$thankLink.confirmable( {
					i18n: {
						confirm: mw.msg( 'thanks-confirmation2', mw.user ),
						no: mw.msg( 'cancel' ),
						noTitle: mw.msg( 'thanks-thank-tooltip-no', mw.user ),
						yes: mw.msg( 'thanks-button-thank', mw.user, $thankLink.data( 'recipient-gender' ) ),
						yesTitle: mw.msg( 'thanks-thank-tooltip-yes', mw.user )
					},
					handler: function ( e ) {
						e.preventDefault();
						sendThanks( $thankLink, $thankLink.closest( '.jquery-confirmable-wrapper' ) );
					}
				} );
			} );
		} else {
			$thankLinks.click( function ( e ) {
				var $thankLink = $( this );
				e.preventDefault();
				sendThanks( $thankLink, $thankLink );
			} );
		}
	}

	if ( $.isReady ) {
		// This condition is required for soft-reloads
		// to also trigger the reloadThankedState
		reloadThankedState();
	} else {
		$( reloadThankedState );
	}

	$( function () {
		addActionToLinks( $( 'body' ) );
	} );

	mw.hook( 'wikipage.diff' ).add( function ( $content ) {
		addActionToLinks( $content );
	} );
}( jQuery, mediaWiki, OO ) );