|
|
|
@ -6,7 +6,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
@ -44,10 +44,10 @@ async function HTTPRequest(options) {
|
|
|
|
|
var success = options.success;
|
|
|
|
|
var error = options.error || (() => window.location = "/");
|
|
|
|
|
var invalidToken = options.invalidToken || error;
|
|
|
|
|
var keepAlertVisible = options.keepAlertVisible;
|
|
|
|
|
var alert = options.alert;
|
|
|
|
|
var loader = options.loader;
|
|
|
|
|
var isFormData = options.isFormData;
|
|
|
|
|
var keepAlertVisible = options.keepAlertVisible || false;
|
|
|
|
|
var alert = options.alert || false;
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
async = true;
|
|
|
|
@ -57,7 +57,7 @@ async function HTTPRequest(options) {
|
|
|
|
|
dataContentType = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hideAlert(keepAlertVisible);
|
|
|
|
|
hideAlert(alert);
|
|
|
|
|
if (loader) {
|
|
|
|
|
loader.html(getLoader());
|
|
|
|
|
}
|
|
|
|
@ -90,11 +90,11 @@ async function HTTPRequest(options) {
|
|
|
|
|
invalidToken();
|
|
|
|
|
break;
|
|
|
|
|
case "error":
|
|
|
|
|
showAlert("danger", "Error!", response.errorMessage, keepAlertVisible);
|
|
|
|
|
showAlert("danger", "Error!", response.errorMessage, alert);
|
|
|
|
|
error();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
showAlert("danger", "Invalid Response!", "The server returned an invalid response status: " + response.status, keepAlertVisible);
|
|
|
|
|
showAlert("danger", "Invalid Response!", "The server returned an invalid response status: " + response.status, alert);
|
|
|
|
|
error();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
@ -103,21 +103,21 @@ async function HTTPRequest(options) {
|
|
|
|
|
loader.html("");
|
|
|
|
|
}
|
|
|
|
|
var message = errorThrown === "" ? "Unable to connect to the server. Please try again." : errorThrown;
|
|
|
|
|
showAlert("danger", "Error!", message, keepAlertVisible);
|
|
|
|
|
showAlert("danger", "Error!", message, alert);
|
|
|
|
|
error();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return successFlag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function HTTPGetFileRequest(url, success, error, keepAlertVisible, loader, keepAlertVisible) {
|
|
|
|
|
async function HTTPGetFileRequest(url, success, error, alert, loader, alert) {
|
|
|
|
|
var options = url;
|
|
|
|
|
if (typeof url === "string") {
|
|
|
|
|
options = { url, success, error, keepAlertVisible, loader, keepAlertVisible };
|
|
|
|
|
options = { url, success, error, alert, loader, alert };
|
|
|
|
|
}
|
|
|
|
|
var { url: finalUrl, success, error, keepAlertVisible, loader, keepAlertVisible } = options;
|
|
|
|
|
var { url: finalUrl, success, error, alert, loader, alert } = options;
|
|
|
|
|
|
|
|
|
|
hideAlert(keepAlertVisible);
|
|
|
|
|
hideAlert(alert);
|
|
|
|
|
if (loader) {
|
|
|
|
|
loader.html(getLoader());
|
|
|
|
|
}
|
|
|
|
@ -141,7 +141,7 @@ async function HTTPGetFileRequest(url, success, error, keepAlertVisible, loader,
|
|
|
|
|
var message = err.message === "Failed to fetch"
|
|
|
|
|
? "Unable to connect to the server. Please try again."
|
|
|
|
|
: err.message;
|
|
|
|
|
showAlert("danger", "Error!", message, keepAlertVisible);
|
|
|
|
|
showAlert("danger", "Error!", message, alert);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -149,22 +149,47 @@ function getLoader() {
|
|
|
|
|
return "<div style='width: 64px; height: inherit; margin: auto;'><div style='height: inherit; display: table-cell; vertical-align: middle;'><img class='rotating' src='/images/logo.svg' style='width:64px;'/> Loading...</div></div>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showAlert(type, title, message, keepAlertVisible) {
|
|
|
|
|
var alertHTML = `<div class="alert alert-${type}">
|
|
|
|
|
<button type="button" class="close" data-dismiss="alert">×</button>
|
|
|
|
|
<strong>${title}</strong> ${htmlEncode(message)}
|
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
|
|
keepAlertVisible = keepAlertVisible || $(".keepAlertVisible");
|
|
|
|
|
keepAlertVisible.html(alertHTML);
|
|
|
|
|
function countdown(callback, duration, countdownInterval = 1000, updateInterval = 100) {
|
|
|
|
|
const bar = document.getElementById('alertProgress');
|
|
|
|
|
let startTime = null;
|
|
|
|
|
function update(timeStamp) {
|
|
|
|
|
if (!startTime) {
|
|
|
|
|
startTime = timeStamp;
|
|
|
|
|
}
|
|
|
|
|
const elapsedTime = timeStamp - startTime;
|
|
|
|
|
const progress = Math.floor((elapsedTime / duration) * 100);
|
|
|
|
|
bar.style.width = progress + '%';
|
|
|
|
|
if (progress >= 100) {
|
|
|
|
|
callback && setTimeout(callback, updateInterval);
|
|
|
|
|
} else {
|
|
|
|
|
requestAnimationFrame(update);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
requestAnimationFrame(update);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === "success") {
|
|
|
|
|
setTimeout(() => hideAlert(keepAlertVisible), 5000);
|
|
|
|
|
function showAlert(type, title, message, alert, duration = 5, countdownInterval = 1000, updateInterval = 100) {
|
|
|
|
|
var alertHTML = `<div class="alert alert-${type}">
|
|
|
|
|
<button type="button" class="close" data-bs-dismiss="alert">×</button>
|
|
|
|
|
<strong>${title}</strong> ${htmlEncode(message)}`;
|
|
|
|
|
|
|
|
|
|
alert = alert || $('.EonaCatAlert');
|
|
|
|
|
|
|
|
|
|
if (type === 'success') {
|
|
|
|
|
alertHTML += '<div class="progress" style="heigth:10px">\
|
|
|
|
|
<div id="alertProgress" class="progress-bar progress-bar-striped bg-info" role="progressbar"></div>\
|
|
|
|
|
</div>';
|
|
|
|
|
alertHTML += '</div>';
|
|
|
|
|
alert.html(alertHTML);
|
|
|
|
|
countdown(() => hideAlert(alert), duration * 1000, countdownInterval, updateInterval);
|
|
|
|
|
} else {
|
|
|
|
|
alertHTML += '</div>';
|
|
|
|
|
alert.html(alertHTML);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hideAlert(keepAlertVisible) {
|
|
|
|
|
keepAlertVisible = keepAlertVisible || $(".keepAlertVisible");
|
|
|
|
|
keepAlertVisible.html("");
|
|
|
|
|
function hideAlert(alert) {
|
|
|
|
|
alert = alert || $(".EonaCatAlert");
|
|
|
|
|
alert.html("");
|
|
|
|
|
}
|