lmc/volumes/clone/clone.js

121 lines
3.4 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, parseParams, regionNames, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.cloneButton = "clone-button";
elements.label = "label";
elements.location = "location";
elements.newLabel = "new-label";
elements.size = "size";
elements.volumeLabel = "volume-label";
// Data recieved from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.cloneButton = {};
ui.label = {};
ui.location = {};
ui.newLabel = {};
ui.size = {};
ui.volumeLabel = {};
// 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)
{
// Set page title and header stuff
document.title += " // " + response.label;
ui.volumeLabel.innerHTML = response.label;
// Populate info table
ui.label.innerHTML = response.label;
ui.size.innerHTML = response.size;
if (regionNames[response.region])
ui.location.innerHTML = regionNames[response.region];
else
ui.location.innerHTML = response.region;
ui.cloneButton.disabled = false;
apiGet("/regions/" + response.region, displayRegion, null);
};
// Click handler for clone button
var handleClone = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"label": ui.newLabel.value
};
apiPost("/volumes/" + data.params.vid + "/clone", req, function(response)
{
location.href = "/volumes";
});
};
// 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.cloneButton = document.getElementById(elements.cloneButton);
ui.label = document.getElementById(elements.label);
ui.location = document.getElementById(elements.location);
ui.newLabel = document.getElementById(elements.newLabel);
ui.size = document.getElementById(elements.size);
ui.volumeLabel = document.getElementById(elements.volumeLabel);
// Attach event handlers
ui.cloneButton.addEventListener("click", handleClone);
// Get data from API
apiGet("/volumes/" + data.params.vid, displayVolume, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();