2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2023-02-02 21:22:40 +01:00
|
|
|
import checkbox_image from "../images/checkbox-green.svg";
|
|
|
|
|
2023-08-16 12:23:31 +02:00
|
|
|
import type {AjaxRequestHandler} from "./channel";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-02-28 00:36:14 +01:00
|
|
|
import * as loading from "./loading";
|
2021-02-28 00:58:55 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2021-02-28 00:36:14 +01:00
|
|
|
|
2023-08-04 09:58:55 +02:00
|
|
|
type RequestOpts = {
|
|
|
|
success_msg_html?: string;
|
|
|
|
failure_msg_html?: string;
|
|
|
|
success_continuation?: (response_data: unknown) => void;
|
|
|
|
error_continuation?: (xhr: JQuery.jqXHR) => void;
|
|
|
|
sticky?: boolean;
|
|
|
|
$error_msg_element?: JQuery;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function display_checkmark($elem: JQuery): void {
|
2024-04-04 00:18:17 +02:00
|
|
|
const $check_mark = $("<img>");
|
|
|
|
$check_mark.attr("src", checkbox_image);
|
|
|
|
$check_mark.css("width", "13px");
|
|
|
|
$elem.prepend($check_mark);
|
2021-02-10 17:09:37 +01:00
|
|
|
}
|
2018-03-05 03:13:29 +01:00
|
|
|
|
2021-02-10 17:09:37 +01:00
|
|
|
export const strings = {
|
2021-04-13 05:18:25 +02:00
|
|
|
success_html: $t_html({defaultMessage: "Saved"}),
|
|
|
|
failure_html: $t_html({defaultMessage: "Save failed"}),
|
2021-04-13 06:51:54 +02:00
|
|
|
saving: $t({defaultMessage: "Saving"}),
|
2018-03-05 03:23:38 +01:00
|
|
|
};
|
|
|
|
// Generic function for informing users about changes to the settings
|
|
|
|
// UI. Intended to replace the old system that was built around
|
|
|
|
// direct calls to `ui_report`.
|
2021-03-24 21:44:43 +01:00
|
|
|
export function do_settings_change(
|
2023-08-04 09:58:55 +02:00
|
|
|
request_method: AjaxRequestHandler,
|
|
|
|
url: string,
|
2024-04-04 03:36:46 +02:00
|
|
|
data: Omit<Parameters<AjaxRequestHandler>[0]["data"], "undefined">,
|
2023-10-05 08:17:51 +02:00
|
|
|
$status_element: JQuery,
|
2021-03-24 21:44:43 +01:00
|
|
|
{
|
2021-04-13 05:18:25 +02:00
|
|
|
success_msg_html = strings.success_html,
|
2021-04-20 16:41:39 +02:00
|
|
|
failure_msg_html = strings.failure_html,
|
2021-03-24 21:44:43 +01:00
|
|
|
success_continuation,
|
|
|
|
error_continuation,
|
|
|
|
sticky = false,
|
2022-01-25 11:36:19 +01:00
|
|
|
$error_msg_element,
|
2023-08-04 09:58:55 +02:00
|
|
|
}: RequestOpts = {},
|
|
|
|
): void {
|
2023-10-05 08:17:51 +02:00
|
|
|
const $spinner = $status_element.expectOne();
|
2022-01-25 11:36:19 +01:00
|
|
|
$spinner.fadeTo(0, 1);
|
|
|
|
loading.make_indicator($spinner, {text: strings.saving});
|
2021-09-14 20:58:05 +02:00
|
|
|
const remove_after = sticky ? undefined : 1000;
|
2019-11-02 00:06:25 +01:00
|
|
|
const appear_after = 500;
|
2018-03-30 02:31:11 +02:00
|
|
|
|
2023-08-16 12:23:31 +02:00
|
|
|
void request_method({
|
2020-07-20 22:18:43 +02:00
|
|
|
url,
|
|
|
|
data,
|
2023-01-02 20:50:23 +01:00
|
|
|
success(response_data) {
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.success(success_msg_html, $spinner, remove_after);
|
|
|
|
display_checkmark($spinner);
|
2018-03-30 02:31:11 +02:00
|
|
|
}, appear_after);
|
2018-03-23 15:23:31 +01:00
|
|
|
if (success_continuation !== undefined) {
|
2023-01-02 20:50:23 +01:00
|
|
|
success_continuation(response_data);
|
2018-03-23 15:23:31 +01:00
|
|
|
}
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($error_msg_element) {
|
|
|
|
loading.destroy_indicator($spinner);
|
|
|
|
ui_report.error(failure_msg_html, xhr, $error_msg_element);
|
2019-07-10 19:17:26 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.error(failure_msg_html, xhr, $spinner);
|
2019-07-10 19:17:26 +02:00
|
|
|
}
|
2018-03-23 15:23:31 +01:00
|
|
|
if (error_continuation !== undefined) {
|
|
|
|
error_continuation(xhr);
|
|
|
|
}
|
|
|
|
},
|
2018-03-05 03:23:38 +01:00
|
|
|
});
|
2021-02-10 17:09:37 +01:00
|
|
|
}
|
2018-03-05 03:23:38 +01:00
|
|
|
|
2018-03-03 05:44:58 +01:00
|
|
|
// This function is used to disable sub-setting when main setting is checked or unchecked
|
2022-02-08 00:13:33 +01:00
|
|
|
// or two settings are inter-dependent on their values.
|
2018-03-03 05:44:58 +01:00
|
|
|
// * is_checked is boolean, shows if the main setting is checked or not.
|
|
|
|
// * sub_setting_id is sub setting or setting which depend on main setting,
|
|
|
|
// string id of setting.
|
|
|
|
// * disable_on_uncheck is boolean, true if sub setting should be disabled
|
|
|
|
// when main setting unchecked.
|
2021-07-18 19:13:27 +02:00
|
|
|
export function disable_sub_setting_onchange(
|
2023-08-04 09:58:55 +02:00
|
|
|
is_checked: boolean,
|
|
|
|
sub_setting_id: string,
|
|
|
|
disable_on_uncheck: boolean,
|
|
|
|
include_label: boolean,
|
|
|
|
): void {
|
2020-07-15 00:34:28 +02:00
|
|
|
if ((is_checked && disable_on_uncheck) || (!is_checked && !disable_on_uncheck)) {
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#${CSS.escape(sub_setting_id)}`).prop("disabled", false);
|
2021-07-18 19:13:27 +02:00
|
|
|
if (include_label) {
|
|
|
|
$(`#${CSS.escape(sub_setting_id)}_label`)
|
|
|
|
.parent()
|
|
|
|
.removeClass("control-label-disabled");
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
} else if ((is_checked && !disable_on_uncheck) || (!is_checked && disable_on_uncheck)) {
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#${CSS.escape(sub_setting_id)}`).prop("disabled", true);
|
2021-07-18 19:13:27 +02:00
|
|
|
if (include_label) {
|
|
|
|
$(`#${CSS.escape(sub_setting_id)}_label`)
|
|
|
|
.parent()
|
|
|
|
.addClass("control-label-disabled");
|
|
|
|
}
|
2018-03-03 05:44:58 +01:00
|
|
|
}
|
2021-02-10 17:09:37 +01:00
|
|
|
}
|