mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-14 12:41:07 +01:00
FEAT: improvement of DHApplicationMixin
FEAT: create a new DHBaseItemSheet
This commit is contained in:
parent
3464717958
commit
8574a1d93f
15 changed files with 381 additions and 162 deletions
2
module/applications/sheets/api/_modules.mjs
Normal file
2
module/applications/sheets/api/_modules.mjs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export {default as DHApplicationMixin} from "./application-mixin.mjs";
|
||||
export {default as DHBaseItemSheet} from "./base-item.mjs";
|
||||
102
module/applications/sheets/api/application-mixin.mjs
Normal file
102
module/applications/sheets/api/application-mixin.mjs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
const { HandlebarsApplicationMixin } = foundry.applications.api;
|
||||
|
||||
/**
|
||||
* @typedef {object} DragDropConfig
|
||||
* @property {string|null} dragSelector - A CSS selector that identifies draggable elements.
|
||||
* @property {string|null} dropSelector - A CSS selector that identifies drop targets.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import("@client/applications/api/handlebars-application.mjs").HandlebarsRenderOptions} HandlebarsRenderOptions
|
||||
* @typedef {foundry.applications.types.ApplicationConfiguration & HandlebarsRenderOptions & { dragDrop?: DragDropConfig[] }} DHSheetV2Configuration
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {Constructor<foundry.applications.api.DocumentSheet>} BaseDocumentSheet
|
||||
* @param {BaseDocumentSheet} Base - The base class to extend.
|
||||
* @returns {BaseDocumentSheet}
|
||||
*/
|
||||
export default function DHApplicationMixin(Base) {
|
||||
class DHSheetV2 extends HandlebarsApplicationMixin(Base) {
|
||||
/**
|
||||
* @param {DHSheetV2Configuration} [options={}]
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
super(options);
|
||||
/**
|
||||
* @type {foundry.applications.ux.DragDrop[]}
|
||||
* @private
|
||||
*/
|
||||
this._dragDrop = this._createDragDropHandlers();
|
||||
}
|
||||
|
||||
/**
|
||||
* The default options for the sheet.
|
||||
* @type {DHSheetV2Configuration}
|
||||
*/
|
||||
static DEFAULT_OPTIONS = {
|
||||
classes: ['daggerheart', 'sheet', 'dh-style'],
|
||||
position: {
|
||||
width: 480,
|
||||
height: 'auto'
|
||||
},
|
||||
|
||||
dragDrop: []
|
||||
};
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/**@inheritdoc */
|
||||
_attachPartListeners(partId, htmlElement, options) {
|
||||
super._attachPartListeners(partId, htmlElement, options);
|
||||
this._dragDrop.forEach(d => d.bind(htmlElement));
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
/* Drag and Drop */
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Creates drag-drop handlers from the configured options.
|
||||
* @returns {foundry.applications.ux.DragDrop[]}
|
||||
* @private
|
||||
*/
|
||||
_createDragDropHandlers() {
|
||||
return this.options.dragDrop.map(d => {
|
||||
d.callbacks = {
|
||||
drop: this._onDrop.bind(this)
|
||||
};
|
||||
return new foundry.applications.ux.DragDrop.implementation(d);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle drop event.
|
||||
* @param {DragEvent} event
|
||||
* @protected
|
||||
*/
|
||||
_onDrop(event) { }
|
||||
|
||||
/* -------------------------------------------- */
|
||||
/* Prepare Context */
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Prepare the template context.
|
||||
* @param {object} options
|
||||
* @param {string} [objectPath='document']
|
||||
* @returns {Promise<object>}
|
||||
* @inheritdoc
|
||||
*/
|
||||
async _prepareContext(options, objectPath = 'document') {
|
||||
const context = await super._prepareContext(options);
|
||||
context.config = CONFIG.daggerheart;
|
||||
context.source = this[objectPath];
|
||||
context.fields = this[objectPath].schema.fields;
|
||||
context.systemFields = this[objectPath].system ? this[objectPath].system.schema.fields : {};
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
return DHSheetV2;
|
||||
}
|
||||
129
module/applications/sheets/api/base-item.mjs
Normal file
129
module/applications/sheets/api/base-item.mjs
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
import DHApplicationMixin from "./application-mixin.mjs";
|
||||
|
||||
const { ItemSheetV2 } = foundry.applications.sheets;
|
||||
|
||||
/**
|
||||
* A base item sheet extending {@link ItemSheetV2} via {@link DHApplicationMixin}
|
||||
* @extends ItemSheetV2
|
||||
* @mixes DHSheetV2
|
||||
*/
|
||||
export default class DHBaseItemSheet extends DHApplicationMixin(ItemSheetV2) {
|
||||
/** @inheritDoc */
|
||||
static DEFAULT_OPTIONS = {
|
||||
classes: ['item'],
|
||||
position: { width: 600 },
|
||||
form: {
|
||||
submitOnChange: true
|
||||
},
|
||||
actions: {
|
||||
addAction: DHBaseItemSheet.#addAction,
|
||||
editAction: DHBaseItemSheet.#editAction,
|
||||
removeAction: DHBaseItemSheet.#removeAction
|
||||
}
|
||||
};
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @inheritdoc */
|
||||
static TABS = {
|
||||
primary: {
|
||||
tabs: [
|
||||
{ id: 'description' },
|
||||
{ id: 'actions' },
|
||||
{ id: 'settings' }
|
||||
],
|
||||
initial: "description",
|
||||
labelPrefix: "DAGGERHEART.Sheets.TABS"
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
/* Application Clicks Actions */
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Render a dialog prompting the user to select an action type.
|
||||
*
|
||||
* @returns {Promise<object>} An object containing the selected action type.
|
||||
*/
|
||||
static async selectActionType() {
|
||||
const content = await foundry.applications.handlebars.renderTemplate(
|
||||
'systems/daggerheart/templates/views/actionType.hbs',
|
||||
{ types: SYSTEM.ACTIONS.actionTypes }
|
||||
),
|
||||
title = 'Select Action Type', //useless var
|
||||
type = 'form',
|
||||
data = {}; //useless var
|
||||
//TODO: use DialogV2
|
||||
return Dialog.prompt({
|
||||
title,
|
||||
label: title,
|
||||
content,
|
||||
type, //this prop is useless
|
||||
callback: html => {
|
||||
const form = html[0].querySelector('form'),
|
||||
fd = new foundry.applications.ux.FormDataExtended(form);
|
||||
foundry.utils.mergeObject(data, fd.object, { inplace: true });
|
||||
// if (!data.name?.trim()) data.name = game.i18n.localize(SYSTEM.ACTIONS.actionTypes[data.type].name);
|
||||
return data;
|
||||
},
|
||||
rejectClose: false
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new action to the item, prompting the user for its type.
|
||||
* @param {PointerEvent} _event - The originating click event
|
||||
* @param {HTMLElement} _button - The capturing HTML element which defines the [data-action="addAction"]
|
||||
*/
|
||||
static async #addAction(_event, _button) {
|
||||
const actionType = await DHBaseItemSheet.selectActionType(),
|
||||
actionIndexes = this.document.system.actions.map(x => x._id.split('-')[2]).sort((a, b) => a - b);
|
||||
try {
|
||||
const cls = actionsTypes[actionType?.type] ?? actionsTypes.attack,
|
||||
action = new cls(
|
||||
{
|
||||
// id: `${this.document.id}-Action-${actionIndexes.length > 0 ? actionIndexes[0] + 1 : 1}`
|
||||
_id: foundry.utils.randomID(),
|
||||
type: actionType.type,
|
||||
name: game.i18n.localize(SYSTEM.ACTIONS.actionTypes[actionType.type].name),
|
||||
...cls.getSourceConfig(this.document)
|
||||
},
|
||||
{
|
||||
parent: this.document
|
||||
}
|
||||
);
|
||||
await this.document.update({ 'system.actions': [...this.document.system.actions, action] });
|
||||
await new DHActionConfig(this.document.system.actions[this.document.system.actions.length - 1]).render(
|
||||
true
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit an existing action on the item
|
||||
* @param {PointerEvent} event - The originating click event
|
||||
* @param {HTMLElement} button - The capturing HTML element which defines the [data-action="editAction"]
|
||||
*/
|
||||
static async #editAction(_event, button) {
|
||||
const action = this.document.system.actions[button.dataset.index];
|
||||
await new DHActionConfig(action).render(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an action from the item.
|
||||
* @param {PointerEvent} event - The originating click event
|
||||
* @param {HTMLElement} button - The capturing HTML element which defines the [data-action="removeAction"]
|
||||
*/
|
||||
static async #removeAction(event, button) {
|
||||
event.stopPropagation();
|
||||
await this.document.update({
|
||||
'system.actions': this.document.system.actions.filter(
|
||||
(_, index) => index !== Number.parseInt(button.dataset.index)
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue