Combatant order is now established by re-sorting table after every value change (instead of re-positioning the modified row)
This commit is contained in:
parent
9b7826568b
commit
75d6a54e57
60
js/sr2ini.js
60
js/sr2ini.js
@ -13,6 +13,18 @@ function rollForInitiative(dice, rea) {
|
|||||||
return ini + parseInt(rea);
|
return ini + parseInt(rea);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sorts the combatants by ini value
|
||||||
|
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");
|
||||||
|
});
|
||||||
|
for ( var i = 0; i < $rows.length; i++ ) {
|
||||||
|
$table.append($rows[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Event handler functions
|
* Event handler functions
|
||||||
@ -38,9 +50,7 @@ function handleBlur (e) {
|
|||||||
|
|
||||||
// Ini value changed => reposition row
|
// Ini value changed => reposition row
|
||||||
$tr.attr("data-ini", $tr.find(".combIni")[0].value);
|
$tr.attr("data-ini", $tr.find(".combIni")[0].value);
|
||||||
let $trc = $tr.clone(true);
|
sortTable()
|
||||||
$tr.remove();
|
|
||||||
insertCombRow($trc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,18 +58,15 @@ function handleBlur (e) {
|
|||||||
// click handler for act buttons
|
// click handler for act buttons
|
||||||
function handleActButtonClick (e) {
|
function handleActButtonClick (e) {
|
||||||
// find current table row
|
// find current table row
|
||||||
let $tr = $(e.target).parents("tr.combRow").clone(true);
|
let $tr = $(e.target).parents("tr.combRow");
|
||||||
let input = $tr.find(".combIni")[0];
|
let input = $tr.find(".combIni")[0];
|
||||||
|
|
||||||
// reduce ini by 10 but not lower than 0
|
// reduce ini by 10 but not lower than 0
|
||||||
input.value = Math.max(parseInt(input.value) - 10, 0);
|
input.value = Math.max(parseInt(input.value) - 10, 0);
|
||||||
$(input).parents("tr").attr("data-ini", input.value);
|
$tr.attr("data-ini", input.value);
|
||||||
|
|
||||||
// remove original current table row and insert clone at newly calculated position
|
// resort table
|
||||||
$(e.target).parents("tr.combRow").remove();
|
sortTable();
|
||||||
insertCombRow($tr);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -88,7 +95,6 @@ function validateCombRowValues(tr) {
|
|||||||
// do standard HTML5 form validation first (makes sure that name is not empty and that all other values are numbers within their individual ranges)
|
// do standard HTML5 form validation first (makes sure that name is not empty and that all other values are numbers within their individual ranges)
|
||||||
let valid = true;
|
let valid = true;
|
||||||
Object.values(inputElements).forEach(function(input) {
|
Object.values(inputElements).forEach(function(input) {
|
||||||
console.log(input);
|
|
||||||
if ( ! input.reportValidity() ) {
|
if ( ! input.reportValidity() ) {
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
@ -109,7 +115,6 @@ console.log(input);
|
|||||||
let ini = inputElements["ini"].value.trim();
|
let ini = inputElements["ini"].value.trim();
|
||||||
let dice = inputElements["dice"].value.trim();
|
let dice = inputElements["dice"].value.trim();
|
||||||
let rea = inputElements["rea"].value.trim();
|
let rea = inputElements["rea"].value.trim();
|
||||||
console.log("I/D/R: " + ini + "/" + dice + "/" + rea);
|
|
||||||
|
|
||||||
// invalidate if ini, dice and rea are all empty
|
// invalidate if ini, dice and rea are all empty
|
||||||
if ( ini == "" && ( dice == "" || rea == "" ) ) {
|
if ( ini == "" && ( dice == "" || rea == "" ) ) {
|
||||||
@ -148,6 +153,7 @@ console.log("I/D/R: " + ini + "/" + dice + "/" + rea);
|
|||||||
* Main functions
|
* Main functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
// inserts a combatant table row (in form of a jQuery object) at the correct position in the table
|
// inserts a combatant table row (in form of a jQuery object) at the correct position in the table
|
||||||
function insertCombRow($tr) {
|
function insertCombRow($tr) {
|
||||||
|
|
||||||
@ -199,7 +205,7 @@ console.log("rea is " + rea + " and currentRowRea is " + currentRowRea);
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// add new combatant
|
// add new combatant
|
||||||
function addCombatant (e) {
|
function addCombatant (e) {
|
||||||
@ -208,6 +214,7 @@ function addCombatant (e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get values
|
// get values
|
||||||
|
let name = $("#addCombModal .combName").val().trim();
|
||||||
let ini = $("#addCombModal .combIni").val().trim();
|
let ini = $("#addCombModal .combIni").val().trim();
|
||||||
let dice = $("#addCombModal .combDice").val().trim();
|
let dice = $("#addCombModal .combDice").val().trim();
|
||||||
let rea = $("#addCombModal .combRea").val().trim();
|
let rea = $("#addCombModal .combRea").val().trim();
|
||||||
@ -220,7 +227,7 @@ function addCombatant (e) {
|
|||||||
'<tr class="combRow" data-ini="', ini, '">\n',
|
'<tr class="combRow" data-ini="', ini, '">\n',
|
||||||
'<form name="combForm" class="combForm needs-validation" onsubmit="return false;">\n',
|
'<form name="combForm" class="combForm needs-validation" onsubmit="return false;">\n',
|
||||||
'<td>\n',
|
'<td>\n',
|
||||||
'<input type="text" required="" maxlength="40" class="form-control combName" title="click to edit" value="', $("#addCombModal .combName").val().trim(), '" />\n',
|
'<input type="text" required="" maxlength="40" class="form-control combName" title="click to edit" value="', name, '" />\n',
|
||||||
'</td>\n',
|
'</td>\n',
|
||||||
'<td class="col-sm-2">\n',
|
'<td class="col-sm-2">\n',
|
||||||
'<input type="number" min="0" max="50" class="form-control combIni" title="click to edit" value="', ini, '" />\n',
|
'<input type="number" min="0" max="50" class="form-control combIni" title="click to edit" value="', ini, '" />\n',
|
||||||
@ -244,17 +251,16 @@ function addCombatant (e) {
|
|||||||
|
|
||||||
// add handlers to table row object
|
// 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("input[type='number']").bind('keyup input change', handleBlur);
|
||||||
$tr.find("button.act-button").on("click", handleActButtonClick);
|
$tr.find("button.act-button").on("click", handleActButtonClick);
|
||||||
$tr.find("button.remove-button").on("click", handleRemoveButtonClick);
|
$tr.find("button.remove-button").on("click", handleRemoveButtonClick);
|
||||||
|
|
||||||
// insert combatant row
|
// insert combatant row
|
||||||
insertCombRow($tr);
|
$("table#combatants").append($tr);
|
||||||
|
sortTable();
|
||||||
|
|
||||||
//reset form values
|
//reset form values
|
||||||
$("#addCombModal input[class*='comb']").val("");
|
$("#addCombModal input[class*='comb']").val("");
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -262,16 +268,13 @@ function addCombatant (e) {
|
|||||||
function newRound() {
|
function newRound() {
|
||||||
|
|
||||||
// are there rows at all?
|
// are there rows at all?
|
||||||
let $oldRows = $("tr.combRow");
|
let $rows = $("tr.combRow");
|
||||||
if ( $oldRows.length == 0 ) {
|
if ( $rows.length == 0 ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// clone existing rows
|
|
||||||
let $newRows = $oldRows.clone(true);
|
|
||||||
|
|
||||||
// reset ini values in cloned rows
|
// reset ini values in cloned rows
|
||||||
$newRows.each( function() {
|
$rows.each( function() {
|
||||||
let $input = $(this).find(".combIni");
|
let $input = $(this).find(".combIni");
|
||||||
let dice = $(this).find(".combDice").val();
|
let dice = $(this).find(".combDice").val();
|
||||||
if ( dice == "" ) {
|
if ( dice == "" ) {
|
||||||
@ -282,13 +285,8 @@ function newRound() {
|
|||||||
$(this).attr("data-ini", $input.val());
|
$(this).attr("data-ini", $input.val());
|
||||||
});
|
});
|
||||||
|
|
||||||
// remove old rows
|
// resort table
|
||||||
$oldRows.remove();
|
sortTable();
|
||||||
|
|
||||||
// insert cloned rows
|
|
||||||
$newRows.each( function() {
|
|
||||||
insertCombRow($(this));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user