kopparhavet/module/items/item-sheet.js

221 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-11-28 19:12:19 +01:00
/**
* 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, {
2020-11-30 06:23:35 +01:00
classes: ["kopparhavet", "sheet", "item"],
2020-11-28 19:12:19 +01:00
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
if(game.settings.get("kopparhavet", "gameSystem") === "hjaltarnas-tid") {
skillList2 = await game.packs.get("kopparhavet.skills-ht").getDocuments();
} else {
skillList2 = await game.packs.get("kopparhavet.skills").getDocuments();
}
2020-11-28 19:12:19 +01:00
for (let item of skillList2) {
if(item.data.type === "skill" && item.data.data.type.value === "combat") {
skillList.push(item.data)
2020-11-28 19:12:19 +01:00
}
}
// 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.data)
2020-11-28 19:12:19 +01:00
}
}
2020-11-28 19:12:19 +01:00
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;
case "spell":
// Load Skills Compendium skills
let skillList3
if(game.settings.get("kopparhavet", "gameSystem") === "hjaltarnas-tid") {
skillList3 = await game.packs.get("kopparhavet.skills-ht").getDocuments();
} else {
skillList3 = await game.packs.get("kopparhavet.skills").getDocuments();
}
for (let item of skillList3) {
if(item.data.type === "skill") {
skillList.push(item.data)
}
}
// Retrieve any created skills as well
for (let item of game.items.entities) {
if(item.data.data.type === "skill") {
skillList.push(item.data)
}
}
this.position.width = 450;
this.position.height = 605;
break;
case "shipweapon":
// Load Skills Compendium skills
let skillList4
if(game.settings.get("kopparhavet", "gameSystem") === "hjaltarnas-tid") {
skillList4 = await game.packs.get("kopparhavet.skills-ht").getDocuments();
} else {
skillList4 = await game.packs.get("kopparhavet.skills").getDocuments();
}
for (let item of skillList4) {
if(item.data.type === "skill" && item.data.data.type.value === "combat") {
skillList.push(item.data)
}
}
// 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.data)
}
}
this.position.width = 530;
this.position.height = 750;
break;
case "shiptalent":
this.position.width = 405;
this.position.height = 570;
break;
2020-11-28 19:12:19 +01:00
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);
2020-11-30 06:23:35 +01:00
2020-11-28 19:12:19 +01:00
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;
}
}