diff --git a/master.js b/master.js index 6037a450..e9f18df0 100644 --- a/master.js +++ b/master.js @@ -83,10 +83,15 @@ app.set('views', path.join(__dirname, 'static')); app.set('view engine', 'html'); app.engine('html', ejs.renderFile); -var routes = require("./routes.js"); -routes(app); - +// give ejs access to some interesting information +app.use(function(req, res, next){ + res.locals.res = res; + res.locals.req = req; + res.locals.masterPlugins = masterPlugins; + next(); +}); +require("./routes.js")(app); // Set folder to serve static content from (the website) app.use(express.static('static')); // mod downloads diff --git a/static/head.html b/static/head.html index c503fe6b..943c3324 100644 --- a/static/head.html +++ b/static/head.html @@ -1,9 +1,9 @@ Clusterio - - - - - - - - \ No newline at end of file + + + + + + + + diff --git a/static/main.js b/static/main.js index a1b805f1..0f3d43bd 100644 --- a/static/main.js +++ b/static/main.js @@ -49,6 +49,24 @@ debugTools = { } } +let cachedInstanceNames = {}; +async function getInstanceName(instanceID){ + let instance = cachedInstanceNames[instanceID]; + if(!instance){ + let slaves = await getJSON("/api/slaves"); + try { + for(id in slaves){ + cachedInstanceNames[id] = slaves[id].instanceName; + } + } catch(e){ + console.log("Error while looking for instance name!"); + console.log(e); + } + return cachedInstanceNames[instanceID] || instanceID; + } else { + return cachedInstanceNames[instanceID]; + } +} var g = {} contents = { "iron-plate":100, @@ -65,22 +83,27 @@ function djb2(str){ } // callback(err, json) function getJSON(url, callback) { - var xhr = new XMLHttpRequest(); - xhr.open('GET', url, true); - xhr.responseType = 'json'; - xhr.onload = function() { - var status = xhr.status; - if (status == 200) { - callback(null, xhr.response); - } else { - callback(status); - } - }; - // triggers if connection is refused - xhr.onerror = function(e){ - callback(e); - }; - xhr.send(); + return new Promise((resolve, reject) => { + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, true); + xhr.responseType = 'json'; + xhr.onload = function() { + var status = xhr.status; + if (status == 200) { + if(callback) callback(null, xhr.response); + resolve(xhr.response); + } else { + if(callback) callback(status); + resolve(status); + } + }; + // triggers if connection is refused + xhr.onerror = function(e){ + if(callback) callback(e); + reject(e); + }; + xhr.send(); + }); }; // callback(err, json) function postJSON(url, data, callback) { @@ -89,12 +112,12 @@ function postJSON(url, data, callback) { xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); xhr.responseType = 'json'; xhr.onload = function() { - var status = xhr.status; - if (status == 200) { + var status = xhr.status; + if (status == 200) { callback(null, xhr.response); - } else { + } else { callback(status); - } + } }; // triggers if connection is refused xhr.onerror = function(e){ diff --git a/static/sidebar.html b/static/sidebar.html index b24a4d2e..84f60cde 100644 --- a/static/sidebar.html +++ b/static/sidebar.html @@ -8,10 +8,15 @@ -