From 053ab05dcba53ff83f00cf0fbd3c2a094dcfc5a4 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Wed, 9 Apr 2025 21:51:49 -0600 Subject: [PATCH] Pull the foreign document updating into a utility method and add it to the GenericPopoverMixin --- module/Apps/GenericApp.mjs | 20 ++----------------- module/Apps/popovers/GenericPopoverMixin.mjs | 19 ++++++++++++++++++ module/Apps/utils.mjs | 21 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/module/Apps/GenericApp.mjs b/module/Apps/GenericApp.mjs index c131baa..1fe5edf 100644 --- a/module/Apps/GenericApp.mjs +++ b/module/Apps/GenericApp.mjs @@ -1,4 +1,4 @@ -import { createItemFromElement, deleteItemFromElement, editItemFromElement } from "./utils.mjs"; +import { createItemFromElement, deleteItemFromElement, editItemFromElement, updateForeignDocumentFromEvent } from "./utils.mjs"; import { DicePool } from "./DicePool.mjs"; import { RichEditor } from "./RichEditor.mjs"; import { toBoolean } from "../consts.mjs"; @@ -77,7 +77,7 @@ export function GenericAppMixin(HandlebarsApp) { this.element.querySelectorAll(`input[data-foreign-update-on]`).forEach(el => { const events = el.dataset.foreignUpdateOn.split(`,`); for (const event of events) { - el.addEventListener(event, this.updateEmbedded); + el.addEventListener(event, updateForeignDocumentFromEvent); }; }); }; @@ -150,22 +150,6 @@ export function GenericAppMixin(HandlebarsApp) { }); app.render({ force: true }); }; - - /** - * @param {Event} event - */ - async updateForeign(event) { - const target = event.currentTarget; - const data = target.dataset; - const document = await fromUuid(data.foreignUuid); - - let value = target.value; - switch (target.type) { - case `checkbox`: value = target.checked; break; - }; - - await document?.update({ [data.foreignName]: value }); - }; // #endregion }; return GenericRipCryptApp; diff --git a/module/Apps/popovers/GenericPopoverMixin.mjs b/module/Apps/popovers/GenericPopoverMixin.mjs index a64b067..271bb9a 100644 --- a/module/Apps/popovers/GenericPopoverMixin.mjs +++ b/module/Apps/popovers/GenericPopoverMixin.mjs @@ -1,3 +1,5 @@ +import { updateForeignDocumentFromEvent } from "../utils.mjs"; + const { ApplicationV2 } = foundry.applications.api; /** @@ -65,6 +67,23 @@ export function GenericPopoverMixin(HandlebarsApp) { }; }; + async _onRender(...args) { + await super._onRender(...args); + + /* + Foreign update listeners so that we can easily update items that may not + be this document itself, but are useful to be able to be edited from this + sheet. Primarily useful for editing the Actors' Item collection, or an Items' + ActiveEffect collection. + */ + this.element.querySelectorAll(`input[data-foreign-update-on]`).forEach(el => { + const events = el.dataset.foreignUpdateOn.split(`,`); + for (const event of events) { + el.addEventListener(event, updateForeignDocumentFromEvent); + }; + }); + }; + async close(options = {}) { // prevent locked popovers from being closed if (this.popover.locked && !options.force) { return }; diff --git a/module/Apps/utils.mjs b/module/Apps/utils.mjs index 1baee18..7c55ec4 100644 --- a/module/Apps/utils.mjs +++ b/module/Apps/utils.mjs @@ -42,3 +42,24 @@ export async function deleteItemFromElement(target) { const item = await fromUuid(itemId); item.delete(); }; + +/** + * Updates a document using the UUID, expects there to be the following + * dataset attributes: + * - "data-foreign-uuid" : The UUID of the document to update + * - "data-foreign-name" : The dot-separated path of the value to update + * + * @param {Event} event + */ +export async function updateForeignDocumentFromEvent(event) { + const target = event.currentTarget; + const data = target.dataset; + const document = await fromUuid(data.foreignUuid); + + let value = target.value; + switch (target.type) { + case `checkbox`: value = target.checked; break; + }; + + await document?.update({ [data.foreignName]: value }); +};