2021-07-05 20:56:40 +02:00
|
|
|
/* globals $, get_php_value, infoMessage */
|
2020-11-04 16:41:25 +01:00
|
|
|
/**
|
|
|
|
* -------------------------------------
|
|
|
|
* Connection Check
|
|
|
|
* --------------------------------------
|
|
|
|
*/
|
2023-03-14 13:16:58 +01:00
|
|
|
$(document).ready(function() {
|
|
|
|
checkConnection(get_php_value("check_conexion_interval"));
|
|
|
|
});
|
2020-11-04 16:41:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs connection tests every minutes and add connection listeners
|
|
|
|
* @param {integer} time in minutes
|
|
|
|
*/
|
|
|
|
|
2023-03-14 13:16:58 +01:00
|
|
|
function checkConnection(seconds) {
|
|
|
|
var cicle = seconds * 1000;
|
2020-11-04 16:41:25 +01:00
|
|
|
var checkConnection = setInterval(handleConnection, cicle);
|
|
|
|
|
|
|
|
// Connection listeters.
|
|
|
|
window.addEventListener("online", handleConnection);
|
|
|
|
window.addEventListener("offline", handleConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle connection status test.
|
|
|
|
*
|
|
|
|
* Test conectivity with server and shows modal message.
|
|
|
|
*/
|
|
|
|
function handleConnection() {
|
|
|
|
var connected;
|
|
|
|
var msg = "online";
|
|
|
|
|
2020-12-03 12:27:55 +01:00
|
|
|
var homeurl = getServerUrl();
|
|
|
|
if (homeurl == null || homeurl == "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-08 08:20:32 +01:00
|
|
|
$.ajax({
|
|
|
|
url: homeurl + "include/connection_check.php",
|
|
|
|
type: "post",
|
|
|
|
dataType: "json"
|
|
|
|
})
|
|
|
|
.done(function(response) {
|
|
|
|
connected = true;
|
|
|
|
showConnectionMessage(connected, msg);
|
2020-12-03 12:27:55 +01:00
|
|
|
})
|
2022-11-08 08:20:32 +01:00
|
|
|
.fail(function(err) {
|
|
|
|
// If test connection file is not found, do not show message.
|
|
|
|
if (err.status != 404) {
|
|
|
|
connected = false;
|
2023-03-14 13:16:58 +01:00
|
|
|
msg = err.statusText;
|
2022-11-08 08:20:32 +01:00
|
|
|
} else {
|
2020-12-03 12:27:55 +01:00
|
|
|
connected = true;
|
2022-11-08 08:20:32 +01:00
|
|
|
}
|
2020-12-03 12:27:55 +01:00
|
|
|
|
2022-11-08 08:20:32 +01:00
|
|
|
showConnectionMessage(connected, msg);
|
|
|
|
});
|
2020-11-04 16:41:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets server origin url
|
|
|
|
*/
|
|
|
|
function getServerUrl() {
|
|
|
|
var server_url;
|
|
|
|
|
2020-12-03 12:27:55 +01:00
|
|
|
try {
|
|
|
|
server_url = get_php_value("absolute_homeurl");
|
|
|
|
} catch (error) {
|
|
|
|
return "";
|
|
|
|
}
|
2020-11-04 16:41:25 +01:00
|
|
|
|
|
|
|
return server_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows or hide connection infoMessage.
|
|
|
|
*
|
|
|
|
* @param {bool} conn
|
|
|
|
* @param {string} msg
|
|
|
|
*/
|
|
|
|
function showConnectionMessage(conn = true, msg = "") {
|
|
|
|
var data = {};
|
2020-12-03 12:27:55 +01:00
|
|
|
if (conn && closed == false) {
|
2020-11-04 16:41:25 +01:00
|
|
|
$("div#message_dialog_connection")
|
|
|
|
.closest(".ui-dialog-content")
|
|
|
|
.dialog("close");
|
|
|
|
} else {
|
|
|
|
data.title = "Connection with server has been lost";
|
|
|
|
data.text = "Connection status: " + msg;
|
|
|
|
|
|
|
|
infoMessage(data, "message_dialog_connection");
|
|
|
|
}
|
|
|
|
}
|