REFACTOR: rename dhpItem to DHItem

REFACTOR: improvement Item#isInventoryItem getter
REFACTOR: simplify Item's createDialog static method.
REFACTOR: remove documentCreate template
This commit is contained in:
Joaquin Pereyra 2025-06-28 15:37:04 -03:00
parent a4881854a3
commit 15b696398c
9 changed files with 61 additions and 115 deletions

View file

@ -52,7 +52,7 @@ Hooks.once('init', () => {
CONFIG.MeasuredTemplate.objectClass = DhMeasuredTemplate; CONFIG.MeasuredTemplate.objectClass = DhMeasuredTemplate;
CONFIG.Item.documentClass = documents.DhpItem; CONFIG.Item.documentClass = documents.DHItem;
//Registering the Item DataModel //Registering the Item DataModel
CONFIG.Item.dataModels = models.items.config; CONFIG.Item.dataModels = models.items.config;

View file

@ -9,7 +9,8 @@ export default class DHArmor extends BaseDataItem {
label: 'TYPES.Item.armor', label: 'TYPES.Item.armor',
type: 'armor', type: 'armor',
hasDescription: true, hasDescription: true,
isQuantifiable: true isQuantifiable: true,
isInventoryItem: true,
}); });
} }

View file

@ -5,6 +5,7 @@
* @property {string} type - The system type that this data model represents. * @property {string} type - The system type that this data model represents.
* @property {boolean} hasDescription - Indicates whether items of this type have description field * @property {boolean} hasDescription - Indicates whether items of this type have description field
* @property {boolean} isQuantifiable - Indicates whether items of this type have quantity field * @property {boolean} isQuantifiable - Indicates whether items of this type have quantity field
* @property {boolean} isInventoryItem- Indicates whether items of this type is a Inventory Item
*/ */
const fields = foundry.data.fields; const fields = foundry.data.fields;
@ -16,7 +17,8 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
label: 'Base Item', label: 'Base Item',
type: 'base', type: 'base',
hasDescription: false, hasDescription: false,
isQuantifiable: false isQuantifiable: false,
isInventoryItem: false,
}; };
} }

View file

@ -8,7 +8,8 @@ export default class DHConsumable extends BaseDataItem {
label: 'TYPES.Item.consumable', label: 'TYPES.Item.consumable',
type: 'consumable', type: 'consumable',
hasDescription: true, hasDescription: true,
isQuantifiable: true isQuantifiable: true,
isInventoryItem: true,
}); });
} }

View file

@ -8,7 +8,8 @@ export default class DHMiscellaneous extends BaseDataItem {
label: 'TYPES.Item.miscellaneous', label: 'TYPES.Item.miscellaneous',
type: 'miscellaneous', type: 'miscellaneous',
hasDescription: true, hasDescription: true,
isQuantifiable: true isQuantifiable: true,
isInventoryItem: true,
}); });
} }

View file

@ -12,9 +12,7 @@ export default class DHWeapon extends BaseDataItem {
type: 'weapon', type: 'weapon',
hasDescription: true, hasDescription: true,
isQuantifiable: true, isQuantifiable: true,
embedded: { isInventoryItem: true,
feature: 'featureTest'
}
}); });
} }

View file

@ -1,4 +1,4 @@
export { default as DhpActor } from './actor.mjs'; export { default as DhpActor } from './actor.mjs';
export { default as DhpItem } from './item.mjs'; export { default as DHItem } from './item.mjs';
export { default as DhpCombat } from './combat.mjs'; export { default as DhpCombat } from './combat.mjs';
export { default as DhActiveEffect } from './activeEffect.mjs'; export { default as DhActiveEffect } from './activeEffect.mjs';

View file

@ -1,4 +1,8 @@
export default class DhpItem extends Item { /**
* Override and extend the basic Item implementation.
* @extends {foundry.documents.Item}
*/
export default class DHItem extends foundry.documents.Item {
/** @inheritDoc */ /** @inheritDoc */
prepareEmbeddedDocuments() { prepareEmbeddedDocuments() {
super.prepareEmbeddedDocuments(); super.prepareEmbeddedDocuments();
@ -25,75 +29,59 @@ export default class DhpItem extends Item {
return data; return data;
} }
isInventoryItem() { /**
return ['weapon', 'armor', 'miscellaneous', 'consumable'].includes(this.type); * Determine if this item is classified as an inventory item based on its metadata.
* @returns {boolean} Returns `true` if the item is an inventory item.
*/
get isInventoryItem() {
return this.system.constructor.metadata.isInventoryItem ?? false;
} }
static async createDialog(data = {}, { parent = null, pack = null, ...options } = {}) { /** @inheritdoc */
const documentName = this.metadata.name; static async createDialog(data = {}, createOptions = {}, options = {}) {
const types = game.documentTypes[documentName].filter(t => t !== CONST.BASE_DOCUMENT_TYPE); const { folders, types, template, context = {}, ...dialogOptions } = options;
let collection;
if (!parent) {
if (pack) collection = game.packs.get(pack);
else collection = game.collections.get(documentName);
}
const folders = collection?._formatFolderSelectOptions() ?? [];
const label = game.i18n.localize(this.metadata.label);
const title = game.i18n.format('DOCUMENT.Create', { type: label });
const typeObjects = types.reduce((obj, t) => {
const label = CONFIG[documentName]?.typeLabels?.[t] ?? t;
obj[t] = { value: t, label: game.i18n.has(label) ? game.i18n.localize(label) : t };
return obj;
}, {});
// Render the document creation form if (types?.length === 0) {
const html = await foundry.applications.handlebars.renderTemplate( throw new Error('The array of sub-types to restrict to must not be empty.');
'systems/daggerheart/templates/sidebar/documentCreate.hbs', }
{
folders, const documentTypes = this.TYPES.filter(type => type !== 'base' && (!types || types.includes(type))).map(
name: data.name || game.i18n.format('DOCUMENT.New', { type: label }), type => {
folder: data.folder, const labelKey = CONFIG.Item?.typeLabels?.[type];
hasFolders: folders.length >= 1, const label = labelKey && game.i18n.has(labelKey) ? game.i18n.localize(labelKey) : type;
type: data.type || CONFIG[documentName]?.defaultType || typeObjects.armor,
types: { const isInventoryItem = CONFIG.Item.dataModels[type]?.metadata?.isInventoryItem;
Items: [typeObjects.armor, typeObjects.weapon, typeObjects.consumable, typeObjects.miscellaneous], const group =
Character: [ isInventoryItem === true
typeObjects.class, ? 'Inventory Items'
typeObjects.subclass, : isInventoryItem === false
typeObjects.ancestry, ? 'Character Items'
typeObjects.community, : 'Other';
typeObjects.feature,
typeObjects.domainCard return { value: type, label, group };
]
},
hasTypes: types.length > 1
} }
); );
// Render the confirmation dialog window if (!documentTypes.length) {
return Dialog.prompt({ throw new Error('No document types were permitted to be created.');
title: title, }
content: html,
label: title, const sortedTypes = documentTypes.sort((a, b) => a.label.localeCompare(b.label, game.i18n.lang));
callback: html => {
const form = html[0].querySelector('form'); return await super.createDialog(data, createOptions, {
const fd = new FormDataExtended(form); folders,
foundry.utils.mergeObject(data, fd.object, { inplace: true }); types,
if (!data.folder) delete data.folder; template,
if (types.length === 1) data.type = types[0]; context: { types: sortedTypes, ...context },
if (!data.name?.trim()) data.name = this.defaultName(); ...dialogOptions
return this.create(data, { parent, pack, renderSheet: true });
},
rejectClose: false,
options
}); });
} }
async selectActionDialog() { async selectActionDialog() {
const content = await foundry.applications.handlebars.renderTemplate( const content = await foundry.applications.handlebars.renderTemplate(
'systems/daggerheart/templates/views/actionSelect.hbs', 'systems/daggerheart/templates/views/actionSelect.hbs',
{ actions: this.system.actions } { actions: this.system.actions }
), ),
title = 'Select Action', title = 'Select Action',
type = 'div', type = 'div',
data = {}; data = {};
@ -139,8 +127,8 @@ export default class DhpItem extends Item {
this.type === 'ancestry' this.type === 'ancestry'
? game.i18n.localize('DAGGERHEART.Chat.FoundationCard.AncestryTitle') ? game.i18n.localize('DAGGERHEART.Chat.FoundationCard.AncestryTitle')
: this.type === 'community' : this.type === 'community'
? game.i18n.localize('DAGGERHEART.Chat.FoundationCard.CommunityTitle') ? game.i18n.localize('DAGGERHEART.Chat.FoundationCard.CommunityTitle')
: game.i18n.localize('DAGGERHEART.Chat.FoundationCard.SubclassFeatureTitle'), : game.i18n.localize('DAGGERHEART.Chat.FoundationCard.SubclassFeatureTitle'),
origin: origin, origin: origin,
img: this.img, img: this.img,
name: this.name, name: this.name,

View file

@ -1,45 +0,0 @@
<form id="document-create" autocomplete="off">
<div class="form-group">
<label>{{localize "Name"}}</label>
<div class="form-fields">
<input type="text" name="name" placeholder="{{name}}" autofocus>
</div>
</div>
{{#if hasTypes}}
<div class="form-group">
<label>{{localize "Type"}}</label>
<div class="form-fields">
<select name="type">
{{#select type}}
{{#each types as |values key|}}
<optgroup label="{{key}}">
{{#each values}}
<option value="{{this.value}}">{{this.label}}</option>
{{/each}}
</optgroup>
{{/each}}
{{/select}}
</select>
</div>
</div>
{{/if}}
{{#if hasFolders}}
<div class="form-group">
<label>{{ localize "DOCUMENT.Folder" }}</label>
<div class="form-fields">
<select name="folder">
{{#select folder}}
<option value=""></option>
{{#each folders}}
<option value="{{ this.id }}">{{ this.name }}</option>
{{/each}}
{{/select}}
</select>
</div>
</div>
{{/if}}
{{{content}}}
</form>