mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 21:56:50 -04:00
56 lines
2.1 KiB
JavaScript
56 lines
2.1 KiB
JavaScript
// Copyright 2014 Team 254. All Rights Reserved.
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
//
|
|
// Client-side methods for editing a match in the match review page.
|
|
|
|
var scoreTemplate = Handlebars.compile($("#scoreTemplate").html());
|
|
var allianceResults = {};
|
|
|
|
// Hijack the form submission to inject the data in JSON form so that it's easier for the server to parse.
|
|
$("form").submit(function() {
|
|
updateResults("red");
|
|
updateResults("blue");
|
|
|
|
var redScoreJson = JSON.stringify(allianceResults["red"].score);
|
|
var blueScoreJson = JSON.stringify(allianceResults["blue"].score);
|
|
|
|
// Inject the JSON data into the form as hidden inputs.
|
|
$("<input />").attr("type", "hidden").attr("name", "redScoreJson").attr("value", redScoreJson).appendTo("form");
|
|
$("<input />").attr("type", "hidden").attr("name", "blueScoreJson").attr("value", blueScoreJson).appendTo("form");
|
|
|
|
return true;
|
|
});
|
|
|
|
// Draws the match-editing form for one alliance based on the cached result data.
|
|
var renderResults = function(alliance) {
|
|
var result = allianceResults[alliance];
|
|
var scoreContent = scoreTemplate(result);
|
|
$("#" + alliance + "Score").html(scoreContent);
|
|
|
|
getInputElement(alliance, "AutoPoints").val(result.score.AutoPoints);
|
|
getInputElement(alliance, "TeleopPoints").val(result.score.TeleopPoints);
|
|
getInputElement(alliance, "EndgamePoints").val(result.score.EndgamePoints);
|
|
};
|
|
|
|
// Converts the current form values back into JSON structures and caches them.
|
|
var updateResults = function(alliance) {
|
|
var result = allianceResults[alliance];
|
|
var formData = {};
|
|
$.each($("form").serializeArray(), function(k, v) {
|
|
formData[v.name] = v.value;
|
|
});
|
|
|
|
result.score.AutoPoints = parseInt(formData[alliance + "AutoPoints"]);
|
|
result.score.TeleopPoints = parseInt(formData[alliance + "TeleopPoints"]);
|
|
result.score.EndgamePoints = parseInt(formData[alliance + "EndgamePoints"]);
|
|
};
|
|
|
|
// Returns the form input element having the given parameters.
|
|
var getInputElement = function(alliance, name, value) {
|
|
var selector = "input[name=" + alliance + name + "]";
|
|
if (value !== undefined) {
|
|
selector += "[value=" + value + "]";
|
|
}
|
|
return $(selector);
|
|
};
|