2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-02-28 00:58:55 +01:00
|
|
|
import _ from "lodash";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:58:55 +01:00
|
|
|
import * as common from "./common";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-02-28 00:35:35 +01:00
|
|
|
|
2017-04-03 16:13:25 +02:00
|
|
|
/* Arguments used in the report_* functions are,
|
|
|
|
response- response that we want to display
|
|
|
|
status_box- element being used to display the response
|
|
|
|
cls- class that we want to add/remove to/from the status_box
|
2018-01-03 14:24:49 +01:00
|
|
|
*/
|
2017-04-03 16:13:25 +02:00
|
|
|
|
2021-06-04 21:44:07 +02:00
|
|
|
export function message(
|
|
|
|
response_html: string,
|
2022-01-25 11:36:19 +01:00
|
|
|
$status_box: JQuery,
|
2021-06-04 21:44:07 +02:00
|
|
|
cls = "alert",
|
|
|
|
remove_after?: number,
|
|
|
|
): void {
|
2017-11-03 21:55:19 +01:00
|
|
|
// Note we use html() below, since we can rely on our callers escaping HTML
|
2021-04-13 05:18:25 +02:00
|
|
|
// via $t_html when interpolating data.
|
2022-01-25 11:36:19 +01:00
|
|
|
$status_box
|
2020-07-15 00:34:28 +02:00
|
|
|
.removeClass(common.status_classes)
|
|
|
|
.addClass(cls)
|
2021-04-13 05:18:25 +02:00
|
|
|
.html(response_html)
|
2020-07-15 00:34:28 +02:00
|
|
|
.stop(true)
|
|
|
|
.fadeTo(0, 1);
|
2021-06-11 20:01:13 +02:00
|
|
|
if (remove_after !== undefined) {
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2022-01-25 11:36:19 +01:00
|
|
|
$status_box.fadeOut(400);
|
2018-03-30 02:22:33 +02:00
|
|
|
}, remove_after);
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
$status_box.addClass("show");
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2017-04-03 16:13:25 +02:00
|
|
|
|
2021-06-04 21:44:07 +02:00
|
|
|
export function error(
|
|
|
|
response_html: string,
|
|
|
|
xhr: JQuery.jqXHR | undefined,
|
|
|
|
status_box: JQuery,
|
|
|
|
remove_after?: number,
|
|
|
|
): void {
|
2021-06-10 19:56:57 +02:00
|
|
|
if (xhr && xhr.status >= 400 && xhr.status < 500) {
|
2017-04-03 16:13:25 +02:00
|
|
|
// Only display the error response for 4XX, where we've crafted
|
|
|
|
// a nice response.
|
2021-04-13 05:18:25 +02:00
|
|
|
const server_response_html = _.escape(JSON.parse(xhr.responseText).msg);
|
|
|
|
if (response_html) {
|
|
|
|
response_html += ": " + server_response_html;
|
2018-12-18 21:33:29 +01:00
|
|
|
} else {
|
2021-04-13 05:18:25 +02:00
|
|
|
response_html = server_response_html;
|
2018-12-18 21:33:29 +01:00
|
|
|
}
|
2017-04-03 16:13:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 05:18:25 +02:00
|
|
|
message(response_html, status_box, "alert-error", remove_after);
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2021-01-26 10:04:26 +01:00
|
|
|
|
2021-06-04 21:44:07 +02:00
|
|
|
export function client_error(
|
|
|
|
response_html: string,
|
|
|
|
status_box: JQuery,
|
|
|
|
remove_after?: number,
|
|
|
|
): void {
|
2021-04-13 05:18:25 +02:00
|
|
|
message(response_html, status_box, "alert-error", remove_after);
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2017-04-03 16:13:25 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function success(response_html: string, $status_box: JQuery, remove_after?: number): void {
|
|
|
|
message(response_html, $status_box, "alert-success", remove_after);
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2017-04-03 16:13:25 +02:00
|
|
|
|
2021-06-04 21:44:07 +02:00
|
|
|
export function generic_embed_error(error_html: string): void {
|
2019-11-02 00:06:25 +01:00
|
|
|
const $alert = $("<div class='alert home-error-bar'></div>");
|
|
|
|
const $exit = "<div class='exit'></div>";
|
2017-09-16 01:16:42 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$(".alert-box").append(
|
2021-04-13 05:18:25 +02:00
|
|
|
$alert.html($exit + "<div class='content'>" + error_html + "</div>").addClass("show"),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2017-09-16 01:16:42 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function generic_row_button_error(xhr: JQuery.jqXHR, $btn: JQuery): void {
|
2021-06-10 19:56:57 +02:00
|
|
|
if (xhr.status >= 400 && xhr.status < 500) {
|
2021-06-15 19:55:41 +02:00
|
|
|
const $error = $("<p>").addClass("text-error").text(JSON.parse(xhr.responseText).msg);
|
2022-01-25 11:36:19 +01:00
|
|
|
$btn.closest("td").empty().append($error);
|
2018-03-25 11:12:06 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$btn.text($t({defaultMessage: "Failed!"}));
|
2018-03-25 11:12:06 +02:00
|
|
|
}
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2018-03-25 11:12:06 +02:00
|
|
|
|
2021-06-04 21:44:07 +02:00
|
|
|
export function hide_error($target: JQuery): void {
|
2017-03-23 20:37:08 +01:00
|
|
|
$target.addClass("fade-out");
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2017-03-23 20:37:08 +01:00
|
|
|
$target.removeClass("show fade-out");
|
|
|
|
}, 300);
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2017-03-23 20:37:08 +01:00
|
|
|
|
2021-06-04 21:44:07 +02:00
|
|
|
export function show_error($target: JQuery): void {
|
2017-03-23 20:37:08 +01:00
|
|
|
$target.addClass("show");
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|