lmc/volumes/detach/detach.js

112 lines
3.2 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, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.detachButton = "detach-button";
elements.label = "label";
elements.linodeLabel = "linode-label";
elements.volumeLabel = "volume-label";
// Data recieved from API calls
var data = {};
data.volume = {};
// Static references to UI elements
var ui = {};
ui.detachButton = {};
ui.label = {};
ui.linodeLabel = {};
ui.volumeLabel = {};
// Callback for attached linode API call
var displayLinode = function(response)
{
ui.linodeLabel.innerHTML = response.label;
ui.detachButton.disabled = false;
};
// Callback for volume API call
var displayVolume = function(response)
{
data.volume = response;
// Redirect to settings if unattached
if (!data.volume.linode_id)
location.href = "/volumes/settings?vid=" + data.params.vid;
// Set page title and header stuff
document.title += " // " + data.volume.label;
ui.volumeLabel.innerHTML = data.volume.label;
ui.label.innerHTML = data.volume.label;
ui.linodeLabel.href = "/linodes/dashboard?lid=" + data.volume.linode_id;
// Get linode info
apiGet("/linode/instances/" + data.volume.linode_id, displayLinode, null);
};
// Click handler for detach button
var handleDetach = function(event)
{
if (event.currentTarget.disabled)
return;
apiPost("/volumes/" + data.params.vid + "/detach", {}, function(response)
{
location.href = "/linodes/dashboard?lid=" + data.volume.linode_id;
});
};
// 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.detachButton = document.getElementById(elements.detachButton);
ui.label = document.getElementById(elements.label);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.volumeLabel = document.getElementById(elements.volumeLabel);
// Attach event handlers
ui.detachButton.addEventListener("click", handleDetach);
// Get data from API
apiGet("/volumes/" + data.params.vid, displayVolume, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();