mediawiki-extensions-Thanks/modules/ext.thanks.revthank.js
Matthias Mullie 9b7ef1c05b Use postWithToken
Change-Id: I46e27d34bff32b240dc19aa6ef6db1aeddbc3da3
2014-09-19 01:12:12 +00:00

76 lines
2.1 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();
}
} );
};
// $thankLink is the element with the data-revision-id attribute
// $thankElement is the element to be removed on success
var sendThanks = function( $thankLink, $thankElement ) {
var source;
if ( mw.config.get( 'wgAction' ) === 'history' ) {
source = 'history';
} else {
source = 'diff';
}
( new mw.Api ).postWithToken( 'edit', {
'action' : 'thank',
'rev' : $thankLink.attr( 'data-revision-id' ),
'source' : source
} )
.done( function( data ) {
$thankElement.before( mw.message( 'thanks-thanked', mw.user ).escaped() );
$thankElement.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 );
}
$( function() {
if ( mw.config.get( 'thanks-confirmation-required' ) ) {
$( 'a.mw-thanks-thank-link' ).confirmable( {
i18n: { confirm: mw.msg( 'thanks-confirmation2', mw.user ) },
handler: function ( e ) {
var $thankLink = $( this );
e.preventDefault();
sendThanks( $thankLink, $thankLink.closest( '.jquery-confirmable-wrapper' ) );
}
} );
} else {
$( 'a.mw-thanks-thank-link' ).click( function ( e ) {
var $thankLink = $( this );
e.preventDefault();
sendThanks( $thankLink, $thankLink );
} );
}
} );
} )( jQuery, mediaWiki );