From 7018f5747750c5ddbd9568222021108c9680f264 Mon Sep 17 00:00:00 2001 From: catborise Date: Thu, 19 Nov 2026 10:49:45 +0300 Subject: [PATCH] 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. --- computes/tests/test_tags.py | 3 +- instances/tests/js/table_filter.test.js | 18 +++++++++++- instances/tests/test_tags.py | 2 ++ static/js/table_filter.js | 37 ++++++++++++++++++------- templates/common/tag_filter.html | 7 +++-- 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/computes/tests/test_tags.py b/computes/tests/test_tags.py index 950fb6ab..ce6b651f 100644 --- a/computes/tests/test_tags.py +++ b/computes/tests/test_tags.py @@ -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, '', + 'aria-pressed="false">rack-1 1' + '', 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 diff --git a/instances/tests/js/table_filter.test.js b/instances/tests/js/table_filter.test.js index 16f6326f..5bea65a4 100644 --- a/instances/tests/js/table_filter.test.js +++ b/instances/tests/js/table_filter.test.js @@ -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"); diff --git a/instances/tests/test_tags.py b/instances/tests/test_tags.py index 5a446234..3b999699 100644 --- a/instances/tests/test_tags.py +++ b/instances/tests/test_tags.py @@ -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"]) diff --git a/static/js/table_filter.js b/static/js/table_filter.js index 6a98e0bc..8c38ae75 100644 --- a/static/js/table_filter.js +++ b/static/js/table_filter.js @@ -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); diff --git a/templates/common/tag_filter.html b/templates/common/tag_filter.html index 81b44e07..fe16bd6b 100644 --- a/templates/common/tag_filter.html +++ b/templates/common/tag_filter.html @@ -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 %}