2018-11-12 19:22:46 +00:00
|
|
|
( function () {
|
2014-02-26 02:12:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2022-01-10 12:54:51 +00:00
|
|
|
var attrName = 'data-revision-id';
|
|
|
|
|
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 );
|
2022-01-10 12:54:51 +00:00
|
|
|
if ( mw.thanks.thanked.contains( $thankLink.attr( attrName ) ) ) {
|
2019-03-28 00:38:39 +00:00
|
|
|
$thankLink.before(
|
2019-12-12 11:36:24 +00:00
|
|
|
$( '<span>' ).addClass( 'mw-thanks-thank-confirmation' ).text(
|
2019-05-15 16:33:48 +00:00
|
|
|
mw.msg( 'thanks-thanked', mw.user, $thankLink.data( 'recipient-gender' ) ) )
|
2019-03-28 00:38:39 +00:00
|
|
|
);
|
2014-02-26 02:12:47 +00:00
|
|
|
$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 ) {
|
2018-03-29 02:36:40 +00:00
|
|
|
var source, apiParams;
|
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 );
|
|
|
|
|
2018-03-29 02:36:40 +00:00
|
|
|
// Determine the thank source (history, diff, or log).
|
2014-02-26 02:12:47 +00:00
|
|
|
if ( mw.config.get( 'wgAction' ) === 'history' ) {
|
|
|
|
source = 'history';
|
2018-03-29 02:36:40 +00:00
|
|
|
} else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Log' ) {
|
|
|
|
source = 'log';
|
2014-02-26 02:12:47 +00:00
|
|
|
} else {
|
|
|
|
source = 'diff';
|
|
|
|
}
|
2015-04-22 23:21:27 +00:00
|
|
|
|
2018-03-29 02:36:40 +00:00
|
|
|
// Construct the API parameters.
|
|
|
|
apiParams = {
|
2015-05-04 19:13:43 +00:00
|
|
|
action: 'thank',
|
|
|
|
source: source
|
2018-03-29 02:36:40 +00:00
|
|
|
};
|
|
|
|
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 )
|
2017-08-03 23:05:54 +00:00
|
|
|
.then(
|
|
|
|
// Success
|
|
|
|
function () {
|
2018-01-20 01:05:38 +00:00
|
|
|
$thankElement.before( mw.message( 'thanks-thanked', mw.user, $thankLink.data( 'recipient-gender' ) ).escaped() );
|
|
|
|
$thankElement.remove();
|
2022-01-10 12:54:51 +00:00
|
|
|
mw.thanks.thanked.push( $thankLink.attr( attrName ) );
|
2017-08-03 23:05:54 +00:00
|
|
|
},
|
|
|
|
// Fail
|
|
|
|
function ( errorCode ) {
|
2019-03-08 13:48:21 +00:00
|
|
|
// If error occurred, enable attempting to thank again
|
2017-08-03 23:05:54 +00:00
|
|
|
$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;
|
2018-03-02 05:37:16 +00:00
|
|
|
case 'revdeleted':
|
|
|
|
OO.ui.alert( mw.msg( 'thanks-error-revdeleted' ) );
|
|
|
|
break;
|
2017-08-03 23:05:54 +00:00
|
|
|
default:
|
|
|
|
OO.ui.alert( mw.msg( 'thanks-error-undefined', errorCode ) );
|
|
|
|
}
|
2015-04-22 23:21:27 +00:00
|
|
|
}
|
2018-01-20 01:05:38 +00:00
|
|
|
);
|
2015-05-04 19:13:43 +00:00
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2018-02-20 14:01:40 +00:00
|
|
|
/**
|
|
|
|
* Add interactive handlers to all 'thank' links in $content
|
|
|
|
*
|
|
|
|
* @param {jQuery} $content
|
|
|
|
*/
|
2016-08-25 15:25:40 +00:00
|
|
|
function addActionToLinks( $content ) {
|
2018-02-20 14:01:40 +00:00
|
|
|
var $thankLinks = $content.find( 'a.mw-thanks-thank-link' );
|
2014-02-26 02:12:47 +00:00
|
|
|
if ( mw.config.get( 'thanks-confirmation-required' ) ) {
|
2018-02-20 14:01:40 +00:00
|
|
|
$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' ) );
|
|
|
|
}
|
|
|
|
} );
|
2014-08-06 13:46:53 +00:00
|
|
|
} );
|
2014-02-26 02:12:47 +00:00
|
|
|
} else {
|
2019-12-12 11:36:24 +00:00
|
|
|
$thankLinks.on( 'click', function ( e ) {
|
2018-02-20 14:01:40 +00:00
|
|
|
var $thankLink = $( this );
|
2014-08-06 13:46:53 +00:00
|
|
|
e.preventDefault();
|
|
|
|
sendThanks( $thankLink, $thankLink );
|
|
|
|
} );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
2016-08-25 15:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $.isReady ) {
|
|
|
|
// This condition is required for soft-reloads
|
|
|
|
// to also trigger the reloadThankedState
|
|
|
|
reloadThankedState();
|
|
|
|
} else {
|
2017-01-06 13:13:38 +00:00
|
|
|
$( reloadThankedState );
|
2016-08-25 15:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$( function () {
|
|
|
|
addActionToLinks( $( 'body' ) );
|
2014-02-26 02:12:47 +00:00
|
|
|
} );
|
|
|
|
|
2016-09-13 07:51:02 +00:00
|
|
|
mw.hook( 'wikipage.diff' ).add( function ( $content ) {
|
|
|
|
addActionToLinks( $content );
|
|
|
|
} );
|
2018-11-12 19:22:46 +00:00
|
|
|
}() );
|