From 29a1e0eacf81258cc8bf2ee5d07bf42f150a64c8 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Tue, 13 Feb 2024 20:07:34 -0700 Subject: [PATCH] Prevent partial-updates from being overwritten when adding/deleting a dice (closes #87) --- module/dialogs/DiceList.mjs | 23 +++++++++++++++++++++++ templates/dialogs/diceList.hbs | 3 +++ 2 files changed, 26 insertions(+) diff --git a/module/dialogs/DiceList.mjs b/module/dialogs/DiceList.mjs index ae9b002..f9793de 100644 --- a/module/dialogs/DiceList.mjs +++ b/module/dialogs/DiceList.mjs @@ -30,6 +30,16 @@ export class DiceList extends GenericDialog { return ctx; }; + async activateListeners(html) { + super.activateListeners(html); + + if (!this.isEditable) return; + console.debug(`.dungeon | DiceList adding event listeners`); + + html.find(`[data-die-update]`) + .on(`change`, this.updateDieInMemoryOnly.bind(this)) + }; + async _updateObject(_event, formData) { const newDice = this.dice.map(d => { return { @@ -41,6 +51,19 @@ export class DiceList extends GenericDialog { await this.actor.update({ "system.dice": newDice }); }; + updateDieInMemoryOnly($e) { + const target = $e.currentTarget; + const data = target.dataset; + const value = target.value; + const [ dieId, field ] = data.dieUpdate.split(`.`); + for (const die of this.dice) { + if (die.id === dieId) { + die[field] = value; + return + }; + }; + }; + addDie() { this.dice.push({ count: 1, diff --git a/templates/dialogs/diceList.hbs b/templates/dialogs/diceList.hbs index daad431..db0cfb6 100644 --- a/templates/dialogs/diceList.hbs +++ b/templates/dialogs/diceList.hbs @@ -8,6 +8,7 @@ class="count" name="{{die.id}}.count" value="{{die.count}}" + data-die-update="{{die.id}}.count" aria-label="The number of dice to roll at the same time" > @@ -17,6 +18,7 @@ class="sides" name="{{die.id}}.sides" value="{{die.sides}}" + data-die-update="{{die.id}}.sides" aria-label="The number of sides that are on the dice" > @@ -26,6 +28,7 @@ class="repeat" name="{{die.id}}.repeat" value="{{die.repeat}}" + data-die-update="{{die.id}}.repeat" aria-label="The number of times to repeat this dice " >