vpnmanager/static/js/devices.js
2019-02-24 11:16:36 +00:00

230 lines
7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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){
document.buildingfw = false;
document.getElementById("download").src=url;
}
else {
document.buildingfw = true;
checkDownload(url);
}
});
}
function timeSince(obj) {
last = new Date(Number(obj));
var seconds = Math.floor((new Date() - last) / 1000);
interval = Math.floor(seconds / 3600);
out = "";
if (interval > 24) return "at " + last.toUTCString().substr(4);
if (interval > 1) out = interval + " hours ";
interval = Math.floor(seconds / 60);
if (seconds < 120) out = seconds + " seconds "
else if (interval > 1 && interval < 120) out = out + interval + " minutes ";
if (out) return out + "ago";
return "";
}
function styleStatus(msg, device) {
device_status = $("#" + device + "-indicator");
device_ip = $("#" + device + "-ip");
device_network = $("#" + device + "-network");
device_name = $("#" + device + "-name");
device_id = $("#" + device + "-id");
device_status.css("color", msg.status == 1 ? "green" : (msg.status == 2 ? "yellow" : (msg.status == 0 ? "red" : "grey")));
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")));
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")
};
if (msg.hasOwnProperty("update")) {
device_id.css("font-weight", msg.update == 1 ? "bold" : "normal")
};
if (msg.hasOwnProperty("name")) {
device_name.text(msg.name);
};
};
function updateStatus(device_id) {
$.getJSON( "/devices/" + device_id + "/ping/", function(json) { styleStatus(json, device_id); });
};
function askdelete(device_id) {
if (confirm("Are you sure you want to delete this Device?")) window.location.href = "/devices/" + device_id + "/delete";
};
function askdeletewifi(wifi_id) {
if (confirm("Are you sure you want to delete this WiFi?")) window.location.href = "/wifi/" + wifi_id + "/delete";
};
function askdeleteuser(user_id) {
if (confirm("Are you sure you want to delete this User?")) window.location.href = "/users/" + user_id + "/delete";
};
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) {
if (confirm("Are you sure you want to reboot this Device?")) window.location.href = "/devices/" + device_id + "/reboot";
};
function downloadnotice(device_id) {
if (!document.buildingfw) {
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/');
}
else {
alert("You are currently building another firmware. Please wait for this to finish and try again.");
};
};
function showdevices() {
$("#devicespart").show();
$("#wifipart").hide();
$("#userpart").hide();
$("#netpart").hide();
$("#orgapart").hide();
$("#linkdevices").css("font-weight", "bold");
$("#linkwifi").css("font-weight", "normal");
$("#linkusers").css("font-weight", "normal");
$("#linknets").css("font-weight", "normal");
$("#linkorgas").css("font-weight", "normal");
window.history.pushState("", "", "/devices/");
};
function showwifi() {
$("#devicespart").hide();
$("#wifipart").show();
$("#userpart").hide();
$("#netpart").hide();
$("#orgapart").hide();
$("#linkdevices").css("font-weight", "normal");
$("#linkwifi").css("font-weight", "bold");
$("#linkusers").css("font-weight", "normal");
$("#linknets").css("font-weight", "normal");
$("#linkorgas").css("font-weight", "normal");
window.history.pushState("", "", "/wifi/");
};
function showusers() {
$("#devicespart").hide();
$("#wifipart").hide();
$("#userpart").show();
$("#netpart").hide();
$("#orgapart").hide();
$("#linkdevices").css("font-weight", "normal");
$("#linkwifi").css("font-weight", "normal");
$("#linkusers").css("font-weight", "bold");
$("#linknets").css("font-weight", "normal");
$("#linkorgas").css("font-weight", "normal");
window.history.pushState("", "", "/users/");
};
function shownets() {
$("#devicespart").hide();
$("#wifipart").hide();
$("#userpart").hide();
$("#netpart").show();
$("#orgapart").hide();
$("#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");
window.history.pushState("", "", "/networks/");
};
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/");
};
function filtertables() {
orga_id = $("#orgaselect option:selected").val();
$("tbody tr").each(function(i, row)
{
if ($(row).attr('class') == "orgarow" || $(row).data('orga') && $.inArray(parseInt(orga_id, "10"), $(row).data('orga')) !== -1) $(row).show();
else $(row).hide();
});
$.ajax({
url: "/setactiveorga/" + orga_id + "/",
dataType: 'text',
type: 'GET'
});
};
var page = document.location.pathname.substring(1, document.location.pathname.lastIndexOf('/'));
if (page == "users") showusers();
else if (page == "wifi") showwifi();
else if (page == "networks") shownets();
else if (page == "organizations") showorgas();
else showdevices();
filtertables();
$("div[id$='-indicator']").each(function() {
device_id = this.id.split("-")[0];
updateStatus(device_id);
setInterval(updateStatus, 10000, device_id);
});
$("#orgaselect").on('change', function() {
filtertables();
});
document.buildingfw = false;