askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
slots.py · 1.7 KB · 48 lines
| 1 | """Shared slot registry, served at /slots as window.dbbasicSlots. |
| 2 | |
| 3 | Named extension points for the generated list/form UIs (Phase 5b, page hooks — |
| 4 | docs/upgrade-and-customization.md Rule 4). An operator or a page registers a |
| 5 | renderer for a (collection, slot) pair; the generators call it at the matching |
| 6 | hook point and inject its HTML — so behavior can be added to every list/form |
| 7 | without editing the page or the generator itself. Register against a specific |
| 8 | collection or "*" for all collections. |
| 9 | |
| 10 | Slots today: |
| 11 | /list -> before_list(ctx), after_list(ctx), row_actions(record), empty(ctx) |
| 12 | /form -> before_fields(ctx), after_fields(ctx) |
| 13 | A renderer receives a context object and returns an HTML string (or ""). |
| 14 | Interactive slot markup should attach its own delegated listeners (list/form |
| 15 | mounts re-render), e.g. document.addEventListener on a data-attribute. |
| 16 | |
| 17 | Defined once here; the generators call window.dbbasicSlots when present and |
| 18 | no-op when it is absent, so slots are purely additive. |
| 19 | """ |
| 20 | |
| 21 | _JS = r""" |
| 22 | (function () { |
| 23 | const reg = {}; |
| 24 | const key = (collection, slot) => collection + "::" + slot; |
| 25 | window.dbbasicSlots = { |
| 26 | register: function (collection, slot, fn) { |
| 27 | if (typeof fn !== "function") return; |
| 28 | const k = key(collection, slot); |
| 29 | (reg[k] = reg[k] || []).push(fn); |
| 30 | }, |
| 31 | render: function (collection, slot, ctx) { |
| 32 | let out = ""; |
| 33 | for (const scope of [collection, "*"]) { |
| 34 | const fns = reg[key(scope, slot)]; |
| 35 | if (!fns) continue; |
| 36 | for (const fn of fns) { |
| 37 | try { const html = fn(ctx); if (html) out += html; } catch (e) {} |
| 38 | } |
| 39 | } |
| 40 | return out; |
| 41 | } |
| 42 | }; |
| 43 | })(); |
| 44 | """ |
| 45 | |
| 46 | |
| 47 | def GET(request): |
| 48 | return {"content_type": "application/javascript; charset=utf-8", "body": _JS} |