diff --git a/index.html b/index.html
index 6f6fe68..9a030a5 100644
--- a/index.html
+++ b/index.html
@@ -68,7 +68,32 @@
-
+
diff --git a/scripts/index.js b/scripts/index.js
index df539ae..a0c18ff 100644
--- a/scripts/index.js
+++ b/scripts/index.js
@@ -4,6 +4,8 @@ import { setupClientSync } from "./clientsync.js";
window.addEventListener("DOMContentLoaded", init);
+let instances = [];
+
async function init () {
setTitleAndHeader();
const cookie = document.cookie;
@@ -12,19 +14,19 @@ async function init () {
}
document.querySelector("#instance-add").addEventListener("click", handleInstanceAdd);
+ document.querySelector("#vm-search").addEventListener("input", populateInstances);
- setupClientSync(populateInstances);
-
- document.querySelector("#vm-search").addEventListener("input", () => {
-
- });
+ setupClientSync(refreshInstances);
}
-async function populateInstances () {
- const resources = await requestPVE("/cluster/resources", "GET");
- const instanceContainer = document.getElementById("instance-container");
- const instances = [];
+async function refreshInstances () {
+ await getInstances();
+ await populateInstances();
+}
+async function getInstances () {
+ const resources = await requestPVE("/cluster/resources", "GET");
+ instances = [];
resources.data.forEach((element) => {
if (element.type === "lxc" || element.type === "qemu") {
const nodeName = element.node;
@@ -33,34 +35,38 @@ async function populateInstances () {
instances.push(element);
}
});
+}
- instances.sort((a, b) => (a.vmid > b.vmid) ? 1 : -1);
-
- instanceContainer.innerHTML = `
-
- `;
+async function populateInstances () {
+ const searchQuery = document.querySelector("#search").value;
+ let criteria;
+ if (searchQuery === "") {
+ criteria = (a, b) => {
+ return (a.vmid > b.vmid) ? 1 : -1;
+ };
+ }
+ else {
+ criteria = (a, b) => {
+ const a_inc = a.name.includes(searchQuery);
+ const b_inc = b.name.includes(searchQuery);
+ if (a_inc && b_inc) {
+ return a.vmid > b.vmid ? 1 : -1;
+ }
+ else if (a_inc && !b_inc) {
+ return -1;
+ }
+ else if (!a_inc && b_inc) {
+ return 1;
+ }
+ else {
+ return a.vmid > b.vmid ? 1 : -1;
+ }
+ };
+ }
+ instances.sort(criteria);
+ //console.log(instances)
+ const instanceContainer = document.querySelector("#instance-container");
+ instanceContainer.innerHTML = ``;
for (let i = 0; i < instances.length; i++) {
const newInstance = document.createElement("instance-card");
newInstance.data = instances[i];