diff --git a/js/sr2ini.js b/js/sr2ini.js index 7178abc..84ad264 100644 --- a/js/sr2ini.js +++ b/js/sr2ini.js @@ -93,7 +93,7 @@ function getEffectiveIni(tr) { } // otherwise compute effective ini (true ini minus wound penalties) - let effectiveIni = parseInt($(tr).attr("data-true-ini")) - DAMAGE_PENALTY[parseInt($(tr).attr("data-damage-stun")) || 0] - DAMAGE_PENALTY[parseInt($(tr).attr("data-damage-physical")) || 0]; + let effectiveIni = parseInt($(tr).data("true-ini")) - DAMAGE_PENALTY[parseInt($(tr).data("damage-stun")) || 0] - DAMAGE_PENALTY[parseInt($(tr).data("damage-physical")) || 0]; return Math.max(effectiveIni, 0); } @@ -120,10 +120,10 @@ function addTestCombatant() { function handleActButtonClick (e) { // reduce ini by 10 but not lower than 0 - let ini = Math.max(parseInt($(e.target).parents(".combatantRow").attr("data-true-ini")) - 10, 0); + let ini = Math.max(parseInt($(e.target).parents(".combatantRow").data("true-ini")) - 10, 0); // set new ini value - $(e.target).parents(".combatantRow").attr("data-true-ini", ini); + $(e.target).parents(".combatantRow").data("true-ini", ini); // resort table sortTable(); @@ -168,30 +168,6 @@ function handleDamageButtonClick (e) { } -// click handler for damage monitor fields -function handleDamageMonitorClick (e) { - - let $btn = $(e.target); - - // retrieve new damage level and type from button position and "damage-[type]" class - let damageLevel = $btn.parent().parent().index(); - let damageType = $btn.attr("class").split(" ").filter(function(cls) { - return cls.substr(0, 7) == "damage-" ? cls : false; - }).toString().substr(7); - - // add damage level to table row as as data attribute - $btn.parents("tr.combatantRow").attr("data-damage-" + damageType, damageLevel); - - // select/unselect damage buttons above/below - $btn.addClass("active"); - $btn.parent().parent().nextAll().find("button.damage-" + damageType).removeClass("active"); - $btn.parent().parent().prevAll().find("button.damage-" + damageType).addClass("active"); - - // resort - sortTable(); -} - - // click handler for edit buttons function handleEditButtonClick (e) { // find current table row @@ -206,14 +182,14 @@ function handleEditButtonClick (e) { $("#combatantModalName").val($tr.find(".combatantName").text()); $("#combatantModalDice").val($tr.find(".combatantDice").text()); $("#combatantModalRea").val($tr.find(".combatantRea").text()); - $("#combatantModalIni").val($tr.attr("data-true-ini")); + $("#combatantModalIni").val($tr.data("true-ini")); // show effective ini in modal - $("#penalty-stun").text(DAMAGE_PENALTY[parseInt($tr.attr("data-damage-stun")) || 0]); - $("#penalty-physical").text(DAMAGE_PENALTY[parseInt($tr.attr("data-damage-physical")) || 0]); + $("#penalty-stun").text(DAMAGE_PENALTY[parseInt($tr.data("damage-stun")) || 0]); + $("#penalty-physical").text(DAMAGE_PENALTY[parseInt($tr.data("damage-physical")) || 0]); // mark which row is being edited - $("#combatantModal").attr("data-row", $(".combatantRow").index($tr)); + $("#combatantModal").data("row", $(".combatantRow").index($tr)); // add handler for enter key $("#combatantModal input[id*='combatantModal']").off("keydown"); @@ -247,7 +223,7 @@ function handleRemoveButtonClick (e) { $("#confirmModalNewRoundOkButton").addClass("d-none"); // mark which row is being removed - $("#confirmModal").attr("data-row", $(".combatantRow").index($(e.target).parents(".combatantRow"))); + $("#confirmModal").data("row", $(".combatantRow").index($(e.target).parents(".combatantRow"))); // show modal $("#comfirmModal").modal("show"); @@ -281,13 +257,12 @@ function addCombatant (e) { $tr.find(".damage-dropdown").append($.parseHTML(DAMAGE_MONITOR_HTML)); // populate table row with values from modal - $tr.attr("data-true-ini", ini); + $tr.data("true-ini", ini); $tr.find(".combatantName").text($("#combatantModalName").val().trim()); $tr.find(".combatantDice").text($("#combatantModalDice").val().trim()); $tr.find(".combatantRea").text($("#combatantModalRea").val().trim()); //TODO: retrieve initial damage levels -//TODO: mark initial damage levels with active class -> what? don't know what this means // add handler to table cells (click to edit) $tr.find(".combatantName, .combatantIni, .combatantDiceAndRea").on("click", handleEditButtonClick); @@ -299,7 +274,7 @@ function addCombatant (e) { $tr.find("button.remove-button").on("click", handleRemoveButtonClick); // add handler to damage monitor - $tr.find(".damage-stun, .damage-physical").on("click", handleDamageMonitorClick); + $tr.find(".damage-stun, .damage-physical").on("click", applyDamage); // add row to table and sort $("#combatantsTable").append($tr); @@ -307,6 +282,30 @@ function addCombatant (e) { } +// event handler for when any damage monitor is clicked +function applyDamage (e) { + + let $btn = $(e.target); + + // retrieve new damage level and type from button position and "damage-[type]" class + let damageLevel = $btn.parent().parent().index(); + let damageType = $btn.attr("class").split(" ").filter(function(cls) { + return cls.substr(0, 7) == "damage-" ? cls : false; + }).toString().substr(7); + + // add damage level to table row as as data attribute + $btn.parents("tr.combatantRow").data("damage-" + damageType, damageLevel); + + // select/unselect damage buttons above/below + $btn.addClass("active"); + $btn.parent().parent().nextAll().find("button.damage-" + damageType).removeClass("active"); + $btn.parent().parent().prevAll().find("button.damage-" + damageType).addClass("active"); + + // resort + sortTable(); +} + + // edit combatant function editCombatant (e) { e.preventDefault(); @@ -329,14 +328,14 @@ function editCombatant (e) { ini = (ini != "") ? ini : rollForInitiative(dice, rea); // get correct row - let index = parseInt($("#combatantModal").attr("data-row")); + let index = parseInt($("#combatantModal").data("row")); $tr = $("tr.combatantRow").eq(index); // set new values $tr.find(".combatantName").text(name); $tr.find(".combatantDice").text(dice); $tr.find(".combatantRea").text(rea); - $tr.attr("data-true-ini", ini); + $tr.data("true-ini", ini); // sort table sortTable(); @@ -353,7 +352,7 @@ function removeCombatant (e) { $("#confirmModal").modal("hide"); // remove correct row - let index = parseInt($("#confirmModal").attr("data-row")); + let index = parseInt($("#confirmModal").data("row")); $(".combatantRow").eq(index).remove(); // clean up @@ -376,9 +375,9 @@ function startNewRound (e) { // reset ini values $(".combatantRow").each( function() { if ( $(this).find(".combatantDice").text() == "" ) { - $(this).attr("data-true-ini", 1); + $(this).data("true-ini", 1); } else { - $(this).attr("data-true-ini", rollForInitiative(parseInt($(this).find(".combatantDice").text()), parseInt($(this).find(".combatantRea").text()))); + $(this).data("true-ini", rollForInitiative(parseInt($(this).find(".combatantDice").text()), parseInt($(this).find(".combatantRea").text()))); } }); @@ -396,14 +395,14 @@ function sortTable() { // mark KO or death with class $(".combatantRow").each(function() { - if ( parseInt($(this).attr("data-damage-stun")) == 10 || parseInt($(this).attr("data-damage-physical")) == 10 ) { + if ( parseInt($(this).data("damage-stun")) == 10 || parseInt($(this).data("damage-physical")) == 10 ) { $(this).addClass(CONTEXTUAL_CLASSES["KO_OR_DEAD"]); } }); // compute highest effective ini let iniMax = Math.max.apply(null, $.map( $(".combatantRow"), function(tr, i) { - // write current effective ini to table row + // write current effective ini to table row $(tr).find(".combatantIni").text($(tr).hasClass(CONTEXTUAL_CLASSES["KO_OR_DEAD"]) ? 0 : getEffectiveIni($(tr))); return $(tr).find(".combatantIni").text(); })); @@ -411,13 +410,13 @@ function sortTable() { // add damage badges and contextual classes $(".combatantRow").each(function() { // damage badges - if ( $(this).attr("data-damage-stun") && $(this).attr("data-damage-stun") != "0" ) { + if ( $(this).data("damage-stun") && $(this).data("damage-stun") != "0" ) { $(this).find(".combatantIni").append($.parseHTML(STUN_BADGE_HTML)); - $(this).find(".stun-badge").append(DAMAGE_NIVEAU[DAMAGE_PENALTY[$(this).attr("data-damage-stun")]]); + $(this).find(".stun-badge").append(DAMAGE_NIVEAU[DAMAGE_PENALTY[$(this).data("damage-stun")]]); } - if ( $(this).attr("data-damage-physical") && $(this).attr("data-damage-physical") != "0" ) { + if ( $(this).data("damage-physical") && $(this).data("damage-physical") != "0" ) { $(this).find(".combatantIni").append($.parseHTML(PHYSICAL_BADGE_HTML)); - $(this).find(".physical-badge").append(DAMAGE_NIVEAU[DAMAGE_PENALTY[$(this).attr("data-damage-physical")]]); + $(this).find(".physical-badge").append(DAMAGE_NIVEAU[DAMAGE_PENALTY[$(this).data("damage-physical")]]); } // K.O./dead -> don't add anything