mediawiki-extensions-Thanks/modules/ext.thanks.mobilediff.js
kaldari bd5bdba07e Migrating Mobile thanks into Thanks extension
Also making thank button for mobile capitalized.

Also update for new clickTracking code.

Also promoting to stable.

Dependency: I56e2c5bc69f85e83ab3dfd9b9e617dbb98661870
Change-Id: Ifaf44fe8994a8085c30522292bba8b768da533db
2013-09-26 18:42:13 -07:00

81 lines
2.4 KiB
JavaScript

( function( M, $ ) {
var api = M.require( 'api' ),
popup = M.require( 'notifications' ),
schema = M.require( 'loggingSchemas/MobileWebClickTracking' );
function thankUser( name, revision, gender ) {
var d = $.Deferred();
api.getToken( 'edit' ).done( function( token ) {
api.get( {
'action' : 'thank',
'rev' : revision,
'source' : 'mobilediff',
'token' : token
} )
.done( function() {
popup.show( mw.msg( 'thanks-thanked-notice', name, gender ) );
d.resolve();
} )
.fail( function( errorCode ) {
switch( errorCode ) {
case 'invalidrevision':
popup.show( mw.msg( 'thanks-error-invalidrevision' ) );
break;
case 'ratelimited':
popup.show( mw.msg( 'thanks-error-ratelimited' ) );
break;
default:
popup.show( mw.msg( 'thanks-error-undefined' ) );
}
d.reject();
} );
} );
return d;
}
/**
* Create a thank button for a given edit
*
* @param name String The username of the user who made the edit
* @param rev String The revision the user created
* @param gender String The gender of the user who made the edit
*/
function createThankLink( name, rev, gender ) {
var thankImg = mw.config.get( 'wgExtensionAssetsPath' ) + '/Thanks/WhiteSmiley.png';
// Don't make thank button for self
if ( name !== mw.config.get( 'wgUserName' ) ) {
return $( '<button class="mw-mf-action-button">' )
.html( '<img width="25" height="20" src="' + thankImg + '" class="mw-mf-action-button-icon"/>' +
mw.message( 'thanks-button-thank' ).escaped()
)
.on( 'click', function() {
var $thankLink = $( this );
schema.log( 'diff-thank', name );
if ( !$thankLink.hasClass( 'thanked' ) ) {
thankUser( name, rev, gender ).done( function() {
$thankLink.addClass( 'thanked' ).attr( 'disabled', true );
$thankLink.html( '<img width="25" height="20" src="' + thankImg + '" class="mw-mf-action-button-icon"/>' +
mw.message( 'thanks-button-thanked', mw.user ).escaped()
);
} );
}
} );
}
}
$( function() {
var $user = $( '.mw-mf-user' ),
username = $user.data( 'user-name' ),
rev = $user.data( 'revision-id' ),
gender = $user.data( 'user-gender' ),
$thankBtn;
if ( !$user.hasClass( 'mw-mf-anon' ) ) {
$thankBtn = createThankLink( username, rev, gender );
if ( $thankBtn ) {
$thankBtn.prependTo( '#mw-mf-userinfo' );
}
}
} );
} )( mw.mobileFrontend, jQuery );