Feature: Add a group filter to the web console device list

Operators running several groups had to scan the whole fleet. The device
list now has a group filter: desktop renders the groups as chips in the
Total row (click to toggle any number), while touch devices get a compact
dropdown with checkbox rows plus Clear/Done. An empty selection means
"all groups"; multiple selections are OR-ed and combined with the search
box. Hosts without a group appear as "Ungrouped".

Groups are derived from the current device snapshot (no admin-only
get_groups call, so restricted users can filter too), and the selection
persists in localStorage for the browser. The offered list follows the
Offline toggle, while a selected group whose devices are temporarily
gone stays visible with count 0 instead of being silently dropped.
Logout clears the selection so the next account does not inherit it.
The Wall view keeps its own search and is unchanged.

Verified in headless Chrome at desktop and phone widths: multi-select,
counts, dropdown open/close/clear, stale-selection retention, search
focus, long names and persistence across reloads. go build and
go test ./web/ pass.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-09 16:00:58 +01:00
1 parent 2cbed0dcdf
commit 6fd6e9cbaa
1 file changed
+322 -1
+322 -1
View File
@@ -860,6 +860,9 @@
}
.stats-bar {
display: flex;
align-items: center;
flex-wrap: wrap;
position: relative;
gap: 20px;
margin-bottom: 20px;
padding: 12px 16px;
@@ -883,6 +886,137 @@
accent-color: #e94560;
cursor: pointer;
}
/* 分组过滤器:桌面端把各分组平铺在 Total 行(chip),手机端收进下拉面板。 */
.stats-bar .group-filter {
display: flex;
align-items: center;
gap: 6px;
flex-wrap: wrap;
min-width: 0;
}
.group-filter-label { color: #666; }
.group-chip {
display: inline-flex;
align-items: center;
gap: 5px;
max-width: 220px;
padding: 2px 10px;
border-radius: 999px;
border: 1px solid rgba(255, 255, 255, 0.14);
background: transparent;
color: #bbb;
font-size: 12px;
line-height: 1.6;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.group-chip:hover { border-color: rgba(233, 69, 96, 0.5); color: #fff; }
.group-chip.active {
background: #e94560;
border-color: #e94560;
color: #fff;
}
.group-chip-name {
min-width: 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.group-chip .group-chip-count { flex: 0 0 auto; font-size: 11px; opacity: 0.65; }
/* 手机端:分组下拉(面板里用按钮行,避免 label+checkbox 的重复触发) */
.group-dd { display: inline-flex; }
.group-dd-btn {
display: inline-flex;
align-items: center;
gap: 6px;
max-width: 55vw;
padding: 6px 12px;
border-radius: 6px;
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(0, 0, 0, 0.2);
color: #bbb;
font-size: 12px;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.group-dd-btn.active { border-color: #e94560; color: #e94560; }
.group-dd-title { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.group-dd-caret { font-size: 10px; opacity: 0.7; }
/* 面板锚在 Total 这一行的左边缘(.stats-bar 为定位祖先),而不是按钮本身:
按钮在窄屏上常靠右,按按钮定位会让面板探出屏幕右缘。 */
.group-dd-panel {
position: absolute;
top: calc(100% + 6px);
left: 16px;
right: 16px;
max-width: 320px;
z-index: 60;
max-height: 60vh;
overflow-y: auto;
padding: 6px;
border: 1px solid #0f3460;
border-radius: 8px;
background: #16213e;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
.group-dd-item {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
padding: 9px 8px;
border: none;
border-radius: 6px;
background: transparent;
color: #ddd;
font-size: 13px;
text-align: left;
cursor: pointer;
}
.group-dd-item:active { background: rgba(255, 255, 255, 0.06); }
.group-dd-item.active { color: #fff; }
.group-dd-check {
flex: 0 0 16px;
width: 16px;
height: 16px;
line-height: 16px;
text-align: center;
font-size: 12px;
border-radius: 4px;
border: 1px solid rgba(255, 255, 255, 0.25);
color: #fff;
}
.group-dd-item.active .group-dd-check { background: #e94560; border-color: #e94560; }
.group-dd-name {
flex: 1;
min-width: 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.group-dd-count { color: #888; font-size: 11px; }
.group-dd-footer {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 4px;
padding-top: 6px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.group-dd-clear,
.group-dd-done {
padding: 6px 10px;
border-radius: 6px;
border: 1px solid rgba(255, 255, 255, 0.14);
background: transparent;
color: #bbb;
font-size: 12px;
cursor: pointer;
}
.group-dd-clear:disabled { opacity: 0.4; cursor: default; }
.group-dd-done { border-color: rgba(233, 69, 96, 0.5); color: #e94560; }
#screen-page {
position: fixed;
@@ -1748,6 +1882,7 @@
<div id="stats-bar" class="stats-bar">
<div class="stat">Total: <strong id="stat-total">0</strong></div>
<div class="stat">Showing: <strong id="stat-showing">0</strong></div>
<div class="stat group-filter" id="group-filter"></div>
<label class="stat offline-toggle" id="offline-toggle"
title="Show devices that have connected before but are offline now">
<input type="checkbox" id="show-offline" onchange="onOfflineToggle(this.checked)">
@@ -2261,6 +2396,18 @@
try {
showOffline = localStorage.getItem(SHOW_OFFLINE_STORAGE_KEY) === '1';
} catch (e) { /* 隐私模式下 localStorage 可能不可用 */ }
// 分组过滤器:只存浏览器偏好(localStorage),服务端无感知。
// 分组键用设备上报的 group 原值,空字符串代表"未分组";空数组 = 不过滤。
const GROUP_FILTER_STORAGE_KEY = 'deviceGroupFilter';
let selectedGroups = [];
try {
const savedGroups = JSON.parse(localStorage.getItem(GROUP_FILTER_STORAGE_KEY) || '[]');
if (Array.isArray(savedGroups)) {
selectedGroups = savedGroups.filter(g => typeof g === 'string');
}
} catch (e) { /* 隐私模式 / 脏数据:退回"全部" */ }
let groupDropdownOpen = false;
let wall = {
active: false,
layout: isTouchDevice ? 4 : 9,
@@ -4366,6 +4513,171 @@
}
}
function groupKeyOf(d) { return d.group || ''; }
function groupLabel(key) { return key === '' ? 'Ungrouped' : key; }
// 分组清单按当前范围推导(未勾选 Offline 时只看在线设备),再并上
// 已选分组:设备掉线/分组暂时消失时,已选中的 chip 仍然可见、可取消,
// 不会把用户的过滤选择悄悄抹掉。命名分组在前,未分组垫底。
function groupKeys() {
const set = new Set();
for (const d of scopeDevices()) set.add(groupKeyOf(d));
for (const g of selectedGroups) set.add(g);
return Array.from(set).sort((a, b) => {
if (a === b) return 0;
if (a === '') return 1;
if (b === '') return -1;
return a.localeCompare(b);
});
}
function groupCounts() {
const counts = new Map();
for (const d of scopeDevices()) {
const k = groupKeyOf(d);
counts.set(k, (counts.get(k) || 0) + 1);
}
return counts;
}
function saveGroupFilter() {
try {
localStorage.setItem(GROUP_FILTER_STORAGE_KEY, JSON.stringify(selectedGroups));
} catch (e) { /* ignore */ }
}
function escapeAttr(text) {
return escapeHtml(text).replace(/"/g, '&quot;').replace(/'/g, '&#39;');
}
function renderGroupChips(keys, counts, sel) {
return '<span class="group-filter-label">Groups:</span>' + keys.map(k => {
const active = sel.has(k);
return '<button type="button" class="group-chip' + (active ? ' active' : '') + '"' +
' data-group="' + escapeAttr(k) + '"' +
' title="' + escapeAttr(groupLabel(k)) + '">' +
'<span class="group-chip-name">' + escapeHtml(groupLabel(k)) + '</span>' +
'<span class="group-chip-count">' + (counts.get(k) || 0) + '</span>' +
'</button>';
}).join('');
}
function renderGroupDropdown(keys, counts, sel) {
let title;
if (!selectedGroups.length) title = 'All groups';
else if (selectedGroups.length === 1) title = groupLabel(selectedGroups[0]);
else title = selectedGroups.length + ' groups';
let html = '<div class="group-dd">' +
'<button type="button" class="group-dd-btn' + (selectedGroups.length ? ' active' : '') + '"' +
' data-dd-toggle="1" aria-haspopup="true" aria-expanded="' + (groupDropdownOpen ? 'true' : 'false') + '">' +
'<span class="group-dd-title">' + escapeHtml(title) + '</span>' +
'<span class="group-dd-caret">&#9662;</span>' +
'</button>';
if (groupDropdownOpen) {
html += '<div class="group-dd-panel">' + keys.map(k => {
const active = sel.has(k);
return '<button type="button" class="group-dd-item' + (active ? ' active' : '') + '"' +
' data-group="' + escapeAttr(k) + '">' +
'<span class="group-dd-check">' + (active ? '&#10003;' : '') + '</span>' +
'<span class="group-dd-name">' + escapeHtml(groupLabel(k)) + '</span>' +
'<span class="group-dd-count">' + (counts.get(k) || 0) + '</span>' +
'</button>';
}).join('') +
'<div class="group-dd-footer">' +
'<button type="button" class="group-dd-clear" data-group-clear="1"' +
(selectedGroups.length ? '' : ' disabled') + '>Clear</button>' +
'<button type="button" class="group-dd-done" data-dd-toggle="1">Done</button>' +
'</div>' +
'</div>';
}
return html + '</div>';
}
// 内容没变就不重建 DOM:搜索框每敲一个字都会走到这里,重建会丢掉
// 分组按钮上的键盘焦点,也白白重排一遍。
let groupFilterHtml = null;
function renderGroupFilter() {
const el = document.getElementById('group-filter');
if (!el) return;
const keys = groupKeys();
// 没有设备,或只有"未分组"一类且没有已选分组时,过滤器没有意义。
if (!devices.length || keys.length === 0 || (keys.length === 1 && keys[0] === '')) {
el.style.display = 'none';
if (groupFilterHtml !== null) {
el.innerHTML = '';
groupFilterHtml = null;
}
groupDropdownOpen = false;
return;
}
el.style.display = '';
const counts = groupCounts();
const sel = new Set(selectedGroups);
const html = isTouchDevice
? renderGroupDropdown(keys, counts, sel)
: renderGroupChips(keys, counts, sel);
if (html !== groupFilterHtml) {
el.innerHTML = html;
groupFilterHtml = html;
}
}
function onGroupFilterClick(e) {
if (e.target.closest('[data-dd-toggle]')) {
// 阻止冒泡:重渲染会让 e.target 脱离文档,document 上的
// "点外面关闭"监听会误判为外部点击。
e.stopPropagation();
groupDropdownOpen = !groupDropdownOpen;
renderGroupFilter();
return;
}
if (e.target.closest('[data-group-clear]')) {
e.stopPropagation();
clearGroupFilter();
return;
}
const item = e.target.closest('[data-group]');
if (!item) return;
e.stopPropagation();
toggleGroup(item.getAttribute('data-group'));
}
function toggleGroup(key) {
const hadFocus = !!(document.activeElement && document.activeElement.closest('#group-filter'));
const i = selectedGroups.indexOf(key);
if (i >= 0) selectedGroups.splice(i, 1);
else selectedGroups.push(key);
saveGroupFilter();
currentPage = 1;
renderDeviceList();
// 重渲染换掉了按钮节点:把键盘焦点还给同一个分组。
if (hadFocus) {
const btn = Array.from(document.querySelectorAll('#group-filter [data-group]'))
.find(b => b.getAttribute('data-group') === key);
if (btn) btn.focus({ preventScroll: true });
}
}
function clearGroupFilter() {
if (!selectedGroups.length) return;
selectedGroups = [];
saveGroupFilter();
currentPage = 1;
renderDeviceList();
}
document.getElementById('group-filter').addEventListener('click', onGroupFilterClick);
// 手机端下拉:点面板外任意位置收起(面板内点击已被上面的处理器 stopPropagation)。
document.addEventListener('click', (e) => {
if (!groupDropdownOpen) return;
if (e.target.closest('#group-filter')) return;
groupDropdownOpen = false;
renderGroupFilter();
});
function scopeDevices() {
// Total / Showing 都基于当前范围:未勾选 Offline 时只统计在线设备。
if (showOffline) return devices;
@@ -4374,6 +4686,10 @@
function getFilteredDevices() {
let filtered = scopeDevices();
if (selectedGroups.length) {
const sel = new Set(selectedGroups);
filtered = filtered.filter(d => sel.has(groupKeyOf(d)));
}
if (searchQuery) {
const q = searchQuery.toLowerCase();
filtered = filtered.filter(d =>
@@ -4511,6 +4827,7 @@
}
function renderDeviceList() {
renderGroupFilter();
const filtered = getFilteredDevices();
const ps = getPageSize();
const totalPages = Math.ceil(filtered.length / ps) || 1;
@@ -4520,7 +4837,7 @@
const pageDevices = filtered.slice(start, start + ps);
// Update stats. Total is the current scope (online-only unless the
// Offline toggle is on); Showing is after the search filter.
// Offline toggle is on); Showing is after the group + search filters.
const scope = scopeDevices();
document.getElementById('device-count').textContent = scope.length;
document.getElementById('stat-total').textContent = scope.length;
@@ -4721,6 +5038,10 @@
currentUserRole = 'viewer';
localStorage.removeItem('token');
localStorage.removeItem('role');
// 分组过滤器是按账号的临时偏好:换账号不应继承上一个账号的选择。
selectedGroups = [];
groupDropdownOpen = false;
saveGroupFilter();
devices = [];
showPage('login-page');
// Hide admin-only buttons