2017-04-03 16:13:25 +02:00
|
|
|
var ui_report = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
/* 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
|
|
|
type- used to define more complex logic for special cases
|
|
|
|
*/
|
2017-04-03 16:13:25 +02:00
|
|
|
|
2018-03-30 02:22:33 +02:00
|
|
|
exports.message = function (response, status_box, cls, type, remove_after) {
|
2017-04-03 16:13:25 +02:00
|
|
|
if (cls === undefined) {
|
|
|
|
cls = 'alert';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === undefined) {
|
|
|
|
type = ' ';
|
|
|
|
}
|
|
|
|
|
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.
|
2018-01-03 14:24:49 +01:00
|
|
|
status_box.removeClass(common.status_classes).addClass(cls)
|
2018-05-06 21:43:17 +02:00
|
|
|
.html(response).stop(true).fadeTo(0, 1);
|
2018-03-30 02:22:33 +02:00
|
|
|
if (remove_after) {
|
|
|
|
setTimeout(function () {
|
|
|
|
status_box.fadeTo(200, 0);
|
|
|
|
}, remove_after);
|
|
|
|
}
|
2017-03-23 20:37:08 +01:00
|
|
|
status_box.addClass("show");
|
2017-04-03 16:13:25 +02:00
|
|
|
};
|
|
|
|
|
2017-11-03 21:55:19 +01:00
|
|
|
function escape(html) {
|
2018-05-06 21:43:17 +02:00
|
|
|
return html
|
|
|
|
.toString()
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''');
|
2017-11-03 21:55:19 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 16:13:25 +02:00
|
|
|
exports.error = function (response, xhr, status_box, type) {
|
|
|
|
if (xhr && xhr.status.toString().charAt(0) === "4") {
|
|
|
|
// Only display the error response for 4XX, where we've crafted
|
|
|
|
// a nice response.
|
2018-02-06 22:41:59 +01:00
|
|
|
response += ": " + escape(JSON.parse(xhr.responseText).msg);
|
2017-04-03 16:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.message(response, status_box, 'alert-error', type);
|
|
|
|
};
|
|
|
|
|
2018-03-30 02:22:33 +02:00
|
|
|
exports.success = function (response, status_box, type, remove_after) {
|
|
|
|
exports.message(response, status_box, 'alert-success', type, remove_after);
|
2017-04-03 16:13:25 +02:00
|
|
|
};
|
|
|
|
|
2017-09-16 01:16:42 +02:00
|
|
|
exports.generic_embed_error = function (error) {
|
|
|
|
var $alert = $("<div class='alert home-error-bar'></div>");
|
|
|
|
var $exit = "<div class='exit'></div>";
|
|
|
|
|
|
|
|
$(".alert-box").append($alert.html($exit + "<div class='content'>" + error + "</div>").addClass("show"));
|
|
|
|
};
|
|
|
|
|
2018-03-25 11:12:06 +02:00
|
|
|
exports.generic_row_button_error = function (xhr, btn) {
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
|
|
btn.closest("td").html(
|
|
|
|
$("<p>").addClass("text-error").text(JSON.parse(xhr.responseText).msg)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
btn.text(i18n.t("Failed!"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-23 20:37:08 +01:00
|
|
|
exports.hide_error = function ($target) {
|
|
|
|
$target.addClass("fade-out");
|
|
|
|
setTimeout(function () {
|
|
|
|
$target.removeClass("show fade-out");
|
|
|
|
}, 300);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.show_error = function ($target) {
|
|
|
|
$target.addClass("show");
|
|
|
|
};
|
|
|
|
|
2017-04-03 16:13:25 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = ui_report;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.ui_report = ui_report;
|