lmc/profile/profile.js

128 lines
3.5 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, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.changeButton = "change-button";
elements.currentAddress = "current-address";
elements.newAddress = "new-address";
elements.notifications = "notifications";
elements.timezone = "timezone";
elements.toggleNotifications = "toggle-notifications";
elements.user = "user";
// Data received from API calls
var data = {};
data.profile = {};
// Static references to UI elements
var ui = {};
ui.changeButton = {};
ui.currentAddress = {};
ui.newAddress = {};
ui.notifications = {};
ui.timezone = {};
ui.toggleNotifications = {};
ui.user = {};
// Callback for profile API call
var displayProfile = function(response)
{
data.profile = response;
ui.user.innerHTML = data.profile.username;
ui.currentAddress.innerHTML = data.profile.email;
if (data.profile.email_notifications)
ui.notifications.innerHTML = "Enabled";
else
ui.notifications.innerHTML = "Disabled";
ui.toggleNotifications.disabled = false;
};
// Event handler for change email button
var handleChange = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"email": ui.newAddress.value
};
apiPut("/profile", req, function(response)
{
ui.newAddress.value = "";
location.reload();
});
};
// Click handler for toggle notifications button
var handleToggle = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"email_notifications": !data.profile.email_notifications
};
apiPut("/profile", req, function(response)
{
location.reload();
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.changeButton = document.getElementById(elements.changeButton);
ui.currentAddress = document.getElementById(elements.currentAddress);
ui.newAddress = document.getElementById(elements.newAddress);
ui.notifications = document.getElementById(elements.notifications);
ui.timezone = document.getElementById(elements.timezone);
ui.toggleNotifications = document.getElementById(elements.toggleNotifications);
ui.user = document.getElementById(elements.user);
// Display timezone info
var now = new Date();
var offset = now.getTimezoneOffset() / -60;
ui.timezone.innerHTML = "GMT";
if (offset >= 0)
ui.timezone.innerHTML += "+";
ui.timezone.innerHTML += offset;
// Register event handlers
ui.changeButton.addEventListener("click", handleChange);
ui.toggleNotifications.addEventListener("click", handleToggle);
// Get data from API
apiGet("/profile", displayProfile, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();