lmc/images/images.js

146 lines
4.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, regionNames, apiGet, parseParams, setupHeader, timeString } from "/global.js";
(function()
{
// Element names specific to this page
elements.lmcRow = "lmc-tr1";
elements.lmcRowAlt = "lmc-tr2";
elements.loading = "loading";
elements.imageBody = "image-body";
elements.nav = "navlink";
elements.navActive = "navlink-active";
// Data recieved from API calls
var data = {};
data.images = [];
// Static references to UI elements
var ui = {};
ui.loading = {};
ui.imageBody = {};
// Generates a table row for an image
var createImageRow = function(image, alt)
{
var row = document.createElement("tr");
if (alt)
row.className = elements.lmcRowAlt;
else
row.className = elements.lmcRow;
var label = document.createElement("td");
label.innerHTML = image.label;
row.appendChild(label);
var status = document.createElement("td");
status.innerHTML = image.status.charAt(0).toUpperCase() + image.status.slice(1).replace(/_/g, " ");
row.appendChild(status);
var size = document.createElement("td");
size.innerHTML = image.size + " MB";
row.appendChild(size);
var type = document.createElement("td");
type.innerHTML = image.type;
row.appendChild(type);
var created = document.createElement("td");
var now = new Date();
var createDate = new Date(image.created + "Z");
created.innerHTML = timeString(now - createDate, true);
row.appendChild(created);
var expires = document.createElement("td");
if (image.expiry) {
var expireDate = new Date(image.expiry + "Z");
expires.innerHTML = timeString(now - expireDate, true);
}
row.appendChild(expires);
var options = document.createElement("td");
var edit = document.createElement("a");
edit.href = "/images/edit?iid=" + image.id;
edit.innerHTML = "Edit";
var separator = document.createElement("span");
separator.innerHTML = " | ";
var del = document.createElement("a");
del.href = "/images/delete?iid=" + image.id;
del.innerHTML = "Delete";
options.appendChild(edit);
options.appendChild(separator);
options.appendChild(del);
row.appendChild(options);
return row;
};
// Callback for images API call
var displayImages = function(response)
{
// Add images to array
data.images = data.images.concat(response.data);
// Request
if (response.page != response.pages) {
var progress = (response.page / response.pages) * 100;
progress = progress.toFixed(0);
ui.loading.innerHTML = "Loading " + progress + "%...";
var filter = {
"vendor": null
};
apiGet("/images?page=" + (response.page + 1), displayImages, filter);
}
// Insert images
ui.loading.remove();
for (var i = 0; i < data.images.length; i++)
ui.imageBody.appendChild(createImageRow(data.images[i], i % 2));
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
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.loading = document.getElementById(elements.loading);
ui.imageBody = document.getElementById(elements.imageBody);
// Get data from API
var filter = {
"vendor": null
};
apiGet("/images", displayImages, filter);
};
// Attach onload handler
window.addEventListener("load", setup);
})();