lmc/nodebalancers/add/add.js

148 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-08-08 19:46:58 -04:00
/*
* 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.datacenters = "datacenters";
elements.dcOther = "dc-other";
elements.hourly = "hourly";
elements.monthly = "monthly";
// Data recieved from API calls
var data = {};
data.linodes = [];
data.regions = [];
// Static references to UI elements
var ui = {};
ui.addButton = {};
ui.datacenters = {};
ui.dcOther = {};
ui.hourly = {};
ui.monthly = {};
// 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("NodeBalancers"))
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++) {
// Regional pricing
if (data.regions[i].id == "id-cgk") {
data.regions[i].hourly = "$0.018/hr";
data.regions[i].monthly = "$12.00/mo";
} else if (data.regions[i].id == "br-gru") {
data.regions[i].hourly = "$0.021/hr";
data.regions[i].monthly = "$14.00/mo";
} else {
data.regions[i].hourly = "$0.015/hr";
data.regions[i].monthly = "$10.00/mo";
}
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);
}
ui.addButton.disabled = false;
};
// Click handler for add button
var handleAdd = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"region": ui.datacenters.value
};
if (data.params.tag)
req.tags = [data.params.tag];
apiPost("/nodebalancers", req, function(response)
{
location.href = "/nodebalancers/balancer?nbid=" + response.id;
});
};
// Location select handler
var handleLocation = function(event)
{
// Update prices based on location
for (var i = 0; i < data.regions.length; i++) {
if (data.regions[i].id == ui.datacenters.value) {
ui.hourly.innerHTML = data.regions[i].hourly;
ui.monthly.innerHTML = data.regions[i].monthly;
break;
}
}
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.addButton = document.getElementById(elements.addButton);
ui.datacenters = document.getElementById(elements.datacenters);
ui.dcOther = document.getElementById(elements.dcOther);
ui.hourly = document.getElementById(elements.hourly);
ui.monthly = document.getElementById(elements.monthly);
// Attach event handlers
ui.addButton.addEventListener("click", handleAdd);
ui.datacenters.addEventListener("input", handleLocation);
// Get data from API
apiGet("/regions", displayRegions, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();