fvtt-dark-stars/modules/dark-stars-item-sheet.js

153 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-11-27 08:58:19 +01:00
import { DarkStarsUtility } from "./dark-stars-utility.js";
/**
* Extend the basic ItemSheet with some very simple modifications
* @extends {ItemSheet}
*/
export class DarkStarsItemSheet extends ItemSheet {
/** @override */
static get defaultOptions() {
2024-06-10 14:30:12 +02:00
return foundry.utils.mergeObject(super.defaultOptions, {
2022-11-27 08:58:19 +01:00
classes: ["fvtt-dark-stars", "sheet", "item"],
2022-12-04 12:16:25 +01:00
template: "systems/fvtt-dark-stars/templates/items/item-sheet.hbs",
2022-11-27 08:58:19 +01:00
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() {
2024-06-10 14:30:12 +02:00
let objectData = foundry.utils.duplicate(this.object.system)
2022-11-27 08:58:19 +01:00
let formData = {
title: this.title,
id: this.id,
type: this.object.type,
img: this.object.img,
name: this.object.name,
editable: this.isEditable,
cssClass: this.isEditable ? "editable" : "locked",
2022-12-27 09:16:53 +01:00
skills: DarkStarsUtility.getSkills(),
2022-11-27 08:58:19 +01:00
description: await TextEditor.enrichHTML(this.object.system.description, {async: true}),
2022-12-04 12:16:25 +01:00
config: game.system.darkstars.config,
system: objectData,
2022-11-27 08:58:19 +01:00
limited: this.object.limited,
options: this.options,
owner: this.document.isOwner,
isGM: game.user.isGM
}
this.options.editable = !(this.object.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() {
2024-06-10 14:30:12 +02:00
let chatData = foundry.utils.duplicate(DarkStarsUtility.data(this.item));
2022-11-27 08:58:19 +01:00
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,
});
2022-12-04 12:16:25 +01:00
renderTemplate('systems/fvtt-dark-stars/templates/items/post-item.hbs', chatData).then(html => {
2022-11-27 08:58:19 +01:00
let chatOptions = DarkStarsUtility.chatDataSetup(html);
ChatMessage.create(chatOptions)
});
}
/* -------------------------------------------- */
/** @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-spec').click(ev => {
this.object.update({ "data.specialisation": [{ name: 'None' }] });
});
html.find('.delete-subitem').click(ev => {
this.deleteSubitem(ev);
});
html.find('.view-subitem').click(ev => {
this.viewSubitem(ev);
});
}
/* -------------------------------------------- */
get template() {
let type = this.item.type;
2022-12-04 12:16:25 +01:00
return `systems/fvtt-dark-stars/templates/items/item-${type}-sheet.hbs`;
2022-11-27 08:58:19 +01:00
}
/* -------------------------------------------- */
/** @override */
_updateObject(event, formData) {
return this.object.update(formData)
}
}