2011-09-14 18:26:09 +03:00
|
|
|
/**
|
2015-03-05 05:36:05 +02:00
|
|
|
* 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
|
2011-09-14 18:26:09 +03:00
|
|
|
*
|
|
|
|
* @author Jack Phoenix <jack@countervandalism.net>
|
2015-03-05 05:36:05 +02:00
|
|
|
* @author Daniel A. R. Werner < daniel.a.r.werner@gmail.com >
|
2011-09-14 18:26:09 +03:00
|
|
|
*/
|
2015-03-05 05:36:05 +02:00
|
|
|
var VoteNY = function VoteNY() {
|
|
|
|
this.MaxRating = 5;
|
|
|
|
this.clearRatingTimer = null;
|
|
|
|
this.voted_new = [];
|
|
|
|
this.id = 0;
|
|
|
|
this.last_id = 0;
|
|
|
|
this.imagePath = mw.config.get( 'wgExtensionAssetsPath' ) + '/VoteNY/images/';
|
2011-09-14 18:26:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when voting through the green square voting box
|
2012-01-04 21:51:44 +02:00
|
|
|
*
|
2011-09-14 18:26:09 +03:00
|
|
|
* @param TheVote
|
|
|
|
* @param PageID Integer: internal ID number of the current article
|
|
|
|
*/
|
2015-03-05 05:36:05 +02:00
|
|
|
this.clickVote = function( TheVote, PageID ) {
|
2011-09-14 18:26:09 +03:00
|
|
|
sajax_request_type = 'POST';
|
2012-01-04 21:51:44 +02:00
|
|
|
sajax_do_call( 'wfVoteClick', [ TheVote, PageID ], function( request ) {
|
2011-09-14 18:26:09 +03:00
|
|
|
document.getElementById( 'PollVotes' ).innerHTML = request.responseText;
|
|
|
|
document.getElementById( 'Answer' ).innerHTML =
|
2012-01-04 21:51:44 +02:00
|
|
|
'<a href="javascript:void(0);" class="vote-unvote-link">' +
|
2012-08-29 19:05:53 +03:00
|
|
|
mediaWiki.msg( 'voteny-unvote-link' ) + '</a>';
|
2011-09-14 18:26:09 +03:00
|
|
|
} );
|
2015-03-05 05:36:05 +02:00
|
|
|
};
|
2011-09-14 18:26:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when removing your vote through the green square voting box
|
2012-01-04 21:51:44 +02:00
|
|
|
*
|
2011-09-14 18:26:09 +03:00
|
|
|
* @param PageID Integer: internal ID number of the current article
|
|
|
|
* @param mk Mixed: random token
|
|
|
|
*/
|
2015-03-05 05:36:05 +02:00
|
|
|
this.unVote = function( PageID ) {
|
2011-09-14 18:26:09 +03:00
|
|
|
sajax_request_type = 'POST';
|
2012-01-04 21:51:44 +02:00
|
|
|
sajax_do_call( 'wfVoteDelete', [ PageID ], function( request ) {
|
2011-09-14 18:26:09 +03:00
|
|
|
document.getElementById( 'PollVotes' ).innerHTML = request.responseText;
|
|
|
|
document.getElementById( 'Answer' ).innerHTML =
|
2012-01-04 21:51:44 +02:00
|
|
|
'<a href="javascript:void(0);" class="vote-vote-link">' +
|
2012-08-29 19:05:53 +03:00
|
|
|
mediaWiki.msg( 'voteny-link' ) + '</a>';
|
2011-09-14 18:26:09 +03:00
|
|
|
} );
|
2015-03-05 05:36:05 +02:00
|
|
|
};
|
2011-09-14 18:26:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when adding a vote after a user has clicked the yellow voting stars
|
2012-01-04 21:51:44 +02:00
|
|
|
*
|
2011-09-14 18:26:09 +03:00
|
|
|
* @param PageID Integer: internal ID number of the current article
|
|
|
|
* @param id Integer: ID of the current rating star
|
|
|
|
* @param action Integer: controls which AJAX function will be called
|
|
|
|
*/
|
2015-03-05 05:36:05 +02:00
|
|
|
this.clickVoteStars = function( TheVote, PageID, id, action ) {
|
|
|
|
this.voted_new[id] = TheVote;
|
2011-09-14 18:26:09 +03:00
|
|
|
var rsfun;
|
|
|
|
if( action == 3 ) {
|
|
|
|
rsfun = 'wfVoteStars';
|
|
|
|
}
|
|
|
|
if( action == 5 ) {
|
|
|
|
rsfun = 'wfVoteStarsMulti';
|
|
|
|
}
|
|
|
|
|
|
|
|
var resultElement = document.getElementById( 'rating_' + id );
|
|
|
|
sajax_request_type = 'POST';
|
2012-01-04 21:51:44 +02:00
|
|
|
sajax_do_call( rsfun, [ TheVote, PageID ], resultElement );
|
2015-03-05 05:36:05 +02:00
|
|
|
};
|
2011-09-14 18:26:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when removing your vote through the yellow voting stars
|
2012-01-04 21:51:44 +02:00
|
|
|
*
|
2011-09-14 18:26:09 +03:00
|
|
|
* @param PageID Integer: internal ID number of the current article
|
|
|
|
* @param id Integer: ID of the current rating star
|
|
|
|
*/
|
2015-03-05 05:36:05 +02:00
|
|
|
this.unVoteStars = function( PageID, id ) {
|
2011-09-14 18:26:09 +03:00
|
|
|
var resultElement = document.getElementById( 'rating_' + id );
|
|
|
|
sajax_request_type = 'POST';
|
2012-01-04 21:51:44 +02:00
|
|
|
sajax_do_call( 'wfVoteStarsDelete', [ PageID ], resultElement );
|
2015-03-05 05:36:05 +02:00
|
|
|
};
|
2011-09-14 18:26:09 +03:00
|
|
|
|
2015-03-05 05:36:05 +02:00
|
|
|
this.startClearRating = function( id, rating, voted ) {
|
|
|
|
this.clearRatingTimer = setTimeout( function() {
|
|
|
|
this.clearRating( id, 0, rating, voted );
|
2012-01-04 21:51:44 +02:00
|
|
|
}, 200 );
|
2015-03-05 05:36:05 +02:00
|
|
|
};
|
2011-09-14 18:26:09 +03:00
|
|
|
|
2015-03-05 05:36:05 +02:00
|
|
|
this.clearRating = function( id, num, prev_rating, voted ) {
|
|
|
|
if( this.voted_new[id] ) {
|
|
|
|
voted = this.voted_new[id];
|
2011-09-14 18:26:09 +03:00
|
|
|
}
|
|
|
|
|
2015-03-05 05:36:05 +02:00
|
|
|
for( var x = 1; x <= this.MaxRating; x++ ) {
|
2011-09-14 18:26:09 +03:00
|
|
|
var star_on, old_rating;
|
|
|
|
if( voted ) {
|
|
|
|
star_on = 'voted';
|
|
|
|
old_rating = voted;
|
|
|
|
} else {
|
|
|
|
star_on = 'on';
|
|
|
|
old_rating = prev_rating;
|
|
|
|
}
|
|
|
|
var ratingElement = document.getElementById( 'rating_' + id + '_' + x );
|
|
|
|
if( !num && old_rating >= x ) {
|
2015-03-05 05:36:05 +02:00
|
|
|
ratingElement.src = this.imagePath + 'star_' + star_on + '.gif';
|
2011-09-14 18:26:09 +03:00
|
|
|
} else {
|
2015-03-05 05:36:05 +02:00
|
|
|
ratingElement.src = this.imagePath + 'star_off.gif';
|
2011-09-14 18:26:09 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-05 05:36:05 +02:00
|
|
|
};
|
2011-09-14 18:26:09 +03:00
|
|
|
|
2015-03-05 05:36:05 +02:00
|
|
|
this.updateRating = function( id, num, prev_rating ) {
|
|
|
|
if( this.clearRatingTimer && this.last_id == id ) {
|
|
|
|
clearTimeout( this.clearRatingTimer );
|
2011-09-14 18:26:09 +03:00
|
|
|
}
|
2015-03-05 05:36:05 +02:00
|
|
|
this.clearRating( id, num, prev_rating );
|
2011-09-14 18:26:09 +03:00
|
|
|
for( var x = 1; x <= num; x++ ) {
|
2015-03-05 05:36:05 +02:00
|
|
|
document.getElementById( 'rating_' + id + '_' + x ).src = this.imagePath + 'star_voted.gif';
|
2011-09-14 18:26:09 +03:00
|
|
|
}
|
2015-03-05 05:36:05 +02:00
|
|
|
this.last_id = id;
|
|
|
|
};
|
2012-01-04 21:51:44 +02:00
|
|
|
};
|
|
|
|
|
2015-03-05 05:36:05 +02:00
|
|
|
// TODO:Mmake 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.
|
2012-01-04 21:51:44 +02:00
|
|
|
jQuery( document ).ready( function() {
|
2015-03-05 05:36:05 +02:00
|
|
|
var vote = new VoteNY();
|
2012-01-04 21:51:44 +02:00
|
|
|
|
2015-03-05 05:36:05 +02:00
|
|
|
// Green voting box's link
|
|
|
|
jQuery( '.vote-action' ).on( 'click', '> a', function( event ) {
|
|
|
|
if( jQuery( this ).hasClass( 'vote-unvote-link' ) ) {
|
|
|
|
vote.unVote( mw.config.get( 'wgArticleId' ) );
|
|
|
|
} else {
|
|
|
|
vote.clickVote( 1, mw.config.get( 'wgArticleId' ) );
|
|
|
|
}
|
2012-01-04 21:51:44 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Rating stars
|
|
|
|
jQuery( 'img.vote-rating-star' ).click( function() {
|
|
|
|
var that = jQuery( this );
|
2015-03-05 05:36:05 +02:00
|
|
|
vote.clickVoteStars(
|
2012-01-04 21:51:44 +02:00
|
|
|
that.data( 'vote-the-vote' ),
|
|
|
|
mw.config.get( 'wgArticleId' ),
|
|
|
|
that.data( 'vote-id' ),
|
|
|
|
that.data( 'vote-action' )
|
|
|
|
);
|
|
|
|
} ).mouseover( function() {
|
|
|
|
var that = jQuery( this );
|
2015-03-05 05:36:05 +02:00
|
|
|
vote.updateRating(
|
2012-01-04 21:51:44 +02:00
|
|
|
that.data( 'vote-id' ),
|
|
|
|
that.data( 'vote-the-vote' ),
|
|
|
|
that.data( 'vote-rating' )
|
|
|
|
);
|
|
|
|
} ).mouseout( function() {
|
|
|
|
var that = jQuery( this );
|
2015-03-05 05:36:05 +02:00
|
|
|
vote.startClearRating(
|
2012-01-04 21:51:44 +02:00
|
|
|
that.data( 'vote-id' ),
|
|
|
|
that.data( 'vote-rating' ),
|
|
|
|
that.data( 'vote-voted' )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Remove vote (rating stars)
|
|
|
|
jQuery( 'a.vote-remove-stars-link' ).click( function() {
|
2015-03-05 05:36:05 +02:00
|
|
|
vote.unVoteStars(
|
2012-01-04 21:51:44 +02:00
|
|
|
mw.config.get( 'wgArticleId' ),
|
|
|
|
jQuery( this ).data( 'vote-id' )
|
|
|
|
);
|
|
|
|
} );
|
2013-12-01 01:19:47 +02:00
|
|
|
} );
|