ProxmoxAAS-Dashboard/scripts/elements.js

142 lines
4.3 KiB
JavaScript
Raw Normal View History

import {requestPVE, goToPage, instances} from "./utils.js";
2022-12-16 07:03:50 +00:00
2022-12-18 00:34:40 +00:00
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
2022-12-12 04:20:32 +00:00
class Instance extends HTMLElement {
constructor () {
super();
let shadowRoot = this.attachShadow({mode: "open"});
shadowRoot.innerHTML = `
<article>
<div>
<div>
<img id="instance-type">
<p id="instance-id"></p>
<p id="instance-name"></p>
</div>
<div>
<img id="node-status" alt="instance node">
<p id="node-name"></p>
</div>
</div>
<hr>
<div class="btn-group">
<img id="power-btn">
<img id="configure-btn" alt="change instance configuration">
</div>
</article>
`;
2022-12-12 04:20:32 +00:00
let styleLink = document.createElement("link");
styleLink.rel = "stylesheet";
styleLink.href = "css/style.css";
2022-12-12 04:20:32 +00:00
styleLink.type = "text/css";
shadowRoot.append(styleLink);
2022-12-14 22:30:46 +00:00
let instanceLink = document.createElement("link");
instanceLink.rel = "stylesheet";
instanceLink.href = "css/instance.css";
instanceLink.type = "text/css";
shadowRoot.append(instanceLink);
2022-12-12 04:20:32 +00:00
this.shadowElement = shadowRoot;
2022-12-19 00:53:07 +00:00
this.actionLock = false;
2022-12-12 04:20:32 +00:00
}
set data (data) {
if (data.status === "unknown") {
data.status = "stopped";
}
2022-12-16 07:03:50 +00:00
this.type = data.type;
this.status = data.status;
this.vmid = data.vmid;
this.name = data.name;
this.node = data.node;
this.update();
}
update () {
let typeImg = this.shadowElement.querySelector("#instance-type");
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
typeImg.alt = `${this.status} instance`;
2022-12-14 23:28:22 +00:00
2022-12-15 00:10:28 +00:00
let vmidParagraph = this.shadowElement.querySelector("#instance-id");
vmidParagraph.innerText = this.vmid;
2022-12-14 23:28:22 +00:00
2022-12-15 00:10:28 +00:00
let nameParagraph = this.shadowElement.querySelector("#instance-name");
nameParagraph.innerText = this.name ? this.name : "";
2022-12-14 23:38:13 +00:00
2022-12-15 00:10:28 +00:00
let nodeImg = this.shadowElement.querySelector("#node-status");
nodeImg.src = `images/nodes/${this.node.status}.svg`;
2022-12-14 23:28:22 +00:00
2022-12-15 00:10:28 +00:00
let nodeParagraph = this.shadowElement.querySelector("#node-name");
nodeParagraph.innerText = this.node.name;
2022-12-16 02:04:20 +00:00
let powerButton = this.shadowElement.querySelector("#power-btn");
powerButton.src = instances[this.status].powerButtonSrc;
powerButton.alt = instances[this.status].powerButtonAlt;
powerButton.addEventListener("click", this.handlePowerButton.bind(this));
2022-12-19 00:53:07 +00:00
let configButton = this.shadowElement.querySelector("#configure-btn");
configButton.src = instances[this.status].configButtonSrc;
configButton.alt = instances[this.status].configButtonAlt;
configButton.addEventListener("click", () => {
if (!this.actionLock && this.status !== "running") {
goToPage("config.html", {node: this.node.name, type: this.type, vmid: this.vmid});
}
});
if (this.node.status !== "online") {
powerButton.classList.add("hidden");
configButton.classList.add("hidden");
}
}
async handlePowerButton () {
if(!this.actionLock) {
this.actionLock = true;
let targetAction = this.status === "running" ? "shutdown" : "start";
let targetStatus = this.status === "running" ? "stopped" : "running";
let prevStatus = this.status;
this.status = "loading";
this.update();
let task;
try {
task = await requestPVE(`/nodes/${this.node.name}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node.name, vmid: this.vmid});
2022-12-19 00:53:07 +00:00
}
catch (error) {
this.status = prevStatus;
this.update();
this.actionLock = false;
console.error(error);
return;
2022-12-19 05:52:39 +00:00
}
while (true) {
let taskStatus = await requestPVE(`/nodes/${this.node.name}/tasks/${task.data}/status`);
if(taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") { // task stopped and was successful
this.status = targetStatus;
this.update();
this.actionLock = false;
return;
}
else if (taskStatus.data.status === "stopped") { // task stopped but was not successful
this.status = prevStatus;
console.error(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${taskStatus.data.exitstatus}`);
this.update();
this.actionLock = false;
return;
}
else{
await waitFor(1000);
}
}
}
2022-12-12 04:20:32 +00:00
}
}
2022-12-14 21:37:30 +00:00
customElements.define("instance-article", Instance);