180 lines
4.6 KiB
JavaScript
180 lines
4.6 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.cost = "cost";
|
|
elements.doIt = "doit";
|
|
elements.linodeCount = "linode-count";
|
|
elements.subnav = "subnav-link";
|
|
elements.subnavActive = "subnav-link-active";
|
|
|
|
// Data received from API calls
|
|
var data = {};
|
|
data.linodes = [];
|
|
data.plans = {};
|
|
|
|
// Static references to UI elements
|
|
var ui = {};
|
|
ui.cost = {};
|
|
ui.dotIt = {};
|
|
ui.linodeCount = [];
|
|
|
|
// Temporary state
|
|
var state = {};
|
|
state.haveLinodes = false;
|
|
state.havePlans = false;
|
|
state.numResponses = 0;
|
|
state.numRequests = 0;
|
|
|
|
// Computes and displays the total monthly cost
|
|
var displayCost = function()
|
|
{
|
|
var cost = 0;
|
|
var haveAll = true;
|
|
for (var i = 0; i < data.linodes.length; i++) {
|
|
if (data.linodes[i].backups.enabled)
|
|
continue;
|
|
|
|
if (!data.plans[data.linodes[i].type]) {
|
|
haveAll = false;
|
|
apiGet("/linode/types/" + data.linodes[i].type, function(response)
|
|
{
|
|
data.plans[response.id] = response;
|
|
displayCost();
|
|
}, null);
|
|
continue;
|
|
}
|
|
|
|
cost += data.plans[data.linodes[i].type].addons.backups.price.monthly;
|
|
}
|
|
|
|
if (haveAll) {
|
|
ui.cost.innerHTML = cost.toFixed(2);
|
|
ui.doIt.disabled = false;
|
|
}
|
|
};
|
|
|
|
// Callback for linodes API call
|
|
var displayLinodes = function(response)
|
|
{
|
|
data.linodes = data.linodes.concat(response.data);
|
|
|
|
// Request the next page if there are more
|
|
if (response.page != response.pages) {
|
|
apiGet("/linode/instances?page=" + (response.page + 1), displayLinodes, null);
|
|
return;
|
|
}
|
|
|
|
state.haveLinodes = true;
|
|
|
|
// Display count of linodes without backups
|
|
var count = 0;
|
|
for (var i = 0; i < data.linodes.length; i++) {
|
|
if (!data.linodes[i].backups.enabled)
|
|
count++;
|
|
}
|
|
|
|
// If there are no linodes without backups, go back
|
|
if (!count)
|
|
location.href = "/account/settings";
|
|
|
|
for (var i = 0; i < ui.linodeCount.length; i++)
|
|
ui.linodeCount[i].innerHTML = count;
|
|
|
|
if (state.havePlans)
|
|
displayCost();
|
|
};
|
|
|
|
// Callback for plans API call
|
|
var displayPlans = function(response)
|
|
{
|
|
for (var i = 0; i < response.data.length; i++)
|
|
data.plans[response.data[i].id] = response.data[i];
|
|
|
|
// Request the next page if there are more
|
|
if (response.page != response.pages) {
|
|
apiGet("/linode/types?page=" + (response.page + 1), displayPlans, null);
|
|
return;
|
|
}
|
|
|
|
state.havePlans = true;
|
|
|
|
if (state.haveLinode)
|
|
displayCost();
|
|
};
|
|
|
|
// Click handler for enable button
|
|
var doIt = function(event)
|
|
{
|
|
if (event.currentTarget.disabled)
|
|
return;
|
|
|
|
// Enable backups for each linode without backups
|
|
for (var i = 0; i < data.linodes.length; i++) {
|
|
if (data.linodes[i].backups.enabled)
|
|
continue;
|
|
|
|
state.numRequests++;
|
|
apiPost("/linode/instances/" + data.linodes[i].id + "/backups/enable", {}, function(response)
|
|
{
|
|
state.numResponses++;
|
|
if (state.numResponses >= state.numRequests)
|
|
location.href = "/account/settings";
|
|
});
|
|
}
|
|
|
|
ui.doIt.disabled = true;
|
|
};
|
|
|
|
// Initial setup
|
|
var setup = function()
|
|
{
|
|
// Parse URL parameters
|
|
data.params = parseParams();
|
|
|
|
setupHeader();
|
|
|
|
// Highlight the account settings subnav link
|
|
var subnavLinks = document.getElementsByClassName(elements.subnav);
|
|
for (var i = 0; i < subnavLinks.length; i++) {
|
|
if (subnavLinks[i].pathname == "/account/settings")
|
|
subnavLinks[i].className = elements.subnav + " " + elements.subnavActive;
|
|
else
|
|
subnavLinks[i].className = elements.subnav;
|
|
}
|
|
|
|
// Get element references
|
|
ui.cost = document.getElementById(elements.cost);
|
|
ui.doIt = document.getElementById(elements.doIt);
|
|
ui.linodeCount = document.getElementsByClassName(elements.linodeCount);
|
|
|
|
// Attach event handlers
|
|
ui.doIt.addEventListener("click", doIt);
|
|
|
|
// Get data from the API
|
|
apiGet("/linode/instances", displayLinodes, null);
|
|
apiGet("/linode/types", displayPlans, null);
|
|
};
|
|
|
|
// Attach onload handler
|
|
window.addEventListener("load", setup);
|
|
})();
|