mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Thanks
synced 2024-11-15 19:09:52 +00:00
078875980e
This makes the Thank button function correctly for posts that are dynamically loaded with JavaScript (from http://stackoverflow.com/q/9344306) Bug: 63205 Change-Id: If51e1801e633573c8f0814832e26d643e1237d53
60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
( function ( $, mw ) {
|
|
'use strict';
|
|
|
|
mw.thanks.thanked.cookieName = 'flow-thanked';
|
|
mw.thanks.thanked.attrName = 'data-post-id';
|
|
|
|
var $thankedLabel = $( '<span></span>' )
|
|
.append( mw.msg( 'thanks-button-thanked', mw.user ) )
|
|
.addClass( 'mw-thanks-flow-thanked mw-ui-button mw-ui-quiet mw-ui-disabled' );
|
|
|
|
var reloadThankedState = function() {
|
|
$( 'a.mw-thanks-flow-thank-link' ).each( function( idx, el ) {
|
|
var $thankLink = $( el );
|
|
if ( mw.thanks.thanked.contains( $thankLink ) ) {
|
|
$thankLink.before( $thankedLabel.clone() );
|
|
$thankLink.remove();
|
|
}
|
|
} );
|
|
};
|
|
|
|
var sendFlowThanks = function( $thankLink ) {
|
|
( new mw.Api ).get( {
|
|
'action' : 'flowthank',
|
|
'postid' : $thankLink.attr( 'data-post-id' ),
|
|
'token' : mw.user.tokens.get( 'editToken' )
|
|
} )
|
|
.done( function( data ) {
|
|
$thankLink.before( $thankedLabel.clone() );
|
|
$thankLink.remove();
|
|
mw.thanks.thanked.push( $thankLink );
|
|
} )
|
|
.fail( 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:
|
|
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 );
|
|
}
|
|
|
|
// .on() is needed to make the button work for dynamically loaded posts
|
|
$( '.flow-topics' ).on( 'click', 'a.mw-thanks-flow-thank-link', function( e ) {
|
|
var $thankLink = $( this );
|
|
e.preventDefault();
|
|
sendFlowThanks( $thankLink );
|
|
} );
|
|
|
|
} )( jQuery, mediaWiki );
|