lmc/linodes/disk/disk.js

298 lines
9.0 KiB
JavaScript

/*
* 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, apiPost, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.currentSize = "current-size";
elements.currentSizeRow = "current-size-row";
elements.currentType = "current-type";
elements.duplicateButton = "duplicate-button";
elements.imageButton = "image-button";
elements.label = "label";
elements.linodeLabel = "linode-label";
elements.linodeTag = "linode-tag";
elements.linodeTagLink = "linode-tag-link";
elements.maxSize = "max-size";
elements.resizeAddl = "resize-addl";
elements.saveButton = "save-button";
elements.size = "size";
elements.spaceAllocated = "space-allocated";
elements.spaceFree = "space-free";
elements.spaceTotal = "space-total";
elements.spaceWarning = "space-warning";
elements.subnav = "subnav-link";
elements.subnavActive = "subnav-link-active";
elements.type = "type";
// Data recieved from API calls
var data = {};
data.disks = [];
data.linode = {};
// Static references to UI elements
var ui = {};
ui.currentSize = {};
ui.currentSizeRow = {};
ui.currentType = {};
ui.duplicateButton = {};
ui.imageButton = {};
ui.label = {};
ui.linodeLabel = {};
ui.linodeTag = {};
ui.linodeTagLink = {};
ui.maxSize = {};
ui.resizeAddl = {};
ui.saveButton = {};
ui.size = {};
ui.spaceAllocated = {};
ui.spaceFree = {};
ui.spaceTotal = {};
ui.spaceWarning = {};
ui.type = {};
// Temporary state
var state = {};
state.haveDisks = false;
// 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";
}
if (state.haveDisks)
displayStorage();
};
// Callback for linode disks API call
var displayDisks = function(response)
{
// Add disks to array
data.disks = data.disks.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/linode/instances/" + data.params.lid + "/disks?page=" + (response.page + 1), displayDisks, null);
return;
}
for (var i = 0; i < data.disks.length; i++) {
if (data.disks[i].id == data.params.did) {
data.disk = data.disks[i];
break;
}
}
state.haveDisks = true;
if (data.linode.specs)
displayStorage();
if (!data.disk)
return;
// Update form
ui.label.value = data.disk.label;
ui.currentType.innerHTML = data.disk.filesystem;
ui.currentSize.innerHTML = data.disk.size;
ui.size.value = data.disk.size;
if (data.disk.filesystem != "raw" && data.disk.filesystem != "swap")
ui.imageButton.disabled = false;
};
// Show storage totals
var displayStorage = function()
{
var storageUsed = 0;
for (var i = 0; i < data.disks.length; i++)
storageUsed += data.disks[i].size;
var storageFree = data.linode.specs.disk - storageUsed;
ui.spaceAllocated.innerHTML = storageUsed;
ui.spaceFree.innerHTML = storageFree;
ui.spaceTotal.innerHTML = data.linode.specs.disk;
if (data.params.did == 0) {
ui.size.max = storageFree;
ui.maxSize.innerHTML = storageFree;
if (storageFree <= 0)
ui.spaceWarning.style.display = "block";
else
ui.saveButton.disabled = false;
}
if (!data.disk)
return;
ui.size.max = storageFree + data.disk.size;
ui.maxSize.innerHTML = storageFree + data.disk.size;
ui.saveButton.disabled = false;
if (storageFree >= data.disk.size)
ui.duplicateButton.disabled = false;
};
// Duplicate button handler
var handleDuplicate = function(event)
{
if (event.currentTarget.disabled)
return;
apiPost("/linode/instances/" + data.params.lid + "/disks/" + data.params.did + "/clone", {}, function(response)
{
location.href = "/linodes/dashboard?lid=" + data.params.lid;
});
};
// Image button handler
var handleImage = function(event)
{
if (event.currentTarget.disabled)
return;
location.href = "/images/create?lid=" + data.params.lid + "&did=" + data.params.did;
};
// Save button handler
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var diskSize = parseInt(ui.size.value);
var standardCallback = function(response)
{
location.href = "/linodes/dashboard?lid=" + data.params.lid;
};
if (data.params.did == 0) {
var req = {
"label": ui.label.value,
"size": diskSize,
"filesystem": ui.type.value
};
apiPost("/linode/instances/" + data.params.lid + "/disks", req, standardCallback);
} else {
var labelReq = {
"label": ui.label.value
};
var resizeReq = {
"size": diskSize
};
if (data.disk.size != diskSize) {
if (data.disk.label != ui.label.value) {
apiPut("/linode/instances/" + data.params.lid + "/disks/" + data.params.did, labelReq, function(response)
{
apiPost("/linode/instances/" + data.params.lid + "/disks/" + data.params.did + "/resize", resizeReq, standardCallback);
});
} else {
apiPost("/linode/instances/" + data.params.lid + "/disks/" + data.params.did + "/resize", resizeReq, standardCallback);
}
} else if (data.disk.label != ui.label.value) {
apiPut("/linode/instances/" + data.params.lid + "/disks/" + data.params.did, labelReq, standardCallback);
} else {
location.href = "/linodes/dashboard?lid=" + data.params.lid;
}
}
};
// 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;
}
// We also need a disk ID
if (!data.params.did) {
alert("No disk 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=00", data.params.lid);
anchors[i].href = anchors[i].href.replace("lid=0", "lid=" + data.params.lid);
}
// Highlight the dashboard subnav link
var subnavLinks = document.getElementsByClassName(elements.subnav);
for (var i = 0; i < subnavLinks.length; i++) {
if (subnavLinks[i].pathname == "/linodes/dashboard")
subnavLinks[i].className += " " + elements.subnavActive;
}
// Get element references
ui.currentSize = document.getElementById(elements.currentSize);
ui.currentSizeRow = document.getElementById(elements.currentSizeRow);
ui.currentType = document.getElementById(elements.currentType);
ui.duplicateButton = document.getElementById(elements.duplicateButton);
ui.imageButton = document.getElementById(elements.imageButton);
ui.label = document.getElementById(elements.label);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.linodeTag = document.getElementById(elements.linodeTag);
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
ui.maxSize = document.getElementById(elements.maxSize);
ui.resizeAddl = document.getElementById(elements.resizeAddl);
ui.saveButton = document.getElementById(elements.saveButton);
ui.size = document.getElementById(elements.size);
ui.spaceAllocated = document.getElementById(elements.spaceAllocated);
ui.spaceFree = document.getElementById(elements.spaceFree);
ui.spaceTotal = document.getElementById(elements.spaceTotal);
ui.spaceWarning = document.getElementById(elements.spaceWarning);
ui.type = document.getElementById(elements.type);
if (data.params.did != 0) {
ui.currentSizeRow.style.display = "table-row";
ui.duplicateButton.style.display = "initial";
ui.imageButton.style.display = "initial";
ui.type.style.display = "none";
ui.resizeAddl.style.display = "initial";
}
// Register event handlers
ui.duplicateButton.addEventListener("click", handleDuplicate);
ui.imageButton.addEventListener("click", handleImage);
ui.saveButton.addEventListener("click", handleSave);
// Get data from API
apiGet("/linode/instances/" + data.params.lid, displayDetails, null);
apiGet("/linode/instances/" + data.params.lid + "/disks", displayDisks, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();