tablo-core 0.9.0

The core toolkit types for Tablo, a server-rendered admin toolkit on Topcoat and Toasty.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! The shared create/edit form page and the create page.

use topcoat::{
    Result,
    context::Cx,
    icon::icon,
    router::{Body, error::forbidden},
    view::{BoxView, StaticClass, ViewExt, attributes, class, view},
};

use super::{
    super::{
        bar::{ActionBar, record_bar},
        gate::{gate, return_target},
    },
    related::{open_related, render_related},
};
pub(super) use crate::form::VERSION_FIELD;
use crate::{
    form::{FieldErrors, FormValues},
    panel::record_nav::{RecordNavLink, record_navigation, render_record_navigation},
    resource::{Mounted, Places, PublicLink, Resource},
    schema::{FormMask, Schema},
    t,
    topcoat_compat::async_page,
};

/// The row of a form page's buttons.
const FOOTER: StaticClass = class!("flex items-center gap-2");

/// The row of a framed form's buttons: the dialog's footer, edge to edge over `[data-pick-page]`'s
/// padding and in view while a long form scrolls behind it, which fades out above the row.
const PICK_FOOTER: StaticClass = class!(
    "sticky bottom-0 z-10 -mx-6 -mb-6 flex items-center gap-2 bg-card px-6 pt-2 pb-6",
    "before:pointer-events-none before:absolute before:inset-x-0 before:-top-4 before:h-4",
    "before:bg-linear-to-t before:from-card before:to-transparent",
);

/// What a form page shows around its form: the create page and the edit page differ only here.
pub(crate) struct FormChrome<'a> {
    title: String,
    submit_label: String,
    /// Whether the submit button renders destructive: an action that confirms, whose page says
    /// it cannot be undone.
    destructive: bool,
    public_link: Option<PublicLink>,
    /// The header's action buttons: the edit page's record actions and Delete.
    actions: ActionBar,
    /// Where Cancel leads without a `?return=` target: the resource's list, or the page of a
    /// header action.
    cancel: String,
    /// Hidden `(name, value)` controls the submit carries besides the schema's.
    hidden: Vec<(String, String)>,
    /// Where a searchable choice fetches its options: an action's input route, or `None` for the
    /// resource's own.
    options: Option<String>,
    /// Whether the form edits a stored record.
    editing: bool,
    /// Whether a second submit button saves and lands on a fresh create form.
    another: bool,
    /// The relationship seeds of the create page's query, which the form posts back to and a
    /// create-another save lands on.
    seed: Vec<(String, String)>,
    /// The record's relation tables, under the form.
    relations: Vec<BoxView<'a>>,
    /// The record's navigation, under the header.
    navigation: Vec<RecordNavLink>,
    /// The sections of the record's related forms, inside the form after its own fields.
    related: Vec<BoxView<'a>>,
    /// Whether the page refuses a save because another changed the record since the form opened.
    stale: bool,
    /// The message a record fn declined the write with, shown above the fields.
    declined: Option<String>,
    /// The keys the request's field access hides or locks.
    mask: FormMask,
    /// Whether a creatable choice's dialog frames the create page: Cancel closes the dialog, the
    /// form does not save another, and it posts back in pick mode.
    picking: bool,
}

impl<'a> FormChrome<'a> {
    pub(super) fn create<R: Resource>(cx: &Cx, resource: &Mounted<R>) -> Self {
        let picking = crate::panel::url::picking(cx);
        Self {
            title: t!(
                cx,
                "tablo-form-create-title",
                label = resource.label.clone()
            ),
            submit_label: t!(cx, "tablo-form-create"),
            destructive: false,
            public_link: None,
            actions: ActionBar::default(),
            cancel: resource.url.clone(),
            hidden: Vec::new(),
            options: None,
            editing: false,
            another: !picking,
            seed: seed_pairs(cx, resource),
            relations: Vec::new(),
            navigation: Vec::new(),
            related: Vec::new(),
            stale: false,
            declined: None,
            mask: resource.form_mask(cx),
            picking,
        }
    }

    /// The edit page's: its header carries the record's actions placed on [`Places::EDIT`], each
    /// landing back on the page, and its Delete, and its form the record's version.
    pub(super) fn edit<R: Resource>(cx: &Cx, resource: &Mounted<R>, record: &R::Model) -> Self {
        use crate::ModelColumns as _;
        let hidden = record
            .version()
            .map(|version| (VERSION_FIELD.to_string(), version.to_string()))
            .into_iter()
            .collect();
        Self {
            title: t!(cx, "tablo-form-edit-title", label = resource.label.clone()),
            submit_label: t!(cx, "tablo-form-save"),
            destructive: false,
            public_link: resource.public_link(cx, record),
            // An action lands back on the page as it was left, its own `?return=` included.
            actions: {
                let uri = topcoat::router::request::uri(cx);
                let here = uri
                    .path_and_query()
                    .map_or(uri.path(), |full| full.as_str());
                record_bar(cx, resource, record, Places::EDIT, here)
            },
            cancel: resource.url.clone(),
            hidden,
            options: None,
            editing: true,
            another: false,
            seed: Vec::new(),
            relations: Vec::new(),
            navigation: record_navigation(cx, resource, record),
            related: Vec::new(),
            stale: false,
            declined: None,
            mask: resource.form_mask(cx),
            picking: false,
        }
    }

    /// Marks the page as refusing a save another save overtook: the form shows the alert, and
    /// carries the version it was rendered from, so saving again overwrites.
    pub(super) fn stale(mut self) -> Self {
        self.stale = true;
        self
    }

    /// Shows `message`, the reason a record fn declined the write, above the fields.
    pub(super) fn declined(mut self, message: String) -> Self {
        self.declined = Some(message);
        self
    }

    /// Renders `relations` under the form.
    pub(super) fn with_relations(mut self, relations: Vec<BoxView<'a>>) -> Self {
        self.relations = relations;
        self
    }

    /// Renders `related`, the sections of the record's related forms, inside the form.
    pub(super) fn with_related(mut self, related: Vec<BoxView<'a>>) -> Self {
        self.related = related;
        self
    }

    /// A settings page's, such as the profile: titled `title`, saved by `Save`, cancelled to
    /// `home`.
    pub(crate) fn settings(cx: &Cx, title: String, home: String) -> Self {
        Self {
            title,
            submit_label: t!(cx, "tablo-form-save"),
            destructive: false,
            public_link: None,
            actions: ActionBar::default(),
            cancel: home,
            hidden: Vec::new(),
            options: None,
            editing: true,
            another: false,
            seed: Vec::new(),
            relations: Vec::new(),
            navigation: Vec::new(),
            related: Vec::new(),
            stale: false,
            declined: None,
            mask: FormMask::default(),
            picking: false,
        }
    }

    /// The chrome with `actions` in its header: a settings page's header actions.
    pub(crate) fn with_actions(mut self, actions: ActionBar) -> Self {
        self.actions = actions;
        self
    }

    /// The chrome with the hidden `(name, value)` control the submit carries besides the schema's.
    pub(crate) fn with_hidden(mut self, name: &str, value: String) -> Self {
        self.hidden.push((name.to_string(), value));
        self
    }

    /// An action's input page: titled `title`, submitted by the action's `label`, carrying
    /// `hidden` back to the action's POST, cancelled to `cancel`, and searching its choices'
    /// options at `options`.
    pub(crate) fn action(
        title: String,
        label: String,
        destructive: bool,
        hidden: Vec<(String, String)>,
        cancel: String,
        options: String,
    ) -> Self {
        Self {
            title,
            submit_label: label,
            destructive,
            public_link: None,
            actions: ActionBar::default(),
            cancel,
            hidden,
            options: Some(options),
            editing: false,
            another: false,
            seed: Vec::new(),
            relations: Vec::new(),
            navigation: Vec::new(),
            related: Vec::new(),
            stale: false,
            declined: None,
            mask: FormMask::default(),
            picking: false,
        }
    }
}

/// Each upload a refused submission carries to the next one: its field's name and its path.
pub(crate) type Carried = std::collections::BTreeSet<(String, String)>;

/// Renders the shared form shell with `FormChrome`, carrying uploader-answered uploads as hidden
/// `keep_<field>` controls.
pub(crate) async fn render_form_page<'a>(
    cx: &'a Cx,
    schema: &Schema,
    chrome: FormChrome<'a>,
    values: &FormValues,
    errors: &FieldErrors,
    carried: &Carried,
) -> Result<BoxView<'a>> {
    let FormChrome {
        title,
        submit_label,
        destructive,
        public_link,
        actions,
        cancel,
        hidden,
        options,
        editing,
        another,
        seed,
        relations,
        related,
        stale,
        declined,
        mask,
        picking,
        navigation,
    } = chrome;
    // A hidden field's value never reaches the page, nor its error, which could tell it: an
    // alert says only that the save cannot pass.
    let unseen = errors.iter().any(|error| mask.hides(&error.key));
    let values = &mask.shown(values);
    let mut source = crate::schema::Source::form(values, errors)
        .editing(editing)
        .masked(mask)
        .flat(picking);
    if let Some(url) = options {
        source = source.options_at(url);
    }
    let form_html = schema.render(cx, source).await?;
    let action = form_action(cx, &seed, picking);
    let cancel = return_target(cx).unwrap_or(cancel);
    // A refused save keeps what the user typed, which leaving the page would lose.
    let dirty = !errors.is_empty() || stale || declined.is_some();
    let has_actions = public_link.is_some() || !actions.is_empty();
    let actions = actions.render(cx);
    let enctype: Option<String> = schema
        .fields()
        .any(|field| field.is_file())
        .then(|| "multipart/form-data".to_string());
    let csrf = crate::csrf::current_token(cx);
    // Carried paths are signed for their field, and re-verified against the store before use.
    let mut carried_fields: Vec<BoxView<'a>> = Vec::new();
    for (name, path) in carried {
        let control = format!("keep_{name}");
        let path = crate::upload::carry::seal(cx, name, path);
        carried_fields.push(view! { <input type="hidden" name=(control) value=(path)> }.boxed());
    }
    for (name, value) in hidden {
        carried_fields.push(view! { <input type="hidden" name=(name) value=(value)> }.boxed());
    }
    let submit_variant = if destructive {
        tablo_ui::ButtonVariant::Destructive
    } else {
        tablo_ui::ButtonVariant::Primary
    };
    let footer = if picking { PICK_FOOTER } else { FOOTER };
    let form = view! {
        <form
            method="post"
            action=(action)
            enctype=(enctype)
            class="flex flex-col gap-4"
            data-form-guard=(t!(cx, "tablo-form-unsaved"))
            data-form-dirty=(dirty)
        >
            (crate::csrf::field(cx, &csrf))
            if stale {
                tablo_ui::alert(
                    variant: tablo_ui::AlertVariant::Destructive,
                    attrs: attributes! { role="alert" data-stale="" },
                    tablo_ui::alert_title((t!(cx, "tablo-form-stale")))
                    tablo_ui::alert_description(
                        (t!(cx, "tablo-form-stale-description"))
                    )
                )
            }
            if let Some(message) = declined {
                tablo_ui::alert(
                    variant: tablo_ui::AlertVariant::Destructive,
                    attrs: attributes! { role="alert" data-declined="" },
                    tablo_ui::alert_description((message))
                )
            }
            if unseen {
                tablo_ui::alert(
                    variant: tablo_ui::AlertVariant::Destructive,
                    attrs: attributes! { role="alert" data-unseen-error="" },
                    tablo_ui::alert_title((t!(cx, "tablo-form-unseen")))
                    tablo_ui::alert_description(
                        (t!(cx, "tablo-form-unseen-description"))
                    )
                )
            }
            for carried in carried_fields {
                (carried)
            }
            (form_html)
            for related in related {
                (related)
            }
            <footer class=(footer)>
                tablo_ui::button(
                    variant: submit_variant,
                    attrs: attributes! { type="submit" },
                    (submit_label)
                )
                if another {
                    tablo_ui::button(
                        variant: tablo_ui::ButtonVariant::Outline,
                        attrs: attributes! {
                            type="submit"
                            name=(crate::form::ANOTHER_FIELD)
                            value="1"
                        },
                        (t!(cx, "tablo-form-create-another"))
                    )
                }
                if picking {
                    tablo_ui::button(
                        variant: tablo_ui::ButtonVariant::Outline,
                        attrs: attributes! { type="button" data-pick-cancel="" },
                        (t!(cx, "tablo-cancel"))
                    )
                } else {
                    <a
                        (crate::navigation::runtime_link(cx, &cancel))
                        class=(tablo_ui::button_variants(
                            tablo_ui::ButtonVariant::Outline,
                            tablo_ui::ButtonSize::Md,
                        ))
                    >
                        (t!(cx, "tablo-cancel"))
                    </a>
                }
            </footer>
        </form>
    }
    .boxed();
    // The dialog around a framed form titles it and owns its width.
    if picking {
        return Ok(form);
    }
    Ok(view! {
        tablo_ui::page(
            tablo_ui::page_header(
                tablo_ui::page_title((title.clone()))
                if destructive {
                    tablo_ui::page_description((t!(cx, "tablo-confirm-irreversible")))
                }
                if has_actions {
                    tablo_ui::page_actions(
                        if let Some(link) = public_link {
                            <a
                                href=(link.url)
                                class=(tablo_ui::button_variants(
                                    tablo_ui::ButtonVariant::Outline,
                                    tablo_ui::ButtonSize::Md,
                                ))
                            >
                                icon(data: tablo_ui::icons::EXTERNAL_LINK)
                                (link.label.localize(cx))
                            </a>
                        }
                        (actions)
                    )
                }
            )
            (render_record_navigation(cx, navigation))
            tablo_ui::page_content(
                (form)
                for relation in relations {
                    (relation)
                }
            )
        )
    }
    .boxed())
}

/// Where the form posts, and where a create-another save lands: the current path with `seed`,
/// the create page's relationship seeds, pick mode when `picking`, and a validated `?return=`
/// target.
pub(super) fn form_action(cx: &Cx, seed: &[(String, String)], picking: bool) -> String {
    let path = topcoat::router::request::uri(cx).path();
    let mut query = form_urlencoded::Serializer::new(String::new());
    query.extend_pairs(seed);
    if picking {
        query.append_pair(crate::panel::url::PICK_PARAM, "1");
    }
    if let Some(target) = return_target(cx) {
        query.append_pair(crate::panel::url::RETURN_PARAM, &target);
    }
    let query = query.finish();
    if query.is_empty() {
        path.to_string()
    } else {
        format!("{path}?{query}")
    }
}

/// Renders the create page, seeding only relationship controls from query parameters.
pub(crate) fn resource_create<R: Resource>(cx: &Cx, _body: Body) -> BoxView<'_> {
    async_page(async move {
        let resource = gate::<R>(cx)?;
        if !resource.can_create(cx) {
            return Err(forbidden().into());
        }
        crate::csrf::ensure_token(cx);
        let related = open_related(cx, &resource, None, &mut crate::db::db(cx)?).await?;
        let html = render_form_page(
            cx,
            &resource.form,
            FormChrome::create(cx, &resource).with_related(render_related(cx, &related)),
            &seeded_values(cx, &resource),
            &FieldErrors::new(),
            &Carried::new(),
        )
        .await?;
        Ok(html)
    })
}

/// The create form's values: each field's default, under what the query parameters post for a
/// relationship control, read as the form posts it: a multiple choice's every value, a single
/// one's last.
fn seeded_values<R: Resource>(cx: &Cx, resource: &Mounted<R>) -> FormValues {
    let schema = &resource.form;
    let mut values = schema.defaults();
    // Only relationship choices post here, and only a repeater's key refuses a read.
    values.extend(
        schema
            .read(cx, &seed_pairs(cx, resource))
            .unwrap_or_default(),
    );
    values
}

/// The query parameters that seed a relationship control of the create form: one a relation
/// table's create link sets to its owner. A control that is or may be disabled stores its
/// default, which the form must show, and a masked one takes what the user may not choose, so
/// neither is seedable.
pub(super) fn seed_pairs<R: Resource>(cx: &Cx, resource: &Mounted<R>) -> Vec<(String, String)> {
    let mask = resource.form_mask(cx);
    let seedable: Vec<&str> = resource
        .form
        .fields()
        .filter(|field| {
            !field.may_post_nothing_on_create()
                && mask.writes(field.name()).is_none()
                && field
                    .as_choice()
                    .is_some_and(|choice| choice.is_relationship())
        })
        .map(|field| field.name())
        .collect();
    query_pairs(cx, &seedable)
}

/// The current query's `(name, value)` pairs whose name is one of `names`, in order.
fn query_pairs(cx: &Cx, names: &[&str]) -> Vec<(String, String)> {
    let query = topcoat::router::request::uri(cx)
        .query()
        .unwrap_or_default();
    form_urlencoded::parse(query.as_bytes())
        .filter(|(name, _)| names.contains(&name.as_ref()))
        .map(|(name, value)| (name.into_owned(), value.into_owned()))
        .collect()
}