Checkbox toggles for fields
This commit is contained in:
parent
6abb47f20d
commit
47686bf761
37
module/helpers/object-helper.mjs
Normal file
37
module/helpers/object-helper.mjs
Normal 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);
|
||||
}
|
@ -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) {
|
||||
|
@ -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>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user