mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Thanks
synced 2024-12-24 12:13:01 +00:00
98726b568b
bug: 61930 Change-Id: Id37a14e3c75c63419fc34e0b7c2e21f74b3fa875 Co-authored-by: Bencmq <bencmqwiki@gmail.com>
95 lines
2.4 KiB
JavaScript
95 lines
2.4 KiB
JavaScript
( function ( $, mw ) {
|
|
'use strict';
|
|
|
|
var reloadThankedState = function() {
|
|
$( 'a.mw-thanks-thank-link' ).each( function( idx, el ) {
|
|
var $thankLink = $( el );
|
|
if ( mw.thanks.thanked.contains( $thankLink ) ) {
|
|
$thankLink.before( mw.msg( 'thanks-thanked' ) );
|
|
$thankLink.remove();
|
|
}
|
|
} );
|
|
};
|
|
|
|
var confirmThanks = function( $thankLink ) {
|
|
var recipient = $thankLink.parent().find( '.mw-userlink' ).text();
|
|
if ( recipient === '' ) { // for Diff view
|
|
recipient = $thankLink.parents( '.diff-ntitle' ).find( '.mw-userlink' ).text();
|
|
}
|
|
var $dialog = $( '<div>' ).msg( 'thanks-confirmation', mw.user, recipient );
|
|
$dialog.dialog( {
|
|
autoOpen: false,
|
|
width: 400,
|
|
modal: true,
|
|
resizable: false,
|
|
buttons: [
|
|
{
|
|
text: mw.msg( 'ok' ),
|
|
'class': 'ui-button-green',
|
|
click: function() {
|
|
$( this ).dialog( "close" );
|
|
sendThanks( $thankLink );
|
|
}
|
|
},
|
|
{
|
|
text: mw.msg( 'cancel' ),
|
|
click: function() { $( this ).dialog( "close" ); }
|
|
}
|
|
]
|
|
} );
|
|
$dialog.dialog( 'open' );
|
|
};
|
|
|
|
var sendThanks = function( $thankLink ) {
|
|
var source;
|
|
if ( mw.config.get( 'wgAction' ) === 'history' ) {
|
|
source = 'history';
|
|
} else {
|
|
source = 'diff';
|
|
}
|
|
( new mw.Api ).get( {
|
|
'action' : 'thank',
|
|
'rev' : $thankLink.attr( 'data-revision-id' ),
|
|
'source' : source,
|
|
'token' : mw.user.tokens.values.editToken
|
|
} )
|
|
.done( function( data ) {
|
|
$thankLink.before( mw.message( 'thanks-thanked', mw.user ).escaped() );
|
|
$thankLink.remove();
|
|
mw.thanks.thanked.push( $thankLink );
|
|
} )
|
|
.fail( function( errorCode, details ) {
|
|
// TODO: use something besides alert for the error messages
|
|
switch( errorCode ) {
|
|
case 'invalidrevision':
|
|
alert( mw.msg( 'thanks-error-invalidrevision' ) );
|
|
break;
|
|
case 'ratelimited':
|
|
alert( mw.msg( 'thanks-error-ratelimited', mw.user ) );
|
|
break;
|
|
default:
|
|
alert( mw.msg( 'thanks-error-undefined' ) );
|
|
}
|
|
} );
|
|
};
|
|
|
|
if ( $.isReady ) {
|
|
// This condition is required for soft-reloads
|
|
// to also trigger the reloadThankedState
|
|
reloadThankedState();
|
|
} else {
|
|
$( document ).ready( reloadThankedState );
|
|
}
|
|
|
|
$( 'a.mw-thanks-thank-link' ).click( function( e ) {
|
|
var $thankLink = $( this );
|
|
e.preventDefault();
|
|
if ( mw.config.get( 'thanks-confirmation-required' ) ) {
|
|
confirmThanks( $thankLink );
|
|
} else {
|
|
sendThanks( $thankLink );
|
|
}
|
|
} );
|
|
|
|
} )( jQuery, mediaWiki );
|