196 lines
5.8 KiB
JavaScript
196 lines
5.8 KiB
JavaScript
import { Imperium5Utility } from "./imperium5-utility.js";
|
|
|
|
/* -------------------------------------------- */
|
|
/**
|
|
* Extend the basic ItemSheet with some very simple modifications
|
|
* @extends {ItemSheet}
|
|
*/
|
|
export class Imperium5ItemSheet extends ItemSheet {
|
|
|
|
/* -------------------------------------------- */
|
|
/** @override */
|
|
static get defaultOptions() {
|
|
|
|
return mergeObject(super.defaultOptions, {
|
|
classes: ["fvtt-imperium5", "sheet", "item"],
|
|
template: "systems/fvtt-imperium5/templates/item-sheet.html",
|
|
dragDrop: [{ dragSelector: null, dropSelector: null }],
|
|
width: 620,
|
|
height: 550,
|
|
tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}]
|
|
});
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
_getHeaderButtons() {
|
|
let buttons = super._getHeaderButtons();
|
|
// Add "Post to chat" button
|
|
// We previously restricted this to GM and editable items only. If you ever find this comment because it broke something: eh, sorry!
|
|
buttons.unshift(
|
|
{
|
|
class: "post",
|
|
icon: "fas fa-comment",
|
|
onclick: ev => { }
|
|
})
|
|
return buttons
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
/** @override */
|
|
setPosition(options = {}) {
|
|
const position = super.setPosition(options);
|
|
const sheetBody = this.element.find(".sheet-body");
|
|
const bodyHeight = position.height - 192;
|
|
sheetBody.css("height", bodyHeight);
|
|
if (this.item.type.includes('weapon')) {
|
|
position.width = 640;
|
|
}
|
|
return position;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
async getData() {
|
|
const objectData = Imperium5Utility.data(this.object);
|
|
|
|
let itemData = foundry.utils.deepClone(Imperium5Utility.templateData(this.object));
|
|
let formData = {
|
|
title: this.title,
|
|
id: this.id,
|
|
type: objectData.type,
|
|
img: objectData.img,
|
|
name: objectData.name,
|
|
editable: this.isEditable,
|
|
cssClass: this.isEditable ? "editable" : "locked",
|
|
data: itemData,
|
|
limited: this.object.limited,
|
|
options: this.options,
|
|
owner: this.document.isOwner,
|
|
isGM: game.user.isGM
|
|
}
|
|
|
|
this.options.editable = !(this.object.data.origin == "embeddedItem");
|
|
console.log("ITEM DATA", formData, this);
|
|
return formData;
|
|
}
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
_getHeaderButtons() {
|
|
let buttons = super._getHeaderButtons();
|
|
buttons.unshift({
|
|
class: "post",
|
|
icon: "fas fa-comment",
|
|
onclick: ev => this.postItem()
|
|
});
|
|
return buttons
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
postItem() {
|
|
let chatData = duplicate( Imperium5Utility.data(this.item) )
|
|
if (this.actor) {
|
|
chatData.actor = { id: this.actor.id }
|
|
}
|
|
// Don't post any image for the item (which would leave a large gap) if the default image is used
|
|
if (chatData.img.includes("/blank.png")) {
|
|
chatData.img = null
|
|
}
|
|
// JSON object for easy creation
|
|
chatData.jsondata = JSON.stringify(
|
|
{
|
|
compendium: "postedItem",
|
|
payload: chatData,
|
|
});
|
|
|
|
renderTemplate('systems/fvtt-imperium5/templates/post-item.html', chatData).then(html => {
|
|
let chatOptions = Imperium5Utility.chatDataSetup(html);
|
|
ChatMessage.create(chatOptions)
|
|
});
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
async viewSubitem(ev) {
|
|
let field = $(ev.currentTarget).data('type');
|
|
let idx = Number($(ev.currentTarget).data('index'));
|
|
let itemData = this.object.data.data[field][idx];
|
|
if (itemData.name != 'None') {
|
|
let spec = await Item.create(itemData, { temporary: true });
|
|
spec.data.origin = "embeddedItem";
|
|
new Imperium5ItemSheet(spec).render(true);
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
async deleteSubitem(ev) {
|
|
let field = $(ev.currentTarget).data('type');
|
|
let idx = Number($(ev.currentTarget).data('index'));
|
|
let oldArray = this.object.data.data[field];
|
|
let itemData = this.object.data.data[field][idx];
|
|
if (itemData.name != 'None') {
|
|
let newArray = [];
|
|
for (var i = 0; i < oldArray.length; i++) {
|
|
if (i != idx) {
|
|
newArray.push(oldArray[i]);
|
|
}
|
|
}
|
|
this.object.update({ [`data.${field}`]: newArray });
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
async manageSpec() {
|
|
let itemData = this.object.data.data.specialisation[0];
|
|
if (itemData.name != 'None') {
|
|
let spec = await Item.create(itemData, { temporary: true });
|
|
spec.data.origin = "embeddedItem";
|
|
new Imperium5ItemSheet(spec).render(true);
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
/** @override */
|
|
activateListeners(html) {
|
|
super.activateListeners(html);
|
|
|
|
// Everything below here is only needed if the sheet is editable
|
|
if (!this.options.editable) return;
|
|
|
|
|
|
// Update Inventory Item
|
|
html.find('.item-edit').click(ev => {
|
|
const li = $(ev.currentTarget).parents(".item");
|
|
const item = this.object.options.actor.getOwnedItem(li.data("item-id"));
|
|
item.sheet.render(true);
|
|
})
|
|
|
|
html.find('.delete-subitem').click(ev => {
|
|
this.deleteSubitem(ev);
|
|
})
|
|
|
|
// Update Inventory Item
|
|
html.find('.item-delete').click(ev => {
|
|
const li = $(ev.currentTarget).parents(".item");
|
|
let itemId = li.data("item-id");
|
|
let itemType = li.data("item-type");
|
|
})
|
|
|
|
}
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
async _onDrop(event) {
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
get template() {
|
|
let type = this.item.type;
|
|
return `systems/fvtt-imperium5/templates/item-default-sheet.html`;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
/** @override */
|
|
_updateObject(event, formData) {
|
|
return this.object.update(formData);
|
|
}
|
|
}
|