dialog_widget: Remove is_confirm_dialog variable.

Making the controls declarative makes the system more flexible for
future use cases.
This commit is contained in:
Tim Abbott
2021-07-14 13:07:03 -07:00
parent fd55df9f7c
commit 6fb0d414b4
3 changed files with 26 additions and 13 deletions

View File

@@ -1,10 +1,13 @@
import {$t_html} from "./i18n";
import * as dialog_widget from "./dialog_widget"; import * as dialog_widget from "./dialog_widget";
import {$t_html} from "./i18n";
export function launch(conf) { export function launch(conf) {
dialog_widget.launch( dialog_widget.launch({
{...conf, ...conf,
close_on_submit: true,
danger_submit_button: true,
focus_submit_on_open: true,
html_submit_button: $t_html({defaultMessage: "Confirm"}), html_submit_button: $t_html({defaultMessage: "Confirm"}),
is_confirm_dialog: true, // Used to control button colors in the template.
}); });
} }

View File

@@ -62,9 +62,6 @@ export function show_dialog_spinner() {
} }
export function launch(conf) { export function launch(conf) {
const html = render_dialog_widget({fade: conf.fade, is_confirm_dialog: conf.is_confirm_dialog});
const dialog = $(html);
const mandatory_fields = [ const mandatory_fields = [
// The html_ fields should be safe HTML. If callers // The html_ fields should be safe HTML. If callers
// interpolate user data into strings, they should use // interpolate user data into strings, they should use
@@ -75,12 +72,25 @@ export function launch(conf) {
"parent", "parent",
]; ];
// Optional parameters:
// * html_submit_button: Submit button text.
// * close_on_submit: Whether to close modal on clicking submit.
// * focus_submit_on_open: Whether to focus submit button on open.
// * danger_submit_button: Whether to use danger button styling for submit button.
// * help_link: A help link in the heading area.
for (const f of mandatory_fields) { for (const f of mandatory_fields) {
if (conf[f] === undefined) { if (conf[f] === undefined) {
blueslip.error("programmer omitted " + f); blueslip.error("programmer omitted " + f);
} }
} }
const html = render_dialog_widget({
fade: conf.fade,
danger_submit_button: conf.danger_submit_button,
});
const dialog = $(html);
conf.parent.append(dialog); conf.parent.append(dialog);
// Close any existing modals--on settings screens you can // Close any existing modals--on settings screens you can
@@ -99,7 +109,7 @@ export function launch(conf) {
const submit_button_span = dialog.find(".dialog_submit_button span"); const submit_button_span = dialog.find(".dialog_submit_button span");
let html_submit_button = conf.html_submit_button || $t_html({defaultMessage: "Save changes"}); const html_submit_button = conf.html_submit_button || $t_html({defaultMessage: "Save changes"});
submit_button_span.html(html_submit_button); submit_button_span.html(html_submit_button);
if (conf.post_render !== undefined) { if (conf.post_render !== undefined) {
@@ -111,7 +121,7 @@ export function launch(conf) {
submit_button.on("click", () => { submit_button.on("click", () => {
if (conf.loading_spinner) { if (conf.loading_spinner) {
show_dialog_spinner(); show_dialog_spinner();
} else if (conf.is_confirm_dialog) { } else if (conf.close_on_submit) {
overlays.close_modal("#dialog_widget_modal"); overlays.close_modal("#dialog_widget_modal");
} }
conf.on_click(); conf.on_click();
@@ -124,7 +134,7 @@ export function launch(conf) {
// Open the modal // Open the modal
overlays.open_modal("#dialog_widget_modal"); overlays.open_modal("#dialog_widget_modal");
if (conf.is_confirm_dialog) { if (conf.focus_submit_on_open) {
conf.parent.on("shown.bs.modal", () => { conf.parent.on("shown.bs.modal", () => {
submit_button.trigger("focus"); submit_button.trigger("focus");
}); });

View File

@@ -8,7 +8,7 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button class="button rounded close-modal-btn" data-dismiss="modal">{{t "Cancel" }}</button> <button class="button rounded close-modal-btn" data-dismiss="modal">{{t "Cancel" }}</button>
<button class="button rounded {{#if is_confirm_dialog}}btn-danger{{else}}sea-green{{/if}} dialog_submit_button"> <button class="button rounded {{#if danger_submit_button}}btn-danger{{else}}sea-green{{/if}} dialog_submit_button">
<object class="loader" type="image/svg+xml" data="/static/images/loader.svg"></object> <object class="loader" type="image/svg+xml" data="/static/images/loader.svg"></object>
<span></span> <span></span>
</button> </button>