lmc/linodes/settings/settings.js

239 lines
7.4 KiB
JavaScript
Raw Permalink Normal View History

/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.alertsSaved = "alerts-saved";
elements.cpuEnabled = "cpu-enabled";
elements.cpuValue = "cpu-value";
elements.incomingEnabled = "incoming-enabled";
elements.incomingValue = "incoming-value";
elements.ioEnabled = "io-enabled";
elements.ioValue = "io-value";
elements.label = "label";
elements.lassie = "lassie";
elements.lassieSaved = "lassie-saved";
elements.linodeLabel = "linode-label";
elements.linodeTag = "linode-tag";
elements.linodeTagLink = "linode-tag-link";
elements.outboundEnabled = "outbound-enabled";
elements.outboundValue = "outbound-value";
elements.saveAlerts = "save-alerts";
elements.saveDisplay = "save-display";
elements.saveLassie = "save-lassie";
elements.tags = "tags";
elements.transferEnabled = "transfer-enabled";
elements.transferValue = "transfer-value";
// Data recieved from API calls
var data = {};
data.linode = {};
// Static references to UI elements
var ui = {};
ui.alertsSaved = {};
ui.cpuEnabled = {};
ui.cpuValue = {};
ui.incomingEnabled = {};
ui.incomingValue = {};
ui.ioEnabled = {};
ui.ioValue = {};
ui.label = {};
ui.lassie = {};
ui.lassieSaved = {};
ui.linodeLabel = {};
ui.linodeTag = {};
ui.linodeTagLink = {};
ui.outboundEnabled = {};
ui.outboundValue = {};
ui.saveAlerts = {};
ui.saveDisplay = {};
ui.saveLassie = {};
ui.tags = {};
ui.transferEnabled = {};
ui.transferValue = {};
// Callback for linode details API call
var displayDetails = function(response)
{
data.linode = response;
// Set page title and header stuff
document.title += " // " + data.linode.label;
ui.linodeLabel.innerHTML = data.linode.label;
if (data.linode.tags.length == 1) {
ui.linodeTagLink.href = "/linodes?tag=" + data.linode.tags[0];
ui.linodeTagLink.innerHTML = "(" + data.linode.tags[0] + ")";
ui.linodeTag.style.display = "inline";
}
// Fill in values
ui.label.value = data.linode.label;
ui.tags.value = data.linode.tags.join(",");
ui.saveDisplay.disabled = false;
if (data.linode.watchdog_enabled)
ui.lassie.value = "enabled";
else
ui.lassie.value = "disabled";
ui.saveLassie.disabled = false;
ui.cpuEnabled.checked = data.linode.alerts.cpu;
ui.cpuValue.value = data.linode.alerts.cpu;
ui.ioEnabled.checked = data.linode.alerts.io;
ui.ioValue.value = data.linode.alerts.io;
ui.incomingEnabled.checked = data.linode.alerts.network_in;
ui.incomingValue.value = data.linode.alerts.network_in;
ui.outboundEnabled.checked = data.linode.alerts.network_out;
ui.outboundValue.value = data.linode.alerts.network_out;
ui.transferEnabled.checked = data.linode.alerts.transfer_quota;
ui.transferValue.value = data.linode.alerts.transfer_quota;
ui.saveAlerts.disabled = false;
};
// Handler for alert settings save
var handleSaveAlerts = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"alerts": {
"cpu": parseInt(ui.cpuValue.value),
"io": parseInt(ui.ioValue.value),
"network_in": parseInt(ui.incomingValue.value),
"network_out": parseInt(ui.outboundValue.value),
"transfer_quota": parseInt(ui.transferValue.value)
}
};
// Set values to 0 if not enabled
if (!ui.cpuEnabled.checked)
req.alerts.cpu = 0;
if (!ui.ioEnabled.checked)
req.alerts.io = 0;
if (!ui.incomingEnabled.checked)
req.alerts.network_in = 0;
if (!ui.outboundEnabled.checked)
req.alerts.network_out = 0;
if (!ui.transferEnabled.checked)
req.alerts.transfer_quota = 0;
var callback = function(response)
{
ui.alertsSaved.style.display = "block";
};
apiPut("/linode/instances/" + data.params.lid, req, callback);
};
// Handler for display settings save
var handleSaveDisplay = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"label": ui.label.value,
"tags": []
};
if (ui.tags.value.length)
req.tags = ui.tags.value.split(",");
var callback = function(response)
{
location.reload();
};
apiPut("/linode/instances/" + data.params.lid, req, callback);
};
// Handler for Lassie settings save
var handleSaveLassie = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"watchdog_enabled": (ui.lassie.value == "enabled")
};
var callback = function(response)
{
ui.lassieSaved.style.display = "block";
};
apiPut("/linode/instances/" + data.params.lid, req, callback);
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need a Linode ID, so die if we don't have it
if (!data.params.lid) {
alert("No Linode ID supplied!");
return;
}
setupHeader();
// Update links on page to include proper Linode ID
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++)
anchors[i].href = anchors[i].href.replace("lid=0", "lid=" + data.params.lid);
// Get element references
ui.alertsSaved = document.getElementById(elements.alertsSaved);
ui.cpuEnabled = document.getElementById(elements.cpuEnabled);
ui.cpuValue = document.getElementById(elements.cpuValue);
ui.incomingEnabled = document.getElementById(elements.incomingEnabled);
ui.incomingValue = document.getElementById(elements.incomingValue);
ui.ioEnabled = document.getElementById(elements.ioEnabled);
ui.ioValue = document.getElementById(elements.ioValue);
ui.label = document.getElementById(elements.label);
ui.lassie = document.getElementById(elements.lassie);
ui.lassieSaved = document.getElementById(elements.lassieSaved);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.linodeTag = document.getElementById(elements.linodeTag);
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
ui.outboundEnabled = document.getElementById(elements.outboundEnabled);
ui.outboundValue = document.getElementById(elements.outboundValue);
ui.saveAlerts = document.getElementById(elements.saveAlerts);
ui.saveDisplay = document.getElementById(elements.saveDisplay);
ui.saveLassie = document.getElementById(elements.saveLassie);
ui.tags = document.getElementById(elements.tags);
ui.transferEnabled = document.getElementById(elements.transferEnabled);
ui.transferValue = document.getElementById(elements.transferValue);
// Attach event handlers
ui.saveAlerts.addEventListener("click", handleSaveAlerts);
ui.saveDisplay.addEventListener("click", handleSaveDisplay);
ui.saveLassie.addEventListener("click", handleSaveLassie);
// Get data from API
apiGet("/linode/instances/" + data.params.lid, displayDetails, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();