vgquotes/Src/VideoGameQuotes.Web/media/js/vgquotes.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2011-02-15 00:14:24 +00:00
(function($, window, undefined){
$(document).ready(function() {
var voting = false;
$(".vote-for, .vote-against").live("click", function() {
if (voting) {
alert("Please wait for the current vote to process before voting again");
return false;
}
voting = true;
var $votingLink = $(this);
var $container = $votingLink.parents(".quote-container");
var direction = $votingLink.hasClass("vote-for") ? 1 : 0;
var quoteId = $container.find("input.quote-id").val();
$.ajax("/vote", {
type: "POST",
data: {
QuoteId: quoteId,
Direction: direction
},
success: function(data, status, $xhr) {
if (data.Error !== null) {
alert(data.Error);
return;
}
$container
.find(".quote-score")
.attr("title", "+" + data.Data.upVotes + ", -" + data.Data.downVotes)
.text(data.Data.netVotes);
//remove the voting arrow, and add the other one if needed
$votingLink.remove();
if (direction === 1) {
if ($container.find(".vote-against").length === 0) {
$("<span/>")
.addClass("vote-against")
.attr("title", "I hate this quote")
.text(String.fromCharCode(0x25BC))
.appendTo($container.find(".vote-container:last"));
}
} else {
if ($container.find(".vote-for").length === 0) {
$("<span/>")
.addClass("vote-for")
.attr("title", "I like this quote")
.text(String.fromCharCode(0x25B2))
.appendTo($container.find(".vote-container:first"));
}
}
},
complete: function() { voting = false; }
});
return false;
});
});
}(jQuery, window));