Checkbox toggles for fields

This commit is contained in:
walcutt 2024-12-02 22:18:47 -05:00
parent 6abb47f20d
commit 47686bf761
3 changed files with 67 additions and 2 deletions

View File

@ -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);
}

View File

@ -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) {

View File

@ -1,6 +1,6 @@
<form>
<h1> {{ actor.name }} </h1>
<button id="hench-console-log">Log data to console.</button>
<h2> Details: </h2>
<div id="hench-explicit-details">
@ -42,7 +42,7 @@
<h3>Gear</h3>
<ul>
{{#each actor.system.gear}}
<li><b>{{#if this.marked}} X {{else}} O {{/if}}</b> {{this.description}}</li>
<li><input type="checkbox" class="hench-checkbox-toggle-field" data-is-array="true" data-field-path="system.fixedGear" data-index="{{@index}}" data-sub-path="marked" {{#if this.marked}}checked{{/if}}/> {{this.description}}</li>
{{/each}}
</ul>