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

fix(tags): the tag pills count as the state pills do

A tag pill's count was the page's; the state pills' follow the search and
the tag. Now a tag pill counts the rows with its tag the search and the
state find, and one with none is hidden unless chosen, as a state pill.
This commit is contained in:
catborise 2026-11-19 10:49:45 +03:00
parent 9e4dcbe6fc
commit 7018f57477
5 changed files with 52 additions and 15 deletions

View file

@ -163,7 +163,8 @@ class HostTagsTests(TestCase):
self.assertContains(response, 'data-show-tags title="ssd"') # three, and "…" for the others
self.assertContains(response, "rack-1")
self.assertContains(response, '<button type="button" class="nav-link py-1 px-2" data-pill="rack-1" '
'aria-pressed="false">rack-1 <span class="badge text-bg-light ms-1">1</span></button>',
'aria-pressed="false">rack-1 <span class="badge text-bg-light ms-1" data-count-tag="rack-1">1</span>'
'</button>',
html=True)
self.assertContains(response, 'data-tag-filter="rack-1"')
# a search and a tag that match no host: the page says so and clears them

View file

@ -25,7 +25,8 @@ function page(groups, text, state, noMatch, extra = {}) {
"no-match": noMatch, "filter-status": extra.status};
return {
getElementById: (id) => inputs[id],
querySelectorAll: (s) => (s === ".searchable" ? groups : s === "[data-count-state]" ? extra.counts || [] : []),
querySelectorAll: (s) => (s === ".searchable" ? groups : s === "[data-count-state]" ? extra.counts || []
: s === "[data-count-tag]" ? extra.tagCounts || [] : []),
};
}
const visible = (rows) => rows.filter((r) => !r.hidden).map((r) => r.textContent);
@ -120,6 +121,21 @@ assert.deepStrictEqual(visible(tagged), ["web-2"], "-: the rows without a tag");
const tagCounts = [count(""), count("Active"), count("Off")];
filterInstances(page([group(tagged)], "", "Active", undefined, {tag: "prod", counts: tagCounts}));
assert.deepStrictEqual(tagCounts.map((c) => c.textContent), [2, 1, 1], "the state counts follow the tag");
// the tag pills count as the state pills: the rows the search and the state find, the tag aside; one with none hides
const countTag = (name) => ({dataset: {countTag: name}, textContent: "", parentElement: {hidden: false}});
const tagPillCounts = [countTag("-"), countTag("prod"), countTag("web")];
filterInstances(page([group(tagged)], "", "Off", undefined, {tag: "web", tagCounts: tagPillCounts}));
assert.deepStrictEqual(tagPillCounts.map((c) => c.textContent), [0, 1, 0]);
assert.deepStrictEqual(tagPillCounts.map((c) => c.parentElement.hidden), [true, false, false], "the chosen tag stays");
filterInstances(page([group(tagged)], "web", "", undefined, {tag: "", tagCounts: tagPillCounts}));
assert.deepStrictEqual(tagPillCounts.map((c) => c.textContent), [1, 1, 1], "and follow the search");
// a tag named as an object's own property ("constructor") is counted as any other
const objectNamed = [withTags(vm("app-1", "Active", "app-1"), "constructor")];
const constructorCount = [countTag("constructor")];
filterInstances(page([group(objectNamed)], "", "", undefined, {tag: "", tagCounts: constructorCount}));
assert.strictEqual(constructorCount[0].textContent, 1);
filterInstances(page([group(objectNamed)], "nothing", "", undefined, {tag: "", tagCounts: constructorCount}));
assert.deepStrictEqual([constructorCount[0].textContent, constructorCount[0].parentElement.hidden], [0, true]);
const hostVms = [hostRow("host-a"), withTags(vm("web-1", "Active", "web-1"), "prod"), withTags(vm("db-1", "Active", "db-1"), "")];
filterInstances(page([group(hostVms, "host-a")], "host-a", "", undefined, {tag: "prod"}));
assert.deepStrictEqual(visible(hostVms), ["host-a", "web-1"], "a host's name and a tag: its VMs with the tag");

View file

@ -147,6 +147,8 @@ class VmListTagFilterTests(TestCase):
self.assertEqual([pill.get("data-pill") for pill in pills], ["", "-", "prod", "secret", "web"])
self.assertEqual([" ".join(pill.text_content().split()) for pill in pills],
["All tags", "No tag 1", "prod 1", "secret 1", "web 1"])
# table_filter.js counts them again as the search and the state change
self.assertEqual(doc.xpath("//div[@id='tag-pills']//@data-count-tag"), ["-", "prod", "secret", "web"])
self.assertEqual(doc.xpath("//tr[@data-tags]/@data-tags"), ["", "prod web", "secret"])
# a badge chooses its tag in the list
self.assertEqual(doc.xpath("//tr//button[@data-tag-filter]/@data-tag-filter"), ["prod", "web", "secret"])

View file

@ -14,8 +14,9 @@
//
// 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, the tag not), "" all of them; a state's pill with none is hidden
// unless chosen; the chosen state and tag are remembered as the search is, so that a
// state left aside, the tag not), "" all of them; a tag's pill's [data-count-tag]
// the rows with its tag ("-" none) the search and the state find. A pill with none
// is hidden unless chosen; the chosen state and tag are 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.
@ -25,6 +26,17 @@ function fold(text) {
return text.normalize("NFKD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/ı/g, "i");
}
// each count (selector, its value in dataset[key]) shows how many rows counts has for
// its value; its pill is hidden with none, unless chosen or "" (all)
function showCounts(doc, selector, key, counts, chosen) {
doc.querySelectorAll(selector).forEach(function (count) {
var value = count.dataset[key];
var n = counts[value] || 0;
count.textContent = n;
count.parentElement.hidden = n === 0 && value !== "" && value !== chosen;
});
}
function filterTable(doc) {
var text = fold(doc.getElementById("filter").value);
var states = doc.getElementById("state-filter");
@ -32,7 +44,8 @@ function filterTable(doc) {
var tags = doc.getElementById("tag-filter");
var tag = tags ? tags.value : "";
var anyShown = false;
var counts = {"": 0}, vms = 0, shownVms = 0;
// by tag name: no names of an object's own ("constructor") to start with
var counts = {"": 0}, tagCounts = Object.create(null), vms = 0, shownVms = 0;
doc.querySelectorAll(".searchable").forEach(function (group) {
var host = group.dataset.host;
var hostMatches = host !== undefined && fold(host).includes(text);
@ -46,8 +59,15 @@ function filterTable(doc) {
var searched = row.dataset.search !== undefined ? row.dataset.search : row.textContent;
var rowTags = row.dataset.tags || "";
var tagged = !tag || (tag === "-" ? rowTags === "" : rowTags.split(" ").indexOf(tag) >= 0);
var found = (hostMatches || fold(searched).includes(text)) && tagged;
row.hidden = !((!state || (mark && mark.dataset.state === state)) && found);
var searchFound = hostMatches || fold(searched).includes(text);
var found = searchFound && tagged;
var inState = !state || (mark && mark.dataset.state === state);
row.hidden = !(inState && found);
if (row.dataset.tags !== undefined && searchFound && inState) {
(rowTags ? rowTags.split(" ") : ["-"]).forEach(function (name) {
tagCounts[name] = (tagCounts[name] || 0) + 1;
});
}
shown = shown || !row.hidden;
if (mark) {
vms += 1;
@ -67,11 +87,8 @@ function filterTable(doc) {
if (noMatch) {
noMatch.hidden = anyShown;
}
doc.querySelectorAll("[data-count-state]").forEach(function (count) {
var n = counts[count.dataset.countState] || 0;
count.textContent = n;
count.parentElement.hidden = n === 0 && count.dataset.countState !== "" && count.dataset.countState !== state;
});
showCounts(doc, "[data-count-state]", "countState", counts, state);
showCounts(doc, "[data-count-tag]", "countTag", tagCounts, tag);
var status = doc.getElementById("filter-status");
if (status) {
status.textContent = status.dataset.text.replace("%(shown)s", shownVms).replace("%(all)s", vms);

View file

@ -1,5 +1,6 @@
{% load i18n %}{% load bootstrap_icons %}
{# tags: Tag.counts of the listed rows; the pills as the VM states' (table_filter.js): the rows with the one pressed show #}
{# tags: Tag.counts of the listed rows; the pills as the VM states' (table_filter.js): the rows with the one pressed show, #}
{# each count follows the search and the state #}
{% with names=tags.0 untagged=tags.1 %}{% if names %}
<input type="hidden" id="tag-filter" value="">
<nav class="mb-3" aria-label="{% trans "Tags" %}">
@ -7,12 +8,12 @@
<button type="button" class="nav-link active py-1 px-2" data-pill="" aria-pressed="true">{% bs_icon 'tag' %} {% trans "All tags" %}</button>
{% if untagged %}
<button type="button" class="nav-link py-1 px-2" data-pill="-" aria-pressed="false">
{% trans "No tag" %} <span class="badge text-bg-light ms-1">{{ untagged }}</span>
{% trans "No tag" %} <span class="badge text-bg-light ms-1" data-count-tag="-">{{ untagged }}</span>
</button>
{% endif %}
{% for name, count in names %}
<button type="button" class="nav-link py-1 px-2" data-pill="{{ name }}" aria-pressed="false">
{{ name }} <span class="badge text-bg-light ms-1">{{ count }}</span>
{{ name }} <span class="badge text-bg-light ms-1" data-count-tag="{{ name }}">{{ count }}</span>
</button>
{% endfor %}
</div>