kopparhavet/module/helpers/item-helpers.js

35 lines
1.1 KiB
JavaScript

export default class ItemHelpers {
static async itemUpdate(event, formData) {
formData = expandObject(formData);
if (this.object.isOwned && this.object.actor?.compendium?.metadata) {
return;
}
// Handle the free-form modifications list
const formAttrs = expandObject(formData)?.data?.modifications || {};
const modifications = Object.values(formAttrs).reduce((obj, v) => {
let k = v["key"].trim();
delete v["key"];
obj[k] = v;
return obj;
}, {});
// Remove modifications which are no longer used
if (this.object.data?.modifications) {
for (let k of Object.keys(this.object.data.modifications)) {
if (!modifications.hasOwnProperty(k)) modifications[`-=${k}`] = null;
}
}
// recombine modifications to formData
if (Object.keys(modifications).length > 0) {
setProperty(formData, `data.modifications`, modifications);
}
// Update the Item
this.item.data.flags.loaded = false;
this.object.update(formData);
}
}