Refactoring

2.0
Matheus Clemente 2023-11-08 16:34:25 -03:00
parent 327068c7e9
commit 459a0aab23
1 changed files with 58 additions and 58 deletions

View File

@ -17,27 +17,22 @@ class CompendiumBrowser extends Application {
return options; return options;
} }
get maxLoad() {
return game.settings.get("compendium-browser", "maxload");
}
async initialize() { async initialize() {
// load settings // load settings
if (this.settings === undefined) { this.initSettings();
this.initSettings();
}
await loadTemplates([ Hooks.on("changeSidebarTab", (app) => {
"modules/compendium-browser/template/spell-browser.html", if (app.tabName !== "compendium") return;
"modules/compendium-browser/template/spell-browser-list.html", this.hookCompendiumList(app.element);
"modules/compendium-browser/template/npc-browser.html", });
"modules/compendium-browser/template/npc-browser-list.html", Hooks.on("renderSidebarTab", (app, html, data) => {
"modules/compendium-browser/template/feat-browser.html", if (app.tabName !== "compendium") return;
"modules/compendium-browser/template/feat-browser-list.html", this.hookCompendiumList(html);
"modules/compendium-browser/template/item-browser.html", });
"modules/compendium-browser/template/item-browser-list.html",
"modules/compendium-browser/template/filter-container.html",
"modules/compendium-browser/template/settings.html",
"modules/compendium-browser/template/loading.html",
]);
this.hookCompendiumList();
//Reset the filters used in the dialog //Reset the filters used in the dialog
this.spellFilters = { this.spellFilters = {
@ -58,14 +53,14 @@ class CompendiumBrowser extends Application {
}; };
} }
/** override */ /** @override */
_onChangeTab(event, tabs, active) { _onChangeTab(event, tabs, active) {
super._onChangeTab(event, tabs, active); super._onChangeTab(event, tabs, active);
const html = this.element; const html = this.element;
this.replaceList(html, active, { reload: false }); this.replaceList(html, active, { reload: false });
} }
/** override */ /** @override */
async getData() { async getData() {
//0.4.1 Filter as we load to support new way of filtering //0.4.1 Filter as we load to support new way of filtering
//Previously loaded all data and filtered in place; now loads minimal (preload) amount, filtered as we go //Previously loaded all data and filtered in place; now loads minimal (preload) amount, filtered as we go
@ -127,7 +122,7 @@ class CompendiumBrowser extends Application {
}); });
} }
/** override */ /** @override */
activateListeners(html) { activateListeners(html) {
super.activateListeners(html); super.activateListeners(html);
@ -447,7 +442,7 @@ class CompendiumBrowser extends Application {
numItemsLoaded = Object.keys(itemsList).length; numItemsLoaded = Object.keys(itemsList).length;
if (maxLoad <= numItemsLoaded) { if (this.maxLoad <= numItemsLoaded) {
if (updateLoading) { if (updateLoading) {
updateLoading(numItemsLoaded, true); updateLoading(numItemsLoaded, true);
} }
@ -483,7 +478,7 @@ class CompendiumBrowser extends Application {
numItemsLoaded = Object.keys(itemsList).length; numItemsLoaded = Object.keys(itemsList).length;
if (maxLoad <= numItemsLoaded) { if (this.maxLoad <= numItemsLoaded) {
if (updateLoading) { if (updateLoading) {
updateLoading(numItemsLoaded, true); updateLoading(numItemsLoaded, true);
} }
@ -516,7 +511,7 @@ class CompendiumBrowser extends Application {
numItemsLoaded = Object.keys(itemsList).length; numItemsLoaded = Object.keys(itemsList).length;
if (maxLoad <= numItemsLoaded) { if (this.maxLoad <= numItemsLoaded) {
if (updateLoading) { if (updateLoading) {
updateLoading(numItemsLoaded, true); updateLoading(numItemsLoaded, true);
} }
@ -574,8 +569,6 @@ class CompendiumBrowser extends Application {
console.time("loadAndFilterNpcs"); console.time("loadAndFilterNpcs");
let npcs = {}; let npcs = {};
const maxLoad = game.settings.get("compendium-browser", "maxload");
let numNpcsLoaded = 0; let numNpcsLoaded = 0;
this.npcsLoaded = false; this.npcsLoaded = false;
@ -608,7 +601,7 @@ class CompendiumBrowser extends Application {
numNpcsLoaded = Object.keys(npcs).length; numNpcsLoaded = Object.keys(npcs).length;
if (maxLoad <= numNpcsLoaded) { if (this.maxLoad <= numNpcsLoaded) {
if (updateLoading) { if (updateLoading) {
updateLoading(numNpcsLoaded, true); updateLoading(numNpcsLoaded, true);
} }
@ -662,15 +655,7 @@ class CompendiumBrowser extends Application {
return npcs; return npcs;
} }
hookCompendiumList() { hookCompendiumList(html) {
Hooks.on("renderCompendiumDirectory", (app, html, data) => {
this.hookCompendiumList();
});
let html = $("#compendium");
if (this.settings === undefined) {
this.initSettings();
}
if (game.user.isGM || this.settings.allowSpellBrowser || this.settings.allowNpcBrowser) { if (game.user.isGM || this.settings.allowSpellBrowser || this.settings.allowNpcBrowser) {
const cbButton = $( const cbButton = $(
`<button class="compendium-browser-btn"><i class="fas fa-fire"></i> ${game.i18n.localize( `<button class="compendium-browser-btn"><i class="fas fa-fire"></i> ${game.i18n.localize(
@ -733,26 +718,31 @@ class CompendiumBrowser extends Application {
let elements = null; let elements = null;
//0.4.2 Display a Loading... message while the data is being loaded and filtered //0.4.2 Display a Loading... message while the data is being loaded and filtered
let loadingMessage = null; let loadingMessage = null;
if (browserTab === "spell") { const tabElements = {
elements = html.find("ul#CBSpells"); spell: { elements: "ul#CBSpells", message: "#CBSpellsMessage" },
loadingMessage = html.find("#CBSpellsMessage"); npc: { elements: "ul#CBNPCs", message: "#CBNpcsMessage" },
} else if (browserTab === "npc") { feat: { elements: "ul#CBFeats", message: "#CBFeatsMessage" },
elements = html.find("ul#CBNPCs"); item: { elements: "ul#CBItems", message: "#CBItemsMessage" },
loadingMessage = html.find("#CBNpcsMessage"); };
} else if (browserTab === "feat") {
elements = html.find("ul#CBFeats"); if (browserTab in tabElements) {
loadingMessage = html.find("#CBFeatsMessage"); const tabInfo = tabElements[browserTab];
} else if (browserTab === "item") { elements = html.find(tabInfo.elements);
elements = html.find("ul#CBItems"); loadingMessage = html.find(tabInfo.message);
loadingMessage = html.find("#CBItemsMessage");
} }
if (elements?.length) { if (elements?.length) {
//0.4.2b: On a tab-switch, only reload if there isn't any data already //0.4.2b: On a tab-switch, only reload if there isn't any data already
if (options?.reload || !elements[0].children.length) { if (options?.reload || !elements[0].children.length) {
const maxLoad = game.settings.get("compendium-browser", "maxload");
const updateLoading = async (numLoaded, doneLoading) => { const updateLoading = async (numLoaded, doneLoading) => {
if (loadingMessage.length) { if (loadingMessage.length) {
this.renderLoading(loadingMessage[0], browserTab, numLoaded, numLoaded >= maxLoad, doneLoading); this.renderLoading(
loadingMessage[0],
browserTab,
numLoaded,
numLoaded >= this.maxLoad,
doneLoading
);
} }
}; };
updateLoading(0, false); updateLoading(0, false);
@ -1317,8 +1307,6 @@ class CompendiumBrowser extends Application {
} }
async addSpellFilters() { async addSpellFilters() {
// Spellfilters
//Foundry v10+ Item#data is now Item#system
this.addSpellFilter("CMPBrowser.general", "DND5E.Source", "system.source", "text"); this.addSpellFilter("CMPBrowser.general", "DND5E.Source", "system.source", "text");
this.addSpellFilter("CMPBrowser.general", "DND5E.Level", "system.level", "multiSelect", { this.addSpellFilter("CMPBrowser.general", "DND5E.Level", "system.level", "multiSelect", {
0: "DND5E.SpellCantrip", 0: "DND5E.SpellCantrip",
@ -1388,10 +1376,6 @@ class CompendiumBrowser extends Application {
} }
async addItemFilters() { async addItemFilters() {
// Item Filters
// Feature Filters
//Foundry v10+ Item#data is now Item#system
this.addItemFilter("CMPBrowser.general", "DND5E.Source", "system.source", "text"); this.addItemFilter("CMPBrowser.general", "DND5E.Source", "system.source", "text");
this.addItemFilter("CMPBrowser.general", "Item Type", "type", "select", { this.addItemFilter("CMPBrowser.general", "Item Type", "type", "select", {
@ -1924,12 +1908,28 @@ class CompendiumBrowser extends Application {
} }
} }
Hooks.on("ready", async () => { Hooks.on("init", async () => {
await loadTemplates([
"modules/compendium-browser/template/spell-browser.html",
"modules/compendium-browser/template/spell-browser-list.html",
"modules/compendium-browser/template/npc-browser.html",
"modules/compendium-browser/template/npc-browser-list.html",
"modules/compendium-browser/template/feat-browser.html",
"modules/compendium-browser/template/feat-browser-list.html",
"modules/compendium-browser/template/item-browser.html",
"modules/compendium-browser/template/item-browser-list.html",
"modules/compendium-browser/template/filter-container.html",
"modules/compendium-browser/template/settings.html",
"modules/compendium-browser/template/loading.html",
]);
});
Hooks.on("ready", () => {
if (game.compendiumBrowser === undefined) { if (game.compendiumBrowser === undefined) {
game.compendiumBrowser = new CompendiumBrowser(); game.compendiumBrowser = new CompendiumBrowser();
//0.4.0 Defer loading content until we actually use the Compendium Browser //0.4.0 Defer loading content until we actually use the Compendium Browser
//A compromise approach would be better (periodic loading) except would still create the memory use problem //A compromise approach would be better (periodic loading) except would still create the memory use problem
await game.compendiumBrowser.initialize(); game.compendiumBrowser.initialize();
} }
game.compendiumBrowser.addSpellFilters(); game.compendiumBrowser.addSpellFilters();