2014-02-26 02:12:47 +00:00
|
|
|
( function ( $, mw ) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var $thankedLabel = $( '<span></span>' )
|
2014-07-14 19:08:43 +00:00
|
|
|
.addClass( 'mw-thanks-flow-thanked mw-ui-quiet' );
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2016-04-22 19:47:08 +00:00
|
|
|
mw.thanks.thanked.cookieName = 'flow-thanked';
|
|
|
|
mw.thanks.thanked.attrName = 'data-flow-id';
|
|
|
|
|
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() {
|
|
|
|
$( 'a.mw-thanks-flow-thank-link' ).each( function ( idx, el ) {
|
2015-04-22 23:21:27 +00:00
|
|
|
var $thankLink = $( el ),
|
2016-01-29 19:28:20 +00:00
|
|
|
author = findPostAuthorFromThankLink( $thankLink );
|
2014-07-14 19:08:43 +00:00
|
|
|
if ( mw.thanks.thanked.contains( $thankLink.closest( '.flow-post' ) ) ) {
|
2015-04-22 23:21:27 +00:00
|
|
|
mw.thanks.getUserGender( author )
|
|
|
|
.done( function ( recipientGender ) {
|
|
|
|
$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',
|
|
|
|
postid: $thankLink.closest( '.flow-post' ).attr( mw.thanks.thanked.attrName )
|
2014-02-26 02:12:47 +00:00
|
|
|
} )
|
2015-04-22 23:21:27 +00:00
|
|
|
.then(
|
|
|
|
// Success
|
|
|
|
function ( data ) {
|
2016-01-29 19:28:20 +00:00
|
|
|
var author = findPostAuthorFromThankLink( $thankLink );
|
2015-04-22 23:21:27 +00:00
|
|
|
// Get the user who was thanked (for gender purposes)
|
|
|
|
return mw.thanks.getUserGender( author );
|
|
|
|
},
|
|
|
|
// Failure
|
|
|
|
function ( errorCode, details ) {
|
|
|
|
// TODO: use something besides alert for the error messages
|
|
|
|
switch ( errorCode ) {
|
|
|
|
case 'ratelimited':
|
|
|
|
alert( mw.msg( 'thanks-error-ratelimited', mw.user ) );
|
|
|
|
break;
|
|
|
|
default:
|
2016-03-29 21:55:29 +00:00
|
|
|
alert( mw.msg( 'thanks-error-undefined', errorCode ) );
|
2015-04-22 23:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then( function ( recipientGender ) {
|
|
|
|
var $thankUserLabel = $thankedLabel.clone();
|
|
|
|
$thankUserLabel.append(
|
|
|
|
mw.msg( 'thanks-button-thanked', mw.user, recipientGender )
|
|
|
|
);
|
2014-07-14 19:08:43 +00:00
|
|
|
mw.thanks.thanked.push( $thankLink.closest( '.flow-post' ) );
|
2015-04-22 23:21:27 +00:00
|
|
|
$thankLink.before( $thankUserLabel );
|
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
|
|
|
|
|
|
|
if ( $.isReady ) {
|
|
|
|
// This condition is required for soft-reloads
|
|
|
|
// to also trigger the reloadThankedState
|
|
|
|
reloadThankedState();
|
|
|
|
} else {
|
|
|
|
$( document ).ready( reloadThankedState );
|
|
|
|
}
|
|
|
|
|
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 ) {
|
2014-02-26 02:12:47 +00:00
|
|
|
var $thankLink = $( this );
|
|
|
|
e.preventDefault();
|
|
|
|
sendFlowThanks( $thankLink );
|
|
|
|
} );
|
|
|
|
|
|
|
|
} )( jQuery, mediaWiki );
|