<commit_before>;(function($){
	
	var contests = window.localtv_contests = [],
		ContestWidget = function (element) {
			this.element = element;
			this.init();
		};
	
	// Define ContestWidget methods.
	ContestWidget.prototype = {
		init: function () {
			// Push this to the registry of contest widgets.
			window.localtv_contests.push(this);
			// Wipe the widget's current text.
			this.element.html('')
			// Create voting buttons.
			this.upvoteElement = $('<button class="upvote" value="1">Vote Up</button>').appendTo(this.element);
			if (this.data('downvotes') === 1) this.downvoteElement = $('<button class="downvote" value="-1">Vote Down</button>').appendTo(this.element);
			// Bind the click event.
			this.bindClick();
			// Request the initial state of the votes.
			this.requestState();
		},
		bindClick: function () {
			var this_ = this;
			// Bind the click event.
			this.element.on('click.localtv.contest', 'button', function (event) { this_.clickHandler(event, this); })
		},
		unbindClick: function () {
			// Unbind the click event.
			this.element.off('click.localtv.contest', 'button');
		},
		data: function (arg) {
			// Shortcut to the element's data.
			return this.element.data(arg);
		},
		requestState: function () {
			// Triggers an AJAX request for the state of this user's votes on the contest.
			// Request completion triggers `this.receiveState`.
			var this_ = this;
			this.startLoading();
			$.getJSON(this.data('contestvote-list-uri'),
				{
					'user': this.data('user'),
					'contestvideo': this.data('contestvideo'),
				},
				function(data){
					this_.receiveState(data['objects'][0]);
				}
			);
		},
		receiveState: function (data) {
			// Receives a contestvote object and updates the state of the buttons to reflect it.
			this.endLoading();
			// Cache the current vote value.
			if (data === "") data = undefined; // Empty strings... can't live with them, can't live without them.
			this.voteData = data;
			console.log(this.voteData);
			// Clear the current state of buttons, regardless.
			this.element.find('.button-selected').removeClass('button-selected');
			// If the vote is undefined, stop here.
			if (this.voteData === undefined) return;
			// Update the button states.
			switch (this.voteData.vote) {
				case -1: // downvote
					this.downvoteElement.addClass('button-selected');
					break;
				case 1: // upvote
					this.upvoteElement.addClass('button-selected');
					break;
			}
		},
		sendVote: function (vote) {
			// Sends an AJAX request to delete, create, or update a contestvote.
			// When the request completes, it triggers `this.receiveState`.
			var this_ = this,
				request_uri, request_options = {};
			
			if (vote === 0) {
				// Short-circuit if the vote matches the current state.
				if (this.voteData === undefined) return;
				// Otherwise, prepare a DELETE request.
				request_uri = this.voteData['resource_uri'];
				request_options.type = "DELETE";
			} else if (this.voteData == undefined) {
				// Prepare to POST a request to create a vote.
				request_uri = this.data('contestvote-list-uri')
				request_options.type = "POST";
			} else {
				// Prepate to PUT a request to update a vote.
				request_uri = this.voteData['resource_uri'];
				request_options.type = "PUT";
			}
			// Prepare the rest of the request.
			request_options.data = JSON.stringify({
					'contestvideo': this.data('contestvideo-detail-uri'),
					'user': this.data('user-detail-uri'),
					'vote': vote,
				});
			request_options.contentType = "application/json";
			request_options.processData = false;
			request_options.success = function (data) { this_.receiveState(data) };
			// Send the request.
			this.startLoading();
			$.ajax(request_uri, request_options);
		},
		clickHandler: function (event, button) {
			var $button = $(button);
			// If the button already is active, take this as switching to a vote for 0.
			if ($button.hasClass('button-selected')){
				this.sendVote(0);<commit_msg>Added vote counts to the voting widget buttons.<commit_after>;(function($){
	
	var contests = window.localtv_contests = [],
		ContestWidget = function (element) {
			this.element = element;
			this.init();
		};
	
	// Define ContestWidget methods.
	ContestWidget.prototype = {
		init: function () {
			// Push this to the registry of contest widgets.
			window.localtv_contests.push(this);
			// Wipe the widget's current text.
			this.element.html('')
			// Create voting buttons.
			this.upvoteElement = $('<button class="upvote" value="1">Vote Up (<span class="count"></span>)</button>').appendTo(this.element);
			this.upvoteCountElement = this.upvoteElement.find('.count').eq(0);
			if (this.data('downvotes') === 1){
				this.downvoteElement = $('<button class="downvote" value="-1">Vote Down (<span class="count"></span>)</button>').appendTo(this.element);
				this.downvoteCountElement = this.downvoteElement.find('.count').eq(0);
			}
			// Bind the click event.
			this.bindClick();
			// Request the initial state of the votes.
			this.requestState();
		},
		bindClick: function () {
			var this_ = this;
			// Bind the click event.
			this.element.on('click.localtv.contest', 'button', function (event) { this_.clickHandler(event, this); })
		},
		unbindClick: function () {
			// Unbind the click event.
			this.element.off('click.localtv.contest', 'button');
		},
		data: function (arg) {
			// Shortcut to the element's data.
			return this.element.data(arg);
		},
		requestState: function () {
			// Triggers an AJAX request for the state of this user's votes on the contest
			// and the state of *all* votes on the contest.
			// Request completion triggers `this.receiveState`.
			var this_ = this,
				user_contest_vote_data = {'user': this.data('user'), 'contestvideo': this.data('contestvideo')},
				all_contest_vote_data = {'contestvideo': this.data('contestvideo')};
				
			this.startLoading();
			$.getJSON(this.data('contestvote-list-uri'), all_contest_vote_data, function (a){
				this_.receiveState(a);
			})
		},
		receiveState: function(contest_vote_data) {
			// Receives contest vote data, parses it out into the user vote, upvotes, and downvotes, then triggers the appropriate functions for displaying the data.
			var this_ = this,
				// find the current user's vote
				user_contest_vote = $.grep(contest_vote_data['objects'], function (item, index) { return (item['user'] === this_.data('user-detail-uri')); })[0],
				// find upvotes and downvotes
				upvotes = $.grep(contest_vote_data['objects'], function (item, index) { return (item['vote'] === 1); }),
				downvotes = $.grep(contest_vote_data['objects'], function (item, index) { return (item['vote'] === -1); });
			this.receiveUserVote(user_contest_vote);
			this.receiveVotes(upvotes, downvotes);
			this.endLoading();
		},
		receiveUserVote: function (data) {
			// Receives a contestvote object and updates the state of the buttons to reflect it.
			// Cache the current vote value.
			if (data === "") data = undefined; // Empty strings... can't live with them, can't live without them.
			this.voteData = data;
			// Clear the current state of buttons, regardless.
			this.element.find('.button-selected').removeClass('button-selected');
			// If the vote is undefined, stop here.
			if (this.voteData === undefined) return;
			// Update the button states.
			switch (this.voteData.vote) {
				case -1: // downvote
					this.downvoteElement.addClass('button-selected');
					break;
				case 1: // upvote
					this.upvoteElement.addClass('button-selected');
					break;
			}
		},
		receiveVotes: function (upvotes, downvotes) {
			// Updates the counts on the HTML elements.
			this.upvoteCountElement.html(upvotes.length);
			if (this.data('downvotes') === 1) this.downvoteCountElement.html(downvotes.length);
		},
		sendVote: function (vote) {
			// Sends an AJAX request to delete, create, or update a contestvote.
			// When the request completes, it triggers `this.requestState`.
			var this_ = this,
				request_uri, request_options = {};
			
			if (vote === 0) {
				// Short-circuit if the vote matches the current state.
				if (this.voteData === undefined) return;
				// Otherwise, prepare a DELETE request.
				request_uri = this.voteData['resource_uri'];
				request_options.type = "DELETE";
			} else if (this.voteData == undefined) {
				// Prepare to POST a request to create a vote.
				request_uri = this.data('contestvote-list-uri')
				request_options.type = "POST";
			} else {
				// Prepate to PUT a request to update a vote.
				request_uri = this.voteData['resource_uri'];
				request_options.type = "PUT";
			}
			// Prepare the rest of the request.
			request_options.data = JSON.stringify({
					'contestvideo': this.data('contestvideo-detail-uri'),
					'user': this.data('user-detail-uri'),
					'vote': vote,
				});
			request_options.contentType = "application/json";
			request_options.processData = false;
			request_options.success = function (data) { this_.requestState() };
			// Send the request.
			this.startLoading();
			$.ajax(request_uri, request_options);
		},
		clickHandler: function (event, button) {
			var $button = $(button);
			// If the button already is active, take this as switching to a vote for 0.
			if ($button.hasClass('button-selected')){
				this.sendVote(0);
{'fraction_non_alphanumeric': 0.14496024070492156, 'fraction_numerical': 0.0024715237481194928, 'mean_word_length': 3.3694835680751174, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3226596', 'n_tokens_mistral': 3405, 'n_tokens_neox': 2842, 'n_words': 775}