/*! @file @id $Id$ This is the main application as it is fully run in the user's browser. */ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 var socket = io.connect(); var focused = null; function DockerContainers() { var Status = Object.freeze({ Error: {color: "red", action1: "start", action2: "remove", bash: false}, Terminated: {color: "yellow", action1: "start", action2: "remove", bash: false}, Restarting: {color: "lightblue", action1: "start", action2: "remove", bash: false}, Paused: {color: "lightgrey", action1: "unpause", action2: null, bash: false}, Running: {color: "lightgreen", action1: "pause", action2: "stop", bash: true} }); var containers = []; var nodes = []; function protocol(port) { if (port.toString().match("443")) return "https://"; if (port.toString().match("3304")) return "mysql://"; if (port.toString().match("22")) return "ssh://"; return "http://"; } this.exists = function(name) { if (nodes[name]) return true; return false; } this.contextmenu = function(selector) { $('a[xlink\\:href^=#]').click(function(e) { name = $(this).attr("xlink:href").replace(/^#/, ""); var n = nodes[name]; $(selector).prepend('
') $("#popup").empty(); if (n.status.action1) { $("#popup").append(''); $("#popup1").click(function() { emit(n.status.action1, name); }); } $("#popup").append(''); $("#popup2").click(function() { if (focused) overview(); else details(name); }); if (n.status.action2) { $("#popup").append(''); $("#popup3").click(function() { emit(n.status.action2, name); }); } $("#popup").append('"+a.project+"-"+a.version+"
"+ ""+a.docker+"
"+ ""+a.readme+""); } catch(e) { status("
"+res+""); error("Exception Caught: "+e); } }}).fail(function() { error("offline"); }); } var zoomlevel = 0; function zoom(incr = 0) { zoomlevel = (zoomlevel+incr)%2; switch (zoomlevel) { case 0: { $("#main svg").css("width", "auto"); $("#main svg").css("height", "auto"); $("#main svg").css("max-width", "100%"); $("#main svg").css("max-height", "100%"); } break; case 1: { $("#main svg").css("width", "100%"); $("#main svg").css("height", "auto"); $("#main svg").css("max-width", "100%"); $("#main svg").css("max-height", "none"); } break; case 2: { $("#main.svg").css("width", "auto"); $("#main.svg").css("height", "100%"); $("#main.svg").css("max-width", "none"); $("#main.svg").css("max-height", "100%"); } break; } } var viz = null; var vizmore = null; var rankdir = "LR"; function rotateviz() { if (!viz) return; if (rankdir == "LR") rankdir = "TB"; else rankdir = "LR"; showviz(); } function showviz(vizpath, more) { $("#imagetools").show(); if (!vizpath) { vizpath = viz; more = vizmore; } else { viz = vizpath; vizmore = more; } res = "digraph {\n"+" rankdir="+rankdir+";\n"+viz+"\n}"; try { status(more?Viz(res)+more:Viz(res)); } catch(e) { (res = res.split("\n")).forEach(function(v, i, a) { a[i] = ("000"+(i+1)).slice(-3)+": "+v; }); status("
"+e+"
"+res.join("\n")+"");
}
}
function details(name) {
if (name) focused = name;
else if (!focused) return overview();
showviz(dc.subgraph(focused));
}
function containers(c) {
console.log("->rcv containers");
dc.setContainers(c);
if (focused && dc.exists(focused))
details(focused);
else
overview();
}
function logs(data) {
console.log("->rcv logs("+data.name+")");
$("#main").hide();
$("#console").hide();
$("#logs").show();
if (data.type=='done') {
$("#logs").append('\nDONE');
} else {
$("#logs").append(''+htmlenc(data.text)+'');
}
}
function strInsert(str, pos, txt) {
return str.slice(0, pos)+txt+str.slice(pos);
}
function ansifilter(data) {
console.log("ansifilter");
var res = data;
var pos = -1;
var spans = 0;
while ((pos=res.indexOf("\x1B[")) >= 0) {
var end = res.indexOf("m", pos+2);
if (end>0) {
var control= res.slice(pos+2, end);
res = res.slice(0, pos)+res.slice(end+1);
control.split(';').forEach(function(c) {
switch (parseInt(c)) {
// set
case 1: res = strInsert(res, pos, ''); ++spans; break;
case 2: res = strInsert(res, pos, ''); ++spans; break;
case 4: res = strInsert(res, pos, ''); ++spans; break;
case 5: res = strInsert(res, pos, ''); ++spans; break;
case 7: res = strInsert(res, pos, ''); ++spans; break;
case 8: res = strInsert(res, pos, ''); ++spans; break;
// reset
case 0: for (;spans;--spans) res = strInsert(res, pos, ""); break;
case 21: if (spans) --spans; res = strInsert(res, pos, ""); break;
case 22: if (spans) --spans; res = strInsert(res, pos, ""); break;
case 23: if (spans) --spans; res = strInsert(res, pos, ""); break;
case 25: if (spans) --spans; res = strInsert(res, pos, ""); break;
case 27: if (spans) --spans; res = strInsert(res, pos, ""); break;
case 28: if (spans) --spans; res = strInsert(res, pos, ""); break;
// fg colors
case 39: res = strInsert(res, pos, ''); ++spans; break;
case 30: res = strInsert(res, pos, ''); ++spans; break;
case 31: res = strInsert(res, pos, ''); ++spans; break;
case 32: res = strInsert(res, pos, ''); ++spans; break;
case 33: res = strInsert(res, pos, ''); ++spans; break;
case 34: res = strInsert(res, pos, ''); ++spans; break;
case 35: res = strInsert(res, pos, ''); ++spans; break;
case 36: res = strInsert(res, pos, ''); ++spans; break;
case 37: res = strInsert(res, pos, ''); ++spans; break;
case 90: res = strInsert(res, pos, ''); ++spans; break;
case 91: res = strInsert(res, pos, ''); ++spans; break;
case 92: res = strInsert(res, pos, ''); ++spans; break;
case 93: res = strInsert(res, pos, ''); ++spans; break;
case 94: res = strInsert(res, pos, ''); ++spans; break;
case 95: res = strInsert(res, pos, ''); ++spans; break;
case 96: res = strInsert(res, pos, ''); ++spans; break;
case 97: res = strInsert(res, pos, ''); ++spans; break;
// bg colors
case 49: res = strInsert(res, pos, ''); ++spans; break;
case 40: res = strInsert(res, pos, ''); ++spans; break;
case 41: res = strInsert(res, pos, ''); ++spans; break;
case 42: res = strInsert(res, pos, ''); ++spans; break;
case 43: res = strInsert(res, pos, ''); ++spans; break;
case 44: res = strInsert(res, pos, ''); ++spans; break;
case 45: res = strInsert(res, pos, ''); ++spans; break;
case 46: res = strInsert(res, pos, ''); ++spans; break;
case 47: res = strInsert(res, pos, ''); ++spans; break;
case 100: res = strInsert(res, pos, ''); ++spans; break;
case 101: res = strInsert(res, pos, ''); ++spans; break;
case 102: res = strInsert(res, pos, ''); ++spans; break;
case 103: res = strInsert(res, pos, ''); ++spans; break;
case 104: res = strInsert(res, pos, ''); ++spans; break;
case 105: res = strInsert(res, pos, ''); ++spans; break;
case 106: res = strInsert(res, pos, ''); ++spans; break;
case 107: res = strInsert(res, pos, ''); ++spans; break;
}
});
} else {
break;
}
}
for (;spans;--spans) res += "";
console.log(res);
return res.replace(/\r\r\n/g, '\n');
}
function bash_data(data) {
console.log("->rcv bash-data("+data.name+")", data);
$("#main").hide();
$("#logs").hide();
$("#console").show();
if (data.type=='done') {
$("#screen").append('\nDONE');
} else {
$("#screen").append(''+ansifilter(htmlenc(data.text))+'');
}
}
function overview() {
focused = null;
showviz(dc.graph());
}
/// Initial Function: Startup
/** Decide whether to login or to create a new user */
function start() {
$("#imagetools").hide();
$("#menu").hide();
$("#username").html(window.location.hostname)
try {
status("Starting up ...");
emit("containers");
} catch (m) {
error(m);
}
}
function init() {
socket.io
.on("connect", connected)
.on("reconnect", connected)
.on("disconnect", disconnected)
.on("error", disconnected);
socket
.on("fail", error)
.on("containers", containers)
.on("logs", logs)
.on("bash-data", bash_data);
start();
}
/// On Load, Call @ref start
/*
$(window.onbeforeunload = function() {
return "Are you sure you want to navigate away?";
});
*/
$(init);