From cfa217981705b2a8cfc97120022f229a122efc7a Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sat, 30 Dec 2023 21:08:43 -0700 Subject: [PATCH] Refactor some of the generic sheet for listener handling --- module/sheets/GenericSheet.mjs | 42 +++++++++++++++++++++++++++++++++- module/sheets/PlayerSheet.mjs | 24 ++++--------------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/module/sheets/GenericSheet.mjs b/module/sheets/GenericSheet.mjs index 63d4221..ccaa195 100644 --- a/module/sheets/GenericSheet.mjs +++ b/module/sheets/GenericSheet.mjs @@ -1,4 +1,6 @@ -export class GenericSheet extends ActorSheet { +export class GenericActorSheet extends ActorSheet { + _expanded = new Set(); + #propogatedSettings = [ `devMode`, `showAvatarOnSheet`, @@ -17,6 +19,44 @@ export class GenericSheet extends ActorSheet { ctx.isGM = game.users.current.hasRole(CONST.USER_ROLES.ASSISTANT); + ctx.meta = { + expanded: this._expanded, + }; + return ctx; + }; + + activateListeners(html) { + if (this.document.isEmbedded) return; + if (!this.isEditable) return; + console.debug(`.dungeon | Generic sheet adding listeners`); + + html.find(`.roll`).on(`click`, this._handleRoll); + html.find(`summary`).on(`click`, this._handleSummaryToggle); + }; + + + _handleSummaryToggle($e) { + let data = $e.target.dataset; + let open = $e.target.parentNode.open; + console.debug(`.dungeon | Collapse ID: ${data.collapseId} (open: ${open})`); + /* + This seeming inversion of logic is due to the fact that this handler + gets called before the element is updated to include/reflect the + change, so if the parentNode doesn't actually have it, then we're + opening it and vice-versa. + */ + if (!open) { + this._expanded.add(data.collapseId); + } else { + this._expanded.delete(data.collapseId); + }; } + + + _handleRoll($e) { + let data = $e.target.dataset; + if (!data.roll) return; + console.debug(`.dungeon | Attempting to roll ${data.roll}`); + }; } \ No newline at end of file diff --git a/module/sheets/PlayerSheet.mjs b/module/sheets/PlayerSheet.mjs index 1e14902..cab086a 100644 --- a/module/sheets/PlayerSheet.mjs +++ b/module/sheets/PlayerSheet.mjs @@ -1,6 +1,6 @@ -import { GenericSheet } from "./GenericSheet.mjs"; +import { GenericActorSheet } from "./GenericSheet.mjs"; -export class PlayerSheet extends GenericSheet { +export class PlayerSheet extends GenericActorSheet { static get defaultOptions() { let opts = mergeObject( super.defaultOptions, @@ -23,23 +23,10 @@ export class PlayerSheet extends GenericSheet { Toggles the expanded state for the detail elements in the sheet. */ html.find(`summary`).on(`click`, ($e) => { - console.debug(`.dungeon | summary[data-collapse-id="${$e.target.dataset.collapseId}"] click event (open=${$e.target.parentNode.open})`); - /* - This seeming inversion of logic is due to the fact that this handler - gets called before the element is updated to include/reflect the - change, so if the parentNode doesn't actually have it, then we're - opening it and vice-versa. - */ - if (!$e.target.parentNode.open) { - this._expanded.add($e.target.dataset.collapseId); - } else { - this._expanded.delete($e.target.dataset.collapseId); - }; + }); }; - _expanded = new Set(); - #syncValue() { let delta = 0; for (const actor of game.actors) { @@ -60,10 +47,7 @@ export class PlayerSheet extends GenericSheet { canChangeGroup: ctx.settings.playersCanChangeGroup || ctx.isGM, }; - ctx.meta = { - idp: this.actor.uuid, - expanded: this._expanded, - }; + ctx.meta.idp = this.actor.uuid; console.groupCollapsed(`PlayerSheet.getData`); console.log(`ctx`, ctx);