Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
docs_nav.py · 7.8 KB · 197 lines
1"""Shared docs-site chrome, served as one object at /docs-nav.
2
3Mirrors how /markdown and /style ship one script/stylesheet used by every
4page rather than each page carrying its own copy (see app-theme/markdown.py
5and app-theme/nav.py). Every /docs and /docs/{slug} page includes
6<script src="/docs-nav"></script> and gets `window.dbbasicDocsNav`:
7
8 - loadManifest() fetch the full doc_pages manifest exactly once per
9 browser tab (cached in sessionStorage), sorted by
10 Number(nav_order) then title.
11 - renderHeader(el) the public docs-site brand/logo, linking to /docs.
12 No sign-in prompt, no dependency on the internal
13 app's /nav -- this is a public site with its own
14 chrome.
15 - renderSidebar(el, records, activeSlug)
16 groups records by category (first-appearance order
17 across the sorted manifest), renders a heading per
18 category and a list of pages below it, and marks
19 the current page active.
20 - wireSearch(inputEl, listEl)
21 live, case-insensitive substring filter over
22 title + summary + category. No server round-trip,
23 no debounce -- the manifest tops out around three
24 dozen records.
25 - wireDrawer(toggleEl, drawerEl, scrimEl)
26 collapses the sidebar into a hidden-by-default
27 off-canvas drawer on narrow viewports; the toggle
28 button and a click on the scrim both close it.
29 - mount(options) convenience wrapper that does all of the above in
30 one call and returns the sorted manifest, so a
31 page can look up its own record by id afterward.
32
33The manifest fetch is the one genuinely expensive thing here (combined doc
34content across every page is substantial, and every /docs/{slug} click is a
35full page load in this architecture, not a client-side route change), so it
36is cached in sessionStorage under one key and only re-fetched once per tab.
37"""
38
39_JS = r"""
40(function () {
41 const MANIFEST_KEY = "dbbasicDocsManifest";
42
43 const esc = (s) => String(s == null ? "" : s).replace(/[&<>"']/g,
44 (c) => ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[c]));
45
46 function sortManifest(records) {
47 const sorted = records.slice();
48 sorted.sort((a, b) => {
49 const na = Number(a && a.nav_order);
50 const nb = Number(b && b.nav_order);
51 const va = Number.isFinite(na) ? na : Infinity;
52 const vb = Number.isFinite(nb) ? nb : Infinity;
53 if (va !== vb) return va - vb;
54 return String((a && a.title) || "").localeCompare(String((b && b.title) || ""));
55 });
56 return sorted;
57 }
58
59 async function loadManifest() {
60 try {
61 const cached = sessionStorage.getItem(MANIFEST_KEY);
62 if (cached) {
63 const parsed = JSON.parse(cached);
64 if (Array.isArray(parsed)) return sortManifest(parsed);
65 }
66 } catch (e) { /* sessionStorage unavailable or the cached value is corrupt -- fetch instead */ }
67
68 let records = [];
69 try {
70 const res = await fetch("/collections/doc_pages/records?limit=100",
71 {credentials: "same-origin", headers: {accept: "application/json"}});
72 const body = await res.json();
73 records = (body && body.records) || [];
74 } catch (e) {
75 records = [];
76 }
77 try {
78 sessionStorage.setItem(MANIFEST_KEY, JSON.stringify(records));
79 } catch (e) { /* storage full or disabled -- proceed uncached for this load */ }
80 return sortManifest(records);
81 }
82
83 function renderHeader(el) {
84 if (!el) return;
85 el.innerHTML = '<a class="docsbrand" href="/docs">'
86 + '<span class="docslogo">DB</span><span>DBBASIC Docs</span></a>';
87 }
88
89 // Groups preserve first-appearance order across the already-sorted
90 // manifest -- the category of the lowest-nav_order page in that category
91 // decides where the whole group falls, with no separate category-order
92 // field to keep in sync.
93 function groupByCategory(records) {
94 const groups = [];
95 const byName = new Map();
96 records.forEach((record) => {
97 const name = String((record && record.category) || "").trim() || "General";
98 let group = byName.get(name);
99 if (!group) {
100 group = {category: name, items: []};
101 byName.set(name, group);
102 groups.push(group);
103 }
104 group.items.push(record);
105 });
106 return groups;
107 }
108
109 function renderSidebar(el, records, activeSlug) {
110 if (!el) return;
111 const groups = groupByCategory(records || []);
112 if (!groups.length) {
113 el.innerHTML = '<div class="navempty">No pages yet.</div>';
114 return;
115 }
116 let html = "";
117 groups.forEach((group) => {
118 html += '<div class="navgroup" data-navgroup>';
119 html += '<div class="navheading">' + esc(group.category) + '</div>';
120 html += '<ul class="navlist">';
121 group.items.forEach((record) => {
122 const id = (record && record.id) || "";
123 const title = (record && record.title) || id;
124 const active = activeSlug && id === activeSlug ? " active" : "";
125 const haystack = esc(title + " " + ((record && record.summary) || "")
126 + " " + ((record && record.category) || "")).toLowerCase();
127 html += '<li class="navitem' + active + '" data-search="' + haystack + '">'
128 + '<a href="/docs/' + esc(id) + '">' + esc(title) + '</a></li>';
129 });
130 html += '</ul></div>';
131 });
132 el.innerHTML = html;
133 }
134
135 function wireSearch(inputEl, listEl) {
136 if (!inputEl || !listEl) return;
137 inputEl.addEventListener("input", () => {
138 const query = inputEl.value.trim().toLowerCase();
139 const items = listEl.querySelectorAll(".navitem");
140 items.forEach((item) => {
141 const haystack = item.getAttribute("data-search") || "";
142 const show = !query || haystack.indexOf(query) !== -1;
143 item.style.display = show ? "" : "none";
144 });
145 const groups = listEl.querySelectorAll("[data-navgroup]");
146 groups.forEach((group) => {
147 const hasVisible = Array.prototype.some.call(
148 group.querySelectorAll(".navitem"), (item) => item.style.display !== "none");
149 group.style.display = hasVisible ? "" : "none";
150 });
151 });
152 }
153
154 // Off-canvas drawer for narrow viewports: hidden by default (the CSS in
155 // each page keeps .docsidebar off-screen under the breakpoint), the
156 // toggle button opens/closes it, and a tap on the scrim behind it closes
157 // it too -- nothing here ever grows the page wider than the viewport.
158 function wireDrawer(toggleEl, drawerEl, scrimEl) {
159 if (!toggleEl || !drawerEl) return;
160 function setOpen(open) {
161 drawerEl.classList.toggle("open", open);
162 if (scrimEl) scrimEl.classList.toggle("open", open);
163 toggleEl.setAttribute("aria-expanded", open ? "true" : "false");
164 }
165 toggleEl.addEventListener("click", () => {
166 setOpen(!drawerEl.classList.contains("open"));
167 });
168 if (scrimEl) scrimEl.addEventListener("click", () => setOpen(false));
169 drawerEl.querySelectorAll("a").forEach((link) => {
170 link.addEventListener("click", () => setOpen(false));
171 });
172 }
173
174 async function mount(options) {
175 const opts = options || {};
176 const records = await loadManifest();
177 renderHeader(opts.headerEl);
178 renderSidebar(opts.navListEl, records, opts.activeSlug || null);
179 wireSearch(opts.searchEl, opts.navListEl);
180 wireDrawer(opts.toggleEl, opts.drawerEl, opts.scrimEl);
181 return records;
182 }
183
184 window.dbbasicDocsNav = {
185 loadManifest: loadManifest,
186 renderHeader: renderHeader,
187 renderSidebar: renderSidebar,
188 wireSearch: wireSearch,
189 wireDrawer: wireDrawer,
190 mount: mount,
191 };
192})();
193"""
194
195
196def GET(request):
197 return {"content_type": "application/javascript; charset=utf-8", "body": _JS}