web: split app.js into view modules
Pure move, behaviour unchanged: app.js is now the shell (boot, state, client wiring, connection/banner, navigation and routes, render dispatch, header). Views live under src/views with shared DOM and format helpers. client.js stays DOM-free; no circular imports; every server-derived string still uses textContent.
This commit is contained in:
@@ -0,0 +1,404 @@
|
||||
// The Settings destination: Connection, Workspace, Notifications and the
|
||||
// Templates card. Every string and validation rule comes from settings.js /
|
||||
// notify.js; this module owns the DOM, the local drafts and the shared client's
|
||||
// connection controls.
|
||||
|
||||
import { h } from "../dom.js";
|
||||
import {
|
||||
SERVER_URL_HELPER,
|
||||
SERVER_URL_PLACEHOLDER,
|
||||
TEMPLATES_CARD_LABEL,
|
||||
TEMPLATES_CARD_SECONDARY,
|
||||
TRUST_BODY,
|
||||
TRUST_TITLE,
|
||||
WORKSPACE_HELPER,
|
||||
WORKSPACE_PLACEHOLDER,
|
||||
canSaveWorkspace,
|
||||
connectionActionKind,
|
||||
connectionActions,
|
||||
connectionCard,
|
||||
savedWorkspaceLine,
|
||||
workspaceErrorText,
|
||||
writeWorkspace,
|
||||
} from "../settings.js";
|
||||
import {
|
||||
NOTIFICATION_RATIONALE,
|
||||
notificationEnvironment,
|
||||
notificationPermissionState,
|
||||
} from "../notify.js";
|
||||
import { bannerState } from "../connection.js";
|
||||
|
||||
export function createSettingsView(rt) {
|
||||
const { state, repo, setServerUrlDraft, endpointConfigured } = rt;
|
||||
const renderContent = (...args) => rt.renderContent(...args);
|
||||
const pushRoute = (...args) => rt.pushRoute(...args);
|
||||
const applyRoute = (...args) => rt.applyRoute(...args);
|
||||
const openConfiguredEndpoint = (...args) => rt.openConfiguredEndpoint(...args);
|
||||
const onReconnectClicked = (...args) => rt.onReconnectClicked(...args);
|
||||
const persistNotifyAsked = (...args) => rt.views.attention.persistNotifyAsked(...args);
|
||||
|
||||
/**
|
||||
* The Settings destination: the app's three sections (Connection, Workspace,
|
||||
* Templates) in one pane. It is wiring only — every string, the workspace
|
||||
* validation and the connection card's lines come from the pure `settings.js`,
|
||||
* and the connection controls drive the page's existing single client through
|
||||
* the same handlers the header uses (no second socket or path). Every
|
||||
* server-derived string is set with textContent.
|
||||
*/
|
||||
function renderSettingsView() {
|
||||
const pane = h("div", { class: "settings" });
|
||||
pane.append(
|
||||
renderSettingsConnection(),
|
||||
renderSettingsWorkspace(),
|
||||
renderSettingsNotifications(),
|
||||
renderSettingsTemplates(),
|
||||
);
|
||||
return pane;
|
||||
}
|
||||
|
||||
function renderSettingsConnection() {
|
||||
const section = h("section", { class: "settings-section" });
|
||||
section.append(h("h2", { class: "settings-title", text: "Connection" }));
|
||||
|
||||
section.append(
|
||||
h(
|
||||
"label",
|
||||
{ class: "settings-field", for: "settingsServerUrl" },
|
||||
h("span", { class: "settings-field-label", text: "Server URL" }),
|
||||
h("input", {
|
||||
id: "settingsServerUrl",
|
||||
type: "text",
|
||||
placeholder: SERVER_URL_PLACEHOLDER,
|
||||
value: state.url,
|
||||
spellcheck: "false",
|
||||
autocomplete: "off",
|
||||
"aria-describedby": "settingsUrlHelper",
|
||||
"aria-invalid": state.urlError ? "true" : null,
|
||||
oninput: (event) => {
|
||||
setServerUrlDraft(event.target.value, "settingsServerUrl");
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
// Android's `urlError ?: helper` supporting text. The error clears on the next
|
||||
// edit (AppViewModel.updateServerUrlDraft) and is announced as the field's own
|
||||
// invalid state rather than as a page-level banner.
|
||||
section.append(
|
||||
h(
|
||||
"p",
|
||||
{
|
||||
id: "settingsUrlHelper",
|
||||
class: `settings-helper${state.urlError ? " settings-helper-error" : ""}`,
|
||||
role: state.urlError ? "alert" : null,
|
||||
},
|
||||
serverUrlSupportingText(),
|
||||
),
|
||||
);
|
||||
|
||||
const actions = connectionActions(state.connection);
|
||||
section.append(
|
||||
h(
|
||||
"div",
|
||||
{ class: "settings-actions" },
|
||||
h("button", {
|
||||
id: "settingsPrimary",
|
||||
class: "primary",
|
||||
type: "button",
|
||||
text: actions.primary,
|
||||
onclick: () => runConnectionAction(actions.primary),
|
||||
}),
|
||||
h("button", {
|
||||
id: "settingsSecondary",
|
||||
class: "ghost",
|
||||
type: "button",
|
||||
text: actions.secondary,
|
||||
onclick: () => runConnectionAction(actions.secondary),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const tone = bannerState(state.connection, { hasEndpoint: endpointConfigured() }).tone;
|
||||
const card = h("div", { class: `settings-status status-${tone}` });
|
||||
const lines = connectionCard(state.connection, {
|
||||
version: state.serverBuild?.version,
|
||||
commit: state.serverBuild?.commit,
|
||||
serverTime: state.serverTime,
|
||||
});
|
||||
lines.forEach((line, index) => {
|
||||
// Android's Connecting card leads its one line with a CircularProgressIndicator;
|
||||
// the shared spinner is drawn only for that state, inline with the text, so the
|
||||
// other three stay plain text.
|
||||
if (index === 0 && state.connection.kind === "connecting") {
|
||||
const row = h("div", { class: "settings-status-head" });
|
||||
row.append(h("span", { class: "spinner settings-status-spinner", "aria-hidden": "true" }));
|
||||
row.append(h("p", { class: "settings-status-line", text: line }));
|
||||
card.append(row);
|
||||
return;
|
||||
}
|
||||
card.append(h("p", { class: "settings-status-line", text: line }));
|
||||
});
|
||||
section.append(card, renderTrustWarning());
|
||||
return section;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Notifications section (L5). The Android rationale is shown verbatim; the
|
||||
* status line is the pure permission view for this page's real browser facts.
|
||||
* Only the explicit control asks for permission, never page load. When the API
|
||||
* is unavailable, the page is insecure or permission is denied, the wording says
|
||||
* plainly that the in-app Needs you surface is the only channel — no fake
|
||||
* notification is ever drawn.
|
||||
*/
|
||||
function renderSettingsNotifications() {
|
||||
const view = notificationPermissionState({
|
||||
...notificationEnvironment(),
|
||||
asked: state.notifyAsked,
|
||||
});
|
||||
const section = h("section", { class: "settings-section", id: "settingsNotifications" });
|
||||
section.append(h("h2", { class: "settings-title", text: "Notifications" }));
|
||||
section.append(h("p", { class: "settings-helper", text: NOTIFICATION_RATIONALE }));
|
||||
section.append(
|
||||
h("p", {
|
||||
id: "settingsNotificationState",
|
||||
class: `settings-notify-state settings-notify-${view.state}`,
|
||||
role: "status",
|
||||
text: view.body,
|
||||
}),
|
||||
);
|
||||
if (view.canRequest) {
|
||||
section.append(
|
||||
h(
|
||||
"div",
|
||||
{ class: "settings-actions" },
|
||||
h("button", {
|
||||
id: "settingsEnableNotifications",
|
||||
class: "primary",
|
||||
type: "button",
|
||||
text: view.actionLabel,
|
||||
onclick: requestNotifications,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
return section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask for notification permission from the operator's explicit click. The asked
|
||||
* flag is persisted first and the page re-renders from the browser's real answer;
|
||||
* a refused/dismissed prompt leaves the honest default/denied wording in place.
|
||||
*/
|
||||
function requestNotifications() {
|
||||
persistNotifyAsked(true);
|
||||
const ctor = globalThis.Notification;
|
||||
if (typeof ctor !== "function" || !globalThis.isSecureContext) {
|
||||
renderContent();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = ctor.requestPermission();
|
||||
if (result && typeof result.then === "function") {
|
||||
result.then(
|
||||
() => renderContent(),
|
||||
() => renderContent(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// A browser that refuses the request keeps the honest fallback wording.
|
||||
}
|
||||
renderContent();
|
||||
}
|
||||
|
||||
/** The trusted-network warning card, SettingsScreen.kt's TrustWarning verbatim. */
|
||||
function renderTrustWarning() {
|
||||
return h(
|
||||
"div",
|
||||
{ class: "settings-warning" },
|
||||
h("p", { class: "settings-warning-title", text: TRUST_TITLE }),
|
||||
h("p", { class: "settings-warning-body", text: TRUST_BODY }),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Route a Settings connection control through the page's existing single client,
|
||||
* mirroring AppViewModel: Disconnect closes the shared socket, Connect is
|
||||
* connect-only (validate + persist + open — never the header's toggle), and
|
||||
* Reconnect re-opens the same validated endpoint now. Nothing here opens a second
|
||||
* connection.
|
||||
*/
|
||||
function runConnectionAction(label) {
|
||||
const kind = connectionActionKind(label);
|
||||
if (kind === "disconnect") {
|
||||
repo.disconnect();
|
||||
return;
|
||||
}
|
||||
if (kind === "reconnect") {
|
||||
onReconnectClicked();
|
||||
return;
|
||||
}
|
||||
// The `Connect` label must never fall through to the header's toggle: while
|
||||
// connected that would tear the live socket down (Android's connect() is
|
||||
// connect-only).
|
||||
openConfiguredEndpoint();
|
||||
}
|
||||
|
||||
/** Android's `urlError ?: helper` supporting text for the Server URL field. */
|
||||
function serverUrlSupportingText() {
|
||||
return state.urlError || SERVER_URL_HELPER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the Server URL field's own error state in place (supporting text,
|
||||
* aria-invalid and the error tint) so an invalid Connect never needs a full
|
||||
* re-render and the field being typed in is never rebuilt.
|
||||
*/
|
||||
function refreshServerUrlError() {
|
||||
const field = document.getElementById("settingsServerUrl");
|
||||
const helper = document.getElementById("settingsUrlHelper");
|
||||
if (field) {
|
||||
if (state.urlError) field.setAttribute("aria-invalid", "true");
|
||||
else field.removeAttribute("aria-invalid");
|
||||
}
|
||||
if (helper) {
|
||||
helper.textContent = serverUrlSupportingText();
|
||||
helper.classList.toggle("settings-helper-error", Boolean(state.urlError));
|
||||
if (state.urlError) helper.setAttribute("role", "alert");
|
||||
else helper.removeAttribute("role");
|
||||
}
|
||||
}
|
||||
|
||||
function renderSettingsWorkspace() {
|
||||
const section = h("section", { class: "settings-section" });
|
||||
section.append(h("h2", { class: "settings-title", text: "Workspace" }));
|
||||
section.append(
|
||||
h(
|
||||
"label",
|
||||
{ class: "settings-field", for: "settingsWorkspace" },
|
||||
h("span", { class: "settings-field-label", text: "Workspace path (on the server)" }),
|
||||
h("input", {
|
||||
id: "settingsWorkspace",
|
||||
type: "text",
|
||||
placeholder: WORKSPACE_PLACEHOLDER,
|
||||
value: state.workspaceDraft,
|
||||
spellcheck: "false",
|
||||
autocomplete: "off",
|
||||
"aria-describedby": "workspaceHelper",
|
||||
"aria-invalid": workspaceErrorText(state.workspaceDraft) ? "true" : null,
|
||||
oninput: (event) => {
|
||||
state.workspaceDraft = event.target.value;
|
||||
refreshWorkspaceControls();
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
section.append(
|
||||
h(
|
||||
"p",
|
||||
{
|
||||
id: "workspaceHelper",
|
||||
class: `settings-helper${workspaceErrorText(state.workspaceDraft) ? " settings-helper-error" : ""}`,
|
||||
},
|
||||
workspaceSupportingText(),
|
||||
),
|
||||
);
|
||||
section.append(
|
||||
h("button", {
|
||||
id: "saveWorkspace",
|
||||
class: "primary settings-save",
|
||||
type: "button",
|
||||
text: "Save workspace",
|
||||
disabled: !canSaveWorkspace(state.workspaceDraft),
|
||||
onclick: saveWorkspace,
|
||||
}),
|
||||
);
|
||||
section.append(h("p", { id: "savedWorkspace", class: "settings-saved", text: savedWorkspaceLine(state.workspace) }));
|
||||
return section;
|
||||
}
|
||||
|
||||
/** Android's `workspaceError ?: helper` supporting text for the workspace field. */
|
||||
function workspaceSupportingText() {
|
||||
return workspaceErrorText(state.workspaceDraft) || WORKSPACE_HELPER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the workspace field's dependent controls in place (the helper/error line
|
||||
* and the Save button's enabled state) so typing never re-renders — and so never
|
||||
* blurs — the field being typed in.
|
||||
*/
|
||||
function refreshWorkspaceControls() {
|
||||
const helper = document.getElementById("workspaceHelper");
|
||||
const invalid = Boolean(workspaceErrorText(state.workspaceDraft));
|
||||
if (helper) {
|
||||
helper.textContent = workspaceSupportingText();
|
||||
helper.classList.toggle("settings-helper-error", invalid);
|
||||
}
|
||||
const field = document.getElementById("settingsWorkspace");
|
||||
if (field) {
|
||||
if (invalid) field.setAttribute("aria-invalid", "true");
|
||||
else field.removeAttribute("aria-invalid");
|
||||
}
|
||||
const save = document.getElementById("saveWorkspace");
|
||||
if (save) save.disabled = !canSaveWorkspace(state.workspaceDraft);
|
||||
}
|
||||
|
||||
/** Persist the workspace (trimmed) and update the `Saved workspace:` line. */
|
||||
function saveWorkspace() {
|
||||
if (!canSaveWorkspace(state.workspaceDraft)) return;
|
||||
state.workspace = writeWorkspace(localStorage, state.workspaceDraft);
|
||||
state.workspaceDraft = state.workspace;
|
||||
// Show the trimmed value that was actually stored, in the field as well as the
|
||||
// saved line (both updated in place so the keyboard focus is not disturbed).
|
||||
const field = document.getElementById("settingsWorkspace");
|
||||
if (field) field.value = state.workspace;
|
||||
const saved = document.getElementById("savedWorkspace");
|
||||
if (saved) saved.textContent = savedWorkspaceLine(state.workspace);
|
||||
}
|
||||
|
||||
function openTemplatesFromSettings() {
|
||||
const route = { kind: "templates" };
|
||||
pushRoute(route);
|
||||
applyRoute(route);
|
||||
}
|
||||
|
||||
function renderSettingsTemplates() {
|
||||
const section = h("section", { class: "settings-section" });
|
||||
section.append(h("h2", { class: "settings-title", text: "Templates" }));
|
||||
const card = h(
|
||||
"button",
|
||||
{
|
||||
id: "browseTemplates",
|
||||
class: "card settings-card",
|
||||
type: "button",
|
||||
onclick: openTemplatesFromSettings,
|
||||
},
|
||||
// SettingsScreen.kt's row idiom: a leading glyph, the label and secondary
|
||||
// line, then a forward chevron.
|
||||
h("span", { class: "settings-card-glyph", "aria-hidden": "true", text: "\u25a4" }),
|
||||
h(
|
||||
"span",
|
||||
{ class: "settings-card-text" },
|
||||
h("span", { class: "card-name", text: TEMPLATES_CARD_LABEL }),
|
||||
h("span", { class: "card-desc", text: TEMPLATES_CARD_SECONDARY }),
|
||||
),
|
||||
h("span", { class: "settings-card-chevron", "aria-hidden": "true", text: "\u203a" }),
|
||||
);
|
||||
section.append(card);
|
||||
return section;
|
||||
}
|
||||
|
||||
return {
|
||||
renderSettingsView,
|
||||
renderSettingsConnection,
|
||||
requestNotifications,
|
||||
runConnectionAction,
|
||||
serverUrlSupportingText,
|
||||
refreshServerUrlError,
|
||||
renderSettingsWorkspace,
|
||||
workspaceSupportingText,
|
||||
refreshWorkspaceControls,
|
||||
saveWorkspace,
|
||||
openTemplatesFromSettings,
|
||||
renderSettingsTemplates,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user