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
//! The action buttons a page header carries: a list's or a page's header actions, and one
//! record's custom actions and Delete on its detail and edit pages.
use tablo_ui::{
ButtonSize, ButtonVariant, alert_dialog, button, dialog_content, dialog_description,
dialog_footer, dialog_header, dialog_title,
};
use topcoat::{context::Cx, icon::icon, runtime::Event, view::*};
use crate::{
HeaderActions,
panel::{
input::{InputDialog, Posts, open_dialog},
url::{
action_options_url, bulk_action_url, delete_action_url, row_action_url, with_return,
},
},
policy::Ability,
resource::{HeaderEntry, InputSpec, Mounted, Places, Resource},
t,
};
/// The buttons of one page header, each a POST of its own form.
#[derive(Default)]
pub(crate) struct ActionBar {
buttons: Vec<ActionButton>,
}
/// One button: a direct POST, one a confirmation dialog asks for first, or one whose dialog asks
/// for the action's input.
struct ActionButton {
label: String,
/// The POST target, with its `?return=` when the write lands back on this page.
url: String,
/// The dialog's title, when the button confirms first.
confirm: Option<String>,
delete: bool,
/// The action's input, when it asks for one.
input: Option<Input>,
}
/// What an action's input dialog renders.
struct Input {
spec: InputSpec,
/// Whether the action confirms, which its dialog then says.
confirm: bool,
/// The action's options route.
options: String,
}
impl Input {
/// The input of an action that `takes_input`, confirming when `confirm`, whose options route
/// sits under `url` as `name`'s.
fn of(spec: InputSpec, confirm: bool, url: &str, name: &str) -> Option<Self> {
spec.takes_input.then(|| Self {
spec,
confirm,
options: action_options_url(url, name),
})
}
}
/// The bar of `actions` under `url`, a list's or a page's, each the request may run: one that
/// `allowed` and its own [`can_run`](crate::HeaderAction::can_run) admit.
pub(crate) fn header_bar(
cx: &Cx,
url: &str,
actions: &HeaderActions,
allowed: impl Fn(&HeaderEntry) -> bool,
) -> ActionBar {
let buttons = actions
.entries()
.iter()
.filter(|action| allowed(action) && (action.can_run)(cx))
.map(|action| ActionButton {
label: (action.label)(cx),
url: bulk_action_url(url.trim_end_matches('/'), action.name),
// An action with input confirms in its input dialog, which its button opens.
confirm: (action.confirm && !action.input.takes_input)
.then(|| t!(cx, "tablo-confirm-run")),
delete: false,
input: Input::of(action.input, action.confirm, url, action.name),
})
.collect();
ActionBar { buttons }
}
/// The bar of `record`'s actions placed at `place`, each landing back on `here`, then its Delete,
/// which lands on the list: each one the policy allows on the record, as its row would offer it.
/// A trashed record offers its restore and force delete only, and the force delete lands on the
/// list too.
pub(crate) fn record_bar<R: Resource>(
cx: &Cx,
resource: &Mounted<R>,
record: &R::Model,
place: Places,
here: &str,
) -> ActionBar {
// A composite key resolves as no URL id: its record has no action route.
if !resource.table.is_addressable() || !resource.can(cx, Ability::View(record)) {
return ActionBar::default();
}
let key = resource.table.key_of(record);
let mut buttons: Vec<ActionButton> = resource
.actions
.entries()
.iter()
.filter(|action| {
action.places.contains(place)
&& resource.can(cx, action.resource_wide)
&& action.allows(resource, cx, record)
})
.map(|action| {
let url = row_action_url(&resource.url, &key, action.name);
ActionButton {
label: (action.label)(cx),
url: if action.removes {
url
} else {
with_return(&url, here)
},
confirm: (action.confirm && !action.input.takes_input)
.then(|| t!(cx, action.prompt)),
delete: action.removes,
input: Input::of(action.input, action.confirm, &resource.url, action.name),
}
})
.collect();
if !resource.is_trashed(record)
&& resource.can(cx, Ability::DeleteAny)
&& resource.can(cx, Ability::Delete(record))
{
buttons.push(ActionButton {
label: t!(cx, "tablo-action-delete"),
url: delete_action_url(&resource.url, &key),
confirm: Some(t!(cx, "tablo-confirm-delete-record")),
delete: true,
input: None,
});
}
ActionBar { buttons }
}
impl ActionBar {
/// One button labelled `label`, whose dialog asks for `input` and posts it to `url`,
/// searching its choices' options at `options`.
pub(crate) fn input(label: String, url: String, input: InputSpec, options: String) -> Self {
Self {
buttons: vec![ActionButton {
label,
url,
confirm: None,
delete: false,
input: Some(Input {
spec: input,
confirm: false,
options,
}),
}],
}
}
/// Sends each button's write back to `target`.
pub(crate) fn returning_to(mut self, target: &str) -> Self {
for action in &mut self.buttons {
action.url = with_return(&action.url, target);
}
self
}
/// Whether the bar holds no button.
pub(crate) fn is_empty(&self) -> bool {
self.buttons.is_empty()
}
/// Renders each button as its own form for a page header's
/// [`page_actions`](tablo_ui::page_actions), and a confirming one beside its dialog.
pub(crate) fn render(self, cx: &Cx) -> BoxView<'_> {
if self.buttons.is_empty() {
return ().boxed();
}
let csrf = crate::csrf::ensure_token(cx);
let controls: Vec<BoxView<'_>> = self
.buttons
.into_iter()
.map(|action| {
let id = dialog_id(&action.url, action.input.is_some());
action.render(cx, &csrf, id)
})
.collect();
view! {
cx =>
for control in controls {
(control)
}
}
.boxed()
}
}
/// The DOM id of the dialog confirming a POST to `url`, or asking for its `input`: a page that
/// renders several bars, or one twice, never opens one button's dialog from another's.
fn dialog_id(url: &str, input: bool) -> String {
use std::hash::{BuildHasher, BuildHasherDefault, DefaultHasher};
let hash = BuildHasherDefault::<DefaultHasher>::default().hash_one(url);
let kind = if input { "input" } else { "confirm" };
format!("action-{kind}-{hash:016x}")
}
impl ActionButton {
/// A direct button submits its own form. A confirming one opens the alert dialog `id`, whose
/// form carries the `confirm=1` marker the handlers require; Cancel and Escape close it. One
/// asking for input opens the dialog `id` holding its input form.
fn render<'a>(self, cx: &'a Cx, csrf: &str, id: String) -> BoxView<'a> {
let token = crate::csrf::field(cx, csrf);
let Self {
label,
url,
confirm,
delete,
input,
} = self;
// Delete is outlined like its neighbors and tinted, so the one irreversible button reads
// as a warning without outweighing the page's primary action.
let variant = ButtonVariant::Outline;
let tone = delete.then_some(DELETE_TONE);
let confirm_label = label.clone();
let face = view! {
cx =>
if delete {
icon(data: tablo_ui::icons::TRASH)
}
(label)
}
.boxed();
if let Some(Input {
spec,
confirm,
options,
}) = input
{
let dialog = InputDialog {
id: id.clone(),
title: confirm_label.clone(),
label: confirm_label,
confirm,
input: spec.schema,
options,
}
.render(cx, Posts::To(url.clone()));
return view! {
cx =>
button(
variant: variant,
size: ButtonSize::Md,
attrs: {
let mut attrs = open_dialog(cx, &id, None);
attrs.extend(attributes! { cx => class=(tone) });
attrs
},
(face)
)
(dialog)
}
.boxed();
}
let Some(title) = confirm else {
return view! {
cx =>
<form method="post" action=(url) class="contents">
(token)
button(
variant: variant,
size: ButtonSize::Md,
attrs: attributes! { type="submit" class=(tone) },
(face)
)
</form>
}
.boxed();
};
let title_id = format!("{id}-title");
let description_id = format!("{id}-description");
let dialog = id.clone();
view! {
cx =>
button(
variant: variant,
size: ButtonSize::Md,
attrs: attributes! {
type="button"
class=(tone)
aria-haspopup="dialog"
@click=$(|_e: Event| {
raw!(
"document.getElementById(String(${dialog})).showModal()",
(),
);
})
},
(face)
)
alert_dialog(
open: false,
attrs: attributes! {
id=(id)
aria-labelledby=(title_id.clone())
aria-describedby=(description_id.clone())
},
<form method="post" action=(url) class="contents">
(token)
<input type="hidden" name="confirm" value="1">
dialog_content(
dialog_header(
dialog_title(attrs: attributes! { id=(title_id) }, (title))
dialog_description(
attrs: attributes! { id=(description_id) },
(t!(cx, "tablo-confirm-irreversible"))
)
)
dialog_footer(
button(
variant: ButtonVariant::Outline,
size: ButtonSize::Md,
attrs: attributes! { type="submit" formmethod="dialog" },
(t!(cx, "tablo-cancel"))
)
button(
variant: ButtonVariant::Destructive,
size: ButtonSize::Md,
attrs: attributes! { type="submit" },
(confirm_label)
)
)
)
</form>
)
}
.boxed()
}
}
/// Delete's tint over the outline button, marked important to win over the outline's own colors.
const DELETE_TONE: &str = "text-destructive! hover:bg-destructive/5! active:bg-destructive/10!";