2015-06-16 03:02:29 +00:00
|
|
|
( function ( mw, M, $ ) {
|
|
|
|
var popup = M.require( 'toast' ),
|
2015-01-10 02:09:41 +00:00
|
|
|
SchemaMobileWebClickTracking = M.require( 'loggingSchemas/SchemaMobileWebClickTracking' ),
|
|
|
|
schema = new SchemaMobileWebClickTracking( {}, 'MobileWebDiffClickTracking' );
|
2013-09-26 01:16:56 +00:00
|
|
|
|
2015-01-15 00:14:12 +00:00
|
|
|
/**
|
|
|
|
* Create a thank button for a given edit
|
|
|
|
*
|
|
|
|
* @param {String} name The username of the user who made the edit
|
|
|
|
* @param {String} revision The revision the user created
|
|
|
|
* @param {String} gender The gender of the user who made the edit
|
|
|
|
*/
|
2013-09-26 01:16:56 +00:00
|
|
|
function thankUser( name, revision, gender ) {
|
|
|
|
var d = $.Deferred();
|
2015-06-16 03:02:29 +00:00
|
|
|
( new mw.Api() ).postWithToken( 'edit', {
|
|
|
|
action: 'thank',
|
|
|
|
rev: revision,
|
|
|
|
source: 'mobilediff'
|
|
|
|
} ).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', gender ) );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
popup.show( mw.msg( 'thanks-error-undefined' ) );
|
|
|
|
}
|
|
|
|
d.reject();
|
2013-09-26 01:16:56 +00:00
|
|
|
} );
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a thank button for a given edit
|
|
|
|
*
|
2015-01-15 00:14:12 +00:00
|
|
|
* @param {String} name The username of the user who made the edit
|
|
|
|
* @param {String} rev The revision the user created
|
|
|
|
* @param {String} gender The gender of the user who made the edit
|
2013-09-26 01:16:56 +00:00
|
|
|
*/
|
|
|
|
function createThankLink( name, rev, gender ) {
|
2014-01-15 20:07:17 +00:00
|
|
|
var thankImg = mw.config.get( 'wgExtensionAssetsPath' ) + '/Thanks/WhiteSmiley.png',
|
|
|
|
thankImgTag = '<img width="25" height="20" src="' + thankImg + '" class="mw-mf-action-button-icon"/>',
|
|
|
|
$thankBtn;
|
2013-09-26 01:16:56 +00:00
|
|
|
// Don't make thank button for self
|
|
|
|
if ( name !== mw.config.get( 'wgUserName' ) ) {
|
2014-01-15 20:07:17 +00:00
|
|
|
// See if user has already been thanked for this edit
|
|
|
|
if ( mw.config.get( 'wgThanksAlreadySent' ) ) {
|
2014-03-04 00:39:13 +00:00
|
|
|
$thankBtn = $( '<button class="mw-mf-action-button mw-ui-button mw-ui-constructive thanked">' )
|
2015-01-30 18:12:52 +00:00
|
|
|
.prop( 'disabled', true )
|
2014-01-15 20:07:17 +00:00
|
|
|
.html( thankImgTag + mw.message( 'thanks-button-thanked', mw.user ).escaped() );
|
|
|
|
} else {
|
2014-03-04 00:39:13 +00:00
|
|
|
$thankBtn = $( '<button class="mw-mf-action-button mw-ui-button mw-ui-constructive">' )
|
2015-04-22 23:21:27 +00:00
|
|
|
.html( thankImgTag + mw.message( 'thanks-button-thank', mw.user, gender ).escaped()
|
2014-01-15 20:07:17 +00:00
|
|
|
)
|
2015-05-04 19:13:43 +00:00
|
|
|
.on( 'click', function () {
|
2014-01-15 20:07:17 +00:00
|
|
|
var $this = $( this );
|
2015-01-10 02:09:41 +00:00
|
|
|
schema.log( {
|
|
|
|
name: 'thank',
|
|
|
|
destination: name
|
|
|
|
} );
|
2014-01-15 20:07:17 +00:00
|
|
|
if ( !$this.hasClass( 'thanked' ) ) {
|
2015-05-04 19:13:43 +00:00
|
|
|
thankUser( name, rev, gender ).done( function () {
|
2015-01-30 18:12:52 +00:00
|
|
|
$this.addClass( 'thanked' ).prop( 'disabled', true )
|
2015-04-22 23:21:27 +00:00
|
|
|
.html( thankImgTag + mw.message( 'thanks-button-thanked', mw.user, gender ).escaped() );
|
2014-01-15 20:07:17 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
return $thankBtn;
|
2013-09-26 01:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:14:12 +00:00
|
|
|
/**
|
|
|
|
* Initialise a thank button in the given container.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Object} $user existing element with data attributes associated describing a user.
|
|
|
|
* @param {jQuery.Object} $container to render button in
|
|
|
|
*/
|
|
|
|
function init( $user, $container ) {
|
|
|
|
var username = $user.data( 'user-name' ),
|
2013-09-26 01:16:56 +00:00
|
|
|
rev = $user.data( 'revision-id' ),
|
|
|
|
gender = $user.data( 'user-gender' ),
|
|
|
|
$thankBtn;
|
|
|
|
|
|
|
|
if ( !$user.hasClass( 'mw-mf-anon' ) ) {
|
|
|
|
$thankBtn = createThankLink( username, rev, gender );
|
|
|
|
if ( $thankBtn ) {
|
2015-01-15 00:14:12 +00:00
|
|
|
$thankBtn.prependTo( $container );
|
2013-09-26 01:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 00:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$( function () {
|
|
|
|
init( $( '.mw-mf-user' ), $( '#mw-mf-userinfo' ) );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Expose for testing purposes
|
|
|
|
mw.thanks = $.extend( {}, mw.thanks || {}, {
|
|
|
|
_mobileDiffInit: init
|
2013-09-26 01:16:56 +00:00
|
|
|
} );
|
2015-06-16 03:02:29 +00:00
|
|
|
} )( mediaWiki, mw.mobileFrontend, jQuery );
|