fvtt-hawkmoon-cyd/modules/hawkmoon-item-sheet.js

228 lines
8.1 KiB
JavaScript
Raw Normal View History

2022-10-22 11:09:48 +02:00
import { HawkmoonUtility } from "./hawkmoon-utility.js";
/**
* Extend the basic ItemSheet with some very simple modifications
* @extends {ItemSheet}
*/
export class HawkmoonItemSheet extends ItemSheet {
/** @override */
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
2022-10-23 09:55:02 +02:00
classes: ["fvtt-hawkmoon-cyd", "sheet", "item"],
2022-10-22 11:09:48 +02:00
template: "systems/fvtt-hawkmoon-cyd/templates/item-sheet.html",
dragDrop: [{ dragSelector: null, dropSelector: null }],
width: 620,
2022-10-23 21:26:18 +02:00
height: 550,
tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}]
2022-10-22 11:09:48 +02:00
});
}
/* -------------------------------------------- */
_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 = duplicate(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",
attributs: HawkmoonUtility.getAttributs(),
system: objectData.system,
limited: this.object.limited,
options: this.options,
owner: this.document.isOwner,
description: await TextEditor.enrichHTML(this.object.system.description, {async: true}),
mr: (this.object.type == 'specialisation'),
isGM: game.user.isGM
}
if ( objectData.type == "don") {
formData.sacrifice = await TextEditor.enrichHTML(this.object.system.sacrifice, {async: true})
}
//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() {
let chatData = duplicate(HawkmoonUtility.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-Hawkmoon-rpg/templates/post-item.html', chatData).then(html => {
let chatOptions = HawkmoonUtility.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-subitem').click(ev => {
this.deleteSubitem(ev);
})
2022-10-24 17:34:04 +02:00
html.find('.edit-predilection').change(ev => {
2022-10-22 11:09:48 +02:00
const li = $(ev.currentTarget).parents(".prediction-item")
let index = li.data("prediction-index")
let pred = duplicate(this.object.system.predilections)
pred[index].name = ev.currentTarget.value
2022-10-25 17:54:13 +02:00
pred[index].id = pred[index].id || randomID(16)
this.object.update( { 'system.predilections': pred })
2022-10-22 11:09:48 +02:00
})
2022-10-24 17:34:04 +02:00
html.find('.edit-predilection-description').change(ev => {
const li = $(ev.currentTarget).parents(".prediction-item")
let index = li.data("prediction-index")
let pred = duplicate(this.object.system.predilections)
pred[index].description = ev.currentTarget.value
2022-10-25 17:54:13 +02:00
pred[index].id = pred[index].id || randomID(16)
this.object.update( { 'system.predilections': pred })
2022-10-28 21:44:49 +02:00
})
2022-10-25 17:54:13 +02:00
html.find('.predilection-acquise').change(ev => {
2022-10-22 11:09:48 +02:00
const li = $(ev.currentTarget).parents(".prediction-item")
let index = li.data("prediction-index")
let pred = duplicate(this.object.system.predilections)
2022-10-25 17:54:13 +02:00
pred[index].acquise = ev.currentTarget.checked
pred[index].id = pred[index].id || randomID(16)
this.object.update( { 'system.predilections': pred })
2022-10-28 21:44:49 +02:00
})
2022-10-24 17:34:04 +02:00
html.find('.predilection-maitrise').change(ev => {
const li = $(ev.currentTarget).parents(".prediction-item")
let index = li.data("prediction-index")
let pred = duplicate(this.object.system.predilections)
pred[index].maitrise = ev.currentTarget.checked
2022-10-25 17:54:13 +02:00
pred[index].id = pred[index].id || randomID(16)
this.object.update( { 'system.predilections': pred })
2022-10-24 17:34:04 +02:00
})
html.find('.predilection-used').change(ev => {
2022-10-22 11:09:48 +02:00
const li = $(ev.currentTarget).parents(".prediction-item")
let index = li.data("prediction-index")
let pred = duplicate(this.object.system.predilections)
pred[index].used = ev.currentTarget.checked
2022-10-25 17:54:13 +02:00
pred[index].id = pred[index].id || randomID(16)
this.object.update( { 'system.predilections': pred })
2022-10-22 11:09:48 +02:00
})
2022-10-25 17:54:13 +02:00
2022-10-28 21:44:49 +02:00
html.find('#add-predilection').click(ev => {
let pred = duplicate(this.object.system.predilections)
pred.push( { name: "Nouvelle prédilection", id: randomID(16), used: false })
this.object.update( { 'system.predilections': pred })
})
2022-10-25 17:54:13 +02:00
html.find('.delete-prediction').click(ev => {
const li = $(ev.currentTarget).parents(".prediction-item")
let index = li.data("prediction-index")
let pred = duplicate(this.object.system.predilections)
pred.splice(index,1)
this.object.update( { 'system.predilections': pred })
})
2022-10-28 21:44:49 +02:00
html.find('#add-automation').click(ev => {
let autom = duplicate(this.object.system.automations)
2023-08-16 13:02:06 +02:00
autom.push( { eventtype: "on-drop", name: "Automatisation 1", bonusname: "vigueur", bonus: 0, competence: "", minLevel: 0, baCost: 0, id: randomID(16) })
2022-10-28 21:44:49 +02:00
this.object.update( { 'system.automations': autom })
})
html.find('.delete-automation').click(ev => {
const li = $(ev.currentTarget).parents(".automation-item")
let index = li.data("automation-index")
let autom = duplicate(this.object.system.automations)
autom.splice(index,1)
this.object.update( { 'system.automations': autom })
2022-10-22 11:09:48 +02:00
})
2022-10-28 21:44:49 +02:00
html.find('.automation-edit-field').change(ev => {
2022-11-05 10:03:23 +01:00
let index = $(ev.currentTarget).data("automation-index")
let field = $(ev.currentTarget).data("automation-field")
2022-10-28 21:44:49 +02:00
let auto = duplicate(this.object.system.automations)
auto[index][field] = ev.currentTarget.value
auto[index].id = auto[index].id || randomID(16)
this.object.update( { 'system.automations': auto })
})
2022-10-22 11:09:48 +02:00
// 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");
});
}
/* -------------------------------------------- */
get template() {
let type = this.item.type;
2022-10-23 09:55:02 +02:00
return `systems/fvtt-hawkmoon-cyd/templates/item-${type}-sheet.html`;
2022-10-22 11:09:48 +02:00
}
/* -------------------------------------------- */
/** @override */
_updateObject(event, formData) {
return this.object.update(formData);
}
}