diff --git a/js/sr2ini.js b/js/sr2ini.js
index 1ffdb7b..ad711a6 100644
--- a/js/sr2ini.js
+++ b/js/sr2ini.js
@@ -1,32 +1,32 @@
-/*
- * helper functions
- */
-
const penalty = [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4];
const dmg = ["", "L", "M", "S", "D"];
-const damageMonitorHTML = ['
\n',
- '\n',
- ' |  |
\n',
- '\n',
- '\n',
- ' | |
\n',
- ' | |
\n',
- ' | |
\n',
- ' | |
\n',
- ' | |
\n',
- ' | |
\n',
- ' | |
\n',
- ' | |
\n',
- ' | |
\n',
- ' | |
\n',
- '\n',
+const damageMonitorHTML = ['\n',
+ '\n',
+ ' | \n',
+ ' | \n',
+ '
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' | |
\n',
+ ' |
\n',
'
'].join("");
-const maxIniClass = "max-ini";
+const maxIniClass = "table-primary";
const zeroIniClass = "table-secondary";
+/*
+ * helper functions
+ */
+
// roll for initiative with the given reaction and number of ini dice
function rollForInitiative(dice, rea) {
let ini = 0;
@@ -67,12 +67,16 @@ function sortTable() {
$(".combatantRow").removeClass(maxIniClass + " " + zeroIniClass).find(".act-button").prop("disabled", true).attr("aria-disabled", "true");
// get ini value for every combatant; set to 0 if K.O./dead
- let iniValues = $.map( $(".combatantIni"), function(td, i) {
- return $(td).parents(".combatantRow").hasClass("out-of-commission") ? 0 : parseInt($(td).text());
+ let iniValues = $.map( $(".combatantRow"), function(tr, i) {
+console.log($(tr).find(".combatantIni"));
+ return $(tr).hasClass("out-of-commission") ? 0 : parseInt($(tr).find(".combatantIni").text());
});
+console.log(iniValues);
+//TODO: maxIni wid nicht vergeben wenn damage > 0
// compute highest ini
let iniMax = Math.max.apply(null, iniValues);
+console.log(iniValues);
// add contextual classes
$(".combatantRow").each( function() {
@@ -165,10 +169,18 @@ function handleAddButtonClick (e) {
$("#combatantModal").modal("show");
}
-// click handler for damage buttons
+// click handler for damage buttons; basically toggles visibility of table.damage-monitor
function handleDamageButtonClick (e) {
- let display = $(e.target).parents(".damage-dropdown").find(".damage-monitor").css("display");
- $(e.target).parents(".damage-dropdown").find(".damage-monitor").css("display", display == "block" ? "none" : "block");
+ // get visibility status at click time
+ let visAtClick = $(e.target).parents(".damage-dropdown").find(".damage-monitor").css("display");
+
+ // hide all damage monitors
+ $(".damage-monitor:visible").css("display", "none");
+
+ // if targeted dm was hidden before, show it now
+ if ( visAtClick == "none" ) {
+ $(e.target).parents(".damage-dropdown").find(".damage-monitor").css("display", "block");
+ }
return false;
}
@@ -181,11 +193,12 @@ function handleDamageMonitorClick (e) {
let otherDamageLevel
// calculate new damage level and type
- let damageLevel = $btn.parent().parent().index() + 1;
- if ( $btn.parent().hasClass("damage-stun") ) {
+ let damageLevel = $btn.parent().parent().index();
+console.log("damage level is", damageLevel);
+ if ( $btn.hasClass("damage-stun") ) {
damageType = "stun";
otherDamageLevel = $tr.attr("data-damage-physical") ? parseInt($tr.attr("data-damage-physical")) : 0;
- } else if ( $btn.parent().hasClass("damage-physical") ) {
+ } else if ( $btn.hasClass("damage-physical") ) {
damageType = "physical";
otherDamageLevel = $tr.attr("data-damage-stun") ? parseInt($tr.attr("data-damage-stun")) : 0;
} else {
@@ -202,8 +215,8 @@ function handleDamageMonitorClick (e) {
// select/unselect damage boxes
$btn.addClass("active");
- $btn.parent().parent().nextAll().find("td.damage-" + damageType + " button").removeClass("active");
- $btn.parent().parent().prevAll().find("td.damage-" + damageType + " button").addClass("active");
+ $btn.parent().parent().nextAll().find("button.damage-" + damageType).removeClass("active");
+ $btn.parent().parent().prevAll().find("button.damage-" + damageType).addClass("active");
// recalculate effective ini
$tr.find(".combatantIni").text(getEffectiveIni($tr));
@@ -255,10 +268,29 @@ function handleEditButtonClick (e) {
}
+function handleNewRoundButton (e) {
+ // restyle modal
+ $("#confirmModal .modal-title").text("Start new Round");
+ $("#confirmModalNewRoundOkButton").show();
+ $("#confirmModalRemoveCombatantOkButton").hide();
+
+ // show modal
+ $("#comfirmModal").modal("show");
+}
+
+
// click handler for remove buttons
function handleRemoveButtonClick (e) {
- // remove table row
- $(e.target).parents(".combatantRow").remove();
+ // restyle modal
+ $("#confirmModal .modal-title").text("Remove Combatant");
+ $("#confirmModalRemoveCombatantOkButton").show();
+ $("#confirmModalNewRoundOkButton").hide();
+
+ // mark which row is being removed
+ $("#confirmModal").attr("data-row", $(".combatantRow").index($(e.target).parents(".combatantRow")));
+
+ // show modal
+ $("#comfirmModal").modal("show");
}
@@ -369,8 +401,8 @@ function addCombatant (e) {
// add handlers to table row buttons
$tr.find("button.edit-button").on("click", handleEditButtonClick);
$tr.find("button.act-button").on("click", handleActButtonClick);
- $tr.find("button.remove-button").on("click", handleRemoveButtonClick);
$tr.find("button.damage-button").on("click", handleDamageButtonClick);
+ $tr.find("button.remove-button").on("click", handleRemoveButtonClick);
// add handler to table cells (click to edit)
$tr.find(".combatantName, .combatantIni, .combatantDiceAndRea").on("click", handleEditButtonClick);
@@ -384,7 +416,7 @@ function addCombatant (e) {
}
-// edit combatant values
+// edit combatant
function editCombatant (e) {
e.preventDefault();
@@ -423,9 +455,29 @@ function editCombatant (e) {
$("#combatantModal").removeAttr("data-row");
}
+// remove combatant
+function removeCombatant (e) {
+ e.preventDefault();
+
+ // hide modal
+ $("#confirmModal").modal("hide");
+
+ // remove correct row
+ let index = parseInt($("#confirmModal").attr("data-row"));
+ $(".combatantRow").eq(index).remove();
+
+ // clean up
+ $("#confirmModal").removeAttr("data-row");
+}
+
// start a new combat round
-function newRound() {
+function startNewRound (e) {
+ e.preventDefault();
+
+ // hide modal
+ $("#confirmModal").modal("hide");
+
// are there rows at all?
if ( $(".combatantRow").length == 0 ) {
return;
@@ -468,26 +520,30 @@ function addTestCombatant() {
$(document).ready(function(){
// add event handlers to navbar buttons
$("#addCombatantButton").on("click", handleAddButtonClick);
- $("#newroundModalOkButton").on("click", newRound);
+ $("#newRoundButton").on("click", handleNewRoundButton);
// add event handlers to modal buttons
$("#combatantModalAddOkButton").on("click", addCombatant);
$("#combatantModalEditOkButton").on("click", editCombatant);
+ $("#confirmModalNewRoundOkButton").on("click", startNewRound);
+ $("#confirmModalRemoveCombatantOkButton").on("click", removeCombatant);
// always focus name input field when combatant modal appears
$('#combatantModal').on('shown.bs.modal', function() {
$('#combatantModalName').focus();
});
+
// always empty input fields when combatant modal disappears
$("#combatantModal").on('hidden.bs.modal', function (e) {
$("#combatantModal input[id*='combatantModal']").val("");
});
- // Hide damage monitors if mouse is clicked outside
- $("html").on("click", function(e) {
- if (! $(e.target).is("td[class*='damage-'] > button") ) {
+ // Hide damage monitors after click somewhere else
+ $("html").on("click", function (e) {
+ if ( $(e.target).parents(".damage-monitor").length == 0 ) {
$(".damage-monitor:visible").css("display", "none");
}
+
});
addTestCombatant();