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-03-25 21:38:40 +01:00
|
|
|
import {i18n} 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-03-24 21:44:43 +01:00
|
|
|
export function message(response, status_box, cls = "alert", remove_after = false) {
|
2017-11-03 21:55:19 +01:00
|
|
|
// Note we use html() below, since we can rely on our callers escaping HTML
|
|
|
|
// via i18n.t when interpolating data.
|
2020-07-15 00:34:28 +02:00
|
|
|
status_box
|
|
|
|
.removeClass(common.status_classes)
|
|
|
|
.addClass(cls)
|
|
|
|
.html(response)
|
|
|
|
.stop(true)
|
|
|
|
.fadeTo(0, 1);
|
2018-03-30 02:22:33 +02:00
|
|
|
if (remove_after) {
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2020-07-10 16:43:51 +02:00
|
|
|
status_box.fadeOut(400);
|
2018-03-30 02:22:33 +02:00
|
|
|
}, remove_after);
|
|
|
|
}
|
2017-03-23 20:37:08 +01:00
|
|
|
status_box.addClass("show");
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2017-04-03 16:13:25 +02:00
|
|
|
|
2021-02-28 00:58:55 +01:00
|
|
|
export function error(response, xhr, status_box, remove_after) {
|
2017-04-03 16:13:25 +02:00
|
|
|
if (xhr && xhr.status.toString().charAt(0) === "4") {
|
|
|
|
// Only display the error response for 4XX, where we've crafted
|
|
|
|
// a nice response.
|
2019-12-13 01:41:59 +01:00
|
|
|
const server_response = _.escape(JSON.parse(xhr.responseText).msg);
|
2018-12-18 21:33:29 +01:00
|
|
|
if (response) {
|
|
|
|
response += ": " + server_response;
|
|
|
|
} else {
|
|
|
|
response = server_response;
|
|
|
|
}
|
2017-04-03 16:13:25 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:58:55 +01:00
|
|
|
message(response, status_box, "alert-error", remove_after);
|
|
|
|
}
|
2021-01-26 10:04:26 +01:00
|
|
|
|
2021-02-28 00:58:55 +01:00
|
|
|
export function client_error(response, status_box, remove_after) {
|
|
|
|
message(response, status_box, "alert-error", remove_after);
|
|
|
|
}
|
2017-04-03 16:13:25 +02:00
|
|
|
|
2021-02-28 00:58:55 +01:00
|
|
|
export function success(response, status_box, remove_after) {
|
|
|
|
message(response, status_box, "alert-success", remove_after);
|
|
|
|
}
|
2017-04-03 16:13:25 +02:00
|
|
|
|
2021-02-28 00:58:55 +01:00
|
|
|
export function generic_embed_error(error) {
|
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(
|
|
|
|
$alert.html($exit + "<div class='content'>" + error + "</div>").addClass("show"),
|
|
|
|
);
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2017-09-16 01:16:42 +02:00
|
|
|
|
2021-02-28 00:58:55 +01:00
|
|
|
export function generic_row_button_error(xhr, btn) {
|
2018-03-25 11:12:06 +02:00
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
|
|
btn.closest("td").html(
|
2020-07-02 02:16:03 +02:00
|
|
|
$("<p>").addClass("text-error").text(JSON.parse(xhr.responseText).msg),
|
2018-03-25 11:12:06 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
btn.text(i18n.t("Failed!"));
|
|
|
|
}
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|
2018-03-25 11:12:06 +02:00
|
|
|
|
2021-02-28 00:58:55 +01:00
|
|
|
export function hide_error($target) {
|
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-02-28 00:58:55 +01:00
|
|
|
export function show_error($target) {
|
2017-03-23 20:37:08 +01:00
|
|
|
$target.addClass("show");
|
2021-02-28 00:58:55 +01:00
|
|
|
}
|