Files
fvtt-ecryme/modules/items/sheets/base-item-sheet.js

132 lines
3.6 KiB
JavaScript

const { HandlebarsApplicationMixin } = foundry.applications.api
/**
* Base item sheet for Ecryme using Application V2.
* Subclasses must define static PARTS including header, tabs, description, and optionally details.
*/
export default class EcrymeBaseItemSheet extends HandlebarsApplicationMixin(foundry.applications.sheets.ItemSheetV2) {
constructor(options = {}) {
super(options)
this.#dragDrop = this.#createDragDropHandlers()
}
#dragDrop
/** @override */
static DEFAULT_OPTIONS = {
classes: ["fvtt-ecryme", "item"],
position: {
width: 520,
height: "auto",
},
form: {
submitOnChange: true,
},
window: {
resizable: true,
},
dragDrop: [{ dragSelector: "[data-drag]", dropSelector: null }],
actions: {
editImage: EcrymeBaseItemSheet.#onEditImage,
},
}
/** Active tab group tracking */
tabGroups = {
primary: "description",
}
/**
* Build the tabs definition, adding a "details" tab only if the subclass has a "details" PART.
* @returns {Record<string, object>}
*/
_getTabs() {
const tabs = {
description: { id: "description", group: "primary", label: "ECRY.ui.description" },
}
if (this.constructor.PARTS?.details) {
tabs.details = { id: "details", group: "primary", label: "ECRY.ui.details" }
}
for (const tab of Object.values(tabs)) {
tab.active = this.tabGroups[tab.group] === tab.id
tab.cssClass = tab.active ? "active" : ""
}
return tabs
}
/** @override */
async _prepareContext() {
const context = {
fields: this.document.schema.fields,
systemFields: this.document.system.schema.fields,
item: this.document,
system: this.document.system,
source: this.document.toObject(),
config: game.system.ecryme.config,
isEditable: this.isEditable,
tabs: this._getTabs(),
}
return context
}
/** @override */
async _preparePartContext(partId, context) {
context = await super._preparePartContext(partId, context)
if (partId === "description") {
context.tab = context.tabs.description
context.enrichedDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(
this.document.system.description, { async: true }
)
}
return context
}
/** @override */
_onRender(context, options) {
this.#dragDrop.forEach((d) => d.bind(this.element))
}
// #region Drag-and-Drop
#createDragDropHandlers() {
return this.options.dragDrop.map((d) => {
d.permissions = {
dragstart: this._canDragStart.bind(this),
drop: this._canDragDrop.bind(this),
}
d.callbacks = {
dragstart: this._onDragStart.bind(this),
dragover: this._onDragOver.bind(this),
drop: this._onDrop.bind(this),
}
return new foundry.applications.ux.DragDrop.implementation(d)
})
}
_canDragStart(selector) { return this.isEditable }
_canDragDrop(selector) { return this.isEditable && this.document.isOwner }
_onDragStart(event) {}
_onDragOver(event) {}
async _onDrop(event) {}
// #endregion
// #region Actions
static async #onEditImage(event, target) {
const attr = target.dataset.edit
const current = foundry.utils.getProperty(this.document, attr)
const { img } = this.document.constructor.getDefaultArtwork?.(this.document.toObject()) ?? {}
const fp = new FilePicker({
current,
type: "image",
redirectToRoot: img ? [img] : [],
callback: (path) => {
this.document.update({ [attr]: path })
},
top: this.position.top + 40,
left: this.position.left + 10,
})
return fp.browse()
}
// #endregion
}