From 47686bf761a018f686a30c5e24ee523e8ccd82bc Mon Sep 17 00:00:00 2001 From: walcutt Date: Mon, 2 Dec 2024 22:18:47 -0500 Subject: [PATCH] Checkbox toggles for fields --- module/helpers/object-helper.mjs | 37 ++++++++++++++++++++++++++++++++ module/sheets/hench-debug.mjs | 28 ++++++++++++++++++++++++ templates/hench-debug.hbs | 4 ++-- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 module/helpers/object-helper.mjs diff --git a/module/helpers/object-helper.mjs b/module/helpers/object-helper.mjs new file mode 100644 index 0000000..4feb96b --- /dev/null +++ b/module/helpers/object-helper.mjs @@ -0,0 +1,37 @@ +export function getValueAtPath(obj, fieldPath) { + const pathSequence = fieldPath.split('.'); + + let pointer = obj; + + for(let i = 0; i < pathSequence.length; i++) { + pointer = pointer[pathSequence[i]]; + } + + return pointer; +} + +export function copyAndMutateAtPath(obj, fieldPath, val) { + const copy = deepCopy(obj); + + const changed = mutateAtPath(copy, fieldPath, val); + + return changed; +} + +function mutateAtPath(obj, fieldPath, val) { + const pathSequence = fieldPath.split('.'); + + let pointer = obj; + + for(let i = 0; i < pathSequence.length - 1; i++) { + pointer = pointer[pathSequence[i]]; + } + + pointer[pathSequence[pathSequence.length - 1]] = val; + + return obj; +} + +export function deepCopy(obj) { + return structuredClone(obj); +} \ No newline at end of file diff --git a/module/sheets/hench-debug.mjs b/module/sheets/hench-debug.mjs index 7773576..08149d2 100644 --- a/module/sheets/hench-debug.mjs +++ b/module/sheets/hench-debug.mjs @@ -1,4 +1,5 @@ import { playbookKeys, validatePlaybookKey, getPlaybookMutation } from "../playbooks.mjs"; +import { getValueAtPath, copyAndMutateAtPath, deepCopy } from "../helpers/object-helper.mjs"; export class HenchDebugSheet extends ActorSheet { /** @override */ @@ -20,6 +21,33 @@ export class HenchDebugSheet extends ActorSheet { super.activateListeners(html); html.on('change', '.hench-hench-sheet-playbook-dropdown', this._changePlaybook.bind(this)); + + html.on('click', '#hench-console-log', (event) => console.log(this.actor)); + + // Checkbox logic + // boolean checkboxes + html.find('.hench-checkbox-toggle-field').on('change', (event) => { + const element = event.currentTarget; + + const isArrayPath = element.dataset.isArray; + const fieldPath = element.dataset.fieldPath; + const index = element.dataset.index; + const subPath = element.dataset.subPath; + + const value = element.checked; + + if(isArrayPath) { + const array = getValueAtPath(this.actor, fieldPath); + + const copy = array.map(e => deepCopy(e)); + + copy[index] = copyAndMutateAtPath(array[index], subPath, value); + + this.actor.update({[fieldPath]: copy}); + } else { + this.actor.update({[fieldPath]: value}); + } + }); } _changePlaybook(newPlaybookKeyEvent) { diff --git a/templates/hench-debug.hbs b/templates/hench-debug.hbs index 5ed142f..8fae5f9 100644 --- a/templates/hench-debug.hbs +++ b/templates/hench-debug.hbs @@ -1,6 +1,6 @@

{{ actor.name }}

- +

Details:

@@ -42,7 +42,7 @@

Gear

    {{#each actor.system.gear}} -
  • {{#if this.marked}} X {{else}} O {{/if}} {{this.description}}
  • +
  • {{this.description}}
  • {{/each}}