Have the PopoverManager call a hook to get additional options for the popover Application

This commit is contained in:
Oliver-Akins 2025-03-15 17:05:14 -06:00
parent e8fdf6e952
commit 69bf712eca
2 changed files with 23 additions and 8 deletions

View file

@ -44,12 +44,12 @@ export class HeroSkillsCardV1 extends GenericAppMixin(HandlebarsApplicationMixin
// #region Lifecycle
async _onFirstRender(context, options) {
await super._onFirstRender(context, options);
HeroSkillsCardV1._createPopoverListeners.bind(this)();
};
async _onRender(context, options) {
await super._onRender(context, options);
HeroSkillsCardV1._onRender.bind(this)(context, options);
HeroSkillsCardV1._createPopoverListeners.bind(this)();
};
static async _onRender(_context, options) {
@ -84,13 +84,20 @@ export class HeroSkillsCardV1 extends GenericAppMixin(HandlebarsApplicationMixin
/** @type {Map<string, PopoverEventManager>} */
#popoverManagers = new Map();
/** @type {Map<number, string>} */
#hookIDs = new Map();
/** @this {HeroSkillsCardV1} */
static async _createPopoverListeners() {
const ammoInfoIcon = this.element.querySelector(`.ammo-info-icon`);
this.#popoverManagers.set(
`.ammo-info-icon`,
new PopoverEventManager(ammoInfoIcon, AmmoTracker, { lockable: true }),
new PopoverEventManager(ammoInfoIcon, AmmoTracker),
);
this.#hookIDs.set(Hooks.on(`get${AmmoTracker.name}Options`, (opts) => {
opts.ammo = this.actor.itemTypes.ammo;
}), `get${AmmoTracker.name}Options`);
};
async _preparePartContext(partId, ctx, opts) {
@ -193,6 +200,9 @@ export class HeroSkillsCardV1 extends GenericAppMixin(HandlebarsApplicationMixin
manager.destroy();
};
this.#popoverManagers.clear();
for (const [id, hook] of this.#hookIDs.entries()) {
Hooks.off(hook, id);
}
super._tearDown(options);
};
// #endregion

View file

@ -1,5 +1,4 @@
import { getTooltipDelay } from "../consts.mjs";
import { Logger } from "./Logger.mjs";
export class PopoverEventManager {
#options;
@ -64,16 +63,22 @@ export class PopoverEventManager {
#frameless;
#framed;
#construct(options) {
const valid = Hooks.call(`get${this.#class.name}Options`, options);
if (!valid) { return };
return new this.#class(options);
};
#clickHandler() {
// Cleanup for the frameless lifecycle
this.stopOpen();
this.stopClose();
this.#frameless?.close({ force: true });
if (!this.#framed) {
const app = new this.#class({ popover: { ...this.#options, framed: true } });
this.#framed = app;
this.#framed = this.#construct({ popover: { ...this.#options, framed: true } });
}
this.#framed.render({ force: true });
this.#framed?.render({ force: true });
};
#pointerEnterHandler(event) {
@ -104,14 +109,14 @@ export class PopoverEventManager {
return;
}
this.#frameless = new this.#class({
this.#frameless = this.#construct({
popover: {
...this.#options,
framed: false,
x, y,
},
});
this.#frameless.render({ force: true });
this.#frameless?.render({ force: true });
},
getTooltipDelay(),
);