ripcrypt/module/Apps/utils.mjs
Oliver c23c67280f
Merge pull request #60 from Eldritch-Oliver/feature/embedded-item-creation
Add a way to create items from within the Hero sheet directly
2025-10-07 19:15:56 -06:00

65 lines
1.7 KiB
JavaScript

/*
This file contains utilities used by Applications in order to be DRYer
*/
/**
* @param {HTMLElement} target The element to operate on
*/
export async function createItemFromElement(target, { parent } = {}) {
const data = target.dataset;
const types = data.itemTypes?.split(`,`);
const type = data.defaultItemType;
await Item.createDialog(
{ type },
{ parent, showEquipPrompt: false },
{
types,
folders: [],
},
);
};
/**
* @param {HTMLElement} target The element to operate on
*/
export async function editItemFromElement(target) {
const itemEl = target.closest(`[data-item-id]`);
if (!itemEl) { return };
const itemId = itemEl.dataset.itemId;
if (!itemId) { return };
const item = await fromUuid(itemId);
item.sheet.render({ force: true, orBringToFront: true });
};
/**
* @param {HTMLElement} target The element to operate on
*/
export async function deleteItemFromElement(target) {
const itemEl = target.closest(`[data-item-id]`);
if (!itemEl) { return };
const itemId = itemEl.dataset.itemId;
if (!itemId) { return };
const item = await fromUuid(itemId);
item.deleteDialog();
};
/**
* 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 });
};