2018-11-12 19:22:46 +00:00
|
|
|
( function () {
|
2024-04-19 08:37:25 +00:00
|
|
|
const $thankedLabel = $( '<span>' ).addClass( 'mw-thanks-flow-thanked mw-ui-quiet' );
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2024-04-19 08:37:25 +00:00
|
|
|
const attrName = 'data-flow-id';
|
|
|
|
const cookieName = 'flow-thanked';
|
2016-04-22 19:47:08 +00:00
|
|
|
|
2016-01-29 19:28:20 +00:00
|
|
|
function findPostAuthorFromThankLink( $thankLink ) {
|
|
|
|
// We can't use 'closest' directly because .flow-author is a cousin
|
|
|
|
// of $thankLink rather than its ancestor
|
2016-04-22 19:47:08 +00:00
|
|
|
return $( $thankLink.findWithParent( '< .flow-post .flow-author a.mw-userlink' )[ 0 ] ).text().trim();
|
2016-01-29 19:28:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 19:13:43 +00:00
|
|
|
function reloadThankedState() {
|
2024-06-07 09:00:28 +00:00
|
|
|
$( 'a.mw-thanks-flow-thank-link' ).each( ( idx, el ) => {
|
2024-04-19 08:37:25 +00:00
|
|
|
const $thankLink = $( el );
|
2022-01-10 12:54:51 +00:00
|
|
|
if ( mw.thanks.thanked.contains( $thankLink.closest( '.flow-post' ).attr( attrName ), cookieName ) ) {
|
2024-04-19 08:37:25 +00:00
|
|
|
const author = findPostAuthorFromThankLink( $thankLink );
|
2015-04-22 23:21:27 +00:00
|
|
|
mw.thanks.getUserGender( author )
|
2024-06-07 09:00:28 +00:00
|
|
|
.done( ( recipientGender ) => {
|
2015-04-22 23:21:27 +00:00
|
|
|
$thankLink.before(
|
|
|
|
$thankedLabel
|
|
|
|
.clone()
|
|
|
|
.append(
|
|
|
|
mw.msg( 'thanks-button-thanked', mw.user, recipientGender )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$thankLink.remove();
|
|
|
|
} );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
} );
|
2015-05-04 19:13:43 +00:00
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2015-05-04 19:13:43 +00:00
|
|
|
function sendFlowThanks( $thankLink ) {
|
2016-05-26 07:02:17 +00:00
|
|
|
( new mw.Api() ).postWithToken( 'csrf', {
|
2015-05-04 19:13:43 +00:00
|
|
|
action: 'flowthank',
|
2022-02-03 20:06:08 +00:00
|
|
|
postid: $thankLink.closest( '.flow-post' ).attr( attrName )
|
2014-02-26 02:12:47 +00:00
|
|
|
} )
|
2017-08-03 23:05:54 +00:00
|
|
|
.then(
|
|
|
|
// Success
|
2024-06-07 09:00:28 +00:00
|
|
|
() => {
|
2024-04-19 08:37:25 +00:00
|
|
|
const author = findPostAuthorFromThankLink( $thankLink );
|
2017-08-03 23:05:54 +00:00
|
|
|
// Get the user who was thanked (for gender purposes)
|
|
|
|
return mw.thanks.getUserGender( author );
|
|
|
|
},
|
|
|
|
// Failure
|
2024-06-07 09:00:28 +00:00
|
|
|
( errorCode ) => {
|
2017-08-03 23:05:54 +00:00
|
|
|
switch ( errorCode ) {
|
|
|
|
case 'ratelimited':
|
|
|
|
OO.ui.alert( mw.msg( 'thanks-error-ratelimited', mw.user ) );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OO.ui.alert( mw.msg( 'thanks-error-undefined', errorCode ) );
|
|
|
|
}
|
2015-04-22 23:21:27 +00:00
|
|
|
}
|
2017-08-03 23:05:54 +00:00
|
|
|
)
|
2024-06-07 09:00:28 +00:00
|
|
|
.then( ( recipientGender ) => {
|
2024-04-19 08:37:25 +00:00
|
|
|
const $thankUserLabel = $thankedLabel.clone();
|
2017-08-03 23:05:54 +00:00
|
|
|
$thankUserLabel.append(
|
|
|
|
mw.msg( 'thanks-button-thanked', mw.user, recipientGender )
|
|
|
|
);
|
2022-01-10 12:54:51 +00:00
|
|
|
mw.thanks.thanked.push( $thankLink.closest( '.flow-post' ).attr( attrName ), cookieName );
|
2017-08-03 23:05:54 +00:00
|
|
|
$thankLink.before( $thankUserLabel );
|
|
|
|
$thankLink.remove();
|
|
|
|
} );
|
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 {
|
2017-01-06 13:13:38 +00:00
|
|
|
$( reloadThankedState );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 07:31:10 +00:00
|
|
|
// .on() is needed to make the button work for dynamically loaded posts
|
2015-05-04 19:13:43 +00:00
|
|
|
$( '.flow-board' ).on( 'click', 'a.mw-thanks-flow-thank-link', function ( e ) {
|
2024-04-19 08:37:25 +00:00
|
|
|
const $thankLink = $( this );
|
2014-02-26 02:12:47 +00:00
|
|
|
e.preventDefault();
|
|
|
|
sendFlowThanks( $thankLink );
|
|
|
|
} );
|
|
|
|
|
2018-11-12 19:22:46 +00:00
|
|
|
}() );
|