foundryvtt-reve-de-dragon/module/rdd-compendium-organiser.js

42 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-01-29 22:49:34 +01:00
import { SYSTEM_RDD } from "./constants.js";
2021-01-26 00:25:41 +01:00
export class RddCompendiumOrganiser {
static init() {
Hooks.on('renderCompendium', async (pack, html, compendiumData) => RddCompendiumOrganiser.onRenderCompendium(pack, html, compendiumData))
2021-01-26 00:25:41 +01:00
}
static async onRenderCompendium(compendium, html, compendiumData) {
console.log('onRenderCompendium', compendium, html, compendiumData);
const pack = compendium.collection
2022-01-29 22:49:34 +01:00
if (pack.metadata.system === SYSTEM_RDD) {
2021-01-26 00:25:41 +01:00
html.find('.directory-item').each((i, element) => {
RddCompendiumOrganiser.setEntityTypeName(pack, element);
2021-01-26 00:25:41 +01:00
});
}
}
static async setEntityTypeName(pack, element) {
2022-10-07 19:06:43 +02:00
const label = RddCompendiumOrganiser.getEntityTypeLabel(await pack.getDocument(element.dataset.documentId));
RddCompendiumOrganiser.insertEntityType(element, label);
}
static insertEntityType(element, label) {
if (label) {
element.children[1].insertAdjacentHTML('afterbegin', `<label class="type-compendium">${label}: </label>`);
}
2021-01-26 00:25:41 +01:00
}
2022-10-07 19:06:43 +02:00
static getEntityTypeLabel(entity) {
const documentName = entity?.documentName
2022-11-05 18:06:30 +01:00
const type = entity?.type
2022-10-07 19:06:43 +02:00
if (documentName === 'Actor' || documentName === 'Item') {
const label = CONFIG[documentName]?.typeLabels?.[type] ?? type;
2022-11-05 18:06:30 +01:00
if (game.i18n.has(label)) {
return game.i18n.localize(label);
}
2022-10-07 19:06:43 +02:00
}
return type;
}
2021-01-26 00:25:41 +01:00
}