Add DialogManager to a global API and create the Ask dialog for the user
This commit is contained in:
parent
02b49687cf
commit
44a88cc7b5
10 changed files with 334 additions and 1 deletions
115
module/apps/Ask.mjs
Normal file
115
module/apps/Ask.mjs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
import { __ID__, filePath } from "../consts.mjs";
|
||||
|
||||
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api;
|
||||
|
||||
export class Ask extends HandlebarsApplicationMixin(ApplicationV2) {
|
||||
static DEFAULT_OPTIONS = {
|
||||
tag: `dialog`,
|
||||
classes: [
|
||||
__ID__,
|
||||
`dialog`, // accesses some Foundry-provided styling
|
||||
`Ask`,
|
||||
],
|
||||
position: {
|
||||
width: 330,
|
||||
},
|
||||
window: {
|
||||
title: `Questions`,
|
||||
resizable: true,
|
||||
minimizable: true,
|
||||
contentTag: `form`,
|
||||
},
|
||||
form: {
|
||||
closeOnSubmit: true,
|
||||
submitOnChange: false,
|
||||
handler: this.#submit,
|
||||
},
|
||||
actions: {
|
||||
cancel: this.#cancel,
|
||||
},
|
||||
};
|
||||
|
||||
static PARTS = {
|
||||
inputs: {
|
||||
template: filePath(`templates/Ask/inputs.hbs`),
|
||||
templates: [
|
||||
filePath(`templates/Ask/inputs/input.hbs`),
|
||||
filePath(`templates/Ask/inputs/details.hbs`),
|
||||
],
|
||||
},
|
||||
controls: {
|
||||
template: filePath(`templates/Ask/controls.hbs`),
|
||||
},
|
||||
};
|
||||
|
||||
_inputs = [];
|
||||
alwaysUseAnswerObject = false;
|
||||
|
||||
/** @type {string | undefined} */
|
||||
_description = undefined;
|
||||
|
||||
/** @type {Function | undefined} */
|
||||
_userOnConfirm;
|
||||
|
||||
/** @type {Function | undefined} */
|
||||
_userOnCancel;
|
||||
|
||||
/** @type {Function | undefined} */
|
||||
_userOnClose;
|
||||
|
||||
constructor({
|
||||
inputs = [],
|
||||
description = undefined,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
onClose,
|
||||
alwaysUseAnswerObject,
|
||||
...options
|
||||
} = {}) {
|
||||
super(options);
|
||||
this.alwaysUseAnswerObject = alwaysUseAnswerObject;
|
||||
this._inputs = inputs;
|
||||
this._description = description;
|
||||
this._userOnCancel = onCancel;
|
||||
this._userOnConfirm = onConfirm;
|
||||
this._userOnClose = onClose;
|
||||
};
|
||||
|
||||
// #region Lifecycle
|
||||
async _onFirstRender() {
|
||||
super._onFirstRender();
|
||||
this.element.show();
|
||||
};
|
||||
|
||||
async _prepareContext() {
|
||||
return {
|
||||
inputs: this._inputs,
|
||||
description: this._description,
|
||||
};
|
||||
};
|
||||
|
||||
async _onClose() {
|
||||
super._onClose();
|
||||
this._userOnClose?.();
|
||||
};
|
||||
// #endregion Lifecycle
|
||||
|
||||
// #region Actions
|
||||
/** @this {AskDialog} */
|
||||
static async #submit(_event, _element, formData) {
|
||||
const answers = formData.object;
|
||||
const keys = Object.keys(answers);
|
||||
if (keys.length === 1 && !this.alwaysUseAnswerObject) {
|
||||
this._userOnConfirm?.(answers[keys[0]]);
|
||||
return;
|
||||
};
|
||||
this._userOnConfirm?.(answers);
|
||||
};
|
||||
|
||||
/** @this {AskDialog} */
|
||||
static async #cancel() {
|
||||
this._userOnCancel?.();
|
||||
this.close();
|
||||
};
|
||||
// #endregion Actions
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue