REFACTOR: prettier

This commit is contained in:
Joaquin Pereyra 2025-07-02 21:51:04 -03:00
parent 0ad4c8d71e
commit b743c9b628
3 changed files with 226 additions and 237 deletions

View file

@ -217,7 +217,6 @@ export default class CharacterSheet extends DaggerheartSheet(ActorSheetV2) {
this._createContextMenues(); this._createContextMenues();
this._createFilterMenus(); this._createFilterMenus();
} }
/** @inheritDoc */ /** @inheritDoc */
@ -474,11 +473,11 @@ export default class CharacterSheet extends DaggerheartSheet(ActorSheetV2) {
#filteredItems = { #filteredItems = {
inventory: { inventory: {
search: new Set(), search: new Set(),
menu: new Set(), menu: new Set()
}, },
loadout: { loadout: {
search: new Set(), search: new Set(),
menu: new Set(), menu: new Set()
} }
}; };
@ -571,7 +570,7 @@ export default class CharacterSheet extends DaggerheartSheet(ActorSheetV2) {
content: '.items-section', content: '.items-section',
callback: this._onMenuFilterInventory.bind(this), callback: this._onMenuFilterInventory.bind(this),
target: '.filter-button', target: '.filter-button',
filters: FilterMenu.invetoryFilters, filters: FilterMenu.invetoryFilters
}, },
{ {
key: 'loadout', key: 'loadout',
@ -579,25 +578,23 @@ export default class CharacterSheet extends DaggerheartSheet(ActorSheetV2) {
content: '.items-section', content: '.items-section',
callback: this._onMenuFilterLoadout.bind(this), callback: this._onMenuFilterLoadout.bind(this),
target: '.filter-button', target: '.filter-button',
filters: FilterMenu.cardsFilters, filters: FilterMenu.cardsFilters
} }
]; ];
menus.forEach(m => { menus.forEach(m => {
const container = this.element.querySelector(m.container); const container = this.element.querySelector(m.container);
this.#menu[m.key] = new FilterMenu(container, m.target, m.filters, m.callback, { this.#menu[m.key] = new FilterMenu(container, m.target, m.filters, m.callback, {
contentSelector: m.content, contentSelector: m.content
}); });
}); });
} }
/** /**
* Callback when filters change * Callback when filters change
* @param {PointerEvent} event * @param {PointerEvent} event
* @param {HTMLElement} html * @param {HTMLElement} html
* @param {import('../ux/filter-menu.mjs').FilterItem[]} filters * @param {import('../ux/filter-menu.mjs').FilterItem[]} filters
*/ */
_onMenuFilterInventory(event, html, filters) { _onMenuFilterInventory(event, html, filters) {
this.#filteredItems.inventory.menu.clear(); this.#filteredItems.inventory.menu.clear();
@ -605,21 +602,20 @@ export default class CharacterSheet extends DaggerheartSheet(ActorSheetV2) {
for (const li of html.querySelectorAll('.inventory-item')) { for (const li of html.querySelectorAll('.inventory-item')) {
const item = this.document.items.get(li.dataset.itemId); const item = this.document.items.get(li.dataset.itemId);
const matchesMenu = filters.length === 0 || const matchesMenu =
filters.some(f => foundry.applications.ux.SearchFilter.evaluateFilter(item, f)); filters.length === 0 || filters.some(f => foundry.applications.ux.SearchFilter.evaluateFilter(item, f));
if (matchesMenu) this.#filteredItems.inventory.menu.add(item.id); if (matchesMenu) this.#filteredItems.inventory.menu.add(item.id);
const { search } = this.#filteredItems.inventory; const { search } = this.#filteredItems.inventory;
li.hidden = !(search.has(item.id) && matchesMenu); li.hidden = !(search.has(item.id) && matchesMenu);
} }
} }
/** /**
* Callback when filters change * Callback when filters change
* @param {PointerEvent} event * @param {PointerEvent} event
* @param {HTMLElement} html * @param {HTMLElement} html
* @param {import('../ux/filter-menu.mjs').FilterItem[]} filters * @param {import('../ux/filter-menu.mjs').FilterItem[]} filters
*/ */
_onMenuFilterLoadout(event, html, filters) { _onMenuFilterLoadout(event, html, filters) {
this.#filteredItems.loadout.menu.clear(); this.#filteredItems.loadout.menu.clear();
@ -627,14 +623,13 @@ export default class CharacterSheet extends DaggerheartSheet(ActorSheetV2) {
for (const li of html.querySelectorAll('.items-list .inventory-item, .card-list .card-item')) { for (const li of html.querySelectorAll('.items-list .inventory-item, .card-list .card-item')) {
const item = this.document.items.get(li.dataset.itemId); const item = this.document.items.get(li.dataset.itemId);
const matchesMenu = filters.length === 0 || const matchesMenu =
filters.some(f => foundry.applications.ux.SearchFilter.evaluateFilter(item, f)); filters.length === 0 || filters.some(f => foundry.applications.ux.SearchFilter.evaluateFilter(item, f));
if (matchesMenu) this.#filteredItems.loadout.menu.add(item.id); if (matchesMenu) this.#filteredItems.loadout.menu.add(item.id);
const { search } = this.#filteredItems.loadout; const { search } = this.#filteredItems.loadout;
li.hidden = !(search.has(item.id) && matchesMenu); li.hidden = !(search.has(item.id) && matchesMenu);
} }
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
@ -915,8 +910,9 @@ export default class CharacterSheet extends DaggerheartSheet(ActorSheetV2) {
const cls = getDocumentClass('ChatMessage'); const cls = getDocumentClass('ChatMessage');
const systemData = { const systemData = {
name: game.i18n.localize('DAGGERHEART.General.Experience.Single'), name: game.i18n.localize('DAGGERHEART.General.Experience.Single'),
description: `${experience.description} ${experience.total < 0 ? experience.total : `+${experience.total}` description: `${experience.description} ${
}` experience.total < 0 ? experience.total : `+${experience.total}`
}`
}; };
const msg = new cls({ const msg = new cls({
type: 'abilityUse', type: 'abilityUse',

View file

@ -1 +1 @@
export { default as FilterMenu} from "./filter-menu.mjs"; export { default as FilterMenu } from './filter-menu.mjs';

View file

@ -6,239 +6,232 @@
*/ */
export default class FilterMenu extends foundry.applications.ux.ContextMenu { export default class FilterMenu extends foundry.applications.ux.ContextMenu {
/** /**
* Filter Menu * Filter Menu
* @param {HTMLElement} container - Container element * @param {HTMLElement} container - Container element
* @param {string} selector - CSS selector for menu targets * @param {string} selector - CSS selector for menu targets
* @param {Array} menuItems - Array of menu entries * @param {Array} menuItems - Array of menu entries
* @param {Function} callback - Callback when filters change * @param {Function} callback - Callback when filters change
* @param {Object} [options] - Additional options * @param {Object} [options] - Additional options
*/ */
constructor(container, selector, menuItems, callback, options = {}) { constructor(container, selector, menuItems, callback, options = {}) {
// Set default options // Set default options
const mergedOptions = { const mergedOptions = {
eventName: "click", eventName: 'click',
fixed: true, fixed: true,
...options ...options
}; };
super(container, selector, menuItems, mergedOptions); super(container, selector, menuItems, mergedOptions);
// Initialize filter states // Initialize filter states
this.menuItems = menuItems.map(item => ({ this.menuItems = menuItems.map(item => ({
...item, ...item,
enabled: false, enabled: false
})); }));
this.callback = callback; this.callback = callback;
this.contentElement = container.querySelector(mergedOptions.contentSelector); this.contentElement = container.querySelector(mergedOptions.contentSelector);
const syntheticEvent = { const syntheticEvent = {
type: 'pointerdown', type: 'pointerdown',
bubbles: true, bubbles: true,
cancelable: true, cancelable: true,
pointerType: 'mouse', pointerType: 'mouse',
isPrimary: true, isPrimary: true,
button: 0, button: 0
}; };
this.callback(syntheticEvent, this.contentElement, this.getActiveFilterData()); this.callback(syntheticEvent, this.contentElement, this.getActiveFilterData());
}
/** @inheritdoc */
async render(target, options = {}) {
await super.render(target, { ...options, animate: false });
// Create menu structure
const menu = document.createElement("menu");
menu.className = "filter-menu";
// Group items by their group property
const groups = this.#groupItems(this.menuItems);
// Create sections for each group
for (const [groupName, items] of Object.entries(groups)) {
if (!items.length) continue;
const section = this.#createSection(groupName, items);
menu.appendChild(section);
} }
// Update menu and set position /** @inheritdoc */
this.element.replaceChildren(menu); async render(target, options = {}) {
await super.render(target, { ...options, animate: false });
menu.addEventListener("click", this.#handleClick.bind(this)); // Create menu structure
const menu = document.createElement('menu');
menu.className = 'filter-menu';
this._setPosition(this.element, target, options); // Group items by their group property
const groups = this.#groupItems(this.menuItems);
if (options.animate !== false) await this._animate(true); // Create sections for each group
return this._onRender(options); for (const [groupName, items] of Object.entries(groups)) {
} if (!items.length) continue;
/** const section = this.#createSection(groupName, items);
* Groups an array of items by their `group`. menu.appendChild(section);
* @param {Array<Object>} items - The array of items to group. Each item is expected to have an optional `group` property.
* @returns {Object<string, Array<Object>>} An object where keys are group names and values are arrays of items belonging to each group.
*/
#groupItems(items) {
return items.reduce((groups, item) => {
const group = item.group ?? "_none";
groups[group] = groups[group] || [];
groups[group].push(item);
return groups;
}, {});
}
/**
* Creates a DOM section element for a group of items with corresponding filter buttons.
* @param {string} groupName - The name of the group, used as the section label.
* @param {Array<Object>} items - The items to create buttons for. Each item should have:
* @returns {HTMLDivElement} The section DOM element containing the label and buttons.
*/
#createSection(groupName, items) {
const section = document.createElement("fieldset");
section.className = "filter-section";
const header = document.createElement("legend");
header.textContent = groupName;
section.appendChild(header);
const buttons = document.createElement("div");
buttons.className = "filter-buttons";
items.forEach(item => {
const button = document.createElement("button");
button.className = `filter-button ${item.enabled ? "active" : ""}`;
button.textContent = item.name;
item.element = button;
buttons.appendChild(button);
});
section.appendChild(buttons);
return section;
}
/**
* Get filter data from active filters
* @returns {Array} Array of filter configurations
*/
getActiveFilterData() {
return this.menuItems.filter(item => item.enabled).map(item => item.filter);
}
/**
* Handles click events on filter buttons.
* Toggles the active state of the clicked button and updates the corresponding item's `enabled` state.
* Then triggers the provided callback with the event, the content element, and the current active filter data.
* @param {PointerEvent} event - The click event triggered by interacting with a filter button.
* @returns {void}
*/
#handleClick(event) {
event.preventDefault();
event.stopPropagation();
const button = event.target.closest(".filter-button");
if (!button) return;
const clickedItem = this.menuItems.find(item => item.element === button);
if (!clickedItem) return;
const isActive = button.classList.toggle("active");
clickedItem.enabled = isActive;
const filters = this.getActiveFilterData();
if (filters.length > 0) {
this.target.classList.add("fa-beat", "active");
} else {
this.target.classList.remove("fa-beat", "active");
}
this.callback(event, this.contentElement, filters);
}
/**
* Generate and return a sorted array of inventory filters.
* @returns {Array<Object>} An array of filter objects, sorted by name within each group.
*/
static get invetoryFilters() {
const { OPERATORS } = foundry.applications.ux.SearchFilter;
const typesFilters = Object.entries(CONFIG.Item.dataModels)
.filter(([, { metadata }]) => metadata.isInventoryItem)
.map(([type, { metadata }]) => ({
group: game.i18n.localize("Type"),
name: game.i18n.localize(metadata.label),
filter: {
field: "type",
operator: OPERATORS.EQUALS,
value: type
} }
}));
const burdenFilter = Object.values(CONFIG.daggerheart.GENERAL.burden).map(({ value, label }) => ({ // Update menu and set position
group: game.i18n.localize("DAGGERHEART.Sheets.Weapon.Burden"), this.element.replaceChildren(menu);
name: game.i18n.localize(label),
filter: {
field: "system.burden",
operator: OPERATORS.EQUALS,
value: value
}
}));
const damageTypeFilter = Object.values(CONFIG.daggerheart.GENERAL.damageTypes).map(({ id, abbreviation }) => ({ menu.addEventListener('click', this.#handleClick.bind(this));
group: "Damage Type", //TODO localize
name: game.i18n.localize(abbreviation),
filter: {
field: "system.damage.type",
operator: OPERATORS.EQUALS,
value: id
}
}));
this._setPosition(this.element, target, options);
return [ if (options.animate !== false) await this._animate(true);
...game.i18n.sortObjects(typesFilters, "name"), return this._onRender(options);
...game.i18n.sortObjects(burdenFilter, "name"), }
...game.i18n.sortObjects(damageTypeFilter, "name"),
];
}
/** /**
* Generate and return a sorted array of inventory filters. * Groups an array of items by their `group`.
* @returns {Array<Object>} An array of filter objects, sorted by name within each group. * @param {Array<Object>} items - The array of items to group. Each item is expected to have an optional `group` property.
*/ * @returns {Object<string, Array<Object>>} An object where keys are group names and values are arrays of items belonging to each group.
static get cardsFilters() { */
const { OPERATORS } = foundry.applications.ux.SearchFilter; #groupItems(items) {
return items.reduce((groups, item) => {
const group = item.group ?? '_none';
groups[group] = groups[group] || [];
groups[group].push(item);
return groups;
}, {});
}
const typesFilters = Object.values(CONFIG.daggerheart.DOMAIN.cardTypes) /**
.map(({ id, label }) => ({ * Creates a DOM section element for a group of items with corresponding filter buttons.
group: game.i18n.localize("Type"), * @param {string} groupName - The name of the group, used as the section label.
name: game.i18n.localize(label), * @param {Array<Object>} items - The items to create buttons for. Each item should have:
filter: { * @returns {HTMLDivElement} The section DOM element containing the label and buttons.
field: "system.type", */
operator: OPERATORS.EQUALS, #createSection(groupName, items) {
value: id const section = document.createElement('fieldset');
} section.className = 'filter-section';
}));
const domainFilter = Object.values(CONFIG.daggerheart.DOMAIN.domains).map(({id, label}) => ({ const header = document.createElement('legend');
group: game.i18n.localize("DAGGERHEART.Sheets.DomainCard.Domain"), header.textContent = groupName;
name: game.i18n.localize(label), section.appendChild(header);
filter: {
field: "system.domain",
operator: OPERATORS.EQUALS,
value: id
}
}))
const sort = (arr) => game.i18n.sortObjects(arr, "name"); const buttons = document.createElement('div');
buttons.className = 'filter-buttons';
return [ items.forEach(item => {
...sort(typesFilters), const button = document.createElement('button');
...sort(domainFilter), button.className = `filter-button ${item.enabled ? 'active' : ''}`;
]; button.textContent = item.name;
} item.element = button;
buttons.appendChild(button);
});
section.appendChild(buttons);
return section;
}
/**
* Get filter data from active filters
* @returns {Array} Array of filter configurations
*/
getActiveFilterData() {
return this.menuItems.filter(item => item.enabled).map(item => item.filter);
}
/**
* Handles click events on filter buttons.
* Toggles the active state of the clicked button and updates the corresponding item's `enabled` state.
* Then triggers the provided callback with the event, the content element, and the current active filter data.
* @param {PointerEvent} event - The click event triggered by interacting with a filter button.
* @returns {void}
*/
#handleClick(event) {
event.preventDefault();
event.stopPropagation();
const button = event.target.closest('.filter-button');
if (!button) return;
const clickedItem = this.menuItems.find(item => item.element === button);
if (!clickedItem) return;
const isActive = button.classList.toggle('active');
clickedItem.enabled = isActive;
const filters = this.getActiveFilterData();
if (filters.length > 0) {
this.target.classList.add('fa-beat', 'active');
} else {
this.target.classList.remove('fa-beat', 'active');
}
this.callback(event, this.contentElement, filters);
}
/**
* Generate and return a sorted array of inventory filters.
* @returns {Array<Object>} An array of filter objects, sorted by name within each group.
*/
static get invetoryFilters() {
const { OPERATORS } = foundry.applications.ux.SearchFilter;
const typesFilters = Object.entries(CONFIG.Item.dataModels)
.filter(([, { metadata }]) => metadata.isInventoryItem)
.map(([type, { metadata }]) => ({
group: game.i18n.localize('Type'),
name: game.i18n.localize(metadata.label),
filter: {
field: 'type',
operator: OPERATORS.EQUALS,
value: type
}
}));
const burdenFilter = Object.values(CONFIG.daggerheart.GENERAL.burden).map(({ value, label }) => ({
group: game.i18n.localize('DAGGERHEART.Sheets.Weapon.Burden'),
name: game.i18n.localize(label),
filter: {
field: 'system.burden',
operator: OPERATORS.EQUALS,
value: value
}
}));
const damageTypeFilter = Object.values(CONFIG.daggerheart.GENERAL.damageTypes).map(({ id, abbreviation }) => ({
group: 'Damage Type', //TODO localize
name: game.i18n.localize(abbreviation),
filter: {
field: 'system.damage.type',
operator: OPERATORS.EQUALS,
value: id
}
}));
return [
...game.i18n.sortObjects(typesFilters, 'name'),
...game.i18n.sortObjects(burdenFilter, 'name'),
...game.i18n.sortObjects(damageTypeFilter, 'name')
];
}
/**
* Generate and return a sorted array of inventory filters.
* @returns {Array<Object>} An array of filter objects, sorted by name within each group.
*/
static get cardsFilters() {
const { OPERATORS } = foundry.applications.ux.SearchFilter;
const typesFilters = Object.values(CONFIG.daggerheart.DOMAIN.cardTypes).map(({ id, label }) => ({
group: game.i18n.localize('Type'),
name: game.i18n.localize(label),
filter: {
field: 'system.type',
operator: OPERATORS.EQUALS,
value: id
}
}));
const domainFilter = Object.values(CONFIG.daggerheart.DOMAIN.domains).map(({ id, label }) => ({
group: game.i18n.localize('DAGGERHEART.Sheets.DomainCard.Domain'),
name: game.i18n.localize(label),
filter: {
field: 'system.domain',
operator: OPERATORS.EQUALS,
value: id
}
}));
const sort = arr => game.i18n.sortObjects(arr, 'name');
return [...sort(typesFilters), ...sort(domainFilter)];
}
} }