2023-06-30 00:34:22 +00:00
|
|
|
import { API, organization } from "../vars.js";
|
2023-06-29 22:20:15 +00:00
|
|
|
|
|
|
|
export const resourcesConfig = {
|
2024-08-31 12:31:29 +00:00
|
|
|
cpu: {
|
|
|
|
name: "CPU Type",
|
|
|
|
icon: "images/resources/cpu.svg",
|
|
|
|
id: "proctype",
|
|
|
|
unitText: null
|
|
|
|
},
|
|
|
|
cores: {
|
|
|
|
name: "CPU Amount",
|
|
|
|
icon: "images/resources/cpu.svg",
|
|
|
|
id: "cores",
|
|
|
|
unitText: "Cores"
|
|
|
|
},
|
|
|
|
memory: {
|
|
|
|
name: "Memory",
|
|
|
|
icon: "images/resources/ram.svg",
|
|
|
|
id: "ram",
|
|
|
|
unitText: "MiB"
|
|
|
|
},
|
|
|
|
swap: {
|
|
|
|
name: "Swap",
|
|
|
|
icon: "images/resources/swap.svg",
|
|
|
|
id: "swap",
|
|
|
|
unitText: "MiB"
|
|
|
|
},
|
2023-06-29 22:20:15 +00:00
|
|
|
disk: {
|
|
|
|
actionBarOrder: ["move", "resize", "detach_attach", "delete"],
|
|
|
|
lxc: {
|
|
|
|
prefixOrder: ["rootfs", "mp", "unused"],
|
|
|
|
rootfs: { name: "ROOTFS", icon: "images/resources/drive.svg", actions: ["move", "resize"] },
|
|
|
|
mp: { name: "MP", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"] },
|
|
|
|
unused: { name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"] }
|
|
|
|
},
|
|
|
|
qemu: {
|
|
|
|
prefixOrder: ["ide", "sata", "unused"],
|
|
|
|
ide: { name: "IDE", icon: "images/resources/disk.svg", actions: ["delete"] },
|
|
|
|
sata: { name: "SATA", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"] },
|
|
|
|
unused: { name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"] }
|
2024-07-24 19:32:40 +00:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
attach: {
|
|
|
|
src: "images/actions/disk/attach.svg",
|
|
|
|
title: "Attach Disk"
|
|
|
|
},
|
|
|
|
detach: {
|
|
|
|
src: "images/actions/disk/detach.svg",
|
|
|
|
title: "Detach Disk"
|
|
|
|
},
|
|
|
|
delete: null
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
network: {
|
2024-08-31 12:31:29 +00:00
|
|
|
name: "Network",
|
|
|
|
icon: "images/resources/network.svg",
|
|
|
|
id: "network",
|
|
|
|
unitText: "MB/s",
|
2023-06-29 22:20:15 +00:00
|
|
|
prefix: "net"
|
|
|
|
},
|
2024-08-31 12:31:29 +00:00
|
|
|
pci: {
|
|
|
|
name: "Devices",
|
|
|
|
icon: "images/resources/device.svg",
|
|
|
|
id: "devices",
|
|
|
|
unitText: null,
|
2023-06-29 22:20:15 +00:00
|
|
|
prefix: "hostpci"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const instancesConfig = {
|
|
|
|
running: {
|
|
|
|
status: {
|
|
|
|
src: "images/status/active.svg",
|
|
|
|
alt: "Instance is running",
|
|
|
|
clickable: false
|
|
|
|
},
|
|
|
|
power: {
|
|
|
|
src: "images/actions/instance/stop.svg",
|
|
|
|
alt: "Shutdown Instance",
|
|
|
|
clickable: true
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
src: "images/actions/instance/config-inactive.svg",
|
|
|
|
alt: "Change Configuration (Inactive)",
|
|
|
|
clickable: false
|
|
|
|
},
|
|
|
|
console: {
|
|
|
|
src: "images/actions/instance/console-active.svg",
|
|
|
|
alt: "Open Console",
|
|
|
|
clickable: true
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
src: "images/actions/delete-inactive.svg",
|
|
|
|
alt: "Delete Instance (Inactive)",
|
|
|
|
clickable: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
stopped: {
|
|
|
|
status: {
|
|
|
|
src: "images/status/inactive.svg",
|
|
|
|
alt: "Instance is stopped",
|
|
|
|
clickable: false
|
|
|
|
},
|
|
|
|
power: {
|
|
|
|
src: "images/actions/instance/start.svg",
|
|
|
|
alt: "Start Instance",
|
|
|
|
clickable: true
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
src: "images/actions/instance/config-active.svg",
|
|
|
|
alt: "Change Configuration",
|
|
|
|
clickable: true
|
|
|
|
},
|
|
|
|
console: {
|
|
|
|
src: "images/actions/instance/console-inactive.svg",
|
|
|
|
alt: "Open Console (Inactive)",
|
|
|
|
clickable: false
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
src: "images/actions/delete-active.svg",
|
|
|
|
alt: "Delete Instance",
|
|
|
|
clickable: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
loading: {
|
|
|
|
status: {
|
|
|
|
src: "images/status/loading.svg",
|
|
|
|
alt: "Instance is loading",
|
|
|
|
clickable: false
|
|
|
|
},
|
|
|
|
power: {
|
|
|
|
src: "images/status/loading.svg",
|
|
|
|
alt: "Loading Instance",
|
|
|
|
clickable: false
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
src: "images/actions/instance/config-inactive.svg",
|
|
|
|
alt: "Change Configuration (Inactive)",
|
|
|
|
clickable: false
|
|
|
|
},
|
|
|
|
console: {
|
|
|
|
src: "images/actions/instance/console-inactive.svg",
|
|
|
|
alt: "Open Console (Inactive)",
|
|
|
|
clickable: false
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
src: "images/actions/delete-inactive.svg",
|
|
|
|
alt: "Delete Instance (Inactive)",
|
|
|
|
clickable: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const nodesConfig = {
|
|
|
|
online: {
|
|
|
|
status: {
|
|
|
|
src: "images/status/active.svg",
|
|
|
|
alt: "Node is online"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
offline: {
|
|
|
|
status: {
|
|
|
|
src: "images/status/inactive.svg",
|
|
|
|
alt: "Node is offline"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
uknown: {
|
|
|
|
status: {
|
|
|
|
src: "images/status/inactive.svg",
|
|
|
|
alt: "Node is offline"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-11 22:06:26 +00:00
|
|
|
export const bootConfig = {
|
|
|
|
eligiblePrefixes: ["ide", "sata", "net"],
|
|
|
|
ide: {
|
2023-09-21 22:41:16 +00:00
|
|
|
icon: "images/resources/disk.svg",
|
|
|
|
alt: "IDE Bootable Icon"
|
2023-08-11 22:06:26 +00:00
|
|
|
},
|
|
|
|
sata: {
|
2023-09-21 22:41:16 +00:00
|
|
|
icon: "images/resources/drive.svg",
|
|
|
|
alt: "SATA Bootable Icon"
|
2023-08-11 22:06:26 +00:00
|
|
|
},
|
|
|
|
net: {
|
2023-09-21 22:41:16 +00:00
|
|
|
icon: "images/resources/network.svg",
|
|
|
|
alt: "NET Bootable Icon"
|
2023-08-11 22:06:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-29 22:20:15 +00:00
|
|
|
export function getCookie (cname) {
|
|
|
|
const name = cname + "=";
|
|
|
|
const decodedCookie = decodeURIComponent(document.cookie);
|
|
|
|
const ca = decodedCookie.split(";");
|
|
|
|
for (let i = 0; i < ca.length; i++) {
|
|
|
|
let c = ca[i];
|
|
|
|
while (c.charAt(0) === " ") {
|
|
|
|
c = c.substring(1);
|
|
|
|
}
|
|
|
|
if (c.indexOf(name) === 0) {
|
|
|
|
return c.substring(name.length, c.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function requestPVE (path, method, body = null) {
|
|
|
|
const prms = new URLSearchParams(body);
|
|
|
|
const content = {
|
|
|
|
method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (method === "POST") {
|
|
|
|
content.body = prms.toString();
|
|
|
|
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await request(`${API}/proxmox${path}`, content);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function requestAPI (path, method, body = null) {
|
|
|
|
const prms = new URLSearchParams(body);
|
|
|
|
const content = {
|
|
|
|
method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (method === "POST" || method === "DELETE") {
|
|
|
|
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
|
|
|
|
}
|
|
|
|
if (body) {
|
|
|
|
content.body = prms.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await request(`${API}${path}`, content);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function request (url, content) {
|
2023-10-18 19:33:58 +00:00
|
|
|
try {
|
|
|
|
const response = await fetch(url, content);
|
|
|
|
const contentType = response.headers.get("Content-Type");
|
|
|
|
let data = null;
|
|
|
|
if (contentType.includes("application/json")) {
|
|
|
|
data = await response.json();
|
|
|
|
data.status = response.status;
|
|
|
|
}
|
|
|
|
else if (contentType.includes("text/html")) {
|
|
|
|
data = { data: await response.text() };
|
|
|
|
data.status = response.status;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data = response;
|
|
|
|
}
|
|
|
|
if (!response.ok) {
|
|
|
|
return { status: response.status, error: data ? data.error : response.status };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data.status = response.status;
|
|
|
|
return data || response;
|
|
|
|
}
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|
2023-10-18 19:33:58 +00:00
|
|
|
catch (error) {
|
2023-10-19 17:26:37 +00:00
|
|
|
return { status: 400, error };
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 17:27:37 +00:00
|
|
|
export function goToPage (page, data = null) {
|
|
|
|
const params = data ? (new URLSearchParams(data)).toString() : "";
|
|
|
|
window.location.href = `${page}${data ? "?" : ""}${params}`;
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function goToURL (href, data = {}, newwindow = false) {
|
|
|
|
const url = new URL(href);
|
|
|
|
for (const k in data) {
|
|
|
|
url.searchParams.append(k, data[k]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newwindow) {
|
2023-09-15 22:13:21 +00:00
|
|
|
window.open(url, `${organization} - dashboard`, "height=480,width=848");
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.location.assign(url.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getURIData () {
|
|
|
|
const url = new URL(window.location.href);
|
|
|
|
return Object.fromEntries(url.searchParams);
|
|
|
|
}
|
|
|
|
|
2024-07-16 19:00:30 +00:00
|
|
|
export async function setTitleAndHeader () {
|
2023-09-15 22:13:21 +00:00
|
|
|
document.title = `${organization} - dashboard`;
|
2023-06-29 22:20:15 +00:00
|
|
|
document.querySelector("h1").innerText = organization;
|
|
|
|
}
|
2024-06-14 06:17:15 +00:00
|
|
|
|
2024-06-17 21:02:15 +00:00
|
|
|
const settingsDefault = {
|
|
|
|
"sync-scheme": "always",
|
|
|
|
"sync-rate": 5,
|
|
|
|
"search-criteria": "fuzzy",
|
|
|
|
"appearance-theme": "auto"
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getSyncSettings () {
|
|
|
|
let scheme = localStorage.getItem("sync-scheme");
|
|
|
|
let rate = Number(localStorage.getItem("sync-rate"));
|
|
|
|
if (!scheme) {
|
|
|
|
scheme = settingsDefault["sync-scheme"];
|
|
|
|
localStorage.setItem("sync-scheme", scheme);
|
|
|
|
}
|
|
|
|
if (!rate) {
|
|
|
|
rate = settingsDefault["sync-rate"];
|
|
|
|
localStorage.setItem("sync-rate", rate);
|
|
|
|
}
|
|
|
|
return { scheme, rate };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSearchSettings () {
|
|
|
|
let searchCriteria = localStorage.getItem("search-criteria");
|
|
|
|
if (!searchCriteria) {
|
|
|
|
searchCriteria = settingsDefault["search-criteria"];
|
|
|
|
localStorage.setItem("search-criteria", searchCriteria);
|
|
|
|
}
|
|
|
|
return searchCriteria;
|
|
|
|
}
|
|
|
|
|
2024-06-14 06:17:15 +00:00
|
|
|
export function setAppearance () {
|
|
|
|
let theme = localStorage.getItem("appearance-theme");
|
|
|
|
if (!theme) {
|
2024-06-17 21:02:15 +00:00
|
|
|
theme = settingsDefault["appearance-theme"];
|
|
|
|
localStorage.setItem("appearance-theme", theme);
|
2024-06-14 06:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (theme === "auto") {
|
|
|
|
document.querySelector(":root").classList.remove("dark-theme", "light-theme");
|
|
|
|
}
|
|
|
|
else if (theme === "dark") {
|
|
|
|
document.querySelector(":root").classList.remove("light-theme");
|
|
|
|
document.querySelector(":root").classList.add("dark-theme");
|
|
|
|
}
|
|
|
|
else if (theme === "light") {
|
|
|
|
document.querySelector(":root").classList.add("light-theme");
|
|
|
|
document.querySelector(":root").classList.remove("dark-theme");
|
|
|
|
}
|
|
|
|
}
|
2024-07-24 19:32:40 +00:00
|
|
|
|
|
|
|
// assumes href is path to svg, and id to grab is #symb
|
|
|
|
export function setSVGSrc (svgElem, href) {
|
|
|
|
let useElem = svgElem.querySelector("use");
|
|
|
|
if (!useElem) {
|
|
|
|
useElem = document.createElementNS("http://www.w3.org/2000/svg", "use");
|
|
|
|
}
|
2024-08-31 12:31:29 +00:00
|
|
|
useElem.setAttribute("href", `${href}#symb`);
|
2024-07-24 19:32:40 +00:00
|
|
|
svgElem.append(useElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setSVGAlt (svgElem, alt) {
|
|
|
|
svgElem.setAttribute("aria-label", alt);
|
|
|
|
}
|
2024-08-31 12:31:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple object check.
|
|
|
|
* @param item
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function isObject (item) {
|
|
|
|
return (item && typeof item === "object" && !Array.isArray(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deep merge two objects.
|
|
|
|
* @param target
|
|
|
|
* @param ...sources
|
|
|
|
*/
|
|
|
|
export function mergeDeep (target, ...sources) {
|
|
|
|
if (!sources.length) return target;
|
|
|
|
const source = sources.shift();
|
|
|
|
|
|
|
|
if (isObject(target) && isObject(source)) {
|
|
|
|
for (const key in source) {
|
|
|
|
if (isObject(source[key])) {
|
|
|
|
if (!target[key]) Object.assign(target, { [key]: {} });
|
|
|
|
mergeDeep(target[key], source[key]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Object.assign(target, { [key]: source[key] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergeDeep(target, ...sources);
|
|
|
|
}
|
|
|
|
|
2024-10-28 20:03:54 +00:00
|
|
|
/**
|
|
|
|
* Checks if object or array is empty
|
|
|
|
* @param {*} obj
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function isEmpty (obj) {
|
|
|
|
if (obj instanceof Array) {
|
|
|
|
return obj.length === 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (const prop in obj) {
|
|
|
|
if (Object.hasOwn(obj, prop)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 07:37:16 +00:00
|
|
|
export function addResourceLine (resourceConfig, field, attributesOverride, labelPrefix = null) {
|
2024-08-31 12:31:29 +00:00
|
|
|
const iconHref = resourceConfig.icon;
|
|
|
|
const elementType = resourceConfig.element;
|
|
|
|
const labelText = labelPrefix ? `${labelPrefix} ${resourceConfig.name}` : resourceConfig.name;
|
|
|
|
const id = resourceConfig.id;
|
|
|
|
const unitText = resourceConfig.unitText;
|
|
|
|
const attributes = { ...(resourceConfig.attributes), ...(attributesOverride) };
|
|
|
|
|
|
|
|
const icon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
|
|
setSVGSrc(icon, iconHref);
|
|
|
|
setSVGAlt(icon, labelText);
|
|
|
|
field.append(icon);
|
|
|
|
|
|
|
|
const label = document.createElement("label");
|
|
|
|
label.innerText = labelText;
|
|
|
|
label.htmlFor = id;
|
|
|
|
field.append(label);
|
|
|
|
|
2024-10-28 20:03:54 +00:00
|
|
|
let element;
|
|
|
|
|
2024-08-31 12:31:29 +00:00
|
|
|
if (elementType === "input") {
|
2024-10-28 20:03:54 +00:00
|
|
|
element = document.createElement("input");
|
2024-08-31 12:31:29 +00:00
|
|
|
for (const k in attributes) {
|
2024-10-28 20:03:54 +00:00
|
|
|
element.setAttribute(k, attributes[k]);
|
2024-08-31 12:31:29 +00:00
|
|
|
}
|
2024-10-28 20:03:54 +00:00
|
|
|
element.id = id;
|
|
|
|
element.name = id;
|
|
|
|
element.required = true;
|
|
|
|
element.classList.add("w3-input");
|
|
|
|
element.classList.add("w3-border");
|
|
|
|
field.append(element);
|
2024-08-31 12:31:29 +00:00
|
|
|
}
|
|
|
|
else if (elementType === "select" || elementType === "multi-select") {
|
2024-10-28 20:03:54 +00:00
|
|
|
element = document.createElement("select");
|
2024-08-31 12:31:29 +00:00
|
|
|
for (const option of attributes.options) {
|
2024-10-28 20:03:54 +00:00
|
|
|
element.append(new Option(option));
|
2024-08-31 12:31:29 +00:00
|
|
|
}
|
2024-10-28 20:03:54 +00:00
|
|
|
element.value = attributes.value;
|
|
|
|
element.id = id;
|
|
|
|
element.name = id;
|
|
|
|
element.required = true;
|
|
|
|
element.classList.add("w3-select");
|
|
|
|
element.classList.add("w3-border");
|
2024-08-31 12:31:29 +00:00
|
|
|
if (elementType === "multi-select") {
|
2024-10-28 20:03:54 +00:00
|
|
|
element.setAttribute("multiple", true);
|
2024-08-31 12:31:29 +00:00
|
|
|
}
|
2024-10-28 20:03:54 +00:00
|
|
|
field.append(element);
|
2024-08-31 12:31:29 +00:00
|
|
|
}
|
|
|
|
else if (customElements.get(elementType)) {
|
2024-10-28 20:03:54 +00:00
|
|
|
element = document.createElement(elementType);
|
2024-08-31 12:31:29 +00:00
|
|
|
if (attributes.options) {
|
|
|
|
for (const option of attributes.options) {
|
2024-10-28 20:03:54 +00:00
|
|
|
element.append(new Option(option));
|
2024-08-31 12:31:29 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-28 20:03:54 +00:00
|
|
|
element.value = attributes.value;
|
|
|
|
element.id = id;
|
|
|
|
element.name = id;
|
|
|
|
element.required = true;
|
|
|
|
field.append(element);
|
2024-08-31 12:31:29 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 20:03:54 +00:00
|
|
|
let unit;
|
|
|
|
|
2024-08-31 12:31:29 +00:00
|
|
|
if (unitText) {
|
2024-10-28 20:03:54 +00:00
|
|
|
unit = document.createElement("p");
|
2024-08-31 12:31:29 +00:00
|
|
|
unit.innerText = unitText;
|
|
|
|
field.append(unit);
|
|
|
|
}
|
|
|
|
else {
|
2024-10-28 20:03:54 +00:00
|
|
|
unit = document.createElement("div");
|
2024-08-31 12:31:29 +00:00
|
|
|
unit.classList.add("hidden");
|
|
|
|
field.append(unit);
|
|
|
|
}
|
2024-10-28 20:03:54 +00:00
|
|
|
|
|
|
|
return { icon, label, element, unit };
|
2024-08-31 12:31:29 +00:00
|
|
|
}
|