converted prompt dialog to new pattern

This commit is contained in:
zadam
2022-06-16 21:13:09 +02:00
parent 049261a8ee
commit 7ac8dc6785
8 changed files with 110 additions and 96 deletions

View File

@@ -10,7 +10,13 @@ async function confirm(message) {
appContext.triggerCommand("showConfirmDialog", {message, callback: res}));
}
async function prompt(props) {
return new Promise(res =>
appContext.triggerCommand("showPromptDialog", {...props, callback: res}));
}
export default {
info,
confirm
confirm,
prompt
};

View File

@@ -0,0 +1,92 @@
import utils from "../../services/utils.js";
import BasicWidget from "../basic_widget.js";
const TPL = `
<div class="prompt-dialog modal mx-auto" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<form id="prompt-dialog-form">
<div class="modal-header">
<h5 class="prompt-title modal-title mr-auto">Prompt</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button class="prompt-dialog-ok-button btn btn-primary btn-sm">OK <kbd>enter</kbd></button>
</div>
</form>
</div>
</div>
</div>`;
export default class PromptDialog extends BasicWidget {
constructor() {
super();
this.resolve = null;
this.shownCb = null;
}
doRender() {
this.$widget = $(TPL);
this.$dialogBody = this.$widget.find(".modal-body");
this.$form = this.$widget.find("#prompt-dialog-form");
this.$question = null;
this.$answer = null;
this.$widget.on('shown.bs.modal', () => {
if (this.shownCb) {
this.shownCb({
$dialog: this.$widget,
$question: this.$question,
$answer: this.$answer,
$form: this.$form
});
}
this.$answer.trigger('focus').select();
});
this.$widget.on("hidden.bs.modal", () => {
if (this.resolve) {
this.resolve(null);
}
});
this.$form.on('submit', e => {
e.preventDefault();
this.resolve(this.$answer.val());
this.$widget.modal('hide');
});
}
showPromptDialogEvent({ title, message, defaultValue, shown, callback }) {
this.shownCb = shown;
this.resolve = callback;
this.$widget.find(".prompt-title").text(title || "Prompt");
this.$question = $("<label>")
.prop("for", "prompt-dialog-answer")
.text(message);
this.$answer = $("<input>")
.prop("type", "text")
.prop("id", "prompt-dialog-answer")
.addClass("form-control")
.val(defaultValue || "");
this.$dialogBody.empty().append(
$("<div>")
.addClass("form-group")
.append(this.$question)
.append(this.$answer));
utils.openDialog(this.$widget, false);
}
}

View File

@@ -152,8 +152,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
this.clipboard = null;
this.$createChildNote.on('click', async () => {
const promptDialog = await import('../../dialogs/prompt.js');
const title = await promptDialog.ask({ message: "Enter title of new note", defaultValue: "new note" });
const title = await dialogService.prompt({ message: "Enter title of new note", defaultValue: "new note" });
if (!title.trim()) {
return;
@@ -216,8 +215,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
this.saveData();
}
else if (command === "editTitle") {
const promptDialog = await import("../../dialogs/prompt.js");
const title = await promptDialog.ask({
const title = await dialogService.prompt({
title: "Rename note",
message: "Enter new note title:",
defaultValue: $title.text()
@@ -470,8 +468,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
return;
}
const promptDialog = await import("../../dialogs/prompt.js");
let name = await promptDialog.ask({
let name = await dialogService.prompt({
message: "Specify new relation name (allowed characters: alphanumeric, colon and underscore):",
shown: ({ $answer }) => {
$answer.on('keyup', () => {