vpnmanager/static/js/devices.js

203 lines
6.4 KiB
JavaScript
Raw Normal View History

2019-02-09 16:24:41 +00:00
function urlStatus(url, cb){
$.ajax({
url: url,
dataType: 'text',
type: 'GET',
complete: function(xhr){
if(typeof cb === 'function')
cb.apply(this, [xhr.status]);
}
});
}
function checkDownload(url){
urlStatus(url, function(status) {
if (status === 200){
var working = false;
2019-02-09 16:24:41 +00:00
document.getElementById("download").src=url;
}
else {
var working = true;
2019-02-09 16:24:41 +00:00
checkDownload(url);
}
});
}
2018-11-28 15:38:35 +00:00
function timeSince(obj) {
last = new Date(Number(obj));
2019-01-13 16:28:52 +00:00
var seconds = Math.floor((new Date() - last) / 1000);
interval = Math.floor(seconds / 3600);
out = "";
2018-11-28 15:38:35 +00:00
2019-01-16 12:26:35 +00:00
if (interval > 24) return "at " + last.toUTCString().substr(4);
2019-01-13 16:28:52 +00:00
if (interval > 1) out = interval + " hours ";
2018-11-28 15:38:35 +00:00
2019-01-13 16:28:52 +00:00
interval = Math.floor(seconds / 60);
2018-11-28 15:38:35 +00:00
2019-01-13 16:28:52 +00:00
if (seconds < 120) out = seconds + " seconds "
else if (interval > 1 && interval < 120) out = out + interval + " minutes ";
2018-11-28 15:38:35 +00:00
if (out) return out + "ago";
return "";
2018-11-28 15:38:35 +00:00
}
function styleStatus(msg, device) {
device_status = $("#" + device + "-indicator");
device_ip = $("#" + device + "-ip");
device_network = $("#" + device + "-network");
device_name = $("#" + device + "-name");
device_id = $("#" + device + "-id");
2018-11-28 15:38:35 +00:00
2019-01-13 17:14:15 +00:00
device_status.css("color", msg.status == 1 ? "green" : (msg.status == 2 ? "yellow" : (msg.status == 0 ? "red" : "grey")));
2019-01-13 17:28:59 +00:00
device_status.prop("title", msg.status == 1 ? "Online and in VPN mode" : (msg.status == 2 ? "Online and in local mode" : (msg.status == 0 ? "Offline" : "No information available")));
2018-11-28 15:38:35 +00:00
if (msg.hasOwnProperty("ip")) {
device_ip.text(msg.ip + (msg.status == 1 ? "" :" (" + timeSince(msg.time) + ")"));
};
if (msg.hasOwnProperty("network")) {
device_network.text(msg.network.intip + (msg.network.name ? (" (" + msg.network.name + ")") : ""));
};
if (msg.hasOwnProperty("reboot")) {
device_id.css("font-style", msg.reboot == 1 ? "italic" : "normal")
};
2018-12-26 01:24:36 +00:00
if (msg.hasOwnProperty("update")) {
device_id.css("font-weight", msg.update == 1 ? "bold" : "normal")
};
if (msg.hasOwnProperty("name")) {
device_name.text(msg.name);
};
2018-11-28 15:38:35 +00:00
};
function updateStatus(device_id) {
$.getJSON( "/devices/" + device_id + "/ping/", function(json) { styleStatus(json, device_id); });
};
function askdelete(device_id) {
2019-02-02 12:31:05 +00:00
if (confirm("Are you sure you want to delete this Device?")) window.location.href = "/devices/" + device_id + "/delete";
};
2019-01-06 18:15:13 +00:00
function askdeletewifi(wifi_id) {
if (confirm("Are you sure you want to delete this WiFi?")) window.location.href = "/wifi/" + wifi_id + "/delete";
};
2019-02-02 12:31:05 +00:00
function askdeleteuser(user_id) {
if (confirm("Are you sure you want to delete this User?")) window.location.href = "/users/" + user_id + "/delete";
};
2019-02-02 15:01:18 +00:00
function askdeletenet(network_id) {
if (confirm("Are you sure you want to delete this Network?")) window.location.href = "/networks/" + network_id + "/delete";
};
function askdeleteorga(organization_id) {
if (confirm("Are you sure you want to delete this Organization? Any associated Networks, WiFis, Devices and Users will also be deleted irreversibly!")) window.location.href = "/organizations/" + organization_id + "/delete";
};
function askreboot(device_id) {
2019-02-02 12:31:05 +00:00
if (confirm("Are you sure you want to reboot this Device?")) window.location.href = "/devices/" + device_id + "/reboot";
};
2019-02-09 16:24:41 +00:00
function downloadnotice(device_id) {
if (!working) {
alert("Your file is being prepared. This might take a minute or two, the download will start automatically when the file is ready. You may close this page and click the download button again later to pick the file up we will store it for you.");
checkDownload('/devices/' + device_id + '/download/');
};
};
function showdevices() {
$("#devicespart").show();
$("#wifipart").hide();
2019-01-13 12:55:53 +00:00
$("#userpart").hide();
2019-02-02 13:20:40 +00:00
$("#netpart").hide();
$("#orgapart").hide();
$("#linkdevices").css("font-weight", "bold");
$("#linkwifi").css("font-weight", "normal");
$("#linkusers").css("font-weight", "normal");
2019-02-02 13:20:40 +00:00
$("#linknets").css("font-weight", "normal");
$("#linkorgas").css("font-weight", "normal");
2019-02-02 12:14:12 +00:00
window.history.pushState("", "", "/devices/");
};
function showwifi() {
$("#devicespart").hide();
$("#wifipart").show();
2019-01-13 12:55:53 +00:00
$("#userpart").hide();
2019-02-02 13:20:40 +00:00
$("#netpart").hide();
$("#orgapart").hide();
$("#linkdevices").css("font-weight", "normal");
$("#linkwifi").css("font-weight", "bold");
$("#linkusers").css("font-weight", "normal");
2019-02-02 13:20:40 +00:00
$("#linknets").css("font-weight", "normal");
$("#linkorgas").css("font-weight", "normal");
2019-02-02 12:14:12 +00:00
window.history.pushState("", "", "/wifi/");
2019-01-13 12:55:53 +00:00
};
function showusers() {
2019-01-13 16:28:52 +00:00
$("#devicespart").hide();
$("#wifipart").hide();
$("#userpart").show();
2019-02-02 13:20:40 +00:00
$("#netpart").hide();
$("#orgapart").hide();
$("#linkdevices").css("font-weight", "normal");
$("#linkwifi").css("font-weight", "normal");
$("#linkusers").css("font-weight", "bold");
2019-02-02 13:20:40 +00:00
$("#linknets").css("font-weight", "normal");
$("#linkorgas").css("font-weight", "normal");
2019-02-02 12:14:12 +00:00
window.history.pushState("", "", "/users/");
};
2019-01-13 16:28:52 +00:00
2019-02-02 13:20:40 +00:00
function shownets() {
$("#devicespart").hide();
$("#wifipart").hide();
$("#userpart").hide();
$("#netpart").show();
$("#orgapart").hide();
2019-02-02 13:20:40 +00:00
$("#linkdevices").css("font-weight", "normal");
$("#linkwifi").css("font-weight", "normal");
$("#linkusers").css("font-weight", "normal");
$("#linknets").css("font-weight", "bold");
$("#linkorgas").css("font-weight", "normal");
2019-02-02 13:20:40 +00:00
2019-02-02 13:24:15 +00:00
window.history.pushState("", "", "/networks/");
2019-02-02 13:20:40 +00:00
};
function showorgas() {
$("#devicespart").hide();
$("#wifipart").hide();
$("#userpart").hide();
$("#netpart").hide();
$("#orgapart").show();
$("#linkdevices").css("font-weight", "normal");
$("#linkwifi").css("font-weight", "normal");
$("#linkusers").css("font-weight", "normal");
$("#linknets").css("font-weight", "normal");
$("#linkorgas").css("font-weight", "bold");
window.history.pushState("", "", "/organizations/");
};
var page = document.location.pathname.substring(1, document.location.pathname.lastIndexOf('/'));
if (page == "users") showusers();
else if (page == "wifi") showwifi();
2019-02-02 13:20:40 +00:00
else if (page == "networks") shownets();
else if (page == "organizations") showorgas();
else showdevices();
2019-01-13 16:28:52 +00:00
$("div[id$='-indicator']").each(function() {
device_id = this.id.split("-")[0];
updateStatus(device_id);
setInterval(updateStatus, 10000, device_id);
});