lmc/volumes/attach/attach.js

133 lines
3.7 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.attachButton = "attach-button";
elements.label = "label";
elements.linode = "linode";
elements.volumeLabel = "volume-label";
// Data recieved from API calls
var data = {};
data.volume = {};
// Static references to UI elements
var ui = {};
ui.attachButton = {};
ui.label = {};
ui.linode = {};
ui.volumeLabel = {};
// 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.linode.appendChild(linode);
}
// 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;
}
ui.attachButton.disabled = false;
};
// Callback for volume API call
var displayVolume = function(response)
{
data.volume = response;
// Redirect to settings if already attached
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;
// Get linodes in the same DC
var filter = {
"region": data.volume.region
};
apiGet("/linode/instances", displayLinodes, filter);
};
// Click handler for attach button
var handleAttach = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"linode_id": parseInt(ui.linode.value)
};
apiPost("/volumes/" + data.params.vid + "/attach", req, function(response)
{
location.href = "/linodes/dashboard?lid=" + response.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.attachButton = document.getElementById(elements.attachButton);
ui.label = document.getElementById(elements.label);
ui.linode = document.getElementById(elements.linode);
ui.volumeLabel = document.getElementById(elements.volumeLabel);
// Attach event handlers
ui.attachButton.addEventListener("click", handleAttach);
// Get data from API
apiGet("/volumes/" + data.params.vid, displayVolume, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();