From 459a0aab239eea8d668b0638c5fdf8ed400e4dc9 Mon Sep 17 00:00:00 2001 From: Matheus Clemente Date: Wed, 8 Nov 2023 16:34:25 -0300 Subject: [PATCH] Refactoring --- compendium-browser.js | 116 +++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/compendium-browser.js b/compendium-browser.js index 99ac0e8..a26e397 100644 --- a/compendium-browser.js +++ b/compendium-browser.js @@ -17,27 +17,22 @@ class CompendiumBrowser extends Application { return options; } + get maxLoad() { + return game.settings.get("compendium-browser", "maxload"); + } + async initialize() { // load settings - if (this.settings === undefined) { - this.initSettings(); - } + this.initSettings(); - 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", - ]); - - this.hookCompendiumList(); + Hooks.on("changeSidebarTab", (app) => { + if (app.tabName !== "compendium") return; + this.hookCompendiumList(app.element); + }); + Hooks.on("renderSidebarTab", (app, html, data) => { + if (app.tabName !== "compendium") return; + this.hookCompendiumList(html); + }); //Reset the filters used in the dialog this.spellFilters = { @@ -58,14 +53,14 @@ class CompendiumBrowser extends Application { }; } - /** override */ + /** @override */ _onChangeTab(event, tabs, active) { super._onChangeTab(event, tabs, active); const html = this.element; this.replaceList(html, active, { reload: false }); } - /** override */ + /** @override */ async getData() { //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 @@ -127,7 +122,7 @@ class CompendiumBrowser extends Application { }); } - /** override */ + /** @override */ activateListeners(html) { super.activateListeners(html); @@ -447,7 +442,7 @@ class CompendiumBrowser extends Application { numItemsLoaded = Object.keys(itemsList).length; - if (maxLoad <= numItemsLoaded) { + if (this.maxLoad <= numItemsLoaded) { if (updateLoading) { updateLoading(numItemsLoaded, true); } @@ -483,7 +478,7 @@ class CompendiumBrowser extends Application { numItemsLoaded = Object.keys(itemsList).length; - if (maxLoad <= numItemsLoaded) { + if (this.maxLoad <= numItemsLoaded) { if (updateLoading) { updateLoading(numItemsLoaded, true); } @@ -516,7 +511,7 @@ class CompendiumBrowser extends Application { numItemsLoaded = Object.keys(itemsList).length; - if (maxLoad <= numItemsLoaded) { + if (this.maxLoad <= numItemsLoaded) { if (updateLoading) { updateLoading(numItemsLoaded, true); } @@ -574,8 +569,6 @@ class CompendiumBrowser extends Application { console.time("loadAndFilterNpcs"); let npcs = {}; - const maxLoad = game.settings.get("compendium-browser", "maxload"); - let numNpcsLoaded = 0; this.npcsLoaded = false; @@ -608,7 +601,7 @@ class CompendiumBrowser extends Application { numNpcsLoaded = Object.keys(npcs).length; - if (maxLoad <= numNpcsLoaded) { + if (this.maxLoad <= numNpcsLoaded) { if (updateLoading) { updateLoading(numNpcsLoaded, true); } @@ -662,15 +655,7 @@ class CompendiumBrowser extends Application { return npcs; } - hookCompendiumList() { - Hooks.on("renderCompendiumDirectory", (app, html, data) => { - this.hookCompendiumList(); - }); - - let html = $("#compendium"); - if (this.settings === undefined) { - this.initSettings(); - } + hookCompendiumList(html) { if (game.user.isGM || this.settings.allowSpellBrowser || this.settings.allowNpcBrowser) { const cbButton = $( `