Skip to content

Commit

Permalink
Add Behavior Settings V2
Browse files Browse the repository at this point in the history
  • Loading branch information
mclemente committed May 28, 2024
1 parent 364a543 commit e83a821
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
61 changes: 35 additions & 26 deletions src/module/forms/BehaviorSettings.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { t } from "../utils.js";
import { HealthEstimateSettings } from "./templates/Base.js";
import { HealthEstimateSettingsV2 } from "./templates/BaseV2.js";

export default class HealthEstimateBehaviorSettings extends HealthEstimateSettings {
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
export default class HealthEstimateBehaviorSettings extends HealthEstimateSettingsV2 {
static DEFAULT_OPTIONS = {
actions: {
reset: HealthEstimateBehaviorSettings.reset,
},
window: {
icon: "fas fa-gear",
},
};

static PARTS = {
form: {
id: "health-estimate-behavior-form",
title: `Health Estimate: ${t("core.menuSettings.behaviorSettings.plural")}`,
template: "./modules/healthEstimate/templates/behaviorSettings.hbs",
height: "auto",
});
},
...HealthEstimateSettingsV2.PARTS,
};

get title() {
return `Health Estimate: ${t("core.menuSettings.behaviorSettings.plural")}`;
}

getData(options) {
_prepareContext(options) {
return {
alwaysShow: this.prepSetting("alwaysShow"),
combatOnly: this.prepSetting("combatOnly"),
Expand All @@ -23,27 +35,24 @@ export default class HealthEstimateBehaviorSettings extends HealthEstimateSettin
NPCsJustDie: this.prepSetting("NPCsJustDie"),
deathMarkerEnabled: game.healthEstimate.estimationProvider.deathMarker.config,
deathMarker: this.prepSetting("deathMarker"),
...HealthEstimateSettingsV2.BUTTONS,
};
}

async activateListeners(html) {
super.activateListeners(html);

html.find("button[name=reset]").on("click", async (event) => {
const paths = [
"alwaysShow",
"combatOnly",
"showDescription",
"showDescriptionTokenType",
"deathState",
"deathStateName",
"NPCsJustDie",
"deathMarker",
];
static async reset(event, form, formData) {
const paths = [
"alwaysShow",
"combatOnly",
"showDescription",
"showDescriptionTokenType",
"deathState",
"deathStateName",
"NPCsJustDie",
"deathMarker",
];

await Promise.all(paths.map(this.resetToDefault));
canvas.scene?.tokens.forEach((token) => token.object.refresh());
this.close();
});
await Promise.all(paths.map(this.resetToDefault));
canvas.scene?.tokens.forEach((token) => token.object.refresh());
this.close();
}
}
12 changes: 4 additions & 8 deletions src/module/forms/templates/BaseV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class HealthEstimateSettingsV2 extends HandlebarsApplicationMixin(Applica
height: "auto",
},
form: {
handler: HealthEstimateSettingsV2.#onSubmit,
closeOnSubmit: true,
},
tag: "form",
Expand Down Expand Up @@ -70,14 +71,9 @@ export class HealthEstimateSettingsV2 extends HandlebarsApplicationMixin(Applica
* @param {Event} event - the form submission event
* @param {Object} formData - the form data
*/
async _updateObject(event, formData) {
await Promise.all(
Object.entries(formData).map(async ([key, value]) => {
let current = game.settings.get("healthEstimate", `core.${key}`);
// eslint-disable-next-line eqeqeq
if (value != current) await sSet(`core.${key}`, value);
})
);
static async #onSubmit(event, form, formData) {
const settings = foundry.utils.expandObject(formData.object);
await Promise.all(Object.entries(settings).map(([key, value]) => sSet(`core.${key}`, value)));
if (game.healthEstimate.alwaysShow) canvas.scene?.tokens.forEach((token) => token.object.refresh());
}
}
34 changes: 12 additions & 22 deletions src/templates/behaviorSettings.hbs
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
<form autocomplete="off" onsubmit="event.preventDefault()">
<div class="standard-form">
<div class="form-group">
{{#with alwaysShow}}
<label>{{localize name}}</label>
<input type="checkbox" name="alwaysShow" id="alwaysShow" {{checked value}} />
<p class="notes">{{localize hint}}</p>
<p class="hint">{{localize hint}}</p>
{{/with}}
</div>
<div class="form-group">
{{#with combatOnly}}
<label>{{localize name}}</label>
<input type="checkbox" name="combatOnly" id="combatOnly" {{checked value}} />
<p class="notes">{{localize hint}}</p>
<p class="hint">{{localize hint}}</p>
{{/with}}
</div>
<div class="form-group">
{{#with showDescription}}
<label>{{localize name}}</label>
<select name="showDescription" class="form-fields" id="showDescription">
{{selectOptions select selected=selected nameAttr="key" labelAttr="value"}}
{{selectOptions select selected=selected valueAttr="key" labelAttr="value"}}
</select>
<p class="notes">{{localize hint}}</p>
<p class="hint">{{localize hint}}</p>
{{/with}}
</div>
<div class="form-group">
{{#with showDescriptionTokenType}}
<label>{{localize name}}</label>
<select name="showDescriptionTokenType" class="form-fields" id="showDescriptionTokenType">
{{selectOptions select selected=selected nameAttr="key" labelAttr="value"}}
{{selectOptions select selected=selected valueAttr="key" labelAttr="value"}}
</select>
<p class="notes">{{localize hint}}</p>
<p class="hint">{{localize hint}}</p>
{{/with}}
</div>

<div class="form-group">
{{#with deathState}}
<label>{{localize name}}</label>
<input type="checkbox" name="deathState" id="deathState" {{checked value}} />
<p class="notes">{{localize hint}}</p>
<p class="hint">{{localize hint}}</p>
{{/with}}
</div>
<div class="form-group">
{{#with deathStateName}}
<label>{{localize name}}</label>
<input type="text" name="deathStateName" id="deathStateName" value="{{value}}" />
<p class="notes">{{localize hint}}</p>
<p class="hint">{{localize hint}}</p>
{{/with}}
</div>
<div class="form-group">
{{#with NPCsJustDie}}
<label>{{localize name}}</label>
<input type="checkbox" name="NPCsJustDie" id="NPCsJustDie" {{checked value}} />
<p class="notes">{{localize hint}}</p>
<p class="hint">{{localize hint}}</p>
{{/with}}
</div>
{{#if deathMarkerEnabled}}
Expand All @@ -67,18 +67,8 @@
value="{{value}}"
/>
</div>
<p class="notes">{{localize hint}}</p>
<p class="hint">{{localize hint}}</p>
{{/with}}
</div>
{{/if}}
<footer class="sheet-footer flexrow">
<button type="submit" name="submit">
<i class="far fa-save"></i>
{{localize "SETTINGS.Save"}}
</button>
<button type="button" name="reset" data-action="reset">
<i class="fas fa-undo"></i>
{{localize "SETTINGS.Reset"}}
</button>
</footer>
</form>
</div>

0 comments on commit e83a821

Please sign in to comment.