kopparhavet/module/items/item-sheet.js

159 lines
5.1 KiB
JavaScript

/**
* Extend the basic ItemSheet
* @extends {ItemSheet}
*/
import ItemHelpers from "../helpers/item-helpers.js";
export class ItemSheetKH extends ItemSheet {
/** @override */
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
classes: ["kopparhavet", "sheet", "item"],
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }],
scrollY: [".sheet-body", ".tab"],
});
}
/** @override */
get template() {
const path = "systems/kopparhavet/templates/items";
return `${path}/${this.item.data.type}-sheet.html`;
}
/** @override */
async getData() {
const data = super.getData();
let skillList = [];
data.dtypes = ["String", "Number", "Boolean"];
if (data?.data?.modifications) {
for (let attr of Object.values(data.data.modifications)) {
attr.isCheckbox = attr.dtype === "Boolean";
}
}
switch (this.object.data.type) {
case "weapon":
// Load Skills Compendium skills
let skillList2 = await game.packs.get("kopparhavet.skills").getContent();
for (let item of skillList2) {
if(item.data.type === "skill" && item.data.data.type.value === "combat") {
skillList.push(item)
}
}
// Retrieve any created skills as well
for (let item of game.items.entities) {
if(item.data.type === "skill" && item.data.data.type.value === "combat") {
skillList.push(item)
}
}
this.position.width = 530;
this.position.height = 750;
break;
case "armor":
this.position.width = 385;
this.position.height = 628;
break;
case "gear":
case "skill":
this.position.width = 385;
this.position.height = 606;
break;
case "talent":
this.position.width = 405;
this.position.height = 570;
break;
default:
this.position.width = 450;
this.position.height = 605;
break;
}
data.khskills = skillList;
data.KH = CONFIG.KH;
return data;
}
activateListeners(html) {
super.activateListeners(html);
// Armor related
html.find(".item-bool-click").click(async (ev) => {
const clickedName = $(ev.currentTarget).data("name");
if(clickedName === "equipped") {
const equppiedValue = this.item.data.data.equipable.equipped;
this.item.update({ "data.equipped.equipped": !equppiedValue });
} else {
const clickedValue = (this.item.data.data[clickedName].value == undefined ? false : this.item.data.data[clickedName].value);
let dataName = "data." + clickedName + ".value"
let tempData = {}
tempData[dataName] = !clickedValue
this.item.update(tempData);
}
this._render();
});
// Add or Remove modification
html.find(".modification-control").click(this._onClickModificationControl.bind(this));
}
/**
* DOM event
* @param {object} event
*/
async _onClickModificationControl(event) {
event.preventDefault();
const a = event.currentTarget;
const action = a.dataset.action;
const attrs = this.object.data.data.modifications;
const form = this.form;
// Add new modification
if (action === "create") {
const nk = new Date().getTime();
let newKey = document.createElement("div");
newKey.innerHTML = `<input type="text" name="data.modifications.attr${nk}.key" value="attr${nk}" style="display:none;"/><select class="modification-modtype" name="data.modifications.attr${nk}.modtype"><option value="init">Turordning</option></select><input class="modification-value" type="text" name="data.modifications.attr${nk}.value" value="0" data-dtype="Number" placeholder="0"/>`;
form.appendChild(newKey);
await this._onSubmit(event);
}
// Remove existing modification
else if (action === "delete") {
const li = a.closest(".modification");
li.parentElement.removeChild(li);
await this._onSubmit(event);
}
}
/** @override */
_updateObject(event, formData) {
const itemUpdate = ItemHelpers.itemUpdate.bind(this);
itemUpdate(event, formData);
}
_getHeaderButtons() {
let buttons = super._getHeaderButtons();
buttons = [
{
label: "Post Item",
class: "item-post",
icon: "fas fa-comment",
onclick: (ev) => this.item.sendToChat(),
}
].concat(buttons);
return buttons;
}
}