Migration complétée vers appv2
This commit is contained in:
@@ -1,172 +1,154 @@
|
||||
import { TeDeumUtility } from "../common/tedeum-utility.js";
|
||||
|
||||
const { HandlebarsApplicationMixin } = foundry.applications.api
|
||||
|
||||
/**
|
||||
* Extend the basic ItemSheet with some very simple modifications
|
||||
* @extends {ItemSheet}
|
||||
* Feuille d'item Te Deum - AppV2
|
||||
*/
|
||||
export class TeDeumItemSheet extends foundry.appv1.sheets.ItemSheet {
|
||||
export class TeDeumItemSheet extends HandlebarsApplicationMixin(foundry.applications.sheets.ItemSheetV2) {
|
||||
|
||||
/** @override */
|
||||
static get defaultOptions() {
|
||||
return foundry.utils.mergeObject(super.defaultOptions, {
|
||||
classes: ["fvtt-te-deum", "sheet", "item"],
|
||||
template: "systems/fvtt-te-deum/templates/item-sheet.hbs",
|
||||
dragDrop: [{ dragSelector: null, dropSelector: null }],
|
||||
static DEFAULT_OPTIONS = {
|
||||
classes: ["fvtt-te-deum", "sheet", "item"],
|
||||
position: {
|
||||
width: 620,
|
||||
height: 580,
|
||||
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }]
|
||||
});
|
||||
},
|
||||
form: {
|
||||
submitOnChange: true,
|
||||
closeOnSubmit: false,
|
||||
},
|
||||
window: {
|
||||
resizable: true,
|
||||
},
|
||||
actions: {
|
||||
editImage: TeDeumItemSheet.#onEditImage,
|
||||
postItem: TeDeumItemSheet.#onPostItem,
|
||||
deleteSubitem: TeDeumItemSheet.#onDeleteSubitem,
|
||||
viewSubitem: TeDeumItemSheet.#onViewSubitem,
|
||||
},
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
_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
|
||||
// Static PARTS pointing to the dynamic wrapper template
|
||||
static PARTS = {
|
||||
sheet: { template: "systems/fvtt-te-deum/templates/items/item-sheet.hbs" },
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async getData() {
|
||||
tabGroups = { primary: "description" }
|
||||
|
||||
let formData = {
|
||||
/* -------------------------------------------- */
|
||||
/** @override */
|
||||
async _prepareContext() {
|
||||
const item = this.document
|
||||
const TextEditor = foundry.applications.ux.TextEditor.implementation
|
||||
const enrich = async (val) => val !== undefined ? await TextEditor.enrichHTML(val ?? "", { async: true }) : ""
|
||||
const context = {
|
||||
title: this.title,
|
||||
id: this.id,
|
||||
type: this.object.type,
|
||||
img: this.object.img,
|
||||
name: this.object.name,
|
||||
id: item.id,
|
||||
type: item.type,
|
||||
img: item.img,
|
||||
name: item.name,
|
||||
editable: this.isEditable,
|
||||
cssClass: this.isEditable ? "editable" : "locked",
|
||||
system: foundry.utils.duplicate(this.object.system),
|
||||
system: foundry.utils.duplicate(item.system),
|
||||
systemFields: item.system.schema.fields,
|
||||
config: foundry.utils.duplicate(game.system.tedeum.config),
|
||||
competences: TeDeumUtility.getCompetencesForDropDown(),
|
||||
limited: this.object.limited,
|
||||
options: this.options,
|
||||
owner: this.document.isOwner,
|
||||
description: await foundry.applications.ux.TextEditor.implementation.enrichHTML(this.object.system.description, { async: true }),
|
||||
notes: await foundry.applications.ux.TextEditor.implementation.enrichHTML(this.object.system.notes, { async: true }),
|
||||
isGM: game.user.isGM
|
||||
limited: item.limited,
|
||||
owner: item.isOwner,
|
||||
enrichedDescription: await enrich(item.system.description),
|
||||
enrichedTransmission: await enrich(item.system.transmission),
|
||||
enrichedSymptomes: await enrich(item.system.symptomes),
|
||||
enrichedComplications: await enrich(item.system.complications),
|
||||
enrichedVertus: await enrich(item.system.vertus),
|
||||
enrichedToxicite: await enrich(item.system.toxicite),
|
||||
isGM: game.user.isGM,
|
||||
itemPartialName: `systems/fvtt-te-deum/templates/items/item-${item.type}-sheet.hbs`,
|
||||
}
|
||||
|
||||
if (this.object.type == "education") {
|
||||
TeDeumUtility.prepareEducationContent(formData);
|
||||
if (item.type === "education") {
|
||||
TeDeumUtility.prepareEducationContent(context)
|
||||
}
|
||||
|
||||
this.options.editable = !(this.object.origin == "embeddedItem");
|
||||
return formData;
|
||||
}
|
||||
|
||||
|
||||
/* -------------------------------------------- */
|
||||
_getHeaderButtons() {
|
||||
let buttons = super._getHeaderButtons();
|
||||
buttons.unshift({
|
||||
class: "post",
|
||||
icon: "fas fa-comment",
|
||||
onclick: ev => this.postItem()
|
||||
});
|
||||
return buttons
|
||||
return context
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
postItem() {
|
||||
let chatData = duplicate(this.item)
|
||||
/** @override */
|
||||
_onRender(context, options) {
|
||||
super._onRender(context, options)
|
||||
|
||||
// Tab navigation
|
||||
const nav = this.element.querySelector('nav.tabs[data-group]')
|
||||
if (nav) {
|
||||
const group = nav.dataset.group
|
||||
const activeTab = this.tabGroups[group] || "description"
|
||||
nav.querySelectorAll('[data-tab]').forEach(link => {
|
||||
const tab = link.dataset.tab
|
||||
link.classList.toggle('active', tab === activeTab)
|
||||
link.addEventListener('click', event => {
|
||||
event.preventDefault()
|
||||
this.tabGroups[group] = tab
|
||||
this.render()
|
||||
})
|
||||
})
|
||||
this.element.querySelectorAll(`[data-group="${group}"][data-tab]`).forEach(content => {
|
||||
content.classList.toggle('active', content.dataset.tab === activeTab)
|
||||
})
|
||||
}
|
||||
|
||||
// Ignore Enter key in inputs
|
||||
this.element.addEventListener('keydown', e => {
|
||||
if (e.keyCode === 13 && e.target.tagName !== 'TEXTAREA') e.preventDefault()
|
||||
})
|
||||
}
|
||||
|
||||
// #region Static action handlers
|
||||
|
||||
static async #onEditImage(event, target) {
|
||||
const fp = new FilePicker({
|
||||
type: "image",
|
||||
current: this.document.img,
|
||||
callback: path => this.document.update({ img: path }),
|
||||
})
|
||||
fp.browse()
|
||||
}
|
||||
|
||||
static async #onPostItem(event, target) {
|
||||
const chatData = foundry.utils.duplicate(this.item)
|
||||
if (this.actor) {
|
||||
chatData.actor = { id: this.actor.id };
|
||||
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;
|
||||
if (chatData.img?.includes("/blank.png")) {
|
||||
chatData.img = null
|
||||
}
|
||||
// JSON object for easy creation
|
||||
chatData.jsondata = JSON.stringify(
|
||||
{
|
||||
compendium: "postedItem",
|
||||
payload: chatData,
|
||||
});
|
||||
|
||||
foundry.applications.handlebars.renderTemplate('systems/fvtt-te-deum/templates/post-item.html', chatData).then(html => {
|
||||
let chatOptions = TeDeumUtility.chatDataSetup(html);
|
||||
ChatMessage.create(chatOptions)
|
||||
});
|
||||
chatData.jsondata = JSON.stringify({ compendium: "postedItem", payload: chatData })
|
||||
const html = await foundry.applications.handlebars.renderTemplate(
|
||||
'systems/fvtt-te-deum/templates/post-item.html', chatData
|
||||
)
|
||||
ChatMessage.create(TeDeumUtility.chatDataSetup(html))
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async viewSubitem(ev) {
|
||||
let levelIndex = Number($(ev.currentTarget).parents(".item").data("level-index"))
|
||||
let choiceIndex = Number($(ev.currentTarget).parents(".item").data("choice-index"))
|
||||
let featureId = $(ev.currentTarget).parents(".item").data("feature-id")
|
||||
|
||||
let itemData = this.object.system.levels[levelIndex].choices[choiceIndex].features[featureId]
|
||||
|
||||
if (itemData.name != 'None') {
|
||||
let item = await Item.create(itemData, { temporary: true });
|
||||
item.system.origin = "embeddedItem";
|
||||
new TeDeumItemSheet(item).render(true);
|
||||
static async #onDeleteSubitem(event, target) {
|
||||
const field = target.dataset.type
|
||||
const idx = parseInt(target.dataset.index)
|
||||
const oldArray = this.document.system[field]
|
||||
if (Array.isArray(oldArray) && oldArray[idx]?.name !== 'None') {
|
||||
const newArray = oldArray.filter((_, i) => i !== idx)
|
||||
this.document.update({ [`system.${field}`]: newArray })
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async deleteSubitem(ev) {
|
||||
let field = $(ev.currentTarget).data('type');
|
||||
let idx = Number($(ev.currentTarget).data('index'));
|
||||
let oldArray = this.object.system[field];
|
||||
let itemData = this.object.system[field][idx];
|
||||
if (itemData.name != 'None') {
|
||||
let newArray = [];
|
||||
for (let i = 0; i < oldArray.length; i++) {
|
||||
if (i != idx) {
|
||||
newArray.push(oldArray[i]);
|
||||
}
|
||||
}
|
||||
this.object.update({ [`system.${field}`]: newArray });
|
||||
static async #onViewSubitem(event, target) {
|
||||
const li = target.closest(".item")
|
||||
const levelIndex = parseInt(li?.dataset.levelIndex)
|
||||
const choiceIndex = parseInt(li?.dataset.choiceIndex)
|
||||
const featureId = li?.dataset.featureId
|
||||
const itemData = this.document.system.levels?.[levelIndex]?.choices?.[choiceIndex]?.features?.[featureId]
|
||||
if (itemData?.name !== 'None') {
|
||||
const item = await Item.create(itemData, { temporary: true })
|
||||
item.system.origin = "embeddedItem"
|
||||
new TeDeumItemSheet(item).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");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
get template() {
|
||||
let type = this.item.type;
|
||||
return `systems/fvtt-te-deum/templates/items/item-${type}-sheet.hbs`
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
/** @override */
|
||||
_updateObject(event, formData) {
|
||||
return this.object.update(formData)
|
||||
}
|
||||
// #endregion
|
||||
}
|
||||
Reference in New Issue
Block a user