1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

feat(tags): the eight most used tag pills, the others behind "…"

With dozens of tags the pills took rows of the list's top (six rows for
forty tags, the table below a phone's screen). The pills are now by how many
rows have the tag, the eight most used shown and the others behind "… More
tags N", which shows them as a row's "…" does; its count follows the
search and the state, and it hides when none is left behind it. A pill
behind it chosen (remembered, or by a row's badge) shows them.
This commit is contained in:
catborise 2026-11-19 11:05:40 +03:00
parent 7018f57477
commit a5d0dbfe66
11 changed files with 97 additions and 19 deletions

View file

@ -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):

View file

@ -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"};

View file

@ -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"])

Binary file not shown.

View file

@ -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 <muhammetalisag@gmail.com>, 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"

Binary file not shown.

View file

@ -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 状态"

View file

@ -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;

View file

@ -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]");

View file

@ -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 %}
<input type="hidden" id="tag-filter" value="">
<nav class="mb-3" aria-label="{% trans "Tags" %}">
@ -11,11 +11,13 @@
{% 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" data-count-tag="{{ name }}">{{ count }}</span>
{% for name, count in names|slice:":8" %}{% include 'common/tag_pill.html' %}{% endfor %}
{% with others=names|slice:"8:" %}{% if others %}
<button type="button" class="nav-link py-1 px-2" data-show-tags>
… {% trans "More tags" %} <span class="badge text-bg-light ms-1" data-more-count>{{ others|length }}</span>
</button>
{% endfor %}
<span class="tag-pills-more" hidden>{% for name, count in others %}{% include 'common/tag_pill.html' %}{% endfor %}</span>
{% endif %}{% endwith %}
</div>
</nav>
{% endif %}{% endwith %}

View file

@ -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) #}
<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" data-count-tag="{{ name }}">{{ count }}</span>
</button>