2021-07-05 19:15:37 +02:00
|
|
|
import $ from "jquery";
|
2023-04-28 11:22:17 +02:00
|
|
|
import _ from "lodash";
|
2021-07-05 19:15:37 +02:00
|
|
|
|
|
|
|
import render_dialog_widget from "../templates/dialog_widget.hbs";
|
|
|
|
|
2021-07-07 19:35:50 +02:00
|
|
|
import {$t_html} from "./i18n";
|
2021-07-16 08:45:23 +02:00
|
|
|
import * as loading from "./loading";
|
2021-07-05 19:15:37 +02:00
|
|
|
import * as overlays from "./overlays";
|
2023-04-06 18:26:31 +02:00
|
|
|
import type {AjaxRequestHandler} from "./types";
|
2022-04-28 17:52:03 +02:00
|
|
|
import * as ui_report from "./ui_report";
|
2021-07-05 19:15:37 +02:00
|
|
|
|
|
|
|
/*
|
2021-07-04 08:47:08 +02:00
|
|
|
* Look for confirm_dialog in settings_user_groups
|
|
|
|
* to see an example of how to use this widget. It's
|
|
|
|
* pretty simple to use!
|
|
|
|
*
|
|
|
|
* Some things to note:
|
|
|
|
* 1) We create DOM on the fly, and we remove
|
|
|
|
* the DOM once it's closed.
|
|
|
|
*
|
|
|
|
* 2) We attach the DOM for the modal to the body element
|
|
|
|
* to avoid interference from other elements.
|
|
|
|
*
|
|
|
|
* 3) For settings, we have a click handler in settings.js
|
|
|
|
* that will close the dialog via overlays.close_active_modal.
|
|
|
|
*
|
|
|
|
* 4) We assume that since this is a modal, you will
|
|
|
|
* only ever have one confirm dialog active at any
|
|
|
|
* time.
|
|
|
|
*
|
|
|
|
* 5) If a modal wants a loading spinner, it should pass loading_spinner: true.
|
|
|
|
* This will show a loading spinner when the yes button is clicked.
|
|
|
|
* The caller is responsible for calling hide_confirm_dialog_spinner()
|
|
|
|
* to hide the spinner in both success and error handlers.
|
|
|
|
*
|
|
|
|
* 6) If loading_spinner is used, don't hide it on `success`. This modal has a fade out
|
|
|
|
* animation. This causes the `Confirm` button to be shown for a split second if the
|
|
|
|
* spinner is hidden.
|
|
|
|
* Just close the modal. This will remove the whole modal from the DOM without
|
|
|
|
* needing to remove the spinner.
|
|
|
|
*
|
|
|
|
* 7) If a caller needs to run code after the modal body is added
|
|
|
|
* to DOM, it can do so by passing a post_render hook.
|
|
|
|
*/
|
2021-07-05 19:15:37 +02:00
|
|
|
|
2023-05-18 16:01:57 +02:00
|
|
|
export type DialogWidgetConfig = {
|
2023-03-30 18:21:28 +02:00
|
|
|
html_heading: string;
|
|
|
|
html_body: string;
|
|
|
|
on_click: (e: unknown) => void;
|
|
|
|
html_submit_button?: string;
|
|
|
|
close_on_submit?: boolean;
|
|
|
|
focus_submit_on_open?: boolean;
|
|
|
|
help_link?: string;
|
|
|
|
id?: string;
|
|
|
|
single_footer_button?: boolean;
|
|
|
|
form_id?: string;
|
|
|
|
validate_input?: (e: unknown) => boolean;
|
|
|
|
on_show?: () => void;
|
|
|
|
on_shown?: () => void;
|
|
|
|
on_hide?: () => void;
|
|
|
|
on_hidden?: () => void;
|
|
|
|
post_render?: () => void;
|
|
|
|
loading_spinner?: boolean;
|
2023-04-28 11:22:17 +02:00
|
|
|
update_submit_disabled_state_on_change?: boolean;
|
2023-03-30 18:21:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
type RequestOpts = {
|
|
|
|
failure_msg_html?: string;
|
2023-04-06 18:26:31 +02:00
|
|
|
success_continuation?: Parameters<AjaxRequestHandler>[0]["success"];
|
|
|
|
error_continuation?: Parameters<AjaxRequestHandler>[0]["error"];
|
2023-03-30 18:21:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export function hide_dialog_spinner(): void {
|
2021-07-05 19:15:37 +02:00
|
|
|
$(".dialog_submit_button span").show();
|
2021-07-04 08:47:08 +02:00
|
|
|
$("#dialog_widget_modal .modal__btn").prop("disabled", false);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $spinner = $("#dialog_widget_modal .modal__spinner");
|
|
|
|
loading.destroy_indicator($spinner);
|
2021-07-05 19:15:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-30 18:21:28 +02:00
|
|
|
export function show_dialog_spinner(): void {
|
2021-07-04 08:47:08 +02:00
|
|
|
// Disable both the buttons.
|
|
|
|
$("#dialog_widget_modal .modal__btn").prop("disabled", true);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $spinner = $("#dialog_widget_modal .modal__spinner");
|
2022-10-22 09:23:31 +02:00
|
|
|
const dialog_submit_button_span_width = $(".dialog_submit_button span").width();
|
|
|
|
const dialog_submit_button_span_height = $(".dialog_submit_button span").height();
|
2023-03-05 17:19:06 +01:00
|
|
|
|
|
|
|
// Hide the submit button after computing its height, since submit
|
|
|
|
// buttons with long text might affect the size of the button.
|
|
|
|
$(".dialog_submit_button span").hide();
|
|
|
|
|
2022-10-22 09:23:31 +02:00
|
|
|
loading.make_indicator($spinner, {
|
|
|
|
width: dialog_submit_button_span_width,
|
|
|
|
height: dialog_submit_button_span_height,
|
|
|
|
});
|
2021-07-05 19:15:37 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 08:05:35 +01:00
|
|
|
// Supports a callback to be called once the modal finishes closing.
|
2023-03-30 18:21:28 +02:00
|
|
|
export function close_modal(on_hidden_callback?: () => void): void {
|
2022-07-23 00:15:37 +02:00
|
|
|
overlays.close_modal("dialog_widget_modal", {on_hidden: on_hidden_callback});
|
2021-07-27 18:48:19 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:01:57 +02:00
|
|
|
export function launch(conf: DialogWidgetConfig): void {
|
2023-03-30 18:21:28 +02:00
|
|
|
// Mandatory fields:
|
|
|
|
// * html_heading
|
|
|
|
// * html_body
|
|
|
|
// * on_click
|
|
|
|
// The html_ fields should be safe HTML. If callers
|
|
|
|
// interpolate user data into strings, they should use
|
|
|
|
// templates.
|
2021-07-05 19:15:37 +02:00
|
|
|
|
2021-07-14 22:07:03 +02:00
|
|
|
// 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.
|
|
|
|
// * help_link: A help link in the heading area.
|
2021-07-21 13:43:29 +02:00
|
|
|
// * id: Custom id to the container element to modify styles.
|
2021-07-18 14:20:59 +02:00
|
|
|
// * single_footer_button: If true, don't include the "Cancel" button.
|
2021-11-21 16:03:10 +01:00
|
|
|
// * form_id: Id of the form element in the modal if it exists.
|
2021-11-29 18:42:33 +01:00
|
|
|
// * validate_input: Function to validate the input of the modal.
|
2021-11-21 15:28:46 +01:00
|
|
|
// * on_show: Callback to run when the modal is triggered to show.
|
|
|
|
// * on_shown: Callback to run when the modal is shown.
|
|
|
|
// * on_hide: Callback to run when the modal is triggered to hide.
|
|
|
|
// * on_hidden: Callback to run when the modal is hidden.
|
2022-03-07 17:21:30 +01:00
|
|
|
// * post_render: Callback to run after the modal body is added to DOM.
|
2023-03-30 18:21:28 +02:00
|
|
|
// * loading_spinner: Whether to show a loading spinner inside the
|
|
|
|
// submit button when clicked.
|
2023-04-28 11:22:17 +02:00
|
|
|
// * update_submit_disabled_state_on_change: If true, updates state of submit button
|
|
|
|
// on valid input change in modal.
|
2021-07-05 19:15:37 +02:00
|
|
|
|
2023-04-20 00:50:38 +02:00
|
|
|
const html_submit_button = conf.html_submit_button ?? $t_html({defaultMessage: "Save changes"});
|
2021-07-04 08:47:08 +02:00
|
|
|
const html = render_dialog_widget({
|
2021-07-14 22:21:06 +02:00
|
|
|
heading_text: conf.html_heading,
|
|
|
|
link: conf.help_link,
|
|
|
|
html_submit_button,
|
|
|
|
html_body: conf.html_body,
|
2021-07-21 13:43:29 +02:00
|
|
|
id: conf.id,
|
2021-07-18 14:20:59 +02:00
|
|
|
single_footer_button: conf.single_footer_button,
|
2021-07-14 22:21:06 +02:00
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dialog = $(html);
|
|
|
|
$("body").append($dialog);
|
2021-07-05 19:15:37 +02:00
|
|
|
|
|
|
|
if (conf.post_render !== undefined) {
|
|
|
|
conf.post_render();
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $submit_button = $dialog.find(".dialog_submit_button");
|
2021-11-21 16:03:10 +01:00
|
|
|
|
2023-04-28 11:22:17 +02:00
|
|
|
function get_current_values($inputs: JQuery): Record<string, unknown> {
|
|
|
|
const current_values: Record<string, unknown> = {};
|
|
|
|
$inputs.each(function () {
|
|
|
|
const property_name = $(this).attr("name")!;
|
2023-05-08 22:40:31 +02:00
|
|
|
if (property_name) {
|
|
|
|
if ($(this).is("input[type='file']") && $(this).prop("files")?.length) {
|
|
|
|
// If the input is a file input and a file has been selected, set value to file object
|
|
|
|
current_values[property_name] = $(this).prop("files")[0];
|
|
|
|
} else {
|
|
|
|
current_values[property_name] = $(this).val();
|
|
|
|
}
|
|
|
|
}
|
2023-04-28 11:22:17 +02:00
|
|
|
});
|
|
|
|
return current_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf.update_submit_disabled_state_on_change) {
|
2023-05-02 06:48:01 +02:00
|
|
|
const $inputs = $dialog.find(".modal__content").find("input,select,textarea,button");
|
2023-04-28 11:22:17 +02:00
|
|
|
|
|
|
|
const original_values = get_current_values($inputs);
|
|
|
|
|
|
|
|
$submit_button.prop("disabled", true);
|
|
|
|
|
|
|
|
$inputs.on("input", () => {
|
|
|
|
const current_values = get_current_values($inputs);
|
|
|
|
|
|
|
|
if (!_.isEqual(original_values, current_values)) {
|
|
|
|
$submit_button.prop("disabled", false);
|
|
|
|
} else {
|
|
|
|
$submit_button.prop("disabled", true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:03:10 +01:00
|
|
|
// This is used to link the submit button with the form, if present, in the modal.
|
|
|
|
// This makes it so that submitting this form by pressing Enter on an input element
|
|
|
|
// triggers a click on the submit button.
|
|
|
|
if (conf.form_id) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$submit_button.attr("form", conf.form_id);
|
2021-11-21 16:03:10 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 19:15:37 +02:00
|
|
|
// Set up handlers.
|
2022-01-25 11:36:19 +01:00
|
|
|
$submit_button.on("click", (e) => {
|
2023-06-25 06:41:27 +02:00
|
|
|
e.preventDefault();
|
|
|
|
|
2021-11-29 18:42:33 +01:00
|
|
|
if (conf.validate_input && !conf.validate_input(e)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-05 19:15:37 +02:00
|
|
|
if (conf.loading_spinner) {
|
|
|
|
show_dialog_spinner();
|
2021-07-14 22:07:03 +02:00
|
|
|
} else if (conf.close_on_submit) {
|
2021-07-27 18:48:19 +02:00
|
|
|
close_modal();
|
2021-07-05 19:15:37 +02:00
|
|
|
}
|
2021-11-24 15:52:32 +01:00
|
|
|
$("#dialog_error").hide();
|
2021-07-17 14:23:43 +02:00
|
|
|
conf.on_click(e);
|
2021-07-05 19:15:37 +02:00
|
|
|
});
|
|
|
|
|
2021-07-04 08:47:08 +02:00
|
|
|
overlays.open_modal("dialog_widget_modal", {
|
|
|
|
autoremove: true,
|
2022-11-17 23:33:43 +01:00
|
|
|
on_show() {
|
2021-11-21 15:28:46 +01:00
|
|
|
if (conf.focus_submit_on_open) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$submit_button.trigger("focus");
|
2021-11-21 15:28:46 +01:00
|
|
|
}
|
|
|
|
if (conf.on_show) {
|
|
|
|
conf.on_show();
|
|
|
|
}
|
2021-07-04 08:47:08 +02:00
|
|
|
},
|
2021-11-21 15:28:46 +01:00
|
|
|
on_hide: conf?.on_hide,
|
|
|
|
on_shown: conf?.on_shown,
|
|
|
|
on_hidden: conf?.on_hidden,
|
2021-07-05 19:15:37 +02:00
|
|
|
});
|
|
|
|
}
|
2022-04-28 17:52:03 +02:00
|
|
|
|
|
|
|
export function submit_api_request(
|
2023-04-06 18:26:31 +02:00
|
|
|
request_method: AjaxRequestHandler,
|
2023-03-30 18:21:28 +02:00
|
|
|
url: string,
|
2023-04-06 18:26:31 +02:00
|
|
|
data: Parameters<AjaxRequestHandler>[0]["data"] = {},
|
2022-04-28 17:52:03 +02:00
|
|
|
{
|
|
|
|
failure_msg_html = $t_html({defaultMessage: "Failed"}),
|
|
|
|
success_continuation,
|
|
|
|
error_continuation,
|
2023-03-30 18:21:28 +02:00
|
|
|
}: RequestOpts = {},
|
|
|
|
): void {
|
2022-04-28 17:52:03 +02:00
|
|
|
show_dialog_spinner();
|
|
|
|
request_method({
|
|
|
|
url,
|
|
|
|
data,
|
2023-04-06 18:26:31 +02:00
|
|
|
success(response_data, textStatus, jqXHR) {
|
2022-04-28 17:52:03 +02:00
|
|
|
close_modal();
|
|
|
|
if (success_continuation !== undefined) {
|
2023-04-06 18:26:31 +02:00
|
|
|
success_continuation(response_data, textStatus, jqXHR);
|
2022-04-28 17:52:03 +02:00
|
|
|
}
|
|
|
|
},
|
2023-04-06 18:26:31 +02:00
|
|
|
error(xhr, error_type, xhn) {
|
2022-04-28 17:52:03 +02:00
|
|
|
ui_report.error(failure_msg_html, xhr, $("#dialog_error"));
|
|
|
|
hide_dialog_spinner();
|
|
|
|
if (error_continuation !== undefined) {
|
2023-04-06 18:26:31 +02:00
|
|
|
error_continuation(xhr, error_type, xhn);
|
2022-04-28 17:52:03 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|