1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00

fix(instances): the VM list keeps its state filter and finds names in any case

The VM list reloads when its poll finds a VM added or gone. The search
was kept through the reload, but the chosen state pill (Off, say) went
back to All: on a busy host the filter kept undoing itself. The state is
now remembered with the search, for the tab.

The search compared lower-cased text, which differs in Turkish:
"istanbul" did not find "İSTANBUL". Both sides are now compared without
case and accents (and dotless ı as i).
This commit is contained in:
catborise 2026-11-13 15:22:27 +03:00
parent cf1f7ad055
commit cdc7468af3
2 changed files with 55 additions and 9 deletions

View file

@ -101,3 +101,28 @@ filterInstances(page([group(fleet)], "", "Crashed", undefined, {counts, status})
assert.strictEqual(counts[3].parentElement.hidden, false, "the chosen state stays, even empty");
filterInstances(page([group(fleet)], "no-such-vm", "Active", undefined, {counts, status}));
assert.strictEqual(counts[0].parentElement.hidden, false, "All stays when nothing is found");
// a search is without case and accents in any language: "istanbul" finds "İSTANBUL"
const turkish = [vm("İSTANBUL-1", "Active"), vm("ankara", "Active"), vm("Şişli", "Off")];
filterInstances(page([group(turkish)], "istanbul", ""));
assert.deepStrictEqual(visible(turkish), ["İSTANBUL-1"]);
filterInstances(page([group(turkish)], "sisli", ""));
assert.deepStrictEqual(visible(turkish), ["Şişli"]);
// the chosen state is remembered as the search is: a list the poll reloads keeps it
{
const vmModule = require("vm");
const fs = require("fs");
const stored = {"VMs_filter_state": "Off"};
const pill = (state) => ({dataset: {pillState: state}, classList: {toggle() {}}, setAttribute() {}});
const pillList = [pill(""), pill("Active"), pill("Off")];
const elements = {
filter: {value: "", hasAttribute: () => false, addEventListener() {}},
"state-filter": {value: "", addEventListener() {}},
"state-pills": {querySelectorAll: () => pillList, querySelector: () => pillList[0], addEventListener() {}},
};
const doc = {title: "VMs", getElementById: (id) => elements[id], querySelectorAll: () => []};
const storage = {getItem: (k) => stored[k] || null, setItem: (k, v) => { stored[k] = v; }, removeItem: (k) => { delete stored[k]; }};
vmModule.runInNewContext(fs.readFileSync(process.argv[2], "utf8"), {document: doc, sessionStorage: storage});
assert.strictEqual(elements["state-filter"].value, "Off");
}

View file

@ -12,17 +12,25 @@
// The instance list's state pills (#state-pills) set the state; each one's
// [data-count-state] counts the VMs of its state the search finds (its
// state left aside), "" all of them; a state's pill with none is hidden
// unless chosen. #filter-status tells screen readers how many VMs match,
// collapsed hosts' VMs too.
// unless chosen; the chosen state is remembered as the search is, so that a
// list the poll reloads keeps it. #filter-status tells screen readers how
// many VMs match, collapsed hosts' VMs too.
// text as it is compared: without case and accents, whatever the browser's
// language ("İSTANBUL", "istanbul" and "Istanbul" are one; so are "ş" and "s")
function fold(text) {
return text.normalize("NFKD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/ı/g, "i");
}
function filterTable(doc) {
var text = doc.getElementById("filter").value.toLowerCase();
var text = fold(doc.getElementById("filter").value);
var states = doc.getElementById("state-filter");
var state = states ? states.value : "";
var anyShown = false;
var counts = {"": 0}, vms = 0, shownVms = 0;
doc.querySelectorAll(".searchable").forEach(function (group) {
var host = group.dataset.host;
var hostMatches = host !== undefined && host.toLowerCase().includes(text);
var hostMatches = host !== undefined && fold(host).includes(text);
var header = null, shown = false;
group.querySelectorAll("tr, [data-search], [data-group-header]").forEach(function (row) {
if (row.hasAttribute("data-group-header")) {
@ -31,7 +39,7 @@ function filterTable(doc) {
}
var mark = row.querySelector("[data-state]");
var searched = row.dataset.search !== undefined ? row.dataset.search : row.textContent;
var found = hostMatches || searched.toLowerCase().includes(text);
var found = hostMatches || fold(searched).includes(text);
row.hidden = !((!state || (mark && mark.dataset.state === state)) && found);
shown = shown || !row.hidden;
if (mark) {
@ -64,7 +72,7 @@ function filterTable(doc) {
}
if (typeof module !== "undefined") {
module.exports = {filterTable: filterTable};
module.exports = {filterTable: filterTable, fold: fold};
} else {
var search = document.getElementById("filter");
var states = document.getElementById("state-filter");
@ -72,12 +80,12 @@ if (typeof module !== "undefined") {
// search ([data-forget], the group form's members) starts empty
var remember = !search.hasAttribute("data-forget");
var key = document.title + "_filter";
function keep(value) {
function keep(value, which) {
try {
if (value) {
sessionStorage.setItem(key, value);
sessionStorage.setItem(which || key, value);
} else {
sessionStorage.removeItem(key);
sessionStorage.removeItem(which || key);
}
} catch (e) {
// storage turned off: the search is not kept
@ -102,6 +110,7 @@ if (typeof module !== "undefined") {
var pills = document.getElementById("state-pills");
function chooseState(pill) {
states.value = pill.dataset.pillState;
keep(states.value, key + "_state");
pills.querySelectorAll("[data-pill-state]").forEach(function (other) {
other.classList.toggle("active", other === pill);
other.setAttribute("aria-pressed", other === pill);
@ -109,6 +118,18 @@ if (typeof module !== "undefined") {
filterTable(document);
}
if (pills) {
var kept = "";
try {
kept = sessionStorage.getItem(key + "_state") || "";
} catch (e) {
// storage turned off: all states
}
var keptPill = kept && Array.prototype.find.call(pills.querySelectorAll("[data-pill-state]"), function (pill) {
return pill.dataset.pillState === kept;
});
if (keptPill) {
chooseState(keptPill);
}
pills.addEventListener("click", function (event) {
var pill = event.target.closest("[data-pill-state]");
if (pill) {