lmc/images/edit/edit.js

112 lines
3.1 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, timeString } from "/global.js";
(function()
{
// Element names specific to this page
elements.created = "created";
elements.description = "description";
elements.imageLabel = "image-label";
elements.label = "label";
elements.nav = "navlink";
elements.navActive = "navlink-active";
elements.saveButton = "save-button";
elements.size = "size";
// Data recieved from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.created = {};
ui.description = {};
ui.imageLabel = {};
ui.label = {};
ui.saveButton = {};
ui.size = {};
// Callback for image API call
var displayImage = function(response)
{
ui.imageLabel.innerHTML = response.label;
ui.label.value = response.label;
var createDate = new Date(response.created + "Z");
ui.created.innerHTML = createDate.toLocaleString();
ui.size.innerHTML = response.size;
ui.description.value = response.description;
ui.saveButton.disabled = false;
};
// Handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"label": ui.label.value,
"description": ui.description.value
};
apiPut("/images/" + data.params.iid, req, function(response)
{
location.href = "/images";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need an image ID, so die if we don't have it
if (!data.params.iid) {
alert("No image ID supplied!");
return;
}
setupHeader();
// Highlight the Linodes nav link
var navLinks = document.getElementsByClassName(elements.nav);
for (var i = 0; i < navLinks.length; i++) {
if (navLinks[i].pathname == "/linodes/")
navLinks[i].className = elements.navActive;
}
// Get element references
ui.created = document.getElementById(elements.created);
ui.description = document.getElementById(elements.description);
ui.imageLabel = document.getElementById(elements.imageLabel);
ui.label = document.getElementById(elements.label);
ui.saveButton = document.getElementById(elements.saveButton);
ui.size = document.getElementById(elements.size);
// Attach event handlers
ui.saveButton.addEventListener("click", handleSave);
// Get data from API
apiGet("/images/" + data.params.iid, displayImage, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();