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";
|
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
import type {AjaxRequestHandler} from "./channel.ts";
|
|
|
|
import {$t_html} from "./i18n.ts";
|
|
|
|
import * as loading from "./loading.ts";
|
|
|
|
import * as modals from "./modals.ts";
|
|
|
|
import * as ui_report from "./ui_report.ts";
|
2021-07-05 19:15:37 +02:00
|
|
|
|
2024-05-15 08:38:06 +02:00
|
|
|
// Since only one dialog widget can be active at a time
|
|
|
|
// and we don't support reopening already closed dialog widgets,
|
|
|
|
// this is also the id of the current / last open dialog widget.
|
|
|
|
// We will use this as id for current dialog widget assuming
|
|
|
|
// the caller has already checked that the dialog widget is open.
|
|
|
|
let widget_id_counter = 0;
|
|
|
|
|
|
|
|
function current_dialog_widget_id(): string {
|
|
|
|
return `dialog_widget_modal_${widget_id_counter}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function current_dialog_widget_selector(): string {
|
2024-10-03 23:03:21 +02:00
|
|
|
return `#${CSS.escape(current_dialog_widget_id())}`;
|
2024-05-15 08:38:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-05 19:15:37 +02:00
|
|
|
/*
|
2024-10-24 13:33:00 +02:00
|
|
|
* Look for dialog_widget or confirm_dialog in various
|
|
|
|
* 'web/src/' files to see examples of how to use this widget.
|
|
|
|
* It's pretty simple to use!
|
2021-07-04 08:47:08 +02:00
|
|
|
*
|
|
|
|
* 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
|
2023-10-10 14:31:02 +02:00
|
|
|
* that will close the dialog via modals.close_active.
|
2021-07-04 08:47:08 +02:00
|
|
|
*
|
|
|
|
* 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;
|
2024-05-28 16:10:38 +02:00
|
|
|
on_click: (e: JQuery.ClickEvent) => void;
|
2023-03-30 18:21:28 +02:00
|
|
|
html_submit_button?: string;
|
2023-07-25 05:41:52 +02:00
|
|
|
html_exit_button?: string;
|
2023-03-30 18:21:28 +02:00
|
|
|
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;
|
2024-05-15 08:38:06 +02:00
|
|
|
post_render?: (modal_unique_id: string) => void;
|
2023-03-30 18:21:28 +02:00
|
|
|
loading_spinner?: boolean;
|
2023-04-28 11:22:17 +02:00
|
|
|
update_submit_disabled_state_on_change?: boolean;
|
2024-04-09 07:18:29 +02:00
|
|
|
always_visible_scrollbar?: 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 {
|
2024-05-15 08:38:06 +02:00
|
|
|
const dialog_widget_selector = current_dialog_widget_selector();
|
2024-10-16 20:59:20 +02:00
|
|
|
const $spinner = $(`${dialog_widget_selector} .modal__spinner`);
|
2024-11-13 13:13:51 +01:00
|
|
|
$(`${dialog_widget_selector} .modal__button`).prop("disabled", false);
|
2021-07-04 08:47:08 +02:00
|
|
|
|
2024-10-16 20:59:20 +02:00
|
|
|
loading.hide_spinner($(".dialog_submit_button"), $spinner);
|
2021-07-05 19:15:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-30 18:21:28 +02:00
|
|
|
export function show_dialog_spinner(): void {
|
2024-05-15 08:38:06 +02:00
|
|
|
const dialog_widget_selector = current_dialog_widget_selector();
|
2021-07-04 08:47:08 +02:00
|
|
|
// Disable both the buttons.
|
2024-11-13 13:13:51 +01:00
|
|
|
$(`${dialog_widget_selector} .modal__button`).prop("disabled", true);
|
2021-07-04 08:47:08 +02:00
|
|
|
|
2024-05-15 08:38:06 +02:00
|
|
|
const $spinner = $(`${dialog_widget_selector} .modal__spinner`);
|
2023-03-05 17:19:06 +01:00
|
|
|
|
2024-10-16 20:55:47 +02:00
|
|
|
loading.show_spinner($(".dialog_submit_button"), $spinner);
|
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-10-10 14:32:20 +02:00
|
|
|
export function close(on_hidden_callback?: () => void): void {
|
2024-05-15 08:38:06 +02:00
|
|
|
modals.close(current_dialog_widget_id(), {on_hidden: on_hidden_callback});
|
2021-07-27 18:48:19 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 11:22:56 +01:00
|
|
|
export function get_current_values($inputs: JQuery): Record<string, unknown> {
|
|
|
|
const current_values: Record<string, unknown> = {};
|
|
|
|
$inputs.each(function () {
|
|
|
|
const property_name = $(this).attr("name")!;
|
|
|
|
if (property_name) {
|
|
|
|
if (this instanceof HTMLInputElement && this.type === "file" && this.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.files[0];
|
|
|
|
} else if (property_name === "edit_bot_owner") {
|
|
|
|
current_values[property_name] = $(this).find(".dropdown_widget_value").text();
|
2024-03-07 11:42:17 +01:00
|
|
|
} else if ($(this).hasClass("pill-container")) {
|
|
|
|
// Notably, this just concatenates the pill labels;
|
|
|
|
// good enough for checking if something has changed,
|
|
|
|
// but not appropriate for many other potential uses.
|
|
|
|
current_values[property_name] = $(this).find(".pill-value").text();
|
2024-03-07 11:22:56 +01:00
|
|
|
} else {
|
|
|
|
current_values[property_name] = $(this).val();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return current_values;
|
|
|
|
}
|
|
|
|
|
2024-05-15 08:38:06 +02:00
|
|
|
export function launch(conf: DialogWidgetConfig): string {
|
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.
|
2023-07-25 05:41:52 +02:00
|
|
|
// * html_exit_button: Exit button text.
|
2021-07-14 22:07:03 +02:00
|
|
|
// * 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.
|
2024-04-09 07:18:29 +02:00
|
|
|
// * always_visible_scrollbar: Whether the scrollbar is always visible if modal body
|
|
|
|
// has scrollable content. Default behaviour is to hide the scrollbar when it is
|
|
|
|
// not in use.
|
2021-07-05 19:15:37 +02:00
|
|
|
|
2024-05-15 08:38:06 +02:00
|
|
|
widget_id_counter += 1;
|
|
|
|
const modal_unique_id = current_dialog_widget_id();
|
2023-04-20 00:50:38 +02:00
|
|
|
const html_submit_button = conf.html_submit_button ?? $t_html({defaultMessage: "Save changes"});
|
2023-07-25 05:41:52 +02:00
|
|
|
const html_exit_button = conf.html_exit_button ?? $t_html({defaultMessage: "Cancel"});
|
2021-07-04 08:47:08 +02:00
|
|
|
const html = render_dialog_widget({
|
2024-05-15 08:38:06 +02:00
|
|
|
modal_unique_id,
|
2021-07-14 22:21:06 +02:00
|
|
|
heading_text: conf.html_heading,
|
|
|
|
link: conf.help_link,
|
|
|
|
html_submit_button,
|
2023-07-25 05:41:52 +02:00
|
|
|
html_exit_button,
|
2021-07-14 22:21:06 +02:00
|
|
|
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,
|
2024-04-09 07:18:29 +02:00
|
|
|
always_visible_scrollbar: conf.always_visible_scrollbar,
|
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) {
|
2024-05-15 08:38:06 +02:00
|
|
|
conf.post_render(modal_unique_id);
|
2021-07-05 19:15:37 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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) {
|
2023-10-10 14:32:20 +02:00
|
|
|
close();
|
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
|
|
|
});
|
|
|
|
|
2024-05-15 08:38:06 +02:00
|
|
|
modals.open(modal_unique_id, {
|
2021-07-04 08:47:08 +02:00
|
|
|
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
|
|
|
});
|
2024-05-15 08:38:06 +02:00
|
|
|
return modal_unique_id;
|
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,
|
2024-04-04 03:36:46 +02:00
|
|
|
data: Omit<Parameters<AjaxRequestHandler>[0]["data"], "undefined">,
|
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();
|
2023-08-16 12:23:31 +02:00
|
|
|
void request_method({
|
2022-04-28 17:52:03 +02:00
|
|
|
url,
|
|
|
|
data,
|
2023-04-06 18:26:31 +02:00
|
|
|
success(response_data, textStatus, jqXHR) {
|
2023-10-10 14:32:20 +02:00
|
|
|
close();
|
2022-04-28 17:52:03 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|