mirror of
https://github.com/UNITRONIX/BetterDesk.git
synced 2026-11-07 03:31:52 +00:00
Add desktop layout overlay & operator session API
Introduce a dedicated desktop layout overlay and auto-arrange features plus operator session endpoints. UI changes: add BD_icon_small.png, update .gitignore, add new overlay styles in desktop-mode.css, and rename widget snap CSS classes in desktop-widgets.css to avoid conflicts. JS updates: implement open/close layout overlay, autoArrangeWindows, shouldReserveTaskbarSpace, improved taskbar hide/toggle logic, and expose layout APIs from DesktopMode; widgets will call DesktopMode.openLayoutOverlay when available and otherwise use the widget picker. Backend changes: add requireOperatorRole middleware, helpers (normalizeSessionAction, toTimestamp, buildSessionHistory), enforce operator/admin checks on device/help-request endpoints, and add POST /api/bd/operator/sessions and GET /api/bd/operator/sessions to record and retrieve operator session history with input validation and duration computation.
This commit is contained in:
@@ -201,3 +201,5 @@ web-nodejs/wallpapers/
|
||||
|
||||
# --- Task tracking (local only) ---
|
||||
tasks/todo.md
|
||||
t
|
||||
k-server
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 702 KiB |
@@ -816,6 +816,175 @@ body.embed-mode .main-content {
|
||||
border-color: rgba(0, 100, 210, 0.4);
|
||||
}
|
||||
|
||||
/* ============ Layout Overlay (Desktop Button) ============ */
|
||||
|
||||
.snap-layout-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 99995;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: rgba(5, 8, 12, 0.42);
|
||||
backdrop-filter: blur(10px);
|
||||
-webkit-backdrop-filter: blur(10px);
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.snap-layout-picker--overlay {
|
||||
min-width: 420px;
|
||||
max-width: 560px;
|
||||
width: min(100%, 560px);
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.snap-layout-picker--overlay.visible {
|
||||
opacity: 1;
|
||||
transform: translateY(0) scale(1);
|
||||
}
|
||||
|
||||
.snap-layout-title {
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
color: rgba(255, 255, 255, 0.92);
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.snap-layout-help {
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
color: rgba(255, 255, 255, 0.64);
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.snap-layout-options {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
gap: 12px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.snap-layout-option {
|
||||
border: 1px solid rgba(255, 255, 255, 0.08);
|
||||
border-radius: 10px;
|
||||
background: rgba(255, 255, 255, 0.04);
|
||||
padding: 10px;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.snap-layout-option:hover {
|
||||
background: rgba(88, 166, 255, 0.12);
|
||||
border-color: rgba(88, 166, 255, 0.34);
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.snap-layout-preview {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
aspect-ratio: 16 / 10;
|
||||
border-radius: 6px;
|
||||
overflow: hidden;
|
||||
background: rgba(0, 0, 0, 0.18);
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.snap-zone-preview {
|
||||
position: absolute;
|
||||
margin: 2px;
|
||||
border-radius: 4px;
|
||||
background: rgba(88, 166, 255, 0.2);
|
||||
border: 1px solid rgba(88, 166, 255, 0.38);
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.snap-layout-label {
|
||||
font-size: 12px;
|
||||
color: rgba(255, 255, 255, 0.72);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.snap-layout-empty {
|
||||
padding: 18px;
|
||||
border: 1px dashed rgba(255, 255, 255, 0.16);
|
||||
border-radius: 10px;
|
||||
color: rgba(255, 255, 255, 0.68);
|
||||
text-align: center;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.snap-auto-arrange-btn {
|
||||
width: 100%;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 10px;
|
||||
padding: 12px 16px;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: rgba(255, 255, 255, 0.82);
|
||||
cursor: pointer;
|
||||
transition: background 0.15s ease, border-color 0.15s ease;
|
||||
}
|
||||
|
||||
.snap-auto-arrange-btn:hover {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-color: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.desktop-theme-light .snap-layout-overlay {
|
||||
background: rgba(232, 237, 243, 0.55);
|
||||
}
|
||||
|
||||
.desktop-theme-light .snap-layout-title {
|
||||
color: rgba(0, 0, 0, 0.86);
|
||||
}
|
||||
|
||||
.desktop-theme-light .snap-layout-help,
|
||||
.desktop-theme-light .snap-layout-label,
|
||||
.desktop-theme-light .snap-layout-empty {
|
||||
color: rgba(0, 0, 0, 0.64);
|
||||
}
|
||||
|
||||
.desktop-theme-light .snap-layout-option {
|
||||
background: rgba(0, 0, 0, 0.03);
|
||||
border-color: rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.desktop-theme-light .snap-layout-option:hover {
|
||||
background: rgba(0, 100, 210, 0.08);
|
||||
border-color: rgba(0, 100, 210, 0.26);
|
||||
}
|
||||
|
||||
.desktop-theme-light .snap-layout-preview {
|
||||
background: rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
.desktop-theme-light .snap-zone-preview {
|
||||
background: rgba(0, 100, 210, 0.14);
|
||||
border-color: rgba(0, 100, 210, 0.3);
|
||||
}
|
||||
|
||||
.desktop-theme-light .snap-auto-arrange-btn {
|
||||
background: rgba(0, 0, 0, 0.04);
|
||||
border-color: rgba(0, 0, 0, 0.1);
|
||||
color: rgba(0, 0, 0, 0.78);
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.snap-layout-picker--overlay {
|
||||
min-width: 0;
|
||||
width: 100%;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.snap-layout-options {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
/* ============ Draggable Zone Borders ============ */
|
||||
|
||||
.zone-divider {
|
||||
|
||||
@@ -2434,7 +2434,7 @@
|
||||
|
||||
/* ─── Phase 42: Snap Layout Overlay ─────────────────────────────────────────── */
|
||||
|
||||
.snap-layout-overlay {
|
||||
.widget-snap-layout-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 9999;
|
||||
@@ -2451,7 +2451,7 @@
|
||||
to { opacity: 1; }
|
||||
}
|
||||
|
||||
.snap-layout-picker {
|
||||
.widget-snap-layout-picker {
|
||||
background: rgba(30, 30, 46, 0.95);
|
||||
backdrop-filter: blur(20px);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
@@ -2462,7 +2462,7 @@
|
||||
box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.snap-layout-title {
|
||||
.widget-snap-layout-title {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: rgba(255, 255, 255, 0.9);
|
||||
@@ -2470,14 +2470,14 @@
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.snap-layout-options {
|
||||
.widget-snap-layout-options {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
gap: 12px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.snap-layout-option {
|
||||
.widget-snap-layout-option {
|
||||
cursor: pointer;
|
||||
padding: 8px;
|
||||
border-radius: 10px;
|
||||
@@ -2487,13 +2487,13 @@
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.snap-layout-option:hover {
|
||||
.widget-snap-layout-option:hover {
|
||||
background: rgba(88, 166, 255, 0.12);
|
||||
border-color: rgba(88, 166, 255, 0.3);
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.snap-layout-preview {
|
||||
.widget-snap-layout-preview {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
aspect-ratio: 16 / 10;
|
||||
@@ -2503,7 +2503,7 @@
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.snap-zone-preview {
|
||||
.widget-snap-zone-preview {
|
||||
position: absolute;
|
||||
background: rgba(88, 166, 255, 0.2);
|
||||
border: 1px solid rgba(88, 166, 255, 0.4);
|
||||
@@ -2513,7 +2513,7 @@
|
||||
margin: 2px;
|
||||
}
|
||||
|
||||
.snap-layout-label {
|
||||
.widget-snap-layout-label {
|
||||
font-size: 11px;
|
||||
color: rgba(255, 255, 255, 0.55);
|
||||
white-space: nowrap;
|
||||
@@ -2521,7 +2521,7 @@
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.snap-auto-arrange-btn {
|
||||
.widget-snap-auto-arrange-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
@@ -2537,41 +2537,41 @@
|
||||
transition: background 0.15s, border-color 0.15s;
|
||||
}
|
||||
|
||||
.snap-auto-arrange-btn:hover {
|
||||
.widget-snap-auto-arrange-btn:hover {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-color: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.snap-auto-arrange-btn .material-icons {
|
||||
.widget-snap-auto-arrange-btn .material-icons {
|
||||
font-size: 18px;
|
||||
color: rgba(88, 166, 255, 0.8);
|
||||
}
|
||||
|
||||
/* Light theme adjustments */
|
||||
[data-desktop-theme="light"] .snap-layout-picker {
|
||||
[data-desktop-theme="light"] .widget-snap-layout-picker {
|
||||
background: rgba(255, 255, 255, 0.95);
|
||||
border-color: rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
[data-desktop-theme="light"] .snap-layout-title {
|
||||
[data-desktop-theme="light"] .widget-snap-layout-title {
|
||||
color: rgba(0, 0, 0, 0.85);
|
||||
}
|
||||
|
||||
[data-desktop-theme="light"] .snap-layout-option {
|
||||
[data-desktop-theme="light"] .widget-snap-layout-option {
|
||||
border-color: rgba(0, 0, 0, 0.08);
|
||||
background: rgba(0, 0, 0, 0.02);
|
||||
}
|
||||
|
||||
[data-desktop-theme="light"] .snap-layout-option:hover {
|
||||
[data-desktop-theme="light"] .widget-snap-layout-option:hover {
|
||||
background: rgba(88, 166, 255, 0.08);
|
||||
border-color: rgba(88, 166, 255, 0.3);
|
||||
}
|
||||
|
||||
[data-desktop-theme="light"] .snap-layout-label {
|
||||
[data-desktop-theme="light"] .widget-snap-layout-label {
|
||||
color: rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
[data-desktop-theme="light"] .snap-auto-arrange-btn {
|
||||
[data-desktop-theme="light"] .widget-snap-auto-arrange-btn {
|
||||
background: rgba(0, 0, 0, 0.04);
|
||||
border-color: rgba(0, 0, 0, 0.1);
|
||||
color: rgba(0, 0, 0, 0.7);
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
let _snapPreview = null; // DOM element for blue snap zone preview
|
||||
let _snapTarget = null; // {zone:'left'|'right'|'top-left'|...} active snap target
|
||||
let _snapPickerEl = null; // Snap layout picker DOM (hover over maximize btn)
|
||||
let _layoutOverlayEl = null; // Dedicated layout overlay opened from desktop sidebar
|
||||
let _snapPickerTimeout = null;
|
||||
let _shakeOrigins = []; // For Aero Shake detection
|
||||
|
||||
@@ -434,11 +435,15 @@
|
||||
var taskbar = document.getElementById('desktop-taskbar');
|
||||
if (!taskbar) return;
|
||||
|
||||
// Show taskbar only in windows mode or when there are open windows
|
||||
if (currentMode === 'widgets' && windows.size === 0) {
|
||||
taskbar.classList.add('taskbar-hidden');
|
||||
} else {
|
||||
taskbar.classList.remove('taskbar-hidden');
|
||||
var shouldHide = windows.size === 0;
|
||||
var wasHidden = taskbar.classList.contains('taskbar-hidden');
|
||||
|
||||
taskbar.classList.toggle('taskbar-hidden', shouldHide);
|
||||
|
||||
if (wasHidden !== shouldHide) {
|
||||
requestAnimationFrame(function() {
|
||||
if (active) clampAllWindows();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1217,6 +1222,129 @@
|
||||
document.removeEventListener('click', _onSnapPickerOutsideClick);
|
||||
}
|
||||
|
||||
function openLayoutOverlay() {
|
||||
if (_layoutOverlayEl) {
|
||||
closeLayoutOverlay();
|
||||
return;
|
||||
}
|
||||
|
||||
var visibleWindows = [];
|
||||
windows.forEach(function(w) {
|
||||
if (!w.minimized) visibleWindows.push(w);
|
||||
});
|
||||
|
||||
_layoutOverlayEl = document.createElement('div');
|
||||
_layoutOverlayEl.className = 'snap-layout-overlay';
|
||||
|
||||
var html = '<div class="snap-layout-picker snap-layout-picker--overlay">' +
|
||||
'<div class="snap-layout-title">' + escapeHtml((typeof _ === 'function' ? _('desktop.label_snap_layout') : 'Window Layout')) + '</div>' +
|
||||
'<div class="snap-layout-help">' + escapeHtml('Arrange open application windows. Widgets stay in place.') + '</div>';
|
||||
|
||||
if (visibleWindows.length > 0) {
|
||||
html += '<div class="snap-layout-options">';
|
||||
SNAP_LAYOUTS.forEach(function(layout) {
|
||||
html += '<button class="snap-layout-option" data-key="' + layout.key + '" title="' + escapeAttr(layout.label) + '">';
|
||||
html += '<div class="snap-layout-preview">';
|
||||
layout.zones.forEach(function(zone) {
|
||||
html += '<div class="snap-zone-preview" style="left:' + (zone.x * 100) + '%;top:' + (zone.y * 100) + '%;width:' + (zone.w * 100) + '%;height:' + (zone.h * 100) + '%"></div>';
|
||||
});
|
||||
html += '</div>' +
|
||||
'<div class="snap-layout-label">' + escapeHtml(layout.label) + '</div>' +
|
||||
'</button>';
|
||||
});
|
||||
html += '</div>' +
|
||||
'<button class="snap-auto-arrange-btn" data-action="auto-arrange"><span class="material-icons">grid_view</span>' + escapeHtml('Tile Open Windows') + '</button>';
|
||||
} else {
|
||||
html += '<div class="snap-layout-empty">' + escapeHtml('Open at least one app window to use multi-window layouts.') + '</div>';
|
||||
}
|
||||
|
||||
html += '</div>';
|
||||
_layoutOverlayEl.innerHTML = html;
|
||||
|
||||
_layoutOverlayEl.addEventListener('click', function(e) {
|
||||
if (e.target === _layoutOverlayEl) closeLayoutOverlay();
|
||||
});
|
||||
|
||||
if (visibleWindows.length > 0) {
|
||||
_layoutOverlayEl.querySelectorAll('.snap-layout-option').forEach(function(option) {
|
||||
option.addEventListener('click', function(e) {
|
||||
e.stopPropagation();
|
||||
var primaryWinId = focusedWindowId || (visibleWindows[0] && visibleWindows[0].id) || null;
|
||||
applySnapLayoutToWindows(option.getAttribute('data-key'), primaryWinId);
|
||||
closeLayoutOverlay();
|
||||
});
|
||||
});
|
||||
|
||||
var autoArrangeBtn = _layoutOverlayEl.querySelector('[data-action="auto-arrange"]');
|
||||
if (autoArrangeBtn) {
|
||||
autoArrangeBtn.addEventListener('click', function(e) {
|
||||
e.stopPropagation();
|
||||
autoArrangeWindows();
|
||||
closeLayoutOverlay();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
document.body.appendChild(_layoutOverlayEl);
|
||||
requestAnimationFrame(function() {
|
||||
var picker = _layoutOverlayEl && _layoutOverlayEl.querySelector('.snap-layout-picker--overlay');
|
||||
if (picker) picker.classList.add('visible');
|
||||
});
|
||||
}
|
||||
|
||||
function closeLayoutOverlay() {
|
||||
if (_layoutOverlayEl && _layoutOverlayEl.parentNode) {
|
||||
_layoutOverlayEl.parentNode.removeChild(_layoutOverlayEl);
|
||||
}
|
||||
_layoutOverlayEl = null;
|
||||
}
|
||||
|
||||
function autoArrangeWindows() {
|
||||
var visibleWindows = [];
|
||||
windows.forEach(function(w) {
|
||||
if (!w.minimized) visibleWindows.push(w);
|
||||
});
|
||||
if (!visibleWindows.length) return;
|
||||
|
||||
var area = getDesktopArea();
|
||||
var cols = Math.ceil(Math.sqrt(visibleWindows.length));
|
||||
var rows = Math.ceil(visibleWindows.length / cols);
|
||||
var pad = 6;
|
||||
var cellWidth = Math.floor(area.width / cols);
|
||||
var cellHeight = Math.floor(area.height / rows);
|
||||
|
||||
clearActiveZoneLayout();
|
||||
|
||||
visibleWindows.forEach(function(win, index) {
|
||||
var col = index % cols;
|
||||
var row = Math.floor(index / cols);
|
||||
|
||||
win.x = Math.round(area.x + col * cellWidth + pad);
|
||||
win.y = Math.round(area.y + row * cellHeight + pad);
|
||||
win.width = Math.max(MIN_WIDTH, Math.round(cellWidth - pad * 2));
|
||||
win.height = Math.max(MIN_HEIGHT, Math.round(cellHeight - pad * 2));
|
||||
win.maximized = false;
|
||||
win.prevBounds = null;
|
||||
win.snappedZone = null;
|
||||
|
||||
var el = document.getElementById(win.id);
|
||||
if (el) {
|
||||
el.classList.remove('maximized');
|
||||
el.style.transition = 'left 0.25s ease, top 0.25s ease, width 0.25s ease, height 0.25s ease';
|
||||
el.style.left = win.x + 'px';
|
||||
el.style.top = win.y + 'px';
|
||||
el.style.width = win.width + 'px';
|
||||
el.style.height = win.height + 'px';
|
||||
|
||||
var maxIcon = el.querySelector('.maximize-btn .material-icons');
|
||||
if (maxIcon) maxIcon.textContent = 'crop_square';
|
||||
setTimeout(function() { el.style.transition = ''; }, 300);
|
||||
}
|
||||
|
||||
saveWindowBounds(win.appId, win.x, win.y, win.width, win.height);
|
||||
});
|
||||
}
|
||||
|
||||
function applySnapLayoutToWindows(layoutKey, primaryWinId) {
|
||||
var layout = SNAP_LAYOUTS.find(function(l) { return l.key === layoutKey; });
|
||||
if (!layout) return;
|
||||
@@ -1651,6 +1779,13 @@
|
||||
|
||||
// ============ Helpers ============
|
||||
|
||||
function shouldReserveTaskbarSpace() {
|
||||
if (!active) return false;
|
||||
var taskbar = document.getElementById('desktop-taskbar');
|
||||
if (windows.size > 0) return true;
|
||||
return !!(taskbar && !taskbar.classList.contains('taskbar-hidden'));
|
||||
}
|
||||
|
||||
function getDesktopArea() {
|
||||
// Use visualViewport for accurate available space (excludes on-screen keyboards,
|
||||
// browser chrome, etc.). Falls back to window.innerWidth/Height.
|
||||
@@ -1658,8 +1793,7 @@
|
||||
var vpWidth = vp ? vp.width : window.innerWidth;
|
||||
var vpHeight = vp ? vp.height : window.innerHeight;
|
||||
|
||||
// In widgets mode, taskbar is hidden — full height minus topnav (42px)
|
||||
var bottomOffset = (currentMode === 'widgets') ? 0 : TASKBAR_HEIGHT;
|
||||
var bottomOffset = shouldReserveTaskbarSpace() ? TASKBAR_HEIGHT : 0;
|
||||
|
||||
// Respect CSS safe-area-inset-bottom (accounts for system UI overlap)
|
||||
var safeBottom = 0;
|
||||
@@ -1815,7 +1949,10 @@
|
||||
// Snap Layout API
|
||||
showSnapPicker: showSnapPicker,
|
||||
hideSnapPicker: hideSnapPicker,
|
||||
openLayoutOverlay: openLayoutOverlay,
|
||||
closeLayoutOverlay: closeLayoutOverlay,
|
||||
applySnapLayout: applySnapLayoutToWindows,
|
||||
autoArrangeWindows: autoArrangeWindows,
|
||||
getSnapLayouts: function() { return SNAP_LAYOUTS; },
|
||||
clearZoneLayout: clearActiveZoneLayout,
|
||||
// Foldable device API
|
||||
|
||||
@@ -1428,7 +1428,11 @@
|
||||
init();
|
||||
}
|
||||
} else if (action === 'snap-layout') {
|
||||
openSnapLayoutPicker();
|
||||
if (window.DesktopMode && typeof window.DesktopMode.openLayoutOverlay === 'function') {
|
||||
window.DesktopMode.openLayoutOverlay();
|
||||
} else {
|
||||
openWidgetSnapLayoutPicker();
|
||||
}
|
||||
} else if (action === 'help') {
|
||||
// Open docs in float window
|
||||
if (window.DesktopMode && typeof window.DesktopMode.openAppByRoute === 'function') {
|
||||
@@ -1591,62 +1595,62 @@
|
||||
{ key: '1_3', name: t('desktop.label_1_3'), zones: [{ x: 0, y: 0, w: 0.5, h: 1 }, { x: 0.5, y: 0, w: 0.5, h: 0.33 }, { x: 0.5, y: 0.33, w: 0.5, h: 0.34 }, { x: 0.5, y: 0.67, w: 0.5, h: 0.33 }] }
|
||||
];
|
||||
|
||||
var _snapOverlay = null;
|
||||
var _widgetSnapOverlay = null;
|
||||
|
||||
/** Show the snap layout picker overlay */
|
||||
function openSnapLayoutPicker() {
|
||||
if (_snapOverlay) { closeSnapLayoutPicker(); return; }
|
||||
_snapOverlay = document.createElement('div');
|
||||
_snapOverlay.className = 'snap-layout-overlay';
|
||||
var html = '<div class="snap-layout-picker">' +
|
||||
'<div class="snap-layout-title">' + esc(t('desktop.label_snap_layout')) + '</div>' +
|
||||
'<div class="snap-layout-options">';
|
||||
function openWidgetSnapLayoutPicker() {
|
||||
if (_widgetSnapOverlay) { closeWidgetSnapLayoutPicker(); return; }
|
||||
_widgetSnapOverlay = document.createElement('div');
|
||||
_widgetSnapOverlay.className = 'widget-snap-layout-overlay';
|
||||
var html = '<div class="widget-snap-layout-picker">' +
|
||||
'<div class="widget-snap-layout-title">' + esc(t('desktop.label_snap_layout')) + '</div>' +
|
||||
'<div class="widget-snap-layout-options">';
|
||||
SNAP_LAYOUTS.forEach(function (layout) {
|
||||
html += '<div class="snap-layout-option" data-key="' + layout.key + '" title="' + esc(layout.name) + '">';
|
||||
html += '<div class="snap-layout-preview">';
|
||||
html += '<div class="widget-snap-layout-option" data-key="' + layout.key + '" title="' + esc(layout.name) + '">';
|
||||
html += '<div class="widget-snap-layout-preview">';
|
||||
layout.zones.forEach(function (z) {
|
||||
html += '<div class="snap-zone-preview" style="left:' + (z.x * 100) + '%;top:' + (z.y * 100) + '%;width:' + (z.w * 100) + '%;height:' + (z.h * 100) + '%"></div>';
|
||||
html += '<div class="widget-snap-zone-preview" style="left:' + (z.x * 100) + '%;top:' + (z.y * 100) + '%;width:' + (z.w * 100) + '%;height:' + (z.h * 100) + '%"></div>';
|
||||
});
|
||||
html += '</div><div class="snap-layout-label">' + esc(layout.name) + '</div></div>';
|
||||
html += '</div><div class="widget-snap-layout-label">' + esc(layout.name) + '</div></div>';
|
||||
});
|
||||
html += '</div>' +
|
||||
'<button class="snap-auto-arrange-btn"><span class="material-icons">auto_fix_high</span>' + esc(t('desktop.label_auto_arrange')) + '</button>' +
|
||||
'<button class="widget-snap-auto-arrange-btn"><span class="material-icons">auto_fix_high</span>' + esc(t('desktop.label_auto_arrange')) + '</button>' +
|
||||
'</div>';
|
||||
_snapOverlay.innerHTML = html;
|
||||
_widgetSnapOverlay.innerHTML = html;
|
||||
|
||||
// Click layout option
|
||||
_snapOverlay.querySelectorAll('.snap-layout-option').forEach(function (opt) {
|
||||
_widgetSnapOverlay.querySelectorAll('.widget-snap-layout-option').forEach(function (opt) {
|
||||
opt.addEventListener('click', function (e) {
|
||||
e.stopPropagation();
|
||||
applySnapLayout(opt.dataset.key);
|
||||
closeSnapLayoutPicker();
|
||||
closeWidgetSnapLayoutPicker();
|
||||
});
|
||||
});
|
||||
|
||||
// Auto-arrange
|
||||
var autoBtn = _snapOverlay.querySelector('.snap-auto-arrange-btn');
|
||||
var autoBtn = _widgetSnapOverlay.querySelector('.widget-snap-auto-arrange-btn');
|
||||
if (autoBtn) {
|
||||
autoBtn.addEventListener('click', function (e) {
|
||||
e.stopPropagation();
|
||||
autoArrangeWidgets();
|
||||
closeSnapLayoutPicker();
|
||||
closeWidgetSnapLayoutPicker();
|
||||
});
|
||||
}
|
||||
|
||||
// Click outside to close
|
||||
_snapOverlay.addEventListener('click', function (e) {
|
||||
if (e.target === _snapOverlay) closeSnapLayoutPicker();
|
||||
_widgetSnapOverlay.addEventListener('click', function (e) {
|
||||
if (e.target === _widgetSnapOverlay) closeWidgetSnapLayoutPicker();
|
||||
});
|
||||
|
||||
var shell = document.getElementById('desktop-shell');
|
||||
(shell || document.body).appendChild(_snapOverlay);
|
||||
(shell || document.body).appendChild(_widgetSnapOverlay);
|
||||
}
|
||||
|
||||
function closeSnapLayoutPicker() {
|
||||
if (_snapOverlay && _snapOverlay.parentNode) {
|
||||
_snapOverlay.parentNode.removeChild(_snapOverlay);
|
||||
function closeWidgetSnapLayoutPicker() {
|
||||
if (_widgetSnapOverlay && _widgetSnapOverlay.parentNode) {
|
||||
_widgetSnapOverlay.parentNode.removeChild(_widgetSnapOverlay);
|
||||
}
|
||||
_snapOverlay = null;
|
||||
_widgetSnapOverlay = null;
|
||||
}
|
||||
|
||||
/** Apply a snap layout — distribute existing widgets across zones */
|
||||
@@ -1952,7 +1956,7 @@
|
||||
loadPreset: loadPreset,
|
||||
deletePreset: deletePreset,
|
||||
listPresets: listPresets,
|
||||
openSnapLayout: openSnapLayoutPicker,
|
||||
openSnapLayout: openWidgetSnapLayoutPicker,
|
||||
autoArrange: autoArrangeWidgets,
|
||||
applySnapLayout: applySnapLayout,
|
||||
// Widget groups (Phase 11)
|
||||
|
||||
@@ -56,6 +56,77 @@ function extractBearerToken(req) {
|
||||
return auth.substring(7).trim();
|
||||
}
|
||||
|
||||
function requireOperatorRole(req, res, next) {
|
||||
const role = req.deviceUser?.role;
|
||||
if (role !== 'admin' && role !== 'operator') {
|
||||
return res.status(403).json({ error: 'Insufficient permissions' });
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
function normalizeSessionAction(value) {
|
||||
const action = String(value || '').trim().toLowerCase();
|
||||
if (action === 'start' || action === 'session_start') return 'session_start';
|
||||
if (action === 'end' || action === 'session_end') return 'session_end';
|
||||
return null;
|
||||
}
|
||||
|
||||
function toTimestamp(value) {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
||||
const parsed = Date.parse(String(value || ''));
|
||||
return Number.isFinite(parsed) ? parsed : 0;
|
||||
}
|
||||
|
||||
function buildSessionHistory(entries, limit) {
|
||||
const now = Date.now();
|
||||
const grouped = new Map();
|
||||
|
||||
const rows = [...entries]
|
||||
.filter((entry) => entry.action === 'session_start' || entry.action === 'session_end')
|
||||
.sort((a, b) => toTimestamp(a.created_at) - toTimestamp(b.created_at));
|
||||
|
||||
for (const entry of rows) {
|
||||
const key = entry.session_id || `${entry.host_id}:${entry.peer_id}:${entry.id}`;
|
||||
const existing = grouped.get(key) || {
|
||||
id: key,
|
||||
device_id: entry.peer_id || '',
|
||||
hostname: entry.peer_name || '',
|
||||
operator: entry.host_id || 'operator',
|
||||
started_at: '',
|
||||
ended_at: '',
|
||||
duration_secs: 0,
|
||||
action: entry.action || 'session_start',
|
||||
};
|
||||
|
||||
if (!existing.device_id && entry.peer_id) existing.device_id = entry.peer_id;
|
||||
if (!existing.hostname && entry.peer_name) existing.hostname = entry.peer_name;
|
||||
if (!existing.operator && entry.host_id) existing.operator = entry.host_id;
|
||||
|
||||
if (entry.action === 'session_start') {
|
||||
existing.started_at = String(entry.created_at || existing.started_at || '');
|
||||
existing.action = 'session_start';
|
||||
}
|
||||
if (entry.action === 'session_end') {
|
||||
existing.ended_at = String(entry.created_at || existing.ended_at || '');
|
||||
existing.action = 'session_end';
|
||||
}
|
||||
|
||||
grouped.set(key, existing);
|
||||
}
|
||||
|
||||
return [...grouped.values()]
|
||||
.map((entry) => {
|
||||
const started = toTimestamp(entry.started_at);
|
||||
const ended = entry.ended_at ? toTimestamp(entry.ended_at) : now;
|
||||
if (started > 0 && ended >= started) {
|
||||
entry.duration_secs = Math.max(0, Math.round((ended - started) / 1000));
|
||||
}
|
||||
return entry;
|
||||
})
|
||||
.sort((a, b) => toTimestamp(b.started_at || b.ended_at) - toTimestamp(a.started_at || a.ended_at))
|
||||
.slice(0, limit);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Middleware — authenticate desktop client via access token
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -462,7 +533,7 @@ router.post('/operator/login', async (req, res) => {
|
||||
// GET /api/bd/operator/devices — List all devices (requires operator auth)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
router.get('/operator/devices', requireDeviceAuth, async (req, res) => {
|
||||
router.get('/operator/devices', requireDeviceAuth, requireOperatorRole, async (req, res) => {
|
||||
try {
|
||||
// Only admin/operator can list devices
|
||||
if (req.deviceUser && req.deviceUser.role !== 'admin' && req.deviceUser.role !== 'operator') {
|
||||
@@ -504,7 +575,7 @@ router.get('/operator/devices', requireDeviceAuth, async (req, res) => {
|
||||
// GET /api/bd/help-requests — List all help requests (requires auth)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
router.get('/help-requests', requireDeviceAuth, async (req, res) => {
|
||||
router.get('/help-requests', requireDeviceAuth, requireOperatorRole, async (req, res) => {
|
||||
try {
|
||||
const items = [...helpRequests.values()]
|
||||
.sort((a, b) => b.created_at - a.created_at);
|
||||
@@ -520,7 +591,7 @@ router.get('/help-requests', requireDeviceAuth, async (req, res) => {
|
||||
// POST /api/bd/help-requests/:id/accept — Accept a help request
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
router.post('/help-requests/:id/accept', requireDeviceAuth, async (req, res) => {
|
||||
router.post('/help-requests/:id/accept', requireDeviceAuth, requireOperatorRole, async (req, res) => {
|
||||
try {
|
||||
const entry = helpRequests.get(req.params.id);
|
||||
if (!entry) {
|
||||
@@ -549,7 +620,7 @@ router.post('/help-requests/:id/accept', requireDeviceAuth, async (req, res) =>
|
||||
// POST /api/bd/help-requests/:id/resolve — Resolve a help request
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
router.post('/help-requests/:id/resolve', requireDeviceAuth, async (req, res) => {
|
||||
router.post('/help-requests/:id/resolve', requireDeviceAuth, requireOperatorRole, async (req, res) => {
|
||||
try {
|
||||
const entry = helpRequests.get(req.params.id);
|
||||
if (!entry) {
|
||||
@@ -578,7 +649,7 @@ router.post('/help-requests/:id/resolve', requireDeviceAuth, async (req, res) =>
|
||||
// DELETE /api/bd/help-requests/:id — Delete a help request
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
router.delete('/help-requests/:id', requireDeviceAuth, async (req, res) => {
|
||||
router.delete('/help-requests/:id', requireDeviceAuth, requireOperatorRole, async (req, res) => {
|
||||
try {
|
||||
if (!helpRequests.has(req.params.id)) {
|
||||
return res.status(404).json({ error: 'Help request not found' });
|
||||
@@ -592,4 +663,67 @@ router.delete('/help-requests/:id', requireDeviceAuth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// POST /api/bd/operator/sessions — Record operator session start/end
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
router.post('/operator/sessions', requireDeviceAuth, requireOperatorRole, async (req, res) => {
|
||||
try {
|
||||
const action = normalizeSessionAction(req.body?.action);
|
||||
const deviceId = String(req.body?.device_id || '').trim();
|
||||
const hostname = String(req.body?.hostname || '').trim().substring(0, 128);
|
||||
const sessionId = String(req.body?.session_id || crypto.randomUUID()).trim().substring(0, 96);
|
||||
|
||||
if (!action) {
|
||||
return res.status(400).json({ error: 'Invalid session action' });
|
||||
}
|
||||
if (!/^[A-Za-z0-9_-]{3,64}$/.test(deviceId)) {
|
||||
return res.status(400).json({ error: 'Invalid device_id' });
|
||||
}
|
||||
|
||||
const operatorName = req.deviceUser?.username || req.deviceUser?.name || 'operator';
|
||||
const operatorClientId = String(req.deviceToken?.client_id || '').substring(0, 64);
|
||||
|
||||
await db.insertAuditConnection({
|
||||
host_id: operatorName,
|
||||
host_uuid: operatorClientId,
|
||||
peer_id: deviceId,
|
||||
peer_name: hostname,
|
||||
action,
|
||||
conn_type: 1,
|
||||
session_id: sessionId,
|
||||
ip: getClientIp(req),
|
||||
});
|
||||
|
||||
res.json({ success: true, session_id: sessionId, action });
|
||||
} catch (err) {
|
||||
console.error('[BD-API] Record operator session error:', err.message);
|
||||
res.status(500).json({ error: 'Failed to record operator session' });
|
||||
}
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// GET /api/bd/operator/sessions — Operator session history
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
router.get('/operator/sessions', requireDeviceAuth, requireOperatorRole, async (req, res) => {
|
||||
try {
|
||||
const requestedLimit = parseInt(req.query.limit, 10);
|
||||
const limit = Number.isFinite(requestedLimit)
|
||||
? Math.min(Math.max(requestedLimit, 1), 200)
|
||||
: 100;
|
||||
|
||||
const rows = await db.getAuditConnections({
|
||||
limit: limit * 4,
|
||||
offset: 0,
|
||||
});
|
||||
|
||||
const sessions = buildSessionHistory(rows || [], limit);
|
||||
res.json({ success: true, sessions, count: sessions.length });
|
||||
} catch (err) {
|
||||
console.error('[BD-API] Session history error:', err.message);
|
||||
res.status(500).json({ error: 'Failed to fetch session history' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user