kopparhavet/module/helpers/item-helpers.js

35 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-11-28 19:12:19 +01:00
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?.data?.modifications) {
for (let k of Object.keys(this.object.data.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);
}
}