lmc/volumes/settings/settings.js

219 lines
6.1 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, regionNames, apiGet, apiPost, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.attached = "attached";
elements.attachment = "attachment";
elements.label = "label";
elements.linodeLabel = "linode-label";
elements.location = "location";
elements.path = "path";
elements.saveButton = "save-button";
elements.size = "size";
elements.status = "status";
elements.volumeLabel = "volume-label";
// Data recieved from API calls
var data = {};
data.volume = {};
// Static references to UI elements
var ui = {};
ui.attached = {};
ui.attachment = {};
ui.label = {};
ui.linodeLabel = {};
ui.location = {};
ui.path = {};
ui.saveButton = {};
ui.size = {};
ui.status = {};
ui.volumeLabel = {};
// Temporary state
var state = {};
state.redirect = "/volumes";
state.requests = 0;
state.returns = 0;
// Callback for attached linode API call
var displayLinode = function(response)
{
ui.linodeLabel.innerHTML = response.label;
};
// Callback for linodes API call
var displayLinodes = function(response)
{
// Add linodes to selector
for (var i = 0; i < response.data.length; i++) {
var linode = document.createElement("option");
linode.value = response.data[i].id;
linode.innerHTML = response.data[i].label;
ui.attachment.appendChild(linode);
if (data.volume.linode_id && data.volume.linode_id == response.data[i].id)
ui.attachment.value = response.data[i].id;
}
// Request the next page if there are more pages
if (response.page != response.pages) {
var filter = {
"region": data.volume.region
};
apiGet("/linode/instances?page=" + (response.page + 1), displayLinodes, null);
return;
}
};
// Callback for region API call
var displayRegion = function(response)
{
if (response.label && response.label.length)
ui.location.innerHTML = response.label;
};
// Callback for volume API call
var displayVolume = function(response)
{
data.volume = response;
// Set page title and header stuff
document.title += " // " + data.volume.label;
ui.volumeLabel.innerHTML = data.volume.label;
// Fill in details
ui.label.value = data.volume.label;
ui.status.innerHTML = data.volume.status;
ui.size.value = data.volume.size;
ui.size.min = data.volume.size;
if (regionNames[data.volume.region])
ui.location.innerHTML = regionNames[data.volume.region];
else
ui.location.innerHTML = data.volume.region;
ui.path.innerHTML = data.volume.filesystem_path;
if (data.volume.linode_id) {
ui.attachment.style.display = "none";
ui.attached.style.display = "initial";
ui.linodeLabel.href = "/linodes/dashboard?lid=" + data.volume.linode_id;
apiGet("/linode/instances/" + data.volume.linode_id, displayLinode, null);
} else {
// Get linodes in the same DC
var filter = {
"region": data.volume.region
};
apiGet("/linode/instances", displayLinodes, filter);
}
ui.saveButton.disabled = false;
apiGet("/regions/" + data.volume.region, displayRegion, null);
};
// Click handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
state.requests = 0;
state.returns = 0;
// Common callback
var callback = function(response)
{
state.returns++;
if (state.returns >= state.requests)
location.href = state.redirect;
};
// Attach request
if (!data.volume.linode_id && ui.attachment.value != "0") {
state.requests++;
state.redirect = "/linodes/dashboard?lid=" + ui.attachment.value;
var attachReq = {
"linode_id": parseInt(ui.attachment.value)
};
apiPost("/volumes/" + data.params.vid + "/attach", attachReq, callback);
}
// Label change request
if (ui.label.value != data.volume.label) {
state.requests++;
var labelReq = {
"label": ui.label.value
};
apiPut("/volumes/" + data.params.vid, labelReq, callback);
}
// Size change
var size = parseInt(ui.size.value);
if (size > data.volume.size) {
state.requests++;
var sizeReq = {
"size": size
};
apiPost("/volumes/" + data.params.vid + "/resize", sizeReq, callback);
}
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need a volume ID, so die if we don't have it
if (!data.params.vid) {
alert("No volume ID supplied!");
return;
}
setupHeader();
// Update links on page to include proper volume ID
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++)
anchors[i].href = anchors[i].href.replace("vid=0", "vid=" + data.params.vid);
// Get element references
ui.attached = document.getElementById(elements.attached);
ui.attachment = document.getElementById(elements.attachment);
ui.label = document.getElementById(elements.label);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.location = document.getElementById(elements.location);
ui.path = document.getElementById(elements.path);
ui.saveButton = document.getElementById(elements.saveButton);
ui.size = document.getElementById(elements.size);
ui.status = document.getElementById(elements.status);
ui.volumeLabel = document.getElementById(elements.volumeLabel);
// Attach event handlers
ui.saveButton.addEventListener("click", handleSave);
// Get data from API
apiGet("/volumes/" + data.params.vid, displayVolume, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();