Skip to content

Commit

Permalink
Add more options to Compendium Browser roll table dialogs (#8246)
Browse files Browse the repository at this point in the history
  • Loading branch information
In3luki authored Jun 12, 2023
1 parent 970d3f2 commit 3f3327c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 47 deletions.
25 changes: 1 addition & 24 deletions src/module/apps/compendium-browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,30 +643,7 @@ class CompendiumBrowser extends Application {
// Add to Roll Table button
htmlQuery(html, "[data-action=add-to-roll-table]")?.addEventListener("click", async () => {
if (!game.tables.contents.length) return;
const selectEl = document.createElement("select");
selectEl.style.marginLeft = "5px";
const options: HTMLOptionElement[] = [];
for (const table of game.tables.contents) {
if (!table._id) continue;

const option = document.createElement("option");
option.value = table._id;
option.text = table.name;
options.push(option);
}
selectEl.append(...options);
const labelEl = document.createElement("label");
labelEl.innerText = game.i18n.localize("PF2E.CompendiumBrowser.RollTable.AddLabel");
labelEl.appendChild(selectEl);
Dialog.confirm({
title: game.i18n.localize("PF2E.CompendiumBrowser.RollTable.SelectTableTitle"),
content: `<p>${labelEl.outerHTML}</p>`,
yes: ($html) => {
const option = htmlQuery<HTMLSelectElement>($html[0], "select")?.selectedOptions[0];
if (!option) return;
currentTab.addToRollTable(option.value);
},
});
currentTab.addToRollTable();
});

// Filters
Expand Down
71 changes: 49 additions & 22 deletions src/module/apps/compendium-browser/tabs/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorPF2e, sluggify } from "@util";
import { ErrorPF2e, htmlQuery, sluggify } from "@util";
import MiniSearch from "minisearch";
import { BrowserTabs, ContentTabName } from "../data.ts";
import { CompendiumBrowser } from "../index.ts";
Expand Down Expand Up @@ -261,7 +261,13 @@ export abstract class CompendiumBrowserTab {
return true;
}

#getRollTableResults(initial = 0): Partial<TableResultSource>[] {
#getRollTableResults({
initial = 0,
weight = 1,
}: {
initial?: number;
weight?: number;
}): Partial<TableResultSource>[] {
return this.currentIndex.flatMap((e, i) => {
const documentData = fromUuidSync(e.uuid);
if (!documentData?.pack || !documentData?._id) return [];
Expand All @@ -272,7 +278,7 @@ export abstract class CompendiumBrowserTab {
collection: documentData.pack,
resultId: documentData._id,
img: e.img,
weight: 1,
weight,
range: [rangeMinMax, rangeMinMax],
drawn: false,
};
Expand All @@ -283,31 +289,52 @@ export abstract class CompendiumBrowserTab {
if (!this.isInitialized) {
throw ErrorPF2e(`Compendium Browser Tab "${this.tabName}" is not initialized!`);
}

const results = this.#getRollTableResults();
const create = await Dialog.confirm({
content: `<p>${game.i18n.format("PF2E.CompendiumBrowser.RollTable.CreateDialogText", {
count: results.length,
})}</p>`,
const content = await renderTemplate("systems/pf2e/templates/compendium-browser/roll-table-dialog.hbs", {
count: this.currentIndex.length,
});
Dialog.confirm({
content,
title: game.i18n.localize("PF2E.CompendiumBrowser.RollTable.CreateLabel"),
yes: async ($html) => {
const html = $html[0];
const name =
htmlQuery<HTMLInputElement>(html, "input[name=name]")?.value ||
game.i18n.localize("PF2E.CompendiumBrowser.Title");
const weight = Number(htmlQuery<HTMLInputElement>(html, "input[name=weight]")?.value) || 1;
const results = this.#getRollTableResults({ weight });
const table = await RollTable.create({
name,
results,
formula: `1d${results.length}`,
});
table?.sheet.render(true);
},
});
if (create) {
const table = await RollTable.create({
name: game.i18n.localize("PF2E.CompendiumBrowser.Title"),
results,
formula: `1d${results.length}`,
});
table?.sheet.render(true);
}
}

async addToRollTable(id: string): Promise<void> {
async addToRollTable(): Promise<void> {
if (!this.isInitialized) {
throw ErrorPF2e(`Compendium Browser Tab "${this.tabName}" is not initialized!`);
}

const table = game.tables.get(id, { strict: true });
await table.createEmbeddedDocuments("TableResult", this.#getRollTableResults(table.results.size));
table?.sheet.render(true);
const content = await renderTemplate("systems/pf2e/templates/compendium-browser/roll-table-dialog.hbs", {
count: this.currentIndex.length,
rollTables: game.tables.contents,
});
Dialog.confirm({
title: game.i18n.localize("PF2E.CompendiumBrowser.RollTable.SelectTableTitle"),
content,
yes: async ($html) => {
const html = $html[0];
const option = htmlQuery<HTMLSelectElement>(html, "select[name=roll-table]")?.selectedOptions[0];
if (!option) return;
const weight = Number(htmlQuery<HTMLInputElement>(html, "input[name=weight]")?.value) || 1;
const table = game.tables.get(option.value, { strict: true });
await table.createEmbeddedDocuments(
"TableResult",
this.#getRollTableResults({ initial: table.results.size, weight })
);
table?.sheet.render(true);
},
});
}
}
4 changes: 3 additions & 1 deletion static/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,12 @@
"FailedToBuyItemWithSomeCharacters": "Could not purchase {item} with some of the selected actor(s).",
"Hint": "Use the tabs above to select a compendium",
"RollTable": {
"AddDialogText": "Add {count} result(s) to Roll Table?",
"AddLabel": "Add to Roll Table",
"CreateDialogText": "Create Roll Table with {count} result(s)?",
"CreateLabel": "Create Roll Table",
"SelectTableTitle": "Select Table"
"SelectTableTitle": "Select Roll Table",
"WeightLabel": "Weight"
},
"Settings": {
"PacksLabel": "Packs",
Expand Down
30 changes: 30 additions & 0 deletions static/templates/compendium-browser/roll-table-dialog.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<form>
{{#if rollTables}}
<div class="form-group">
<label>{{localize "PF2E.CompendiumBrowser.RollTable.AddLabel"}}</label>
<select name="roll-table">
{{#each rollTables as |table|}}
<option value="{{table.id}}">{{table.name}}</option>
{{/each}}
</select>
</div>
<div class="form-group">
<label>{{localize "PF2E.CompendiumBrowser.RollTable.WeightLabel"}}</label>
<input type="text" name="weight" placeholder="1">
</div>
<hr>
<p>{{localize "PF2E.CompendiumBrowser.RollTable.AddDialogText" count=count}}</p>
{{else}}
<div class="form-group">
<label>{{localize "Name"}}</label>
<input type="text" name="name" placeholder="{{localize "PF2E.CompendiumBrowser.Title"}}">
</div>
<div class="form-group">
<label>{{localize "PF2E.CompendiumBrowser.RollTable.WeightLabel"}}</label>
<input type="text" name="weight" placeholder="1">
</div>
<hr>
<p>{{localize "PF2E.CompendiumBrowser.RollTable.CreateDialogText" count=count}}</p>
{{/if}}
<br>
</form>

0 comments on commit 3f3327c

Please sign in to comment.