diff --git a/computes/models.py b/computes/models.py index 1914fb7b..80a1fd58 100644 --- a/computes/models.py +++ b/computes/models.py @@ -37,9 +37,9 @@ class Tag(Model): @staticmethod def counts(rows): """The tag list's choices for rows (VMs or hosts, their tags read): - [(name, how many have it)] by name, and how many have no tag.""" + [(name, how many have it)], the most used first, and how many have no tag.""" counts = Counter(tag.name for row in rows for tag in row.tags.all()) - return sorted(counts.items()), sum(1 for row in rows if not row.tags.all()) + return sorted(counts.items(), key=lambda item: (-item[1], item[0])), sum(1 for row in rows if not row.tags.all()) @classmethod def named(cls, names): diff --git a/instances/tests/js/table_filter.test.js b/instances/tests/js/table_filter.test.js index 5bea65a4..1a039aad 100644 --- a/instances/tests/js/table_filter.test.js +++ b/instances/tests/js/table_filter.test.js @@ -26,7 +26,8 @@ function page(groups, text, state, noMatch, extra = {}) { return { getElementById: (id) => inputs[id], querySelectorAll: (s) => (s === ".searchable" ? groups : s === "[data-count-state]" ? extra.counts || [] - : s === "[data-count-tag]" ? extra.tagCounts || [] : []), + : s === "[data-count-tag]" ? extra.tagCounts || [] + : s === "[data-more-count]" ? extra.more || [] : []), }; } const visible = (rows) => rows.filter((r) => !r.hidden).map((r) => r.textContent); @@ -129,6 +130,24 @@ 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"); +// "…" of the tag pills counts those behind it the search and the state leave, and hides with none +{ + const behind = [{hidden: false}, {hidden: true}]; + const others = {hidden: true, querySelectorAll: (s) => behind.filter((p) => s !== "[data-pill]:not([hidden])" || !p.hidden)}; + const button = {hidden: false, nextElementSibling: others}; + const moreCount = {textContent: "", parentElement: button}; + filterInstances(page([group(tagged)], "", "", undefined, {tag: "", more: [moreCount]})); + assert.deepStrictEqual([moreCount.textContent, button.hidden], [1, false]); + behind[0].hidden = true; + filterInstances(page([group(tagged)], "", "", undefined, {tag: "", more: [moreCount]})); + assert.deepStrictEqual([moreCount.textContent, button.hidden], [0, true]); + // shown once: "…" stays hidden whatever the count + others.hidden = false; + behind[0].hidden = false; + filterInstances(page([group(tagged)], "", "", undefined, {tag: "", more: [moreCount]})); + assert.strictEqual(button.hidden, true); +} + // 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")]; @@ -150,7 +169,8 @@ assert.deepStrictEqual(visible(turkish), ["Şişli"]); // the pills: the chosen state and tag are remembered as the search is, so a list the poll reloads keeps them const vmModule = require("vm"); const fs = require("fs"); -const pill = (value) => ({dataset: {pill: value}, classList: {toggle() {}}, setAttribute() {}}); +const pill = (value) => ({dataset: {pill: value}, classList: {toggle() {}}, setAttribute() {}, + parentElement: {closest: () => null}}); function pillRow(values) { const row = {pills: values.map(pill), querySelectorAll: () => row.pills, addEventListener: (name, fn) => { row.click = fn; }}; @@ -188,7 +208,7 @@ const listPage = () => ({ assert.strictEqual(stored["VMs_filter_tag"], "web", "and it is remembered"); // "…" shows a VM's other tags, and the first of them takes the focus let focused = null; - const others = {hidden: true, querySelector: () => ({focus: () => { focused = "d4"; }})}; + const others = {hidden: true, querySelector: (s) => (s === "button:not([hidden])" ? {focus: () => { focused = "d4"; }} : null)}; const ellipsis = {hidden: false, nextElementSibling: others}; click({target: {closest: (s) => (s === "[data-show-tags]" ? ellipsis : null)}}); assert.deepStrictEqual([others.hidden, ellipsis.hidden, focused], [false, true, "d4"]); @@ -207,6 +227,17 @@ const listPage = () => ({ assert.deepStrictEqual([elements["tag-filter"].value, elements.filter.focused], ["", true]); } +// a pill behind "…" chosen (kept, or by a badge): the pills behind it are shown +{ + const elements = listPage(); + const ellipsis = {hidden: false}; + const others = {hidden: true, previousElementSibling: ellipsis}; + const rare = elements["tag-pills"].pills[3]; // "web" + rare.parentElement = {closest: (s) => (s === "[hidden]" ? others : null)}; + run(elements, {"VMs_filter_tag": "web"}); + assert.deepStrictEqual([elements["tag-filter"].value, others.hidden, ellipsis.hidden], ["web", false, true]); +} + // a kept tag no pill has any longer is forgotten: made again later, it is not chosen by itself { const stored = {"VMs_filter_tag": "gone"}; diff --git a/instances/tests/test_tags.py b/instances/tests/test_tags.py index 3b999699..e4f82cc5 100644 --- a/instances/tests/test_tags.py +++ b/instances/tests/test_tags.py @@ -153,6 +153,21 @@ class VmListTagFilterTests(TestCase): # a badge chooses its tag in the list self.assertEqual(doc.xpath("//tr//button[@data-tag-filter]/@data-tag-filter"), ["prod", "web", "secret"]) + def test_the_most_used_eight_show_the_others_behind_an_ellipsis(self): + from types import SimpleNamespace + # ten tags, t0 on ten VMs ... t9 on one: by how many have them, the first eight shown + rows = [SimpleNamespace(tags=SimpleNamespace(all=lambda n=n: [SimpleNamespace(name=f"t{i}") for i in range(n)])) + for n in range(1, 11)] + counts = Tag.counts(rows) + self.assertEqual(counts[0][:3], [("t0", 10), ("t1", 9), ("t2", 8)]) + doc = lxml_html.fromstring(render_to_string("common/tag_filter.html", {"tags": counts})) + shown = doc.xpath("//div[@id='tag-pills']/button/@data-pill") + self.assertEqual(shown, ["", "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"]) + (more,) = doc.xpath("//button[@data-show-tags]") + self.assertEqual(" ".join(more.text_content().split()), "… More tags 2") + self.assertEqual(more.xpath(".//@data-more-count"), [""]) # table_filter.js counts them again + self.assertEqual(doc.xpath("//*[@hidden]/button/@data-pill"), ["t8", "t9"]) + def test_an_owner_sees_only_the_tags_of_their_vms(self): doc = self.page(self.owner) self.assertEqual(doc.xpath("//div[@id='tag-pills']/button/@data-pill"), ["", "prod", "web"]) diff --git a/locale/tr/LC_MESSAGES/django.mo b/locale/tr/LC_MESSAGES/django.mo index c76d1ce6..222dd900 100644 Binary files a/locale/tr/LC_MESSAGES/django.mo and b/locale/tr/LC_MESSAGES/django.mo differ diff --git a/locale/tr/LC_MESSAGES/django.po b/locale/tr/LC_MESSAGES/django.po index 3cd953fb..7e1ae99a 100644 --- a/locale/tr/LC_MESSAGES/django.po +++ b/locale/tr/LC_MESSAGES/django.po @@ -11,7 +11,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-19 07:03+0000\n" +"POT-Creation-Date: 2026-11-19 08:03+0000\n" "PO-Revision-Date: 2020-11-04 12:00+0000\n" "Last-Translator: catborise , 2020\n" "Language-Team: Turkish (https://www.transifex.com/catborise/teams/110663/tr/)\n" @@ -1031,7 +1031,7 @@ msgstr "Başarısız" #: admin/templates/admin/tag_list.html:5 admin/templates/admin/tag_list.html:7 #: computes/forms.py:55 instances/templates/instances/settings_tab.html:925 -#: instances/views.py:244 templates/common/tag_filter.html:5 +#: instances/views.py:244 templates/common/tag_filter.html:6 #: templates/navbar.html:39 msgid "Tags" msgstr "Etiketler" @@ -9693,14 +9693,18 @@ msgstr "Yalnız bu etiketi göster" msgid "more tags" msgstr "diğer etiketler" -#: templates/common/tag_filter.html:7 +#: templates/common/tag_filter.html:8 msgid "All tags" msgstr "Tüm etiketler" -#: templates/common/tag_filter.html:10 +#: templates/common/tag_filter.html:11 msgid "No tag" msgstr "Etiketsiz" +#: templates/common/tag_filter.html:17 +msgid "More tags" +msgstr "Diğer etiketler" + #: templates/error.html:13 msgid "HTTP status" msgstr "HTTP durumu" diff --git a/locale/zh/LC_MESSAGES/django.mo b/locale/zh/LC_MESSAGES/django.mo index a96ff30e..30ba1ac0 100644 Binary files a/locale/zh/LC_MESSAGES/django.mo and b/locale/zh/LC_MESSAGES/django.mo differ diff --git a/locale/zh/LC_MESSAGES/django.po b/locale/zh/LC_MESSAGES/django.po index c76b5e32..d7be661f 100644 --- a/locale/zh/LC_MESSAGES/django.po +++ b/locale/zh/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-19 07:03+0000\n" +"POT-Creation-Date: 2026-11-19 08:03+0000\n" "PO-Revision-Date: \n" "Last-Translator: \n" "Language-Team: \n" @@ -1003,7 +1003,7 @@ msgstr "失败" #: admin/templates/admin/tag_list.html:5 admin/templates/admin/tag_list.html:7 #: computes/forms.py:55 instances/templates/instances/settings_tab.html:925 -#: instances/views.py:244 templates/common/tag_filter.html:5 +#: instances/views.py:244 templates/common/tag_filter.html:6 #: templates/navbar.html:39 msgid "Tags" msgstr "标签" @@ -9124,14 +9124,18 @@ msgstr "只显示此标签" msgid "more tags" msgstr "更多标签" -#: templates/common/tag_filter.html:7 +#: templates/common/tag_filter.html:8 msgid "All tags" msgstr "所有标签" -#: templates/common/tag_filter.html:10 +#: templates/common/tag_filter.html:11 msgid "No tag" msgstr "无标签" +#: templates/common/tag_filter.html:17 +msgid "More tags" +msgstr "更多标签" + #: templates/error.html:13 msgid "HTTP status" msgstr "HTTP 状态" diff --git a/static/css/webvirtcloud.css b/static/css/webvirtcloud.css index 1e8490c7..e55e4db3 100644 --- a/static/css/webvirtcloud.css +++ b/static/css/webvirtcloud.css @@ -321,6 +321,10 @@ td .tag-more { width: 0; min-width: 100%; } +/* the tag pills behind "…", once shown: in the row of the others */ +.tag-pills-more:not([hidden]) { + display: contents; +} /* a VM's or host's tags: smaller than other badges, so a row with several stays narrow */ .tag-badge { font-size: 0.65rem; diff --git a/static/js/table_filter.js b/static/js/table_filter.js index 8c38ae75..3b55276f 100644 --- a/static/js/table_filter.js +++ b/static/js/table_filter.js @@ -89,6 +89,14 @@ function filterTable(doc) { } showCounts(doc, "[data-count-state]", "countState", counts, state); showCounts(doc, "[data-count-tag]", "countTag", tagCounts, tag); + // "…" of the tag pills: how many behind it the search and the state leave; with none, or once + // it has shown them, it is hidden + doc.querySelectorAll("[data-more-count]").forEach(function (count) { + var button = count.parentElement, others = button.nextElementSibling; + var n = others.querySelectorAll("[data-pill]:not([hidden])").length; + count.textContent = n; + button.hidden = !others.hidden || n === 0; + }); var status = doc.getElementById("filter-status"); if (status) { status.textContent = status.dataset.text.replace("%(shown)s", shownVms).replace("%(all)s", vms); @@ -138,6 +146,12 @@ if (typeof module !== "undefined") { function pillRow(pills, input, which) { var all = pills.querySelectorAll("[data-pill]"); function choose(pill) { + // one behind "…" (kept, or chosen by a badge): the pills behind it show + var others = pill.parentElement.closest("[hidden]"); + if (others) { + others.hidden = false; + others.previousElementSibling.hidden = true; + } input.value = pill.dataset.pill; keep(input.value, which); all.forEach(function (other) { @@ -184,7 +198,7 @@ if (typeof module !== "undefined") { more.hidden = true; others.hidden = false; // the focus is not lost with the button: the first tag shown takes it, or the tags if they are not buttons - (others.querySelector("button") || others).focus(); + (others.querySelector("button:not([hidden])") || others).focus(); return; } var badge = event.target.closest("[data-tag-filter]"); diff --git a/templates/common/tag_filter.html b/templates/common/tag_filter.html index fe16bd6b..f3844919 100644 --- a/templates/common/tag_filter.html +++ b/templates/common/tag_filter.html @@ -1,6 +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, #} -{# each count follows the search and the state #} +{# each count follows the search and the state. The eight most used show, the others behind "…" (as a row's tags) #} {% with names=tags.0 untagged=tags.1 %}{% if names %} {% endif %}{% endwith %} diff --git a/templates/common/tag_pill.html b/templates/common/tag_pill.html new file mode 100644 index 00000000..df3f225d --- /dev/null +++ b/templates/common/tag_pill.html @@ -0,0 +1,4 @@ +{# one of tag_filter.html: the pill of name, with how many rows have it (table_filter.js counts them again) #} +