2018-11-12 19:22:46 +00:00
|
|
|
( function () {
|
2018-06-27 23:12:28 +00:00
|
|
|
// To allow users to cancel a thanks in the event of an accident, the action is delayed.
|
|
|
|
var THANKS_DELAY = 2000,
|
|
|
|
msgOptions = {
|
|
|
|
// tag ensures that only one message in workflow is shown at any time
|
|
|
|
tag: 'thanks'
|
|
|
|
};
|
2015-01-15 00:14:12 +00:00
|
|
|
/**
|
2017-08-03 23:05:54 +00:00
|
|
|
* Attempt to execute a thank operation for a given edit
|
2015-01-15 00:14:12 +00:00
|
|
|
*
|
2016-04-22 19:47:08 +00:00
|
|
|
* @param {string} name The username of the user who made the edit
|
|
|
|
* @param {string} revision The revision the user created
|
2017-06-22 00:30:35 +00:00
|
|
|
* @param {string} recipientGender The gender of the user who made the edit
|
2020-07-09 15:35:57 +00:00
|
|
|
* @return {jQuery.Promise} The thank operation's status.
|
2015-01-15 00:14:12 +00:00
|
|
|
*/
|
2017-06-22 00:30:35 +00:00
|
|
|
function thankUser( name, revision, recipientGender ) {
|
2018-02-04 14:47:22 +00:00
|
|
|
return ( new mw.Api() ).postWithToken( 'csrf', {
|
2015-06-16 03:02:29 +00:00
|
|
|
action: 'thank',
|
|
|
|
rev: revision,
|
|
|
|
source: 'mobilediff'
|
2018-02-04 14:47:22 +00:00
|
|
|
} ).then( function () {
|
2018-06-27 23:12:28 +00:00
|
|
|
mw.notify( mw.msg( 'thanks-button-action-completed', name, recipientGender, mw.user ),
|
|
|
|
msgOptions );
|
2018-02-04 14:47:22 +00:00
|
|
|
}, function ( errorCode ) {
|
2022-09-29 14:08:53 +00:00
|
|
|
var msg;
|
2018-02-04 14:47:22 +00:00
|
|
|
switch ( errorCode ) {
|
|
|
|
case 'invalidrevision':
|
2022-09-29 14:08:53 +00:00
|
|
|
msg = mw.msg( 'thanks-error-invalidrevision' );
|
2018-02-04 14:47:22 +00:00
|
|
|
break;
|
|
|
|
case 'ratelimited':
|
2022-09-29 14:08:53 +00:00
|
|
|
msg = mw.msg( 'thanks-error-ratelimited', recipientGender );
|
2018-02-04 14:47:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-09-29 14:08:53 +00:00
|
|
|
msg = mw.msg( 'thanks-error-undefined', errorCode );
|
2018-02-04 14:47:22 +00:00
|
|
|
}
|
2022-09-29 14:08:53 +00:00
|
|
|
mw.notify( msg, msgOptions );
|
2018-02-04 14:47:22 +00:00
|
|
|
} );
|
2013-09-26 01:16:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 23:44:50 +00:00
|
|
|
/**
|
|
|
|
* Disables the thank button marking the user as thanked
|
|
|
|
*
|
|
|
|
* @param {jQuery} $button used for thanking
|
|
|
|
* @param {string} gender The gender of the user who made the edit
|
|
|
|
* @return {jQuery} $button now disabled
|
|
|
|
*/
|
|
|
|
function disableThanks( $button, gender ) {
|
2023-12-01 00:11:56 +00:00
|
|
|
$button
|
2018-06-13 23:44:50 +00:00
|
|
|
.addClass( 'thanked' )
|
2023-12-01 00:11:56 +00:00
|
|
|
.prop( 'disabled', true );
|
|
|
|
$button.find( 'span' ).eq( 1 )
|
2019-05-15 16:33:48 +00:00
|
|
|
.text( mw.msg( 'thanks-button-thanked', mw.user, gender ) );
|
2023-12-01 00:11:56 +00:00
|
|
|
return $button;
|
2018-06-13 23:44:50 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 01:16:56 +00:00
|
|
|
/**
|
|
|
|
* Create a thank button for a given edit
|
|
|
|
*
|
2016-04-22 19:47:08 +00:00
|
|
|
* @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
|
2018-02-10 19:14:21 +00:00
|
|
|
* @return {jQuery|null} The HTML of the button.
|
2013-09-26 01:16:56 +00:00
|
|
|
*/
|
|
|
|
function createThankLink( name, rev, gender ) {
|
2023-12-01 00:11:56 +00:00
|
|
|
const button = document.createElement( 'button' ),
|
|
|
|
label = document.createElement( 'span' ),
|
|
|
|
icon = document.createElement( 'span' );
|
|
|
|
let timeout;
|
|
|
|
|
|
|
|
// https://doc.wikimedia.org/codex/latest/components/demos/button.html#css-only-version
|
|
|
|
button.classList.add(
|
|
|
|
'cdx-button',
|
|
|
|
'mw-mf-action-button',
|
|
|
|
'cdx-button--action-progressive',
|
|
|
|
'cdx-button--weight-primary'
|
|
|
|
);
|
|
|
|
label.textContent = mw.msg( 'thanks-button-thank', mw.user, gender );
|
|
|
|
icon.classList.add( 'mw-thanks-icon', 'cdx-button__icon' );
|
|
|
|
icon.setAttribute( 'aria-hidden', 'true' );
|
|
|
|
button.appendChild( icon );
|
|
|
|
button.appendChild( label );
|
|
|
|
const $button = $( button );
|
2019-09-13 18:32:59 +00:00
|
|
|
|
2013-09-26 01:16:56 +00:00
|
|
|
// Don't make thank button for self
|
2018-02-10 19:14:21 +00:00
|
|
|
if ( name === mw.config.get( 'wgUserName' ) ) {
|
|
|
|
return null;
|
2013-09-26 01:16:56 +00:00
|
|
|
}
|
2018-02-10 19:14:21 +00:00
|
|
|
// See if user has already been thanked for this edit
|
|
|
|
if ( mw.config.get( 'wgThanksAlreadySent' ) ) {
|
2018-06-13 23:44:50 +00:00
|
|
|
return disableThanks( $button, gender );
|
2018-02-10 19:14:21 +00:00
|
|
|
}
|
2018-06-27 23:12:28 +00:00
|
|
|
|
|
|
|
function cancelThanks( $btn ) {
|
|
|
|
// Hide the notification
|
|
|
|
$( '.mw-notification' ).hide();
|
|
|
|
// Clear the queued thanks!
|
|
|
|
clearTimeout( timeout );
|
|
|
|
timeout = null;
|
|
|
|
$btn.prop( 'disabled', false );
|
|
|
|
}
|
|
|
|
|
|
|
|
function queueThanks( $btn ) {
|
2022-09-29 14:08:53 +00:00
|
|
|
var $msg = $( '<div>' ).addClass( 'mw-thanks-notification' )
|
|
|
|
.text( mw.msg( 'thanks-button-action-queued', name, gender ) )
|
|
|
|
.append( $( '<a>' ).text( mw.msg( 'thanks-button-action-cancel' ) )
|
|
|
|
.on( 'click', function () {
|
|
|
|
cancelThanks( $btn );
|
|
|
|
} )
|
|
|
|
);
|
2018-06-27 23:12:28 +00:00
|
|
|
mw.notify( $msg, msgOptions );
|
|
|
|
timeout = setTimeout( function () {
|
|
|
|
timeout = null;
|
|
|
|
thankUser( name, rev, gender ).then( function () {
|
|
|
|
disableThanks( $btn, gender );
|
|
|
|
} );
|
|
|
|
}, THANKS_DELAY );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $button.on( 'click', function () {
|
|
|
|
var $this = $( this );
|
|
|
|
$this.prop( 'disabled', true );
|
2019-12-12 11:36:24 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
2018-06-27 23:12:28 +00:00
|
|
|
if ( !$this.hasClass( 'thanked' ) && !timeout ) {
|
|
|
|
queueThanks( $this );
|
|
|
|
}
|
|
|
|
} );
|
2013-09-26 01:16:56 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 00:14:12 +00:00
|
|
|
/**
|
|
|
|
* Initialise a thank button in the given container.
|
|
|
|
*
|
2016-04-22 19:47:47 +00:00
|
|
|
* @param {jQuery} $user existing element with data attributes associated describing a user.
|
|
|
|
* @param {jQuery} $container to render button in
|
2015-01-15 00:14:12 +00:00
|
|
|
*/
|
|
|
|
function init( $user, $container ) {
|
|
|
|
var username = $user.data( 'user-name' ),
|
2013-09-26 01:16:56 +00:00
|
|
|
rev = $user.data( 'revision-id' ),
|
2023-11-30 10:58:04 +00:00
|
|
|
gender = $user.data( 'user-gender' );
|
2013-09-26 01:16:56 +00:00
|
|
|
|
2023-11-30 10:58:04 +00:00
|
|
|
var $thankBtn = createThankLink( username, rev, gender );
|
2015-11-24 17:27:25 +00:00
|
|
|
if ( $thankBtn ) {
|
|
|
|
$thankBtn.prependTo( $container );
|
2013-09-26 01:16:56 +00:00
|
|
|
}
|
2015-11-24 17:27:25 +00:00
|
|
|
|
2015-01-15 00:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$( function () {
|
|
|
|
init( $( '.mw-mf-user' ), $( '#mw-mf-userinfo' ) );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Expose for testing purposes
|
|
|
|
mw.thanks = $.extend( {}, mw.thanks || {}, {
|
|
|
|
_mobileDiffInit: init
|
2013-09-26 01:16:56 +00:00
|
|
|
} );
|
2018-11-12 19:22:46 +00:00
|
|
|
}() );
|