cogens 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -103,23 +103,80 @@ export function signInPage(shell: Shell, next: string, error?: string): Html {
103
103
  });
104
104
  }
105
105
 
106
- // ── The type list ───────────────────────────────────────────────────
106
+ // ── The home page ───────────────────────────────────────────────────
107
107
 
108
- export function typeListPage(shell: Shell, stored: { id: string; name: string }[], external: { id: string; name: string }[]): Html {
109
- const { mount } = shell;
110
- const list = (items: { id: string; name: string }[], base: string) =>
111
- html`<ul class="stack">${items.map(t => html`<li><a href="${mount}/${base}/${t.id}">${t.name}</a></li>`)}</ul>`;
108
+ export interface HomeSection {
109
+ title: string;
110
+ line?: string;
111
+ items: { href: string; name: string }[];
112
+ }
113
+
114
+ /** The home page: the sections given, in order, each with its line and its items; a section with no items is not drawn. */
115
+ export function homePage(shell: Shell, sections: HomeSection[]): Html {
116
+ const drawn = sections.filter(s => s.items.length > 0);
112
117
  return page(shell, {
113
- title: 'Content',
118
+ title: shell.siteName,
114
119
  body:
115
- stored.length === 0 && external.length === 0
120
+ drawn.length === 0
116
121
  ? html`<p>There is nothing here you may read.</p>`
117
- : html`${stored.length > 0 ? html`<section class="stack"><h2>Types</h2>${list(stored, 't')}</section>` : ''}${
118
- external.length > 0 ? html`<section class="stack"><h2>Tables</h2>${list(external, 'x')}</section>` : ''
119
- }`
122
+ : html`${drawn.map(
123
+ s => html`<section class="stack"><h2>${s.title}</h2>${s.line ? html`<p>${s.line}</p>` : ''}<ul class="stack">${s.items.map(
124
+ i => html`<li><a href="${i.href}">${i.name}</a></li>`
125
+ )}</ul></section>`
126
+ )}`
120
127
  });
121
128
  }
122
129
 
130
+ // ── Columns ─────────────────────────────────────────────────────────
131
+
132
+ /** A column of a list after the display name: a field (with its meta) or a built-in column (without). */
133
+ export interface ListColumn {
134
+ name: string;
135
+ label: string;
136
+ meta?: FieldMeta;
137
+ /** The host's own text for the cell. */
138
+ value?: (row: any) => string | null | undefined;
139
+ }
140
+
141
+ /** One cell: a field as its read form (a reference as the referred name, a select as its label), a status as a badge. */
142
+ function cell(column: ListColumn, value: unknown, refs: ResolveRef, row?: unknown): Html {
143
+ if (column.value) {
144
+ const text = column.value(row);
145
+ return text ? html`<span>${text}</span>` : html`<span data-tone="secondary" data-variant="ghost">—</span>`;
146
+ }
147
+ if (column.name === 'status' || column.name === 'state') return typeof value === 'string' ? statusBadge(value) : html``;
148
+ if (column.meta) return renderField({ name: column.name, meta: column.meta, value, refs }, 'read');
149
+ return value === undefined || value === null || value === '' ? html`<span data-tone="secondary" data-variant="ghost">—</span>` : html`<span>${String(value)}</span>`;
150
+ }
151
+
152
+ // ── What belongs to an entry ────────────────────────────────────────
153
+
154
+ /** A read-only list on an entry's or row's page: its children of one type, or a host's related rows. */
155
+ export interface Belonging {
156
+ label: string;
157
+ items: { href: string | null; label: string; status?: string }[];
158
+ /** How many there are in all; more than `items` when the list is cut short. */
159
+ total: number;
160
+ /** Where the whole list is, when there is such a page. */
161
+ more?: string;
162
+ }
163
+
164
+ function belongings(lists: Belonging[]): Html {
165
+ return html`${lists.map(
166
+ b => html`<section class="stack" aria-label="${b.label}"><h2>${b.label}</h2>${
167
+ b.items.length === 0
168
+ ? html`<p>None.</p>`
169
+ : html`<ul class="stack">${b.items.map(
170
+ i => html`<li>${i.href ? html`<a href="${i.href}">${i.label}</a>` : html`<span>${i.label}</span>`}${i.status ? html` ${statusBadge(i.status)}` : ''}</li>`
171
+ )}</ul>${
172
+ b.total > b.items.length
173
+ ? html`<p>${b.more ? html`<a href="${b.more}">and ${b.total - b.items.length} more</a>` : html`and ${b.total - b.items.length} more`}</p>`
174
+ : ''
175
+ }`
176
+ }</section>`
177
+ )}`;
178
+ }
179
+
123
180
  // ── An entry list ───────────────────────────────────────────────────
124
181
 
125
182
  export interface ListInput {
@@ -132,6 +189,13 @@ export interface ListInput {
132
189
  limit: number;
133
190
  hasNext: boolean;
134
191
  canCreate: boolean;
192
+ /** The parent entry the list is filtered to, by id. */
193
+ parent: string;
194
+ /** The columns the host named; absent shows slug, status, owner and when updated. */
195
+ columns?: ListColumn[];
196
+ /** Each entry's name, by id. */
197
+ titles: Map<string, string>;
198
+ refs: ResolveRef;
135
199
  notice?: string;
136
200
  }
137
201
 
@@ -146,8 +210,10 @@ export function entryListPage(shell: Shell, input: ListInput): Html {
146
210
  const { mount } = shell;
147
211
  const { type } = input;
148
212
  const base = `${mount}/t/${type.id}`;
149
- const title = (e: Entry) => String(e.data[type.displayField] ?? '') || e.slug || e.id;
150
- const filters = { q: input.q, status: input.status, owner: input.owner };
213
+ const title = (e: Entry) => input.titles.get(e.id) ?? (String(e.data[type.displayField] ?? '') || e.slug || e.id);
214
+ const filters = { q: input.q, status: input.status, owner: input.owner, parent: input.parent };
215
+ const columns = input.columns;
216
+ const valueOf = (e: Entry, name: string): unknown => (name in e && name !== 'data' ? (e as unknown as Record<string, unknown>)[name] : e.data[name]);
151
217
  return page(shell, {
152
218
  title: type.name,
153
219
  crumbs: [],
@@ -157,18 +223,26 @@ export function entryListPage(shell: Shell, input: ListInput): Html {
157
223
  <input type="search" name="q" value="${input.q}" placeholder="Slug" aria-label="Search by slug">
158
224
  <select name="status" aria-label="Status" data-autosubmit><option value="">Any status</option><option value="published"${input.status === 'published' ? ' selected' : ''}>Published</option><option value="draft"${input.status === 'draft' ? ' selected' : ''}>Draft</option></select>${
159
225
  type.owned ? html`<input type="text" name="owner" value="${input.owner}" placeholder="Owner id" aria-label="Owner">` : ''
160
- }
226
+ }${input.parent ? html`<input type="hidden" name="parent" value="${input.parent}">` : ''}
161
227
  <button type="submit" data-variant="outline">Filter</button>${
162
- input.q || input.status || input.owner ? html`<a href="${base}">Clear</a>` : ''
228
+ input.q || input.status || input.owner || input.parent ? html`<a href="${base}">Clear</a>` : ''
163
229
  }${input.canCreate ? html`<a href="${base}/new">New ${type.name.replace(/s$/, '')}</a>` : ''}
164
230
  </form>${
165
231
  input.entries.length === 0
166
232
  ? html`<p>No entries.</p>`
167
233
  : html`
168
234
  <table>
169
- <thead><tr><th scope="col">${type.fieldMeta[type.displayField]?.label ?? 'Title'}</th><th scope="col">Slug</th><th scope="col">Status</th>${type.owned ? html`<th scope="col">Owner</th>` : ''}<th scope="col">Updated</th></tr></thead>
235
+ <thead><tr><th scope="col">${type.fieldMeta[type.displayField]?.label ?? 'Title'}</th>${
236
+ columns
237
+ ? columns.map(col => html`<th scope="col">${col.label}</th>`)
238
+ : html`<th scope="col">Slug</th><th scope="col">Status</th>${type.owned ? html`<th scope="col">Owner</th>` : ''}<th scope="col">Updated</th>`
239
+ }</tr></thead>
170
240
  <tbody>${input.entries.map(
171
- e => html`<tr><td><a href="${base}/${e.id}">${title(e)}</a></td><td>${e.slug ?? ''}</td><td>${statusBadge(e.status)}</td>${type.owned ? html`<td>${e.owner_id ?? ''}</td>` : ''}<td>${e.updated_at}</td></tr>`
241
+ e => html`<tr><td><a href="${base}/${e.id}">${title(e)}</a></td>${
242
+ columns
243
+ ? columns.map(col => html`<td>${cell(col, valueOf(e, col.name), input.refs, e)}</td>`)
244
+ : html`<td>${e.slug ?? ''}</td><td>${statusBadge(e.status)}</td>${type.owned ? html`<td>${e.owner_id ?? ''}</td>` : ''}<td>${e.updated_at}</td>`
245
+ }</tr>`
172
246
  )}</tbody>
173
247
  </table>`
174
248
  }
@@ -228,6 +302,10 @@ export interface EntryInput {
228
302
  canUnpublish: boolean;
229
303
  canDelete: boolean;
230
304
  actions: AdminAction<any>[];
305
+ /** Its children and the host's related rows. */
306
+ belongings: Belonging[];
307
+ /** The entry's name. */
308
+ title?: string;
231
309
  notice?: string;
232
310
  problems?: string[];
233
311
  }
@@ -236,7 +314,7 @@ export function entryPage(shell: Shell, input: EntryInput): Html {
236
314
  const { mount } = shell;
237
315
  const { type, entry } = input;
238
316
  const base = `${mount}/t/${type.id}`;
239
- const title = entry ? String(entry.data[type.displayField] ?? '') || entry.slug || entry.id : `New ${type.name.replace(/s$/, '')}`;
317
+ const title = entry ? input.title || String(entry.data[type.displayField] ?? '') || entry.slug || entry.id : `New ${type.name.replace(/s$/, '')}`;
240
318
  const action = entry ? `${base}/${entry.id}` : base;
241
319
  const fields = fieldRows(input.rows, input.columns, input.refs, type.id);
242
320
  const markdown = input.rows.some(r => r.editable && r.meta.fieldType === 'markdown');
@@ -266,7 +344,7 @@ export function entryPage(shell: Shell, input: EntryInput): Html {
266
344
  input.canDelete
267
345
  ? html`<form method="get" action="${base}/${entry.id}/delete"><button type="submit" data-tone="danger" data-variant="outline">Delete</button></form>`
268
346
  : ''
269
- }</section>`
347
+ }</section>${belongings(input.belongings)}`
270
348
  : ''
271
349
  }`
272
350
  });
@@ -311,17 +389,26 @@ export function externalListPage(
311
389
  rows: ExternalRow[],
312
390
  offset: number,
313
391
  limit: number,
314
- hasNext: boolean
392
+ hasNext: boolean,
393
+ columns?: ListColumn[],
394
+ refs?: ResolveRef
315
395
  ): Html {
316
396
  const base = `${shell.mount}/x/${collection.id}`;
317
397
  const label = (r: ExternalRow) => String(r[collection.displayField] ?? '') || r.id;
398
+ const resolve: ResolveRef = refs ?? ((_, id) => ({ href: null, label: id }));
318
399
  return page(shell, {
319
400
  title: collection.name,
320
401
  body: html`${
321
402
  rows.length === 0
322
403
  ? html`<p>No rows.</p>`
323
- : html`<table><thead><tr><th scope="col">${collection.fields[collection.displayField]?.meta.label ?? 'Name'}</th><th scope="col">State</th></tr></thead><tbody>${rows.map(
324
- r => html`<tr><td><a href="${base}/${r.id}">${label(r)}</a></td><td>${statusBadge(typeof r.state === 'string' ? r.state : r.status)}</td></tr>`
404
+ : html`<table><thead><tr><th scope="col">${collection.fields[collection.displayField]?.meta.label ?? 'Name'}</th>${
405
+ columns ? columns.map(col => html`<th scope="col">${col.label}</th>`) : html`<th scope="col">State</th>`
406
+ }</tr></thead><tbody>${rows.map(
407
+ r => html`<tr><td><a href="${base}/${r.id}">${label(r)}</a></td>${
408
+ columns
409
+ ? columns.map(col => html`<td>${cell(col, r[col.name], resolve, r)}</td>`)
410
+ : html`<td>${statusBadge(typeof r.state === 'string' ? r.state : r.status)}</td>`
411
+ }</tr>`
325
412
  )}</tbody></table>`
326
413
  }<nav class="cluster" aria-label="Pages">${offset > 0 ? html`<a href="${base}${query({ offset: Math.max(offset - limit, 0) })}">Previous</a>` : ''}${
327
414
  hasNext ? html`<a href="${base}${query({ offset: offset + limit })}">Next</a>` : ''
@@ -335,7 +422,8 @@ export function externalRowPage(
335
422
  row: ExternalRow,
336
423
  refs: ResolveRef,
337
424
  actions: AdminAction<any>[],
338
- notice?: string
425
+ notice?: string,
426
+ related: Belonging[] = []
339
427
  ): Html {
340
428
  const base = `${shell.mount}/x/${collection.id}`;
341
429
  const title = String(row[collection.displayField] ?? '') || row.id;
@@ -355,6 +443,6 @@ export function externalRowPage(
355
443
  a => html`<form method="get" action="${base}/${row.id}/do/${a.id}"><button type="submit" data-tone="${a.destructive ? 'danger' : 'secondary'}" data-variant="${a.destructive ? 'outline' : 'solid'}">${a.label}</button></form>`
356
444
  )}</section>`
357
445
  : ''
358
- }`
446
+ }${belongings(related)}`
359
447
  });
360
448
  }
@@ -30,7 +30,10 @@ export interface FieldMeta {
30
30
  /** A system field is writable only by a caller whose rule lists it by name. */
31
31
  system: boolean;
32
32
  refContentType?: string;
33
+ /** A select's stored values, in order. */
33
34
  options?: string[];
35
+ /** A select's label for each value that has one; a value with none is shown as itself. */
36
+ optionLabels?: Record<string, string>;
34
37
  /** Stored when a create leaves the field out; set by the store, so a system field may have one. */
35
38
  default?: unknown;
36
39
  }
@@ -85,6 +88,10 @@ export interface StoredFile {
85
88
  url: string;
86
89
  }
87
90
 
91
+ /** A select's option: its stored value, or the value with the label the admin shows for it. */
92
+ export type SelectOption = string | { readonly value: string; readonly label: string };
93
+ type OptionValue<O> = O extends string ? O : O extends { readonly value: infer V extends string } ? V : never;
94
+
88
95
  export const field = {
89
96
  text: (label: string) => make<string>(label, 'text', { type: 'string' }),
90
97
  longText: (label: string) => make<string>(label, 'longText', { type: 'string' }),
@@ -96,8 +103,18 @@ export const field = {
96
103
  number: (label: string) => make<number>(label, 'number', { type: 'number' }),
97
104
  /** An ISO 8601 date and time. */
98
105
  date: (label: string) => make<string>(label, 'date', { type: 'string', format: 'date-time' }),
99
- select: <const O extends readonly string[]>(label: string, opts: { options: O }) =>
100
- make<O[number]>(label, 'select', { type: 'string', enum: [...opts.options] }, { options: [...opts.options] }),
106
+ /**
107
+ * One of a fixed list. An option is its stored value, or `{ value, label }`: the admin shows and offers the label
108
+ * and stores the value.
109
+ */
110
+ select: <const O extends readonly SelectOption[]>(label: string, opts: { options: O }) => {
111
+ const values = opts.options.map(o => (typeof o === 'string' ? o : o.value));
112
+ const labelled = opts.options.filter((o): o is Exclude<SelectOption, string> => typeof o !== 'string');
113
+ return make<OptionValue<O[number]>>(label, 'select', { type: 'string', enum: values }, {
114
+ options: values,
115
+ ...(labelled.length > 0 ? { optionLabels: Object.fromEntries(labelled.map(o => [o.value, o.label])) } : {})
116
+ });
117
+ },
101
118
  /** The id of an entry of another type. */
102
119
  ref: (contentType: string, label: string) =>
103
120
  make<string>(label, 'reference', { type: 'string' }, { refContentType: contentType }),
package/src/core/index.ts CHANGED
@@ -14,7 +14,7 @@ export type {
14
14
  CmsConfig, ContentType, ContentTypeOptions, Fields, GroupConfig, Infer, ServiceAction, ServiceConfig
15
15
  } from './config/define.ts';
16
16
  export { field, FieldBuilder } from './config/field.ts';
17
- export type { FieldMeta, FieldType, StoredFile } from './config/field.ts';
17
+ export type { FieldMeta, FieldType, SelectOption, StoredFile } from './config/field.ts';
18
18
 
19
19
  export { OPERATIONS } from './access/can.ts';
20
20
  export type { AccessContext, AccessFn, Can, Grant, GrantOptions } from './access/can.ts';