summaryrefslogtreecommitdiff
blob: f6f76d0d4ab220329aec14d6091205d21302cdfe (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
( function ( mw, $ ) {
	/**
	 * Attempt to execute a thank operation for a given edit
	 *
	 * @param {string} name The username of the user who made the edit
	 * @param {string} revision The revision the user created
	 * @param {string} recipientGender The gender of the user who made the edit
	 * @return {Promise} The thank operation's status.
	 */
	function thankUser( name, revision, recipientGender ) {
		var d = $.Deferred();
		( new mw.Api() ).postWithToken( 'csrf', {
			action: 'thank',
			rev: revision,
			source: 'mobilediff'
		} ).done( function () {
			mw.notify( mw.msg( 'thanks-thanked-notice', name, recipientGender, mw.user ) );
			d.resolve();
		} )
			.fail( function ( errorCode ) {
				// FIXME: What is "popup" and where is it defined?
				/* eslint-disable no-undef */
				switch ( errorCode ) {
					case 'invalidrevision':
						popup.show( mw.msg( 'thanks-error-invalidrevision' ) );
						break;
					case 'ratelimited':
						popup.show( mw.msg( 'thanks-error-ratelimited', recipientGender ) );
						break;
					default:
						popup.show( mw.msg( 'thanks-error-undefined', errorCode ) );
				}
				/* eslint-enable no-undef */
				d.reject();
			} );
		return d;
	}

	/**
	 * Create a thank button for a given edit
	 *
	 * @param {string} name The username of the user who made the edit
	 * @param {string} rev The revision the user created
	 * @param {string} gender The gender of the user who made the edit
	 * @return {html} The HTML of the button.
	 */
	function createThankLink( name, rev, gender ) {
		var thankImg = mw.config.get( 'wgExtensionAssetsPath' ) + '/Thanks/WhiteSmiley.png',
			thankImgTag = '<img width="25" height="20" src="' + thankImg + '" class="mw-mf-action-button-icon"/>',
			$thankBtn;

		// Don't make thank button for self
		if ( name !== mw.config.get( 'wgUserName' ) ) {
			// See if user has already been thanked for this edit
			if ( mw.config.get( 'wgThanksAlreadySent' ) ) {
				$thankBtn = $( '<button class="mw-mf-action-button mw-ui-button mw-ui-progressive thanked">' )
					.prop( 'disabled', true )
					.html( thankImgTag + mw.message( 'thanks-button-thanked', mw.user ).escaped() );
			} else {
				$thankBtn = $( '<button class="mw-mf-action-button mw-ui-button mw-ui-progressive">' )
					.html( thankImgTag + mw.message( 'thanks-button-thank', mw.user, gender ).escaped()
					)
					.on( 'click', function () {
						var $this = $( this );
						if ( !$this.hasClass( 'thanked' ) ) {
							thankUser( name, rev, gender ).done( function () {
								$this.addClass( 'thanked' ).prop( 'disabled', true )
									.html( thankImgTag + mw.message( 'thanks-button-thanked', mw.user, gender ).escaped() );
							} );
						}
					} );
			}
			return $thankBtn;
		}
	}

	/**
	 * Initialise a thank button in the given container.
	 *
	 * @param {jQuery} $user existing element with data attributes associated describing a user.
	 * @param {jQuery} $container to render button in
	 */
	function init( $user, $container ) {
		var username = $user.data( 'user-name' ),
			rev = $user.data( 'revision-id' ),
			gender = $user.data( 'user-gender' ),
			$thankBtn;

		$thankBtn = createThankLink( username, rev, gender );
		if ( $thankBtn ) {
			$thankBtn.prependTo( $container );
		}

	}

	$( function () {
		init( $( '.mw-mf-user' ), $( '#mw-mf-userinfo' ) );
	} );

	// Expose for testing purposes
	mw.thanks = $.extend( {}, mw.thanks || {}, {
		_mobileDiffInit: init
	} );
}( mediaWiki, jQuery ) );