Make TableCreator disable the button when the dice syntax doesn't pass validation

This commit is contained in:
Oliver-Akins 2025-05-03 21:48:29 -06:00
parent c5c1c67efe
commit 6a83209a58
2 changed files with 20 additions and 3 deletions

View file

@ -5,6 +5,8 @@ import { Logger } from "../utils/Logger.mjs";
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api; const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api;
const diceNamespacePattern = /^Dice\/d[0-9]+$/;
export class TableCreator extends HandlebarsApplicationMixin(ApplicationV2) { export class TableCreator extends HandlebarsApplicationMixin(ApplicationV2) {
// #region Options // #region Options
static DEFAULT_OPTIONS = { static DEFAULT_OPTIONS = {
@ -29,7 +31,7 @@ export class TableCreator extends HandlebarsApplicationMixin(ApplicationV2) {
}; };
static PARTS = { static PARTS = {
tableSelect: { content: {
template: filePath(`templates/Apps/TableCreator.hbs`), template: filePath(`templates/Apps/TableCreator.hbs`),
root: true, root: true,
}, },
@ -71,14 +73,16 @@ export class TableCreator extends HandlebarsApplicationMixin(ApplicationV2) {
// Special Case for the dice namespace // Special Case for the dice namespace
if (this._name.startsWith(`Dice`)) { if (this._name.startsWith(`Dice`)) {
ctx.createButtonDisabled = !this._name.match(diceNamespacePattern);
ctx.typeDisabled = true; ctx.typeDisabled = true;
ctx.type = BucketTypes.RANGE; ctx.type = BucketTypes.RANGE;
this.#diceNamespaceAlert = ui.notifications.info( this.#diceNamespaceAlert ??= ui.notifications.info(
`Tables in the "Dice" namespace must be formatted as "Dice/dX" where X is the number of sides on the die and are restricted to be ranges 1 to X.`, `Tables in the "Dice" namespace must be formatted as "Dice/dX" where X is the number of sides on the die and are restricted to be ranges 1 to X.`,
{ permanent: true }, { permanent: true },
); );
} else if (this.#diceNamespaceAlert != null) { } else if (this.#diceNamespaceAlert != null) {
ui.notifications.remove(this.#diceNamespaceAlert); ui.notifications.remove(this.#diceNamespaceAlert);
this.#diceNamespaceAlert = null;
}; };
if (import.meta.env.DEV) { if (import.meta.env.DEV) {
@ -115,10 +119,11 @@ export class TableCreator extends HandlebarsApplicationMixin(ApplicationV2) {
const existing = CONFIG.stats.db.getTable(name); const existing = CONFIG.stats.db.getTable(name);
if (existing) { if (existing) {
ui.notifications.error(`A table with the name "${name}" already exists`); ui.notifications.error(`A table with the name "${name}" already exists`);
return;
}; };
if (name.startsWith(`Dice`)) { if (name.startsWith(`Dice`)) {
if (!name.match(/^Dice\/d[0-9]+$/)) { if (!name.match(diceNamespacePattern)) {
ui.notifications.error(`Table name doesn't conform to the "Dice/dX" format required by the Dice namespace.`); ui.notifications.error(`Table name doesn't conform to the "Dice/dX" format required by the Dice namespace.`);
return; return;
}; };
@ -126,5 +131,16 @@ export class TableCreator extends HandlebarsApplicationMixin(ApplicationV2) {
CONFIG.stats.db.createTable(createDiceTable(size)); CONFIG.stats.db.createTable(createDiceTable(size));
return; return;
}; };
CONFIG.stats.db.createTable({
name,
buckets: {
type: this._type,
},
graph: {
type: `bar`,
stacked: true,
},
});
}; };
}; };

View file

@ -26,6 +26,7 @@
<button <button
type="button" type="button"
data-action="createTable" data-action="createTable"
{{ disabled createButtonDisabled }}
> >
Create Table Create Table
</button> </button>