Pull the foreign document updating into a utility method and add it to the GenericPopoverMixin

This commit is contained in:
Oliver-Akins 2025-04-09 21:51:49 -06:00
parent 55cff3c048
commit 053ab05dcb
3 changed files with 42 additions and 18 deletions

View file

@ -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;

View file

@ -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 };

View file

@ -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 });
};