|
| 1 | +import { LANG_ID } from "../constants.mjs"; |
| 2 | +import { ScriptModel } from "../data/script-model.mjs"; |
| 3 | + |
| 4 | +export class ScriptChoiceDialog extends Dialog { |
| 5 | + constructor(scripts, dialogData, options) { |
| 6 | + super(dialogData, options); |
| 7 | + this.options.classes = ["dialog", "dnd5e", "script-choice-dialog"]; |
| 8 | + |
| 9 | + this.scripts = scripts; |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * A constructor function which displays the Script Choice Dialog app for a given Array of Scripts. |
| 14 | + * Returns a Promise which resolves to the chosen Script's id once the choice is made. |
| 15 | + * @param {ScriptModel[]} scripts Array of Scripts being chosen from. |
| 16 | + * @returns {Promise} Promise that is resolved when the use dialog is acted upon. |
| 17 | + */ |
| 18 | + static async create(scripts) { |
| 19 | + if (!scripts.length) return null; |
| 20 | + |
| 21 | + return new Promise((resolve) => { |
| 22 | + const dlg = new this(scripts, { |
| 23 | + title: "Select a Script", |
| 24 | + content: `<p>${game.i18n.localize(`${LANG_ID}.Dialog.ScriptChoice.Prompt`)}</p>`, |
| 25 | + buttons: scripts.reduce((obj, script) => { |
| 26 | + obj[script.id] = { |
| 27 | + icon: `<img src="${script.img}">`, |
| 28 | + label: script.name, |
| 29 | + callback: () => { |
| 30 | + resolve(script.id); |
| 31 | + }, |
| 32 | + }; |
| 33 | + return obj; |
| 34 | + }, {}), |
| 35 | + default: scripts[0].id, |
| 36 | + close: () => resolve(null), |
| 37 | + }); |
| 38 | + dlg.render(true); |
| 39 | + }); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +globalThis["ScriptChoiceDialog"] = ScriptChoiceDialog; |
0 commit comments