simplify power button logic,

add icons for loading instances

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-01-26 20:50:15 +00:00
parent 8134925c03
commit b75f390619
4 changed files with 91 additions and 95 deletions

View File

@ -0,0 +1 @@
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 204.481 204.481"><path d="M162.116 38.31a7.43 7.43 0 00.454-.67c.033-.055.068-.109.1-.164a7.72 7.72 0 00.419-.857c.014-.034.024-.069.038-.104a7.492 7.492 0 00.314-1.008c.068-.288.124-.581.157-.881l.008-.052a7.48 7.48 0 00.043-.796V7.5a7.5 7.5 0 00-7.5-7.5H48.332a7.5 7.5 0 00-7.5 7.5v26.279c0 .269.016.534.043.796l.008.052c.034.3.089.593.157.881.016.069.035.138.053.207.073.273.159.541.261.801.013.034.024.069.038.104.121.296.262.581.419.857.032.056.067.109.1.164.14.232.291.455.454.67.027.035.047.074.074.109l50.255 63.821-50.255 63.821c-.028.035-.047.074-.074.109a7.43 7.43 0 00-.454.67c-.033.055-.068.109-.1.164a7.72 7.72 0 00-.419.857c-.014.034-.024.069-.038.104a7.492 7.492 0 00-.314 1.008 7.308 7.308 0 00-.157.881l-.008.052a7.48 7.48 0 00-.043.796v26.279a7.5 7.5 0 007.5 7.5h107.817a7.5 7.5 0 007.5-7.5v-26.279c0-.269-.016-.534-.043-.796l-.008-.052a7.51 7.51 0 00-.157-.881c-.016-.069-.035-.138-.053-.207a7.492 7.492 0 00-.261-.801c-.013-.034-.024-.069-.038-.104a7.383 7.383 0 00-.419-.857c-.032-.056-.067-.109-.1-.164a7.646 7.646 0 00-.454-.67c-.027-.035-.047-.074-.074-.109l-50.255-63.821 50.255-63.821c.028-.035.047-.074.074-.11zM148.649 15v11.279H55.832V15h92.817zM55.832 189.481v-11.279h92.817v11.279H55.832zm84.866-26.279H63.784l38.457-48.838 38.457 48.838zm-38.457-73.084L63.784 41.279h76.914l-38.457 48.839z" fill="#fff"/></svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -0,0 +1 @@
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 204.481 204.481"><path d="M162.116 38.31a7.43 7.43 0 00.454-.67c.033-.055.068-.109.1-.164a7.72 7.72 0 00.419-.857c.014-.034.024-.069.038-.104a7.492 7.492 0 00.314-1.008c.068-.288.124-.581.157-.881l.008-.052a7.48 7.48 0 00.043-.796V7.5a7.5 7.5 0 00-7.5-7.5H48.332a7.5 7.5 0 00-7.5 7.5v26.279c0 .269.016.534.043.796l.008.052c.034.3.089.593.157.881.016.069.035.138.053.207.073.273.159.541.261.801.013.034.024.069.038.104.121.296.262.581.419.857.032.056.067.109.1.164.14.232.291.455.454.67.027.035.047.074.074.109l50.255 63.821-50.255 63.821c-.028.035-.047.074-.074.109a7.43 7.43 0 00-.454.67c-.033.055-.068.109-.1.164a7.72 7.72 0 00-.419.857c-.014.034-.024.069-.038.104a7.492 7.492 0 00-.314 1.008 7.308 7.308 0 00-.157.881l-.008.052a7.48 7.48 0 00-.043.796v26.279a7.5 7.5 0 007.5 7.5h107.817a7.5 7.5 0 007.5-7.5v-26.279c0-.269-.016-.534-.043-.796l-.008-.052a7.51 7.51 0 00-.157-.881c-.016-.069-.035-.138-.053-.207a7.492 7.492 0 00-.261-.801c-.013-.034-.024-.069-.038-.104a7.383 7.383 0 00-.419-.857c-.032-.056-.067-.109-.1-.164a7.646 7.646 0 00-.454-.67c-.027-.035-.047-.074-.074-.109l-50.255-63.821 50.255-63.821c.028-.035.047-.074.074-.11zM148.649 15v11.279H55.832V15h92.817zM55.832 189.481v-11.279h92.817v11.279H55.832zm84.866-26.279H63.784l38.457-48.838 38.457 48.838zm-38.457-73.084L63.784 41.279h76.914l-38.457 48.839z" fill="#fff"/></svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -1,4 +1,4 @@
import {requestPVE, goToPage} from "./utils.js"; import {requestPVE, goToPage, instances} from "./utils.js";
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
@ -48,122 +48,95 @@ class Instance extends HTMLElement {
if (data.status === "unknown") { if (data.status === "unknown") {
data.status = "stopped"; data.status = "stopped";
} }
let typeImg = this.shadowElement.querySelector("#instance-type");
typeImg.src = `images/instances/${data.type}/${data.status}.svg`;
typeImg.alt = `${data.status} instance`;
this.type = data.type; this.type = data.type;
this.status = data.status; 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`;
let vmidParagraph = this.shadowElement.querySelector("#instance-id"); let vmidParagraph = this.shadowElement.querySelector("#instance-id");
vmidParagraph.innerText = data.vmid; vmidParagraph.innerText = this.vmid;
this.vmid = data.vmid;
let nameParagraph = this.shadowElement.querySelector("#instance-name"); let nameParagraph = this.shadowElement.querySelector("#instance-name");
if (data.name) { nameParagraph.innerText = this.name ? this.name : "";
nameParagraph.innerText = data.name;
}
else {
nameParagraph.innerText = "";
}
let nodeImg = this.shadowElement.querySelector("#node-status"); let nodeImg = this.shadowElement.querySelector("#node-status");
nodeImg.src = `images/nodes/${data.node.status}.svg`; nodeImg.src = `images/nodes/${this.node.status}.svg`;
let nodeParagraph = this.shadowElement.querySelector("#node-name"); let nodeParagraph = this.shadowElement.querySelector("#node-name");
nodeParagraph.innerText = data.node.name; nodeParagraph.innerText = this.node.name;
this.node = data.node.name;
let powerButton = this.shadowElement.querySelector("#power-btn"); let powerButton = this.shadowElement.querySelector("#power-btn");
powerButton.src = data.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg"; powerButton.src = instances[this.status].powerButtonSrc;
powerButton.alt = data.status === "running" ? "shutdown instance" : "start instance"; powerButton.alt = instances[this.status].powerButtonAlt;
powerButton.addEventListener("click", async () => { powerButton.addEventListener("click", this.handlePowerButton.bind(this));
if (!this.actionLock) {
this.actionLock = true;
let targetAction = this.status === "running" ? "shutdown" : "start";
let targetActionDesc = targetAction === "start" ? "starting" : "shutting down";
let targetStatus = this.status === "running" ? "stopped" : "running";
let typeImg = this.shadowElement.querySelector("#instance-type");
typeImg.src = "images/actions/loading.svg";
typeImg.alt = `instance is ${targetActionDesc}`;
let powerButton = this.shadowElement.querySelector("#power-btn");
powerButton.src = "images/actions/loading.svg";
powerButton.alt = `instance is ${targetActionDesc}`;
let configButton = this.shadowElement.querySelector("#configure-btn");
configButton.src = "images/actions/config-inactive.svg";
try {
let task = await requestPVE(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid});
}
catch {
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
typeImg.alt = `${this.status} instance`;
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance";
configButton.src = this.status === "running" ? "images/actions/config-inactive.svg" : "images/actions/config-active.svg";
this.actionLock = false;
console.error(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${taskStatus.data.exitstatus}`);
return;
}
while (true) {
let taskStatus = await requestPVE(`/nodes/${this.node}/tasks/${task.data}/status`);
if(taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") {
this.status = targetStatus;
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
typeImg.alt = `${this.status} instance`;
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance";
configButton.src = this.status === "running" ? "images/actions/config-inactive.svg" : "images/actions/config-active.svg";
this.actionLock = false;
break;
}
else if (taskStatus.data.status === "stopped") { // stopped but not OK -> instance did not change state
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
typeImg.alt = `${this.status} instance`;
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance";
configButton.src = this.status === "running" ? "images/actions/config-inactive.svg" : "images/actions/config-active.svg";
this.actionLock = false;
console.error(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${taskStatus.data.exitstatus}`);
break;
}
else{
await waitFor(1000);
}
}
}
});
let configButton = this.shadowElement.querySelector("#configure-btn"); let configButton = this.shadowElement.querySelector("#configure-btn");
configButton.src = data.status === "running" ? "images/actions/config-inactive.svg" : "images/actions/config-active.svg"; configButton.src = instances[this.status].configButtonSrc;
configButton.alt = instances[this.status].configButtonAlt;
configButton.addEventListener("click", () => { configButton.addEventListener("click", () => {
if (!this.actionLock && this.status !== "running") { if (!this.actionLock && this.status !== "running") {
goToPage("config.html", {node: this.node, type: this.type, vmid: this.vmid}); goToPage("config.html", {node: this.node.name, type: this.type, vmid: this.vmid});
} }
}) });
if (data.node.status !== "online") { if (this.node.status !== "online") {
powerButton.classList.add("hidden"); powerButton.classList.add("hidden");
configButton.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});
}
catch (error) {
this.status = prevStatus;
this.update();
this.actionLock = false;
console.error(error);
return;
}
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);
}
}
}
}
} }
customElements.define("instance-article", Instance); customElements.define("instance-article", Instance);

View File

@ -33,6 +33,27 @@ export const resources = {
} }
} }
export const instances = {
running: {
powerButtonSrc: "images/actions/stop.svg",
powerButtonAlt: "shutdown instance",
configButtonSrc: "images/actions/config-inactive.svg",
configButtonAlt: "configuration disabled"
},
stopped: {
powerButtonSrc: "images/actions/start.svg",
powerButtonAlt: "start instance",
configButtonSrc: "images/actions/config-active.svg",
configButtonAlt: "configure instance"
},
loading: {
powerButtonSrc: "images/actions/loading.svg",
powerButtonAlt: "loading instance",
configButtonSrc: "images/actions/config-inactive.svg",
configButtonAlt: "configuration disabled"
}
}
function getCookie(cname) { function getCookie(cname) {
let name = cname + "="; let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie); let decodedCookie = decodeURIComponent(document.cookie);