use custom element for instances

This commit is contained in:
Arthur Lu 2023-08-15 20:27:45 +00:00
parent 820db1058b
commit 2b75732e92

View File

@ -60,9 +60,9 @@ async function populateInstances () {
</div> </div>
`; `;
for (let i = 0; i < instances.length; i++) { for (let i = 0; i < instances.length; i++) {
const newInstance = new Instance(); const newInstance = document.createElement("instance-card");
newInstance.data = instances[i]; newInstance.data = instances[i];
instanceContainer.append(newInstance.shadowElement); instanceContainer.append(newInstance);
} }
} }
@ -198,12 +198,14 @@ async function handleInstanceAdd () {
d.querySelector("#vmid").max = userCluster.vmid.max; d.querySelector("#vmid").max = userCluster.vmid.max;
} }
class Instance { class InstanceCard extends HTMLElement {
constructor () { constructor () {
const shadowRoot = document.createElement("div"); super();
shadowRoot.classList.add("w3-row"); this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
shadowRoot.innerHTML = ` <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="css/style.css">
<div class="w3-row">
<div class="w3-col l1 m2 s6"> <div class="w3-col l1 m2 s6">
<p id="instance-id"></p> <p id="instance-id"></p>
</div> </div>
@ -230,9 +232,8 @@ class Instance {
<img id="configure-btn"> <img id="configure-btn">
<img id="delete-btn"> <img id="delete-btn">
</div> </div>
</div>
`; `;
this.shadowElement = shadowRoot;
this.actionLock = false; this.actionLock = false;
} }
@ -259,33 +260,33 @@ class Instance {
} }
update () { update () {
const vmidParagraph = this.shadowElement.querySelector("#instance-id"); const vmidParagraph = this.shadowRoot.querySelector("#instance-id");
vmidParagraph.innerText = this.vmid; vmidParagraph.innerText = this.vmid;
const nameParagraph = this.shadowElement.querySelector("#instance-name"); const nameParagraph = this.shadowRoot.querySelector("#instance-name");
nameParagraph.innerText = this.name ? this.name : ""; nameParagraph.innerText = this.name ? this.name : "";
const typeParagraph = this.shadowElement.querySelector("#instance-type"); const typeParagraph = this.shadowRoot.querySelector("#instance-type");
typeParagraph.innerText = this.type; typeParagraph.innerText = this.type;
const statusParagraph = this.shadowElement.querySelector("#instance-status"); const statusParagraph = this.shadowRoot.querySelector("#instance-status");
statusParagraph.innerText = this.status; statusParagraph.innerText = this.status;
const statusIcon = this.shadowElement.querySelector("#instance-status-icon"); const statusIcon = this.shadowRoot.querySelector("#instance-status-icon");
statusIcon.src = instancesConfig[this.status].status.src; statusIcon.src = instancesConfig[this.status].status.src;
statusIcon.alt = instancesConfig[this.status].status.alt; statusIcon.alt = instancesConfig[this.status].status.alt;
const nodeNameParagraph = this.shadowElement.querySelector("#node-name"); const nodeNameParagraph = this.shadowRoot.querySelector("#node-name");
nodeNameParagraph.innerText = this.node.name; nodeNameParagraph.innerText = this.node.name;
const nodeStatusParagraph = this.shadowElement.querySelector("#node-status"); const nodeStatusParagraph = this.shadowRoot.querySelector("#node-status");
nodeStatusParagraph.innerText = this.node.status; nodeStatusParagraph.innerText = this.node.status;
const nodeStatusIcon = this.shadowElement.querySelector("#node-status-icon"); const nodeStatusIcon = this.shadowRoot.querySelector("#node-status-icon");
nodeStatusIcon.src = nodesConfig[this.node.status].status.src; nodeStatusIcon.src = nodesConfig[this.node.status].status.src;
nodeStatusIcon.alt = nodesConfig[this.node.status].status.src; nodeStatusIcon.alt = nodesConfig[this.node.status].status.src;
const powerButton = this.shadowElement.querySelector("#power-btn"); const powerButton = this.shadowRoot.querySelector("#power-btn");
powerButton.src = instancesConfig[this.status].power.src; powerButton.src = instancesConfig[this.status].power.src;
powerButton.alt = instancesConfig[this.status].power.alt; powerButton.alt = instancesConfig[this.status].power.alt;
powerButton.title = instancesConfig[this.status].power.alt; powerButton.title = instancesConfig[this.status].power.alt;
@ -294,7 +295,7 @@ class Instance {
powerButton.onclick = this.handlePowerButton.bind(this); powerButton.onclick = this.handlePowerButton.bind(this);
} }
const configButton = this.shadowElement.querySelector("#configure-btn"); const configButton = this.shadowRoot.querySelector("#configure-btn");
configButton.src = instancesConfig[this.status].config.src; configButton.src = instancesConfig[this.status].config.src;
configButton.alt = instancesConfig[this.status].config.alt; configButton.alt = instancesConfig[this.status].config.alt;
configButton.title = instancesConfig[this.status].config.alt; configButton.title = instancesConfig[this.status].config.alt;
@ -303,7 +304,7 @@ class Instance {
configButton.onclick = this.handleConfigButton.bind(this); configButton.onclick = this.handleConfigButton.bind(this);
} }
const consoleButton = this.shadowElement.querySelector("#console-btn"); const consoleButton = this.shadowRoot.querySelector("#console-btn");
consoleButton.src = instancesConfig[this.status].console.src; consoleButton.src = instancesConfig[this.status].console.src;
consoleButton.alt = instancesConfig[this.status].console.alt; consoleButton.alt = instancesConfig[this.status].console.alt;
consoleButton.title = instancesConfig[this.status].console.alt; consoleButton.title = instancesConfig[this.status].console.alt;
@ -312,7 +313,7 @@ class Instance {
consoleButton.onclick = this.handleConsoleButton.bind(this); consoleButton.onclick = this.handleConsoleButton.bind(this);
} }
const deleteButton = this.shadowElement.querySelector("#delete-btn"); const deleteButton = this.shadowRoot.querySelector("#delete-btn");
deleteButton.src = instancesConfig[this.status].delete.src; deleteButton.src = instancesConfig[this.status].delete.src;
deleteButton.alt = instancesConfig[this.status].delete.alt; deleteButton.alt = instancesConfig[this.status].delete.alt;
deleteButton.title = instancesConfig[this.status].delete.alt; deleteButton.title = instancesConfig[this.status].delete.alt;
@ -403,7 +404,7 @@ class Instance {
const result = await requestAPI(`/cluster/${this.node.name}/${this.type}/${this.vmid}/delete`, "DELETE"); const result = await requestAPI(`/cluster/${this.node.name}/${this.type}/${this.vmid}/delete`, "DELETE");
if (result.status === 200) { if (result.status === 200) {
this.shadowElement.parentElement.removeChild(this.shadowElement); this.parentElement.removeChild(this);
} }
else { else {
alert(result.error); alert(result.error);
@ -416,3 +417,5 @@ class Instance {
} }
} }
} }
customElements.define("instance-card", InstanceCard);