1
0
Fork 0
mirror of https://bitbucket.org/vertlach/iusethis.git synced 2024-05-18 17:08:29 +03:00
mediawiki-iusethis/IUT.js

102 lines
2.8 KiB
JavaScript

/**
* JavaScript functions for Vote extension.
*
* TODO: Should refactor this into a jQuery widget.
* The widget should get a PageID in its constructor so it can work on any page
* for any page and with multiple instances per page.
*
* @constructor
*
* @author Jack Phoenix <jack@countervandalism.net>
* @author Daniel A. R. Werner < daniel.a.r.werner@gmail.com >
*/
var IUT = function IUT() {
this.clearRatingTimer = null;
this.voted_new = [];
this.id = 0;
this.last_id = 0;
this.imagePath = mw.config.get( 'wgExtensionAssetsPath' ) + '/VoteNY/images/';
/**
* Called when voting through the green square voting box
*
* @param TheVote
* @param PageID Integer: internal ID number of the current article
*/
this.clickVote = function( TheVote, PageID ) {
( new mw.Api() ).postWithToken( 'edit', {
action: 'iusethis',
format: 'json',
what: 'vote',
pageId: PageID,
voteValue: TheVote
} ).done( function( data ) {
$( '#PollVotes' ).html( data.iusethis.result );
$( '#Answer' ).html(
'<a href="javascript:void(0);" class="vote-unvote-link">' +
mediaWiki.msg( 'iusethis-unvote-link' ) + '</a>'
);
} );
};
/**
* Called when removing your vote through the green square voting box
*
* @param PageID Integer: internal ID number of the current article
*/
this.unVote = function( PageID ) {
( new mw.Api() ).postWithToken( 'edit', {
action: 'voteny',
format: 'json',
what: 'delete',
pageId: PageID
} ).done( function( data ) {
$( '#PollVotes' ).html( data.iusethis.result );
$( '#Answer' ).html(
'<a href="javascript:void(0);" class="vote-vote-link">' +
mediaWiki.msg( 'iusethis-link' ) + '</a>'
);
} );
};
this.startClearRating = function( id, rating, voted ) {
var voteNY = this;
this.clearRatingTimer = setTimeout( function() {
voteNY.clearRating( id, 0, rating, voted );
}, 200 );
};
this.clearRating = function( id, num, prev_rating, voted ) {
if ( this.voted_new[id] ) {
voted = this.voted_new[id];
}
};
this.updateRating = function( id, num, prev_rating ) {
if ( this.clearRatingTimer && this.last_id == id ) {
clearTimeout( this.clearRatingTimer );
}
this.clearRating( id, num, prev_rating );
for ( var x = 1; x <= num; x++ ) {
$( '#rating_' + id + '_' + x ).attr( 'src', this.imagePath + 'star_voted.gif' );
}
this.last_id = id;
};
};
// TODO: Make event handlers part of a widget as described in the VoteNY's TODO and reduce this
// code to instantiating such a widget for the current wiki page if required.
$( function() {
var vote = new IUT();
// Green voting box's link
$( '.vote-action' ).on( 'click', '> a', function( event ) {
if ( $( this ).hasClass( 'vote-unvote-link' ) ) {
vote.unVote( mw.config.get( 'wgArticleId' ) );
} else {
vote.clickVote( 1, mw.config.get( 'wgArticleId' ) );
}
} );
} );