Feature: fold device-card actions into a "..." menu

Every card now shows a single ellipsis button no matter how many actions
apply; the same terminal/bind/transfer/delete actions live in a popup menu,
each with a one-line description of what it does.

The menu is attached to body and fixed-positioned because the card clips its
own overflow. It right-aligns to the button, flips above when the button sits
near the bottom of the viewport, and closes on outside click, Esc, scroll,
resize or list re-render. Entries are built per click from the live device,
so permission and online/transfer state cannot go stale. Grid and list views
share the flow; the list view centers the button vertically and both views
only need 56px of clearance for it.

Verified in a browser with a copy of the card markup: open/close paths, item
callbacks, bottom-edge flip and both view modes.
This commit is contained in:
yuanyuanxiang committed 2026-11-20 13:51:32 +01:00
1 parent 63c7cdf588
commit 7eebd3bd6c
1 file changed
+188 -64
+188 -64
View File
@@ -657,14 +657,13 @@
max-width: 240px;
flex-shrink: 1;
}
/* 卡片操作图标(终端 / 删除):右上角,独立于卡片本体点击事件
(onclick stopPropagation)。List 视图卡片是一行,改为垂直居中。 */
/* 卡片操作:右上角只留一个「…」按钮,点开才弹出菜单(终端 / 绑定 /
转移 / 删除),菜单项带简短功能说明。独立于卡片本体点击事件
(onclick stopPropagation)。List 视图卡片是一行,按钮垂直居中。 */
.device-card .card-actions {
position: absolute;
top: 12px;
right: 12px;
display: flex;
gap: 6px;
z-index: 1;
}
.device-card .card-icon-btn {
@@ -682,19 +681,57 @@
}
.device-card .card-icon-btn:hover { background: rgba(255,255,255,0.14); color: #fff; }
.device-card .card-icon-btn:active { transform: scale(0.92); }
.device-card .card-term-btn:hover { background: rgba(76, 175, 80, 0.25); color: #4caf50; }
.device-card .card-del-btn:hover { background: rgba(231, 76, 60, 0.25); color: #e74c3c; }
.device-card .card-bind-btn:hover { background: rgba(230, 126, 34, 0.25); color: #e67e22; }
.device-card .card-transfer-btn:hover { background: rgba(214, 137, 16, 0.25); color: #d68910; }
.device-card h3 { padding-right: 124px; } /* 给右上角操作图标让位 */
/* 4 个操作图标(终端/绑定/转移/删除)= 4×32 + 3×6 + 右边距 12,比默认的
3 个宽 38px;不让位标题会钻到图标底下。list-view 的 h3 padding-right:0
选择器权重更高,不受影响。 */
.device-card.many-actions h3 { padding-right: 162px; }
.device-card .card-more-btn[aria-expanded="true"] { background: rgba(255,255,255,0.14); color: #fff; }
.device-card h3 { padding-right: 56px; } /* 给右上角「…」按钮让位 */
.device-grid.list-view .device-card .card-actions {
top: 50%;
transform: translateY(-50%);
}
/* 「…」的弹出菜单:fixed 挂 body(卡片 overflow:hidden 会裁掉菜单),
JS 按按钮位置定位并做视口翻转/夹取。 */
.card-menu {
position: fixed;
z-index: 80;
width: 264px;
max-width: calc(100vw - 16px);
padding: 6px;
border: 1px solid #0f3460;
border-radius: 8px;
background: #16213e;
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
}
.card-menu-item {
display: flex;
align-items: flex-start;
gap: 10px;
width: 100%;
padding: 8px;
border: none;
border-radius: 6px;
background: transparent;
color: #ddd;
text-align: left;
cursor: pointer;
}
.card-menu-item:hover, .card-menu-item:focus-visible { background: rgba(255,255,255,0.07); }
.card-menu-item:active { background: rgba(255,255,255,0.12); }
.card-menu-icon {
flex: 0 0 auto;
display: flex;
align-items: center;
justify-content: center;
width: 18px;
height: 18px;
margin-top: 1px;
}
.card-menu-item.term .card-menu-icon { color: #4caf50; }
.card-menu-item.bind .card-menu-icon { color: #e67e22; }
.card-menu-item.transfer .card-menu-icon { color: #d68910; }
.card-menu-item.del .card-menu-icon { color: #e74c3c; }
.card-menu-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.card-menu-label { font-size: 13px; font-weight: 500; color: #eee; }
.card-menu-item.del .card-menu-label { color: #e74c3c; }
.card-menu-desc { font-size: 11px; color: #8a93a8; line-height: 1.35; }
/* ====== 终端页面 ====== */
#term-page {
@@ -824,7 +861,7 @@
display: flex;
align-items: center;
padding: 12px 16px;
padding-right: 100px; /* 给垂直居中的操作图标列让位 */
padding-right: 56px; /* 给垂直居中的「…」按钮让位 */
gap: 20px;
}
.device-grid.list-view .device-card::before { display: none; }
@@ -5071,7 +5108,130 @@
}
}
// ---- 卡片「…」操作菜单 ----
// 无论有几个操作,卡片上只显示一个「…」;点开弹出的菜单里才是终端/绑定/
// 转移/删除,每项带一句功能说明。菜单项在点击时按设备实时生成,权限和
// 在线状态不会因为列表渲染后变化而过期。
let cardMenuEl = null;
let cardMenuBtn = null;
const cardMenuSvg = {
term: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<polyline points="4 17 10 11 4 5"/><line x1="12" y1="19" x2="20" y2="19"/></svg>',
bind: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>',
transfer: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/><polyline points="10 17 15 12 10 7"/><line x1="15" y1="12" x2="3" y2="12"/></svg>',
del: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<polyline points="3 6 5 6 21 6"/><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/>' +
'<path d="M10 11v6"/><path d="M14 11v6"/><path d="M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"/></svg>'
};
function hasCardActions(d) {
if (d.online !== false) return true; // 在线设备至少有终端
return canBindDevices() || canDeleteDevices();
}
function cardMenuEntries(d) {
const offline = d.online === false;
const out = [];
if (!offline) {
out.push({ kind: 'term', label: 'Terminal', desc: 'Open a remote shell',
run: () => openTerminal(d.id) });
}
if (canBindDevices()) {
out.push({ kind: 'bind', label: 'Bind owner', desc: 'Assign this host to a user',
run: () => openBindModal(d.id) });
}
if (canTransferDevices() && !offline && !d.transferTo) {
out.push({ kind: 'transfer', label: 'Transfer host', desc: 'Move the host to another master',
run: () => openTransferModal(d.id) });
}
if (canDeleteDevices()) {
out.push({
kind: 'del',
label: offline ? 'Delete record' : 'Uninstall host',
desc: offline ? 'Remove this offline record' : 'Remove the client from this host',
run: () => askDeleteDevice(d.id)
});
}
return out;
}
function closeCardMenu() {
if (cardMenuEl) {
cardMenuEl.remove();
cardMenuEl = null;
}
if (cardMenuBtn) {
cardMenuBtn.setAttribute('aria-expanded', 'false');
cardMenuBtn = null;
}
}
function toggleCardMenu(btn, id) {
if (cardMenuBtn === btn) { // 再点一次同一个按钮 = 收起
closeCardMenu();
return;
}
const d = devices.find(x => String(x.id) === String(id));
const entries = d ? cardMenuEntries(d) : [];
closeCardMenu();
if (!entries.length) return;
const menu = document.createElement('div');
menu.className = 'card-menu';
menu.setAttribute('role', 'menu');
entries.forEach(en => {
const item = document.createElement('button');
item.type = 'button';
item.className = 'card-menu-item ' + en.kind;
item.setAttribute('role', 'menuitem');
item.innerHTML =
'<span class="card-menu-icon">' + cardMenuSvg[en.kind] + '</span>' +
'<span class="card-menu-text">' +
'<span class="card-menu-label">' + escapeHtml(en.label) + '</span>' +
'<span class="card-menu-desc">' + escapeHtml(en.desc) + '</span>' +
'</span>';
item.addEventListener('click', (e) => {
e.stopPropagation();
closeCardMenu();
en.run();
});
menu.appendChild(item);
});
document.body.appendChild(menu);
// 右对齐按钮,菜单默认向下;贴底时向上翻,左右夹在视口内。
const r = btn.getBoundingClientRect();
const mw = menu.offsetWidth;
const mh = menu.offsetHeight;
let left = r.right - mw;
let top = r.bottom + 6;
if (top + mh > window.innerHeight - 8) top = r.top - mh - 6;
if (top < 8) top = 8;
left = Math.max(8, Math.min(left, window.innerWidth - mw - 8));
menu.style.left = left + 'px';
menu.style.top = top + 'px';
cardMenuEl = menu;
cardMenuBtn = btn;
btn.setAttribute('aria-expanded', 'true');
}
document.addEventListener('click', (e) => {
if (!cardMenuEl) return;
if (cardMenuEl.contains(e.target)) return;
if (e.target.closest && e.target.closest('.card-more-btn')) return; // 切换由按钮自己处理
closeCardMenu();
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') closeCardMenu();
});
window.addEventListener('scroll', closeCardMenu, true); // 滚动后 fixed 菜单会跟丢锚点
window.addEventListener('resize', closeCardMenu);
function renderDeviceList() {
closeCardMenu();
renderGroupFilter();
const filtered = getFilteredDevices();
const ps = getPageSize();
@@ -5100,56 +5260,20 @@
const rtt = d.rtt || '-';
const ver = d.version || '-';
const activeWin = d.activeWindow || '';
// 操作图标:在线卡片有终端;删除对所有卡片开放(离线=删记录,
// 在线=卸载主机),仅管理员可用。Wall 模式不提供删除。
const actions = [];
if (!offline) {
actions.push(
'<button class="card-icon-btn card-term-btn" title="Open Terminal" aria-label="Terminal" ' +
'onclick="event.stopPropagation();openTerminal(\'' + jsAttr(d.id) + '\')">' +
'<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<polyline points="4 17 10 11 4 5"/><line x1="12" y1="19" x2="20" y2="19"/>' +
'</svg>' +
'</button>');
}
if (canBindDevices()) {
actions.push(
'<button class="card-icon-btn card-bind-btn" title="Bind owner" aria-label="Bind owner" ' +
'onclick="event.stopPropagation();openBindModal(\'' + jsAttr(d.id) + '\')">' +
'<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<rect x="3" y="11" width="18" height="11" rx="2"/>' +
'<path d="M7 11V7a5 5 0 0 1 10 0v4"/>' +
'</svg>' +
'</button>');
}
if (canTransferDevices() && !offline && !d.transferTo) {
actions.push(
'<button class="card-icon-btn card-transfer-btn" title="Transfer host" aria-label="Transfer" ' +
'onclick="event.stopPropagation();openTransferModal(\'' + jsAttr(d.id) + '\')">' +
'<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/>' +
'<polyline points="10 17 15 12 10 7"/><line x1="15" y1="12" x2="3" y2="12"/>' +
'</svg>' +
'</button>');
}
if (canDeleteDevices()) {
actions.push(
'<button class="card-icon-btn card-del-btn" title="' +
(offline ? 'Delete record' : 'Uninstall host') + '" aria-label="Delete" ' +
'onclick="event.stopPropagation();askDeleteDevice(\'' + jsAttr(d.id) + '\')">' +
'<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<polyline points="3 6 5 6 21 6"/>' +
'<path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/>' +
'<path d="M10 11v6"/><path d="M14 11v6"/>' +
'<path d="M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"/>' +
'</svg>' +
'</button>');
}
const actionsHtml = actions.length
? '<div class="card-actions">' + actions.join('') + '</div>' : '';
// 4 个图标时标题要让出更多空间(见 .many-actions 的注释)。
const manyActions = actions.length >= 4 ? ' many-actions' : '';
return '<div class="device-card' + (offline ? ' offline' : '') + manyActions + '"' +
// 操作全部收进右上角的「…」菜单(终端 / 绑定 / 转移 / 删除),
// 具体条目见 cardMenuEntries。这里只决定按钮是否出现。
const actionsHtml = hasCardActions(d)
? '<div class="card-actions">' +
'<button class="card-icon-btn card-more-btn" title="Actions" aria-label="More actions" ' +
'aria-haspopup="menu" aria-expanded="false" ' +
'onclick="event.stopPropagation();toggleCardMenu(this, \'' + jsAttr(d.id) + '\')">' +
'<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor">' +
'<circle cx="5" cy="12" r="1.7"/><circle cx="12" cy="12" r="1.7"/><circle cx="19" cy="12" r="1.7"/>' +
'</svg>' +
'</button>' +
'</div>'
: '';
return '<div class="device-card' + (offline ? ' offline' : '') + '"' +
(offline ? '' : ' onclick="connectDevice(\'' + jsAttr(d.id) + '\')"') + '>' +
actionsHtml +
'<h3>' + escapeHtml(displayName(d)) + '</h3>' +