From 4ddad55232ccbc378d13323b1cd235a398ab790d Mon Sep 17 00:00:00 2001 From: opus1217 Date: Tue, 16 Feb 2021 20:39:39 -0800 Subject: [PATCH] 0.4.2f 16-Feb-2021 - Working with Loading and Maximum message at top - Reset Filters is not resetting filter display compendium-browser.js - Change preload to maxLoaded and display a message to filter if you want more - replaceList(): Pass updateLoading function (with max warning) to renderItemData en.json CMPBrowser.SETTING.Preload -> Maxload CMPBrowser.LOADING.Message, .MaxLoaded: ADDED *-browser.html - Add messages line, convert to flexcol *-browser-list.html - Use same argument (listItems) loading.html - Message (including maximum if found) compendium-browser.css - Add .list-area styling consistent with flex - Adjust max-width - Remove ul setting of width --- compendium-browser.css | 15 ++++- compendium-browser.js | 100 +++++++++++++------------------ lang/en.json | 6 +- template/feat-browser-list.html | 4 +- template/feat-browser.html | 9 ++- template/item-browser-list.html | 4 +- template/item-browser.html | 10 ++-- template/loading.html | 13 ++-- template/npc-browser-list.html | 4 +- template/npc-browser.html | 10 ++-- template/spell-browser-list.html | 5 +- template/spell-browser.html | 9 ++- 12 files changed, 96 insertions(+), 93 deletions(-) diff --git a/compendium-browser.css b/compendium-browser.css index f9e8f8f..8713105 100644 --- a/compendium-browser.css +++ b/compendium-browser.css @@ -53,12 +53,22 @@ position: sticky; display: block; min-width: 250px; - max-width: 400px; - width: 300px; + max-width: 50%; + width: 350px; height: 100%; padding-right: 5px; overflow: scroll; } +.compendium-browser .list-area { + position: sticky; + display: flex; + min-width: 250px; + max-width: 50%; + width: 400px; + height: 100%; + padding-right: 5px; + overflow-y: scroll; +} .compendium-browser .control-area button { background: rgba(0, 0, 0, 0.05); border: 1px solid #bbb; @@ -129,7 +139,6 @@ float: right; display: block; min-width: 335px; - width: 785px; margin: 0; height: 100%; overflow: auto; diff --git a/compendium-browser.js b/compendium-browser.js index 2586f0f..a1a6ebf 100644 --- a/compendium-browser.js +++ b/compendium-browser.js @@ -31,13 +31,13 @@ 0.4.2b: Add Loading... message for NPCs 0.4.2c: Correct Loading... message on initial tab, but not on tab switch 0.4.2d: Display the type of item being loaded +16-Dec-2021 0.4.2f: Change preload to maxLoaded and display a message to filter if you want more */ const CMPBrowser = { MODULE_NAME : "compendium-browser", MODULE_VERSION : "0.4.2", - PRELOAD : 9999, //How many items, spells, or NPCs you load at once (to minimize memory usage) - ignored for now - VISIBLE_ROWS : 50 //Plug for maximum rows visible in window - fetch more when actual < this + MAXLOAD : 500, //Default for the maximum number to load before displaying a message that you need to filter to see more } class CompendiumBrowser extends Application { @@ -62,15 +62,7 @@ class CompendiumBrowser extends Application { if (this.settings === undefined) { this.initSettings(); } - const numToPreload = game.settings.get(CMPBrowser.MODULE_NAME, "preload") ?? CMPBrowser.PRELOAD; -/* - this.loadItems(numToPreload).then(obj => { - this.items = obj; - }); - this.loadNpcs(numToPreload).then(obj => { - this.npcs = obj; - }); //Plug -*/ + await loadTemplates([ "modules/compendium-browser/template/spell-browser.html", "modules/compendium-browser/template/spell-browser-list.html", @@ -129,15 +121,7 @@ class CompendiumBrowser extends Application { /** override */ async getData() { - //Only called on initial display or refresh (including when settings are changed) - const numToPreload = game.settings.get(CMPBrowser.MODULE_NAME, "preload") ?? CMPBrowser.PRELOAD; -/* - if (!this.spellsLoaded) { - // spells will be stored locally to not require full loading each time the browser is opened - this.items = await this.loadItems(numToPreload); //also sets this.spellsLoaded - } -*/ //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 //First time (when you press Compendium Browser button) is called with filters unset @@ -474,11 +458,13 @@ class CompendiumBrowser extends Application { } } - async loadAndFilterItems(browserTab="spell",updateLoading, numToPreload=CMPBrowser.PRELOAD) { + async loadAndFilterItems(browserTab="spell",updateLoading=null) { console.log(`Load and Filter Items | Started loading ${browserTab}s`); console.time("loadAndFilterItems"); await this.checkListsLoaded(); + const maxLoad = game.settings.get(CMPBrowser.MODULE_NAME, "maxload") ?? CMPBrowser.MAXLOAD; + //0.4.1: Load and filter just one of spells, feats, and items (specified by browserTab) let unfoundSpells = ''; let numItemsLoaded = 0; @@ -524,7 +510,7 @@ class CompendiumBrowser extends Application { } if (compactItem) { //Indicates it passed the filters compactItems[decoratedItem._id] = compactItem; - if (numItemsLoaded++ >= numToPreload) break; + if (numItemsLoaded++ >= maxLoad) break; //0.4.2e: Update the UI (e.g. "Loading 142 spells") if (updateLoading) {updateLoading(numItemsLoaded);} } @@ -532,7 +518,7 @@ class CompendiumBrowser extends Application { }//for item5e of content }); }//end if pack entity === Item - if (numItemsLoaded >= numToPreload) break; + if (numItemsLoaded >= maxLoad) break; }//for packs /* @@ -689,12 +675,14 @@ class CompendiumBrowser extends Application { return items; } - async loadAndFilterNpcs(numToPreload=CMPBrowser.PRELOAD) { + async loadAndFilterNpcs(updateLoading=null) { console.log('NPC Browser | Started loading NPCs'); console.time("loadAndFilterNpcs"); let npcs = {}; - let numberLoaded = 0; + const maxLoad = game.settings.get(CMPBrowser.MODULE_NAME, "maxload") ?? CMPBrowser.MAXLOAD; + + let numNpcsLoaded = 0; this.npcsLoaded = false; for (let pack of game.packs) { if (pack['metadata']['entity'] == "Actor" && this.settings.loadedNpcCompendium[pack.collection].load) { @@ -717,15 +705,17 @@ class CompendiumBrowser extends Application { } if (compactNpc) { npcs[decoratedNpc._id] = compactNpc; + //0.4.2 Don't load more than maxLoad; display a message to filter + if (numNpcsLoaded++ > maxLoad) break; + //0.4.2e: Update the UI (e.g. "Loading 142 NPCs") + if (updateLoading) {updateLoading(numNpcsLoaded);} } - //0.4.1 Only preload a limited number and fill more in as needed - if (numberLoaded++ > numToPreload) break; } } }); } //0.4.1 Only preload a limited number and fill more in as needed - if (numberLoaded > numToPreload) break; + if (numNpcsLoaded >= maxLoad) break; } this.npcsLoaded = true; @@ -775,21 +765,28 @@ class CompendiumBrowser extends Application { //After rendering the first time or re-rendering trigger the load/reload of visible data let elements = null; + //0.4.2 Display a Loading... message while the data is being loaded and filtered + let loadingMessage = null; if (browserTab === 'spell') { elements = html.find("ul#CBSpells"); + loadingMessage = html.find("#CBSpellsMessage"); } else if (browserTab === 'npc') { elements = html.find("ul#CBNPCs"); + loadingMessage = html.find("#CBNpcsMessage"); } else if (browserTab === 'feat') { elements = html.find("ul#CBFeats"); + loadingMessage = html.find("#CBFeatsMessage"); } else if (browserTab === 'item') { elements = html.find("ul#CBItems"); + loadingMessage = html.find("#CBItemsMessage"); } if (elements?.length) { //0.4.2b: On a tab-switch, only reload if there isn't any data already if (options?.reload || !elements[0].children.length) { - //0.4.2 Display a Loading... message while the data is being loaded and filtered + + const maxLoad = game.settings.get(CMPBrowser.MODULE_NAME, "maxload") ?? CMPBrowser.MAXLOAD; const updateLoading = async numLoaded => { - this.renderLoading(elements[0], browserTab, numLoaded); + if (loadingMessage.length) {this.renderLoading(loadingMessage[0], browserTab, numLoaded, numLoaded>=maxLoad);} } updateLoading(0); @@ -811,35 +808,22 @@ class CompendiumBrowser extends Application { } - async renderLoading(rootElement, itemType, numLoaded) { - if (!rootElement) return; - const loadingProgress = { - name: "Loading...", - img: "icons/sundries/books/book-open-turquoise.webp", - numLoaded: numLoaded, - itemType: itemType - } + async renderLoading(messageElement, itemType, numLoaded, maxLoaded=false) { + if (!messageElement) return; - let loadingHTML = await renderTemplate("modules/compendium-browser/template/loading.html", { loadingProgress: loadingProgress }); - rootElement.innerHTML = loadingHTML; + let loadingHTML = await renderTemplate("modules/compendium-browser/template/loading.html", {numLoaded: numLoaded, itemType: itemType, maxLoaded: maxLoaded}); + messageElement.innerHTML = loadingHTML; } async renderItemData(browserTab, updateLoading=null) { - let items; - let html; - if (browserTab === "spell") { - items = await this.loadAndFilterItems(browserTab, updateLoading); - html = await renderTemplate("modules/compendium-browser/template/spell-browser-list.html", {spells : items}); - } else if (browserTab === "feat") { - items = await this.loadAndFilterItems(browserTab, updateLoading); - html = await renderTemplate("modules/compendium-browser/template/feat-browser-list.html", {feats : items}); - } else if (browserTab === "npc") { - const npcs = await this.loadAndFilterNpcs(updateLoading); - html = await renderTemplate("modules/compendium-browser/template/npc-browser-list.html", {npcs : npcs}); + let listItems; + if (browserTab === "npc") { + listItems = await this.loadAndFilterNpcs(updateLoading); } else { - items = await this.loadAndFilterItems(browserTab, updateLoading); - html = await renderTemplate("modules/compendium-browser/template/item-browser-list.html", {items : items}); + listItems = await this.loadAndFilterItems(browserTab, updateLoading); } + const html = await renderTemplate(`modules/compendium-browser/template/${browserTab}-browser-list.html`, {listItems : listItems}) + return html; } @@ -1202,16 +1186,16 @@ class CompendiumBrowser extends Application { this.settings = settings; } }); - game.settings.register(CMPBrowser.MODULE_NAME, "preload", { - name: game.i18n.localize("CMPBrowser.SETTING.Preload.NAME"), - hint: game.i18n.localize("CMPBrowser.SETTING.Preload.HINT"), + game.settings.register(CMPBrowser.MODULE_NAME, "maxload", { + name: game.i18n.localize("CMPBrowser.SETTING.Maxload.NAME"), + hint: game.i18n.localize("CMPBrowser.SETTING.Maxload.HINT"), scope: "world", config: true, - default: CMPBrowser.PRELOAD, + default: CMPBrowser.MAXLOAD, type: Number, range: { // If range is specified, the resulting setting will be a range slider - min: 20, - max: 9999, + min: 200, + max: 5000, step: 100 } }); diff --git a/lang/en.json b/lang/en.json index 1c41eff..8c43e23 100644 --- a/lang/en.json +++ b/lang/en.json @@ -60,7 +60,9 @@ "CMPBrowser.Tab.ItemBrowser": "Item Browser", "CMPBrowser.Tab.NPCBrowser":"NPC Browser", "CMPBrowser.Tab.Settings":"Settings", - "CMPBrowser.SETTING.Preload.NAME" : "Number to preload", - "CMPBrowser.SETTING.Preload.HINT" : "How many spells, feats, items, and NPCs to preload to keep memory manageable. " + "CMPBrowser.SETTING.Maxload.NAME" : "Maximum load", + "CMPBrowser.SETTING.Maxload.HINT" : "Maximum number of spells, feats, items, or NPCs to display; to see more use the filters. This setting is to allow manageing memory and server load.", + "CMPBrowser.LOADING.Message" : "Loaded...{numLoaded} {itemType}s", + "CMPBrowser.LOADING.MaxLoaded" : "(maximum displayed; to see more, use the filters)" } \ No newline at end of file diff --git a/template/feat-browser-list.html b/template/feat-browser-list.html index 35fdf09..0fb8d35 100644 --- a/template/feat-browser-list.html +++ b/template/feat-browser-list.html @@ -1,4 +1,4 @@ -{{#each feats as |feat id|}} +{{#each listItems as |feat id|}}
  • @@ -14,4 +14,4 @@
  • -{{/each}} \ No newline at end of file +{{/each}} diff --git a/template/feat-browser.html b/template/feat-browser.html index 26076aa..1d21166 100644 --- a/template/feat-browser.html +++ b/template/feat-browser.html @@ -15,8 +15,11 @@ {{> "modules/compendium-browser/template/filter-container.html" filters=featFilters}} - +
    + + +
    \ No newline at end of file diff --git a/template/item-browser-list.html b/template/item-browser-list.html index 4565fe2..9beaf6b 100644 --- a/template/item-browser-list.html +++ b/template/item-browser-list.html @@ -1,4 +1,4 @@ -{{#each items as |item id|}} +{{#each listItems as |item id|}}
  • @@ -13,4 +13,4 @@
  • -{{/each}} \ No newline at end of file +{{/each}} diff --git a/template/item-browser.html b/template/item-browser.html index 659af6d..2c72c23 100644 --- a/template/item-browser.html +++ b/template/item-browser.html @@ -15,8 +15,10 @@ {{> "modules/compendium-browser/template/filter-container.html" filters=itemFilters}} - - +
    + + +
    \ No newline at end of file diff --git a/template/loading.html b/template/loading.html index e36968d..d558fab 100644 --- a/template/loading.html +++ b/template/loading.html @@ -1,9 +1,6 @@ -
  • -
    - -
    -
    - {{loadingProgress.name}} {{loadingProgress.numLoaded}} {{loadingProgress.itemType}}s -
    -
  • \ No newline at end of file + + + {{localize "CMPBrowser.LOADING.Message" numLoaded=numLoaded itemType=itemType}} + {{#if maxLoaded}}{{localize "CMPBrowser.LOADING.MaxLoaded"}}{{/if}} + diff --git a/template/npc-browser-list.html b/template/npc-browser-list.html index 9a8cc50..818e462 100644 --- a/template/npc-browser-list.html +++ b/template/npc-browser-list.html @@ -1,4 +1,4 @@ -{{#each npcs as |npc id|}} +{{#each listItems as |npc id|}}
  • @@ -18,4 +18,4 @@
  • -{{/each}} \ No newline at end of file +{{/each}} diff --git a/template/npc-browser.html b/template/npc-browser.html index ab86f44..e45c1a2 100644 --- a/template/npc-browser.html +++ b/template/npc-browser.html @@ -16,8 +16,10 @@ {{> "modules/compendium-browser/template/filter-container.html" filters=npcFilters}} - - +
    + + +
    \ No newline at end of file diff --git a/template/spell-browser-list.html b/template/spell-browser-list.html index 6c9f3d8..5eb0d56 100644 --- a/template/spell-browser-list.html +++ b/template/spell-browser-list.html @@ -1,5 +1,5 @@ -{{#each spells as |spell id|}} +{{#each listItems as |spell id|}}
  • @@ -21,4 +21,5 @@
  • -{{/each}} \ No newline at end of file +{{/each}} + \ No newline at end of file diff --git a/template/spell-browser.html b/template/spell-browser.html index cad827e..100315a 100644 --- a/template/spell-browser.html +++ b/template/spell-browser.html @@ -15,7 +15,10 @@ {{> "modules/compendium-browser/template/filter-container.html" filters=spellFilters}} - +
    + + +
    \ No newline at end of file