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

feat(instances): the VM list on Bootstrap's pills, badges and menus, its actions by the user's rights

- The state filter is a row of pills, All and each state, counting the
  VMs the search finds (the chosen state aside); a state without any is
  hidden unless chosen (All never), and a status line tells screen
  readers how many VMs match. Every state keeps its pill.
- A labelled "New VM" button replaces the bare green plus; "Find IP"
  says "Find IP / MAC", which it finds.
- States are badges; a shut-off VM is grey, not red: it is not a fault.
- A VM's cell holds its name, its title, and for those who see owners
  its first owner as a badge, "+N" for the others (named for screen
  readers and on hover); the separate user column goes.
- Each row has the same button group for a superuser or the VM's
  owners, from the actions the user may use (states.allowed_actions),
  all of it disabled on a VM an administrator paused; view-only users
  get none. It holds Console, the power action
  the state calls for (Start, Resume, Shut down, or Clone for a
  template) and a menu of the others (Suspend, Force off, Power cycle),
  what does not apply now disabled. The power button is an icon,
  named on hover and for screen readers, so every group is as wide and
  they line up from row to row; the power cut confirmations stay. On a
  phone Console shows its icon only too.
- A grouped host's row has its name, a Connected or Not connected
  badge, its VM count and a labelled host memory bar; an unreachable
  host's row is table-warning. Its collapse control and the one for
  all hosts are buttons, with aria-expanded kept.
- vCPUs and memory hide on a phone in both layouts, and table cells
  keep Bootstrap's own padding there: Zephyr doubles it and a list's
  last column no longer fit the screen.
This commit is contained in:
catborise 2026-10-27 23:12:02 +03:00
parent 21dca1fb1c
commit b889a0ba51
18 changed files with 295 additions and 153 deletions

View file

@ -18,13 +18,13 @@ function plainRow(text) {
function group(rows, host) {
return {dataset: host ? {host} : {}, querySelectorAll: (s) => (s === "tr" ? rows : [])};
}
function page(groups, text, state, noMatch) {
function page(groups, text, state, noMatch, extra = {}) {
// a list without a state list (state undefined) has no such element
const inputs = {filter: {value: text}, "state-filter": state === undefined ? undefined : {value: state},
"no-match": noMatch};
"no-match": noMatch, "filter-status": extra.status};
return {
getElementById: (id) => inputs[id],
querySelectorAll: (s) => (s === ".searchable" ? groups : []),
querySelectorAll: (s) => (s === ".searchable" ? groups : s === "[data-count-state]" ? extra.counts || [] : []),
};
}
const visible = (rows) => rows.filter((r) => !r.hidden).map((r) => r.textContent);
@ -79,3 +79,18 @@ filterInstances(page([group(menus)], "nothing-like-this", "", noMatch));
assert.strictEqual(noMatch.hidden, false, "no row: the message shows");
filterInstances(page([group(menus)], "web", "", noMatch));
assert.strictEqual(noMatch.hidden, true, "a row: the message goes");
// the state pills count the VMs the search finds, each state's and all; a
// state without any is hidden unless chosen; the status says how many show
const count = (state) => ({dataset: {countState: state}, textContent: "", parentElement: {hidden: false}});
const counts = [count(""), count("Active"), count("Off"), count("Crashed")];
const status = {dataset: {text: "%(shown)s of %(all)s VMs match"}, textContent: ""};
const fleet = [vm("web-1", "Active", "web-1"), vm("web-2", "Off", "web-2"), vm("db-1", "Active", "db-1")];
filterInstances(page([group(fleet)], "web", "Off", undefined, {counts, status}));
assert.deepStrictEqual(counts.map((c) => c.textContent), [2, 1, 1, 0], "counts follow the search, not the state");
assert.deepStrictEqual(counts.map((c) => c.parentElement.hidden), [false, false, false, true]);
assert.strictEqual(status.textContent, "1 of 3 VMs match");
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");

View file

@ -83,7 +83,7 @@ class UnreachableListTests(TestCase):
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="grouped")
response = self.get(reverse("instances:index"), unreachable)
self.assert_unknown(response, [self.vm_a, self.vm_b])
self.assertContains(response, "Not Connected")
self.assertContains(response, "Not connected")
def test_one_unreadable_vm_does_not_hide_the_others(self):
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="nongrouped")

View file

@ -123,8 +123,9 @@ class StateTests(TestCase):
# static/js/table_filter.js filters by these (test_table_filter_js)
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="grouped")
page = html.fromstring(self.get(self.admin, reverse("instances:index")))
options = page.xpath("//select[@id='state-filter']/option/text()")
self.assertEqual(options, ["All states", *LABELS.values(), "Unknown"])
# a pill for all and for each state, every state kept (table_filter.js hides the empty ones)
pills = page.xpath("//div[@id='state-pills']/button/@data-pill-state")
self.assertEqual(pills, ["", *LABELS.values(), "Unknown"])
marks = page.xpath("//tbody[@data-host='st']//tr//@data-state")
self.assertEqual(marks, list(LABELS.values()))
# the host row stays on top of its VMs; memory and vCPUs sort as numbers
@ -133,6 +134,43 @@ class StateTests(TestCase):
self.assertEqual([r.xpath("td[@data-sort]/@data-sort") for r in rows[1:2]], [["2", "2097152"]])
def row_group(self, page, state):
url = reverse("instances:instance", args=[self.vms[state].id])
return page.xpath(f"//tr[.//a[@href='{url}']]//div[@role='group']")
@staticmethod
def enabled(group):
return [c.get("aria-label") for c in group.xpath(".//*[contains(@class,'btn-sm')]") if c.get("disabled") is None]
def test_each_row_has_the_same_group_its_state_s_actions_enabled(self):
# console, the power action, the menu, in every row: those that do not apply now disabled
page = html.fromstring(self.get(self.admin, reverse("instances:index")))
power = {"poweron": "Start", "resume": "Resume", "poweroff": "Shut down"}
in_menu = {"suspend": "Suspend", "force_off": "Force off", "powercycle": "Power cycle"}
for state, label in LABELS.items():
vm = self.vms[state]
with self.subTest(state=label):
(group,) = self.row_group(page, state)
self.assertEqual(group.get("aria-label"), f"Actions for {vm.name}")
self.assertEqual(len(group.xpath(".//*[contains(@class,'btn-sm')]")), 3)
running = state in (1, 2)
expected = (["Console"] if running else []) + [power[a] for a in power if a in ACTIONS[state]]
if ACTIONS[state] & set(in_menu):
expected.append("More actions")
self.assertEqual(self.enabled(group), expected)
items = [i.text_content().strip() for i in group.xpath(".//*[contains(@class,'dropdown-item')]")]
self.assertEqual(items, [in_menu[a] for a in in_menu if a in ACTIONS[state]])
def test_an_owner_sees_the_group_disabled_on_a_vm_an_administrator_paused(self):
page = html.fromstring(self.get(self.owner, reverse("instances:index")))
(group,) = self.row_group(page, 3)
self.assertEqual(self.enabled(group), [])
self.assertTrue(group.xpath(".//button[@aria-label='Resume'][@disabled]"))
# a user who only views VMs gets no group
page = html.fromstring(self.get(self.viewer, reverse("instances:index")))
self.assertEqual(len(page.xpath("//tbody//tr[@data-search]")), len(self.vms))
self.assertEqual(page.xpath("//div[@role='group']"), [])
def test_rows_are_searched_for_their_vm_not_their_buttons(self):
# table_filter.js searches data-search: a menu's "Force off" would match "off" in every row
for style in ("grouped", "nongrouped"):