foundryvtt-beam-saber/module/blades-sheet.js
Megastruktur 009b731b7e GH-76
- Closes #76
- Fixes the "Add multiple items" bug

When multiple items were added, the foreach mechanism caused the
actor to update multiple times and rewrite previously added items.
That's where some items were not actually added to an Actor (the
UI was working fine but not the backend part). So it seemed that
items are added (you can see them on the UI) but actuallythey
were not.
Now everything should work fine.
2020-09-17 10:40:26 +03:00

107 lines
3.1 KiB
JavaScript

/**
* Extend the basic ActorSheet with some very simple modifications
* @extends {ActorSheet}
*/
export class BladesSheet extends ActorSheet {
/* -------------------------------------------- */
/** @override */
activateListeners(html) {
super.activateListeners(html);
html.find(".item-add-popup").click(this._onItemAddClick.bind(this));
// This is a workaround until is being fixed in FoundryVTT.
if ( this.options.submitOnChange ) {
html.on("change", "textarea", this._onChangeInput.bind(this)); // Use delegated listener on the form
}
html.find(".roll-die-attribute").click(this._onRollAttributeDieClick.bind(this));
}
/* -------------------------------------------- */
async _onItemAddClick(event) {
event.preventDefault();
const item_type = $(event.currentTarget).data("itemType")
const distinct = $(event.currentTarget).data("distinct")
let input_type = "checkbox";
if (typeof distinct !== "undefined") {
input_type = "radio";
}
let items = await BladesHelpers.getAllItemsByType(item_type, game);
let html = `<div id="items-to-add">`;
items.forEach(e => {
let addition_price_load = ``;
if (typeof e.data.load !== "undefined") {
addition_price_load += `(${e.data.load})`
} else if (typeof e.data.price !== "undefined") {
addition_price_load += `(${e.data.price})`
}
html += `<input id="select-item-${e._id}" type="${input_type}" name="select_items" value="${e._id}">`;
html += `<label class="flex-horizontal" for="select-item-${e._id}">`;
html += `${game.i18n.localize(e.name)} ${addition_price_load} <i class="tooltip fas fa-question-circle"><span class="tooltiptext">${game.i18n.localize(e.data.description)}</span></i>`;
html += `</label>`;
});
html += `</div>`;
let options = {
// width: "500"
}
let dialog = new Dialog({
title: `${game.i18n.localize('Add')} ${item_type}`,
content: html,
buttons: {
one: {
icon: '<i class="fas fa-check"></i>',
label: game.i18n.localize('Add'),
callback: () => this.addItemsToSheet(item_type, $(document).find('#items-to-add'))
},
two: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize('Cancel'),
callback: () => false
}
},
default: "two"
}, options);
dialog.render(true);
}
/* -------------------------------------------- */
async addItemsToSheet(item_type, el) {
let items = await BladesHelpers.getAllItemsByType(item_type, game);
let items_to_add = [];
el.find("input:checked").each(function() {
items_to_add.push(items.find(e => e._id === $(this).val()));
});
this.actor.createEmbeddedEntity("OwnedItem", items_to_add);
}
/* -------------------------------------------- */
/**
* Roll an Attribute die.
* @param {*} event
*/
async _onRollAttributeDieClick(event) {
const attribute_name = $(event.currentTarget).data("rollAttribute");
this.actor.rollAttributePopup(attribute_name);
}
/* -------------------------------------------- */
}