diff --git a/js/sr2ini.js b/js/sr2ini.js
index 7189645..a07633f 100644
--- a/js/sr2ini.js
+++ b/js/sr2ini.js
@@ -18,7 +18,7 @@ function sortTable() {
console.log("OK let's sort");
let $table = $("table#combatants");
let $rows = $table.find("tr.combRow").toArray().sort(function(a, b) {
- return $(b).attr("data-ini") - $(a).attr("data-ini");
+ return parseInt($(b).attr("data-ini")) - parseInt($(a).attr("data-ini"));
});
for ( var i = 0; i < $rows.length; i++ ) {
$table.append($rows[i]);
@@ -54,6 +54,64 @@ function handleBlur (e) {
}
}
+// click handler for add buttons
+function handleAddButtonClick (e) {
+
+ // restyle modal
+ $modal = $("#addCombModal");
+ $modal.find(".modal-title").text("Add Combatant");
+ $modal.find("#addModalOkButton").show();
+ $modal.find("#editModalOkButton").hide();
+
+ // add handler for enter key
+ $("#addCombModal input[class*='comb']").off("keydown");
+ $("#addCombModal input[class*='comb']").on("keydown", function (e) {
+ if ( e.which == 13 || e.which == 10 ) {
+ addCombatant();
+ $('#addCombModal').modal("hide");
+ }
+ });
+
+ // show modal
+ $modal.modal("show");
+}
+
+
+// click handler for edit buttons
+function handleEditButtonClick (e) {
+
+ // find current table row
+ let $tr = $(e.target).parents("tr.combRow");
+
+ // restyle modal
+ $modal = $("#addCombModal");
+ $modal.find(".modal-title").text("Edit Combatant");
+ $modal.find("#addModalOkButton").hide();
+ $modal.find("#editModalOkButton").show();
+
+ // populate modal with values from row
+ $modal.find(".combName").val($tr.find(".combName").val());
+ $modal.find(".combDice").val($tr.find(".combDice").val());
+ $modal.find(".combRea").val($tr.find(".combRea").val());
+ $modal.find(".combIni").val($tr.find(".combIni").val());
+
+ // note which row is being edited
+console.log($("tr.combRow").index($tr));
+ $modal.attr("data-row", $("tr.combRow").index($tr));
+
+ // add handler for enter key
+ $("#addCombModal input[class*='comb']").off("keydown");
+ $("#addCombModal input[class*='comb']").on("keydown", function (e) {
+ if ( e.which == 13 || e.which == 10 ) {
+ modifyCombatant();
+ $('#addCombModal').modal("hide");
+ }
+ });
+
+ // show modal
+ $modal.modal("show");
+}
+
// click handler for act buttons
function handleActButtonClick (e) {
@@ -84,7 +142,6 @@ function handleRemoveButtonClick (e) {
// validate a combatant row form by checking for all conditions, including regular HTML5 validation
function validateCombRowValues(tr) {
-// let inputElements = $($tr).find("input[class*='comb']").toArray();
let inputElements = {
name: $(tr).find("input[class*='combName']").get(0),
ini: $(tr).find("input[class*='combIni']").get(0),
@@ -99,13 +156,6 @@ function validateCombRowValues(tr) {
valid = false;
}
})
-/* for (let input in inputElements) {
- if ( ! input.reportValidity() ) {
- valid = false;
- break
- }
- } */
-
if ( ! valid ) {
return false;
}
@@ -132,18 +182,6 @@ function validateCombRowValues(tr) {
return false;
}
- // invalidate if value for ini doesn't align with values for dice+rea
-/* if ( ini != "" && dice != "" && rea != "" ) {
- let d = parseInt(dice);
- let r = parseInt(rea);
- if ( ini < d+r || ini > d*6+r ) {
- inputElements["ini"].setCustomValidity("Impossible to reach initiative " + ini + " with " + dice + "D6+" + rea + ", please adjust ini or leave empty");
- inputElements["ini"].reportValidity();
- inputElements["ini"].setCustomValidity("");
- return false;
- }
- }*/
-
// ok fine
return true;
}
@@ -153,60 +191,6 @@ function validateCombRowValues(tr) {
* Main functions
*/
-/*
-// inserts a combatant table row (in form of a jQuery object) at the correct position in the table
-function insertCombRow($tr) {
-
- let ini = parseInt($tr.find(".combIni").val());
- let $combRows = $("tr.combRow");
-
-console.log($("tbody#sortable"));
-
- // find correct position for new row
-
- // no combatants in table => put new row below the form row
- if ( $combRows.length == 0 ) {
- $("tbody#sortable").append($tr);
- }
-
- // ini is less than the lowest in table => put new row at the end
- else if ( ini < parseInt($combRows.last().find(".combIni").val() ) ) {
- $combRows.last().after($tr);
- }
-
- // ok, compare ini with the ones in table (top to bottom)
- else {
- for ( let i = 0; i < $combRows.length; i++ ) {
- let currentRowIni = parseInt($combRows.eq(i).find(".combIni").val());
-
- // ini larger than the one already in table? insert before that row
- if ( ini > currentRowIni ) {
- $combRows.eq(i).before($tr);
- break;
- }
-
- // ini equal to the one already in table => compare rea
- else if ( ini == currentRowIni ) {
- let rea = parseInt($tr.find(".combRea").val());
- let currentRowRea = parseInt($combRows.eq(i).find(".combRea").val());
-console.log("rea is " + rea + " and currentRowRea is " + currentRowRea);
-
- // one or two values for rea missing, or new row rea larger than current row rea => insert before
- if ( rea != NaN && currentRowRea != NaN && rea > currentRowRea ) {
- $combRows.eq(i).before($tr);
- break;
- }
-
- // we're already at the last row => insert after
- if ( i + 1 == $combRows.length ) {
- $combRows.eq(i).after($tr);
- }
- }
- }
- }
-}
-*/
-
// add new combatant
function addCombatant (e) {
if ( ! validateCombRowValues($("#addCombForm")) ) {
@@ -241,6 +225,7 @@ function addCombatant (e) {
'\n',
'
\n',
'
\n',
+ ' \n',
' \n',
'\n',
'
\n',
@@ -250,8 +235,9 @@ function addCombatant (e) {
));
// add handlers to table row object
- $tr.find("input[class*='comb']").on("blur", handleBlur);
+// $tr.find("input[class*='comb']").on("blur", handleBlur);
// $tr.find("input[type='number']").bind('keyup input change', handleBlur);
+ $tr.find("button.edit-button").on("click", handleEditButtonClick);
$tr.find("button.act-button").on("click", handleActButtonClick);
$tr.find("button.remove-button").on("click", handleRemoveButtonClick);
@@ -263,6 +249,41 @@ function addCombatant (e) {
$("#addCombModal input[class*='comb']").val("");
}
+// modify combatant values
+function modifyCombatant (e) {
+
+ $("#addCombModal").modal("hide");
+
+ // validate form
+ if ( ! validateCombRowValues($("#addCombForm")) ) {
+ return;
+ }
+
+ // get values
+ let name = $("#addCombModal .combName").val().trim();
+ let ini = $("#addCombModal .combIni").val().trim();
+ let dice = $("#addCombModal .combDice").val().trim();
+ let rea = $("#addCombModal .combRea").val().trim();
+
+ // roll for initiative if ini is empty
+ ini = (ini != "") ? ini : rollForInitiative(dice, rea);
+
+ // get correct row
+ let index = parseInt($("#addCombModal").attr("data-row"));
+ $tr = $("tr.combRow").eq(index);
+
+ // set new values
+ $tr.find(".combName").val(name);
+ $tr.find(".combDice").val(dice);
+ $tr.find(".combRea").val(rea);
+ $tr.find(".combIni").val(ini);
+ $tr.attr("data-ini", ini);
+
+ // clean up
+ $("#addCombModal").removeAttr("data-row");
+ $("#addCombModal input[class*='comb']").val("");
+}
+
// start a new combat round
function newRound() {
@@ -296,14 +317,11 @@ function newRound() {
$(document).ready(function(){
// add event handlers to initial buttons and input elements
- $("#addCombModalButton").on("click", addCombatant);
- $("#newRoundOkButton").on("click", newRound);
- $("#addCombModal input[class*='comb']").on("keydown", function (e) {
- if ( e.which == 13 || e.which == 10 ) {
- addCombatant();
- $('#addCombModal').modal("hide");
- }
- });
+ $("#newCombAddButton").on("click", handleAddButtonClick);
+ $("#newroundModalOkButton").on("click", newRound);
+ $("#addModalOkButton").on("click", addCombatant);
+ $("#editModalOkButton").on("click", modifyCombatant);
+
$('#addCombModal').on('shown.bs.modal', function() {
$('.combName').focus();
})