Files
espresso_frame/server/app/static/widget_dialog_whiteboard.js
T
tfaour a33a3a71e4
Build and push server image / test (push) Successful in 21s
Build and push server image / build-and-push (push) Successful in 1m57s
Build and push server image / deploy (push) Successful in 52s
Widget system Phase 4b: per-widget gear-icon config dialogs
Replaces the Photos/Calendar/Whiteboard tabs with a single Layout page
(now the frame's landing route) where each widget gets a gear icon
opening a dialog scoped to that specific widget's own settings. This
was the missing piece for genuinely independent same-type widgets --
"the Calendar tab" never made sense once a frame could hold more than
one calendar widget with different settings.

Data layer: FrameCalendar re-keyed from frame_id to widget_id, so each
calendar widget has its own independent included-calendars set. The
rekey runs as an unconditional post-startup step (like the existing
widget backfill), not a numbered migration -- it depends on calendar
widgets already existing, which themselves come from that same
backfill step, not from schema migration. Registering it as a numbered
migration would have run it first during a real upgrade, silently
dropping every row; caught by a new test that exercises the raw-SQL
upgrade path instead of the fresh-install create_all() shortcut every
other migration test takes.

API layer: every endpoint that used to assume "the frame's widget of
this type" (photo queue/thumbnail/preview, calendar select/color/
tasks/weather, whiteboard source/browse/preview) moved into
api_widgets.py under /api/frames/{id}/widgets/{widget_id}/..., with a
new require_widget_view/control dependency pair mirroring the existing
frame-level ones. Device status (battery/last-seen/firmware) got its
own frame-level /status endpoint, split out of the old photo-specific
/queue it used to piggyback on -- fixes the status bar going silently
blank on any frame without a photo widget.

UI layer: each widget type's existing settings markup/JS was ported
into a dialog partial + an explicit init/close function pair (the
content is now fetched and injected on demand, not loaded at page load
time). window.FRAME_API is repointed to the open dialog's widget-scoped
API base for its duration and restored on close; a separate
window.FRAME_BASE_API stays stable for the always-present header/
status-bar scripts.

Caught during manual browser testing: the consolidated config-save
endpoint initially expected a JSON body while the copied-over dialog JS
posts form-urlencoded data (the old convention) -- fixed to match, with
new HTTP-level test coverage that would have caught it immediately.
2026-07-24 14:31:24 -04:00

164 lines
6.3 KiB
JavaScript

// Whiteboard widget dialog: source URL (owner-gated, see
// api_widgets.py's api_widget_whiteboard_source), preview, and the file
// browser. Not a page-load script -- frame_layout.js fetches this
// widget's dialog HTML fragment, injects it into the shared <dialog>,
// points window.FRAME_API at this specific widget (/api/frames/{id}/
// widgets/{widget_id}), then calls initWhiteboardDialog().
// Rewrites #whiteboard-current-source in place instead of telling the
// user to reload -- the API always assigns a successful "set" to the
// caller (see api_widget_whiteboard_source), so after either action we
// already know exactly what the new state is without asking the server
// again.
function renderWhiteboardCurrentSource(url) {
const container = document.getElementById('whiteboard-current-source');
container.innerHTML = '';
const p = document.createElement('p');
p.className = 'sub';
p.style.marginTop = '10px';
if (url) {
p.append('Currently showing ');
const urlEl = document.createElement('strong');
urlEl.textContent = url;
p.append(urlEl, ' using your WebDAV account. ');
const clearBtn = document.createElement('button');
clearBtn.type = 'button';
clearBtn.className = 'btn-inline secondary';
clearBtn.id = 'whiteboard-source-clear';
clearBtn.textContent = 'Clear';
clearBtn.addEventListener('click', clearWhiteboardSource);
p.append(clearBtn);
} else {
p.textContent = 'No whiteboard configured yet.';
}
container.append(p);
const label = document.getElementById('whiteboard-source-form-label');
if (label) {
label.textContent = url ? 'Change to one of your own files' : 'Use one of your own files';
}
}
async function clearWhiteboardSource() {
try {
const resp = await fetch(`${window.FRAME_API}/whiteboard-source`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: null }),
});
if (!resp.ok) throw new Error(await apiError(resp));
showStatus(true, 'Cleared.');
renderWhiteboardCurrentSource(null);
const urlInput = document.getElementById('whiteboard-url-input');
if (urlInput) urlInput.value = '';
loadWhiteboardPreview(false);
} catch (e) {
showStatus(false, e.message);
}
}
function loadWhiteboardPreview(force) {
const forceParam = force ? '&force=1' : '';
document.getElementById('whiteboard-preview').src = `${window.FRAME_API}/preview/whiteboard?_=${Date.now()}${forceParam}`;
}
function initWhiteboardDialog() {
const whiteboardForm = document.getElementById('whiteboard-source-form');
if (whiteboardForm) {
whiteboardForm.addEventListener('submit', async (e) => {
e.preventDefault();
const url = document.getElementById('whiteboard-url-input').value.trim();
if (!url) return;
try {
const resp = await fetch(`${window.FRAME_API}/whiteboard-source`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url }),
});
if (!resp.ok) throw new Error(await apiError(resp));
showStatus(true, 'Saved.');
renderWhiteboardCurrentSource(url);
loadWhiteboardPreview(false);
} catch (e) {
showStatus(false, e.message);
}
});
}
const whiteboardClearBtn = document.getElementById('whiteboard-source-clear');
if (whiteboardClearBtn) {
whiteboardClearBtn.addEventListener('click', clearWhiteboardSource);
}
// Shows whatever's already cached (cheap, no refetch) on open; the
// button is the one place that means "no really, go check now" --
// bypasses the fetch throttle server-side (see api_widget_preview_
// whiteboard's `force` param).
document.getElementById('whiteboard-preview-refresh').addEventListener('click', () => loadWhiteboardPreview(true));
loadWhiteboardPreview(false);
// --- file picker (Browse...) ---
const browseToggle = document.getElementById('whiteboard-browse-toggle');
if (browseToggle) {
const browsePanel = document.getElementById('whiteboard-browser');
const browseList = document.getElementById('whiteboard-browse-list');
const browseCurrent = document.getElementById('whiteboard-browse-current');
const browseUp = document.getElementById('whiteboard-browse-up');
const browseError = document.getElementById('whiteboard-browse-error');
const urlInput = document.getElementById('whiteboard-url-input');
let opened = false;
async function browseTo(url) {
browseError.style.display = 'none';
browseList.innerHTML = '<li class="sub">Loading...</li>';
try {
const qs = url ? `?url=${encodeURIComponent(url)}` : '';
const resp = await fetch(`${window.FRAME_API}/whiteboard-browse${qs}`);
if (!resp.ok) throw new Error(await apiError(resp));
const data = await resp.json();
browseCurrent.textContent = data.current_url;
browseUp.disabled = !data.parent_url;
browseUp.onclick = data.parent_url ? () => browseTo(data.parent_url) : null;
browseList.innerHTML = '';
if (data.entries.length === 0) {
browseList.innerHTML = '<li class="sub">(empty folder)</li>';
}
for (const entry of data.entries) {
const li = document.createElement('li');
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'btn-inline secondary';
btn.style.margin = '2px 0';
btn.textContent = (entry.is_dir ? '📁 ' : '📄 ') + entry.name;
if (entry.is_dir) {
btn.addEventListener('click', () => browseTo(entry.url));
} else {
btn.addEventListener('click', () => {
urlInput.value = entry.url;
browsePanel.style.display = 'none';
});
}
li.appendChild(btn);
browseList.appendChild(li);
}
} catch (e) {
browseList.innerHTML = '';
browseError.textContent = e.message;
browseError.style.display = 'block';
}
}
browseToggle.addEventListener('click', () => {
opened = !opened;
browsePanel.style.display = opened ? 'block' : 'none';
if (opened && !browseCurrent.textContent) {
browseTo(null);
}
});
}
}
function closeWhiteboardDialog() {
// Nothing to tear down -- no poll interval, unlike the photos dialog.
}