lmc/account/settings/settings.js

101 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2020-03-13 23:01:39 -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, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.backupsOff = "backups-off";
elements.backupsOn = "backups-on";
elements.nhOff = "nh-off";
elements.nhOn = "nh-on";
elements.saveButton = "save-button";
elements.saved = "saved";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.backupsOff = {};
ui.backupsOn = {};
ui.nhOff = {};
ui.nhOn = {};
ui.saveButton = {};
ui.saved = {};
// Callback for account settings API call
var displaySettings = function(response)
{
if (response.backups_enabled)
ui.backupsOn.checked = true;
else
ui.backupsOff.checked = true;
if (response.network_helper)
ui.nhOn.checked = true;
else
ui.nhOff.checked = true;
ui.saveButton.disabled = false;
};
// Click handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"backups_enabled": ui.backupsOn.checked,
"network_helper": ui.nhOn.checked
};
apiPut("/account/settings", req, function(response)
{
ui.saved.style.display = "block";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.backupsOff = document.getElementById(elements.backupsOff);
ui.backupsOn = document.getElementById(elements.backupsOn);
ui.nhOff = document.getElementById(elements.nhOff);
ui.nhOn = document.getElementById(elements.nhOn);
ui.saveButton = document.getElementById(elements.saveButton);
ui.saved = document.getElementById(elements.saved);
// Attach event handlers
ui.saveButton.addEventListener("click", handleSave);
// Get data from API
apiGet("/account/settings", displaySettings, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();