lmc/profile/lish/lish.js

116 lines
3.0 KiB
JavaScript
Raw Normal View History

/*
* 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.keys = "keys";
elements.modes = "modes";
elements.saveButton = "save-button";
elements.settingsSaved = "settings-saved";
elements.submitButton = "submit-button";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.keys = {};
ui.modes = {};
ui.saveButton = {};
ui.settingsSaved = {};
ui.submitButton = {};
// Callback for profile API call
var displayProfile = function(response)
{
// Populate auth method settings
ui.modes.value = response.lish_auth_method;
ui.saveButton.disabled = false;
// Populate authorized keys
ui.keys.value = response.authorized_keys.join("\n");
ui.submitButton.disabled = false;
};
// Click handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"lish_auth_method": ui.modes.value
};
apiPut("/profile", req, function(response)
{
ui.settingsSaved.style.display = "block";
});
};
// Click handler for submit button
var handleSubmit = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"authorized_keys": ui.keys.value.split("\n")
};
// Remove empty keys from the request
for (var i = req.authorized_keys.length - 1; i >= 0; i--) {
if (!req.authorized_keys[i].length)
req.authorized_keys.splice(i, 1);
}
apiPut("/profile", req, function(response)
{
ui.settingsSaved.style.display = "block";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.keys = document.getElementById(elements.keys);
ui.modes = document.getElementById(elements.modes);
ui.saveButton = document.getElementById(elements.saveButton);
ui.settingsSaved = document.getElementById(elements.settingsSaved);
ui.submitButton = document.getElementById(elements.submitButton);
// Register event handlers
ui.saveButton.addEventListener("click", handleSave);
ui.submitButton.addEventListener("click", handleSubmit);
// Get data from API
apiGet("/profile", displayProfile, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();