lmc/volumes/add/add.js

210 lines
5.9 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, apiPost, countryContinents, parseParams, regionNames, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.addButton = "add-button";
elements.attachment = "attachment";
elements.dcOther = "dc-other";
elements.label = "label";
elements.location = "location";
elements.size = "size";
// Data recieved from API calls
var data = {};
data.linodes = [];
data.regions = [];
// Static references to UI elements
var ui = {};
ui.addButton = {};
ui.attachment = {};
ui.dcOther = {};
ui.label = {};
ui.location = {};
ui.size = {};
// Callback for linodes API call
var displayLinodes = function(response)
{
// Add linodes to array
data.linodes = data.linodes.concat(response.data);
// Request the next page if there are more pages
var filter = {
"+or": []
};
for (var i = 0; i < data.regions.length; i++)
filter["+or"].push({"region": data.regions[i].id});
if (response.page != response.pages) {
apiGet("/linode/instances?page=" + (response.page + 1), displayLinodes, filter);
return;
}
// Add linodes to selector
for (var i = 0; i < data.linodes.length; i++) {
var linode = document.createElement("option");
linode.value = data.linodes[i].id;
linode.innerHTML = data.linodes[i].label;
ui.attachment.appendChild(linode);
if (data.params.lid && parseInt(data.params.lid) == data.linodes[i].id) {
ui.attachment.value = data.linodes[i].id;
ui.location.value = data.linodes[i].region;
}
}
ui.addButton.disabled = false;
};
// Callback for regions API call
var displayRegions = function(response)
{
// Add regions that support block storage to array
for (var i = 0; i < response.data.length; i++) {
if (response.data[i].capabilities.includes("Block Storage"))
data.regions.push(response.data[i]);
}
// Request the next page if there are more pages
if (response.page != response.pages) {
apiGet("/regions?page=" + (response.page + 1), displayRegions, null);
return;
}
// Add regions to selector
for (var i = 0; i < data.regions.length; i++) {
var loc = document.createElement("option");
loc.value = data.regions[i].id;
if (data.regions[i].label && data.regions[i].label.length)
loc.innerHTML = data.regions[i].label;
else if (regionNames[data.regions[i].id])
loc.innerHTML = regionNames[data.regions[i].id];
else
loc.innerHTML = data.regions[i].id;
var optgroup = null;
if (countryContinents[data.regions[i].country])
optgroup = document.getElementById(countryContinents[data.regions[i].country]);
if (!optgroup)
optgroup = ui.dcOther;
optgroup.style.display = "initial";
optgroup.appendChild(loc);
}
// Request linodes in all the supported regions
var filter = {
"+or": []
};
for (var i = 0; i < data.regions.length; i++)
filter["+or"].push({"region": data.regions[i].id});
apiGet("/linode/instances", displayLinodes, filter);
};
// Click handler for add button
var handleAdd = function(event)
{
if (event.currentTarget.disabled)
return;
if (ui.location.value == "0") {
alert("You must select a location.");
return;
}
var req = {
"label": ui.label.value,
"size": parseInt(ui.size.value),
"region": ui.location.value
};
if (ui.attachment.value != "0")
req.linode_id = parseInt(ui.attachment.value);
apiPost("/volumes", req, function(response)
{
location.href = "/volumes/settings?vid=" + response.id;
});
};
// Linode select handler
var handleLinode = function(event)
{
// Set the location to the selected linode's region
var linode = null;
var lid = parseInt(ui.attachment.value);
for (var i = 0; i < data.linodes.length; i++) {
if (data.linodes[i].id == lid) {
linode = data.linodes[i];
break;
}
}
if (!linode)
return;
ui.location.value = linode.region;
};
// Location select handler
var handleLocation = function(event)
{
// Clear the linode selector
for (var i = ui.attachment.children.length - 1; i >= 1; i--)
ui.attachment.children[i].remove();
// Insert linodes in the same region
for (var i = 0; i < data.linodes.length; i++) {
if (data.linodes[i].region != ui.location.value)
continue;
var linode = document.createElement("option");
linode.value = data.linodes[i].id;
linode.innerHTML = data.linodes[i].label;
ui.attachment.appendChild(linode);
}
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.addButton = document.getElementById(elements.addButton);
ui.attachment = document.getElementById(elements.attachment);
ui.dcOther = document.getElementById(elements.dcOther);
ui.label = document.getElementById(elements.label);
ui.location = document.getElementById(elements.location);
ui.size = document.getElementById(elements.size);
// Attach event handlers
ui.addButton.addEventListener("click", handleAdd);
ui.attachment.addEventListener("input", handleLinode);
ui.location.addEventListener("input", handleLocation);
// Get data from API
apiGet("/regions", displayRegions, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();