2016-07-15 15:16:08 +00:00
|
|
|
( function ( $, mw, OO ) {
|
2014-02-26 02:12:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-04 19:13:43 +00:00
|
|
|
function reloadThankedState() {
|
|
|
|
$( 'a.mw-thanks-thank-link' ).each( function ( idx, el ) {
|
2014-02-26 02:12:47 +00:00
|
|
|
var $thankLink = $( el );
|
|
|
|
if ( mw.thanks.thanked.contains( $thankLink ) ) {
|
|
|
|
$thankLink.before( mw.msg( 'thanks-thanked' ) );
|
|
|
|
$thankLink.remove();
|
|
|
|
}
|
|
|
|
} );
|
2015-05-04 19:13:43 +00:00
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2014-08-06 13:46:53 +00:00
|
|
|
// $thankLink is the element with the data-revision-id attribute
|
|
|
|
// $thankElement is the element to be removed on success
|
2015-05-04 19:13:43 +00:00
|
|
|
function sendThanks( $thankLink, $thankElement ) {
|
2014-02-26 02:12:47 +00:00
|
|
|
var source;
|
2016-04-14 04:44:33 +00:00
|
|
|
|
|
|
|
if ( $thankLink.data( 'clickDisabled' ) ) {
|
|
|
|
// Prevent double clicks while we haven't received a response from API request
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$thankLink.data( 'clickDisabled', true );
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
if ( mw.config.get( 'wgAction' ) === 'history' ) {
|
|
|
|
source = 'history';
|
|
|
|
} else {
|
|
|
|
source = 'diff';
|
|
|
|
}
|
2015-04-22 23:21:27 +00:00
|
|
|
|
2016-05-26 07:02:17 +00:00
|
|
|
( new mw.Api ).postWithToken( 'csrf', {
|
2015-05-04 19:13:43 +00:00
|
|
|
action: 'thank',
|
|
|
|
rev: $thankLink.attr( 'data-revision-id' ),
|
|
|
|
source: source
|
2014-02-26 02:12:47 +00:00
|
|
|
} )
|
2015-04-22 23:21:27 +00:00
|
|
|
.then(
|
|
|
|
// Success
|
|
|
|
function ( data ) {
|
|
|
|
var username = $thankLink.closest(
|
|
|
|
source === 'history' ? 'li' : 'td'
|
|
|
|
).find( 'a.mw-userlink' ).text();
|
|
|
|
// Get the user who was thanked (for gender purposes)
|
|
|
|
return mw.thanks.getUserGender( username );
|
|
|
|
},
|
|
|
|
// Fail
|
|
|
|
function ( errorCode, details ) {
|
2016-04-14 04:44:33 +00:00
|
|
|
// If error occured, enable attempting to thank again
|
|
|
|
$thankLink.data( 'clickDisabled', false );
|
2015-04-22 23:21:27 +00:00
|
|
|
switch ( errorCode ) {
|
|
|
|
case 'invalidrevision':
|
2016-07-15 15:16:08 +00:00
|
|
|
OO.ui.alert( mw.msg( 'thanks-error-invalidrevision' ) );
|
2015-04-22 23:21:27 +00:00
|
|
|
break;
|
|
|
|
case 'ratelimited':
|
2016-07-15 15:16:08 +00:00
|
|
|
OO.ui.alert( mw.msg( 'thanks-error-ratelimited', mw.user ) );
|
2015-04-22 23:21:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-07-15 15:16:08 +00:00
|
|
|
OO.ui.alert( mw.msg( 'thanks-error-undefined', errorCode ) );
|
2015-04-22 23:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then( function ( recipientGender ) {
|
|
|
|
$thankElement.before( mw.message( 'thanks-thanked', mw.user, recipientGender ).escaped() );
|
2014-08-06 13:46:53 +00:00
|
|
|
$thankElement.remove();
|
2014-02-26 02:12:47 +00:00
|
|
|
mw.thanks.thanked.push( $thankLink );
|
|
|
|
} );
|
2015-05-04 19:13:43 +00:00
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
|
|
|
if ( $.isReady ) {
|
|
|
|
// This condition is required for soft-reloads
|
|
|
|
// to also trigger the reloadThankedState
|
|
|
|
reloadThankedState();
|
|
|
|
} else {
|
|
|
|
$( document ).ready( reloadThankedState );
|
|
|
|
}
|
|
|
|
|
2015-05-04 19:13:43 +00:00
|
|
|
$( function () {
|
2014-02-26 02:12:47 +00:00
|
|
|
if ( mw.config.get( 'thanks-confirmation-required' ) ) {
|
2014-08-06 13:46:53 +00:00
|
|
|
$( 'a.mw-thanks-thank-link' ).confirmable( {
|
2015-02-18 02:55:36 +00:00
|
|
|
i18n: {
|
|
|
|
confirm: mw.msg( 'thanks-confirmation2', mw.user ),
|
|
|
|
noTitle: mw.msg( 'thanks-thank-tooltip-no', mw.user ),
|
|
|
|
yesTitle: mw.msg( 'thanks-thank-tooltip-yes', mw.user )
|
|
|
|
},
|
2014-08-06 13:46:53 +00:00
|
|
|
handler: function ( e ) {
|
|
|
|
var $thankLink = $( this );
|
|
|
|
e.preventDefault();
|
|
|
|
sendThanks( $thankLink, $thankLink.closest( '.jquery-confirmable-wrapper' ) );
|
|
|
|
}
|
|
|
|
} );
|
2014-02-26 02:12:47 +00:00
|
|
|
} else {
|
2014-08-06 13:46:53 +00:00
|
|
|
$( 'a.mw-thanks-thank-link' ).click( function ( e ) {
|
|
|
|
var $thankLink = $( this );
|
|
|
|
e.preventDefault();
|
|
|
|
sendThanks( $thankLink, $thankLink );
|
|
|
|
} );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2016-07-15 15:16:08 +00:00
|
|
|
} )( jQuery, mediaWiki, OO );
|