trebol 0.1.0 → 0.1.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.
Files changed (106) hide show
  1. package/README.md +8 -2
  2. package/dist/cli.js +55 -20
  3. package/dist/lib/core/access.d.ts +10 -0
  4. package/dist/lib/core/access.js +210 -0
  5. package/dist/lib/core/base.d.ts +4 -0
  6. package/dist/lib/core/base.js +50 -0
  7. package/dist/lib/core/bitset.d.ts +9 -0
  8. package/dist/lib/core/bitset.js +75 -0
  9. package/dist/lib/core/boot.d.ts +18 -0
  10. package/dist/lib/core/boot.js +72 -0
  11. package/dist/lib/core/compare.d.ts +9 -0
  12. package/dist/lib/core/compare.js +156 -0
  13. package/dist/lib/core/ctx.d.ts +36 -0
  14. package/dist/lib/core/ctx.js +251 -0
  15. package/dist/lib/core/dec.d.ts +28 -0
  16. package/dist/lib/core/dec.js +158 -0
  17. package/dist/lib/core/errors.d.ts +15 -0
  18. package/dist/lib/core/errors.js +44 -0
  19. package/dist/lib/core/escapes.d.ts +14 -0
  20. package/dist/lib/core/escapes.js +80 -0
  21. package/dist/lib/core/eval.d.ts +220 -0
  22. package/dist/lib/core/eval.js +666 -0
  23. package/dist/lib/core/func.d.ts +64 -0
  24. package/dist/lib/core/func.js +129 -0
  25. package/dist/lib/core/hash.d.ts +5 -0
  26. package/dist/lib/core/hash.js +155 -0
  27. package/dist/lib/core/index.d.ts +18 -0
  28. package/dist/lib/core/index.js +20 -0
  29. package/dist/lib/core/int64.d.ts +22 -0
  30. package/dist/lib/core/int64.js +113 -0
  31. package/dist/lib/core/kinds.d.ts +67 -0
  32. package/dist/lib/core/kinds.js +134 -0
  33. package/dist/lib/core/lexer.d.ts +13 -0
  34. package/dist/lib/core/lexer.js +702 -0
  35. package/dist/lib/core/mezz.d.ts +1 -0
  36. package/dist/lib/core/mezz.js +360 -0
  37. package/dist/lib/core/mold.d.ts +20 -0
  38. package/dist/lib/core/mold.js +434 -0
  39. package/dist/lib/core/natives/control.d.ts +1 -0
  40. package/dist/lib/core/natives/control.js +374 -0
  41. package/dist/lib/core/natives/csv.d.ts +15 -0
  42. package/dist/lib/core/natives/csv.js +315 -0
  43. package/dist/lib/core/natives/data.d.ts +11 -0
  44. package/dist/lib/core/natives/data.js +367 -0
  45. package/dist/lib/core/natives/define.d.ts +17 -0
  46. package/dist/lib/core/natives/define.js +47 -0
  47. package/dist/lib/core/natives/encode.d.ts +3 -0
  48. package/dist/lib/core/natives/encode.js +187 -0
  49. package/dist/lib/core/natives/json.d.ts +5 -0
  50. package/dist/lib/core/natives/json.js +273 -0
  51. package/dist/lib/core/natives/math.d.ts +3 -0
  52. package/dist/lib/core/natives/math.js +500 -0
  53. package/dist/lib/core/natives/parse.d.ts +1 -0
  54. package/dist/lib/core/natives/parse.js +600 -0
  55. package/dist/lib/core/natives/series.d.ts +4 -0
  56. package/dist/lib/core/natives/series.js +509 -0
  57. package/dist/lib/core/natives/text.d.ts +1 -0
  58. package/dist/lib/core/natives/text.js +484 -0
  59. package/dist/lib/core/natives/xml.d.ts +9 -0
  60. package/dist/lib/core/natives/xml.js +242 -0
  61. package/dist/lib/core/series.d.ts +26 -0
  62. package/dist/lib/core/series.js +73 -0
  63. package/dist/lib/core/symbols.d.ts +9 -0
  64. package/dist/lib/core/symbols.js +44 -0
  65. package/dist/lib/core/values.d.ts +206 -0
  66. package/dist/lib/core/values.js +223 -0
  67. package/dist/lib/host/browser.d.ts +4 -0
  68. package/dist/lib/host/browser.js +4081 -0
  69. package/dist/lib/host/chart.d.ts +26 -0
  70. package/dist/lib/host/chart.js +158 -0
  71. package/dist/lib/host/compress.d.ts +1 -0
  72. package/dist/lib/host/compress.js +37 -0
  73. package/dist/lib/host/draw.d.ts +31 -0
  74. package/dist/lib/host/draw.js +348 -0
  75. package/dist/lib/host/icons.d.ts +3 -0
  76. package/dist/lib/host/icons.js +97 -0
  77. package/dist/lib/host/index.d.ts +38 -0
  78. package/dist/lib/host/index.js +190 -0
  79. package/dist/lib/host/locale.d.ts +40 -0
  80. package/dist/lib/host/locale.js +241 -0
  81. package/dist/lib/host/markdown.d.ts +28 -0
  82. package/dist/lib/host/markdown.js +151 -0
  83. package/dist/lib/host/modules.d.ts +10 -0
  84. package/dist/lib/host/modules.js +262 -0
  85. package/dist/lib/host/natives.d.ts +6 -0
  86. package/dist/lib/host/natives.js +98 -0
  87. package/dist/lib/host/net.d.ts +12 -0
  88. package/dist/lib/host/net.js +541 -0
  89. package/dist/lib/host/page.d.ts +9 -0
  90. package/dist/lib/host/page.js +55 -0
  91. package/dist/lib/host/rows.d.ts +15 -0
  92. package/dist/lib/host/rows.js +80 -0
  93. package/dist/lib/host/scheduler.d.ts +45 -0
  94. package/dist/lib/host/scheduler.js +173 -0
  95. package/dist/lib/host/theme.d.ts +58 -0
  96. package/dist/lib/host/theme.js +1462 -0
  97. package/dist/lib/ui/i18n.trb +111 -0
  98. package/dist/lib/ui/ui.trb +1237 -0
  99. package/dist/trebol.js +123 -56
  100. package/package.json +17 -5
  101. package/src/host/browser.ts +274 -33
  102. package/src/host/index.ts +2 -2
  103. package/src/host/modules.ts +8 -1
  104. package/src/host/theme.ts +63 -4
  105. package/src/ui/i18n.trb +4 -0
  106. package/src/ui/ui.trb +48 -15
@@ -0,0 +1,1237 @@
1
+ Trebol [
2
+ title: "UI dialect: faces, styles, layout and validation"
3
+ exports: [
4
+ face styles stylize layout view theme looks open close toast confirm print-page
5
+ validate-face field-error form-values set-values ui-pane ui-input ui-files ui-remove-file ui-lookup-search ui-lookup-pick ui-lookup-remove ui-blur ui-click ui-select ui-sort ui-close ui-event ui-menu ui-drop ui-reorder ui-crumb ui-expand
6
+ go go-back go-forward can-go-back? can-go-forward? ui-route set-language ui-rows
7
+ ]
8
+ ]
9
+
10
+ ; --- faces ---------------------------------------------------------------------
11
+ ;
12
+ ; A face is an object. The host keeps one DOM node per face and copies these
13
+ ; fields to it after every scheduler slice, so code changes a face by
14
+ ; setting a field: `total/text: "42"`.
15
+
16
+ face: make object! [
17
+ style: 'face ; what the host renders
18
+ name: none ; field name: the key in `values`, and a word in handlers
19
+ text: none ; label, caption or content
20
+ value: none ; a field's value; also a progress (0-1 or percent) or an image (file or url)
21
+ hint: none ; placeholder
22
+ role: none ; primary accent danger success warning muted
23
+ size: none ; small medium large
24
+ align: none ; left center right spread
25
+ valign: none ; a row's children, top middle bottom; none lines them up on the controls
26
+ cols: none ; columns of a grid: a number, or the weight of each [1 4 1]
27
+ icon: none
28
+ fill: false
29
+ fit: false ; takes the height the window has left, and its children scroll inside
30
+ pad: none ; inner and between-children spacing, a step of the spacing scale (0-7)
31
+ gap: none
32
+ narrow-only: false ; shown only on narrow screens
33
+ wide-only: false ; hidden on narrow screens
34
+ hidden: false
35
+ disabled: false
36
+ readonly: false ; shown and sent, but the user cannot change it, and no rule applies
37
+ inert: false ; shown as it is, but neither it nor its children can be used
38
+ linked: false ; a tile that stands for something kept elsewhere: marked with an arrow
39
+ round: false ; an image: cut to a circle, as for a person's photo
40
+ poster: none ; a video: the image shown before it plays
41
+ captions: none ; a video: its captions, a .vtt file, or a block of one per language [en %a.vtt es %b.vtt]
42
+ looping: false ; a video or a sound starts again when it ends (loop in the dialect)
43
+ silent: false ; a video or a sound plays without its sound (muted in the dialect)
44
+ autoplay: false ; a video or a sound plays on its own, always silent, as browsers allow
45
+ drag: false ; can be dragged: carries its value, or the row in a table, list or tree
46
+ drop: false ; accepts drops (on-drop)
47
+ reorder: false ; its children or rows can be moved around (on-reorder)
48
+ rules: none ; validation: a block of [rule args...] blocks
49
+ error: none ; current message, shown once the face is touched
50
+ touched: false
51
+ pending: false ; an asynchronous check is running
52
+ sent: none ; an upload or photo: how far its files went up, 0-1; none hides the bar
53
+ async-error: none ; result of the last asynchronous check
54
+ action: none ; the block after the face: click, change
55
+ events: none ; [on-change [...] on-select [...]]
56
+ shortcut: none ; a key that activates a button or link: "Ctrl+S"
57
+ menu: none ; context menu (or the menu a button opens): a block of menu items; on-menu runs
58
+ options: none ; choices of a choice or radio; what a lookup's search found
59
+ known: none ; a lookup: what each value shows as, a map; a value not in it shows as itself
60
+ links: none ; a lookup: the screen each value links to when readonly, a map
61
+ query: none ; a lookup: the text last searched for
62
+ currency: none ; a money field's currency code: EUR
63
+ calc: none ; a formula: the value comes from it and the user cannot change it
64
+ template: none ; rows: the block of the fields of one row
65
+ built: none ; rows: what its rows were last made from
66
+ owner: none ; a row of rows: the rows face it belongs to
67
+ typed: none ; what the user typed in a number, money or date field when it is not one;
68
+ ; in an upload or photo, the names of the files it refused
69
+ multiple: false ; an upload or a lookup takes more than one
70
+ accept: none ; the files an upload takes: ["pdf" "image/*"]; a photo, images unless told
71
+ pane: none
72
+ columns: none ; table, list and tree: [field "Label" ...]; a column of images, [field ["Label" image round]]
73
+ data: none ; table, list and tree: a block of map! or object! rows (a tree's
74
+ ; have their own in `children`); breadcrumb: its items
75
+ key: 'id ; the row field that identifies a row
76
+ selected: none ; the row last chosen
77
+ sortable: false ; a table the host sorts, from the rows it has, when a header is chosen
78
+ sorted: none ; a table: the order shown, [column asc] or [column desc]
79
+ totals: none ; a table: a row shown at the foot, never chosen nor sorted
80
+ kind: none ; a chart: bar line area pie
81
+ stacked: false ; a chart: its series one on top of another
82
+ draw: none ; a canvas: the block of the draw dialect it shows
83
+ extent: none ; a canvas: the size of its drawing space, a pair
84
+ form: none ; the enclosing form, if any
85
+ root: none ; the view this face belongs to
86
+ submit: false
87
+ ]
88
+
89
+ ; style name -> prototype face
90
+ styles: make map! []
91
+
92
+ container-styles: [view column row form panel grid dialog scroll split toolbar desktop]
93
+ input-styles: [field password number money date area rich check toggle choice radio upload photo lookup rows]
94
+ data-styles: [table list tree chart]
95
+
96
+ foreach [name base] [
97
+ view column column column row row form form panel panel grid grid scroll scroll desktop desktop
98
+ title title heading heading text text divider divider
99
+ field field password password number number money money date date area area rich rich check check choice choice
100
+ toggle toggle radio radio upload upload photo photo lookup lookup progress progress image image backdrop backdrop icon icon
101
+ video video audio audio
102
+ button button link link tile tile table table list list tabs tabs tab tab
103
+ toolbar toolbar breadcrumb breadcrumb tree tree screens screens screen screen rows rows entry entry
104
+ canvas canvas chart chart
105
+ ] [
106
+ poke styles name make face [style: base]
107
+ ]
108
+ poke styles 'view make face [style: 'view]
109
+ poke styles 'submit make face [style: 'button role: 'primary submit: true]
110
+ poke styles 'dialog make face [style: 'dialog hidden: true]
111
+ poke styles 'tabs make face [style: 'tabs value: 1]
112
+ poke styles 'screens make face [style: 'screens entering: false] ; entering: the screen on show is still the old one
113
+ poke styles 'split make face [style: 'split value: 1] ; value: the pane shown on narrow screens
114
+ poke styles 'canvas make face [style: 'canvas extent: 300x150]
115
+ poke styles 'chart make face [style: 'chart kind: 'bar]
116
+
117
+ stylize: function [
118
+ "Defines styles from existing ones: name: base [field: value ...]"
119
+ spec [block!]
120
+ ] [
121
+ parse spec [some [
122
+ set name set-word! set base word! set defaults block! (
123
+ proto: select styles base
124
+ unless proto [do make error! rejoin ["stylize: unknown style " base]]
125
+ poke styles to word! name make proto defaults
126
+ )
127
+ ]]
128
+ ]
129
+
130
+ ; --- layout --------------------------------------------------------------------
131
+
132
+ roles: [primary accent danger success warning muted]
133
+ sizes: [small medium large]
134
+ aligns: [left center right spread]
135
+ valigns: [top middle bottom]
136
+ flags: [fill fit hidden disabled readonly inert narrow-only wide-only linked round drag drop reorder multiple sortable stacked]
137
+ chart-kinds: [bar line area pie]
138
+ simple-rules: [required email url integer]
139
+ letter: charset [#"a" - #"z" #"A" - #"Z"]
140
+ rule-words: [min max length pattern match check check-async calc width pad gap menu accept known links sorted poster captions]
141
+ media-styles: [video audio]
142
+ media-flags: [loop muted autoplay] ; of a video or a sound
143
+
144
+ ; Words that never name a field. email, url and integer are rules too,
145
+ ; but also common field names, so the first word after an input style names it.
146
+ field-name?: func [w [word!]] [
147
+ not any [
148
+ find roles w find sizes w find aligns w find flags w
149
+ find rule-words w w = 'required event-word? w
150
+ ]
151
+ ]
152
+
153
+ add-rule: func [f rule [block!]] [
154
+ unless f/rules [f/rules: copy []]
155
+ append/only f/rules rule
156
+ ]
157
+
158
+ event-word?: func [w [word!]] [
159
+ to logic! find/match form w "on-"
160
+ ]
161
+
162
+ ; Labels (`search: field`, `table clients`) are local to the layout, as R3
163
+ ; planned for its VID: each layout gets an object holding them, and the
164
+ ; layout's blocks (its handlers too) are bound to it. `global search: field`
165
+ ; sets a global word instead.
166
+
167
+ layout: function [
168
+ "Turns a block of the UI dialect into a view face; /only gives the block of faces."
169
+ spec [block!]
170
+ /only "Return the faces, not a view holding them"
171
+ /local ev b w
172
+ ] [
173
+ ; a copy, so laying out the same block twice never shares bindings
174
+ spec: copy/deep spec
175
+ labels: copy []
176
+ either only [
177
+ out: build-faces spec labels
178
+ ] [
179
+ ; on-xxx handlers and its width before the first face belong to the view itself
180
+ events: copy []
181
+ width: none
182
+ parse spec [any [
183
+ set ev word! if (event-word? ev) set b block! (append events reduce [ev b])
184
+ | 'width set w ['small | 'medium | 'large | 'fill] (width: w)
185
+ ] body:]
186
+ out: make select styles 'view [pane: build-faces body labels]
187
+ unless empty? events [out/events: events]
188
+ out/size: width
189
+ ]
190
+ bind-labels spec labels copy []
191
+ out
192
+ ]
193
+
194
+ ; Binds a laid-out block to an object of its labels and the given [word: value ...].
195
+ bind-labels: function [spec [block!] labels [block!] words [block!] /local w f] [
196
+ foreach [w f] labels [append words reduce [to set-word! w f]]
197
+ unless empty? words [bind spec make object! words]
198
+ ]
199
+
200
+ ; Keeps a label for a face: in the layout's labels, or as a global word.
201
+ label-face: function [label labels [block!] global? [logic!] f] [
202
+ either global? [set label f] [
203
+ w: to word! label
204
+ either p: find/skip labels w 2 [p/2: f] [append labels reduce [w f]]
205
+ ]
206
+ ]
207
+
208
+ ; A text of the interface: a string, a text per language (a map), or a word of
209
+ ; the dictionary written as a lit-word ('save). Faces keep it as given and the
210
+ ; host shows it in the active language, so set-language translates them.
211
+ text-rule: [string! | map! | lit-word!]
212
+ text-of: func [t] [either lit-word? :t [to word! t] [t]]
213
+
214
+ ; The faces of a block of the dialect; labels collects [word face ...].
215
+ build-faces: function [
216
+ spec [block!] labels [block!]
217
+ /local label global? style-word f proto n s b w a i v ev p
218
+ ] [
219
+ out: copy []
220
+ modifier: [
221
+ set s [string! | map!] (either f/text [f/hint: s] [f/text: s])
222
+ ; a word of the dictionary: button 'save (in a link, a lit-word is the screen)
223
+ | if (f/style <> 'link) set v lit-word! (v: to word! v either f/text [f/hint: v] [f/text: v])
224
+ | set ev word! if (event-word? ev) set b block! (
225
+ unless f/events [f/events: copy []]
226
+ append f/events reduce [ev b]
227
+ )
228
+ | set b block! (
229
+ case [
230
+ ; grid [1 4 1] [...]: the weight of each column, then the children
231
+ all [f/style = 'grid none? f/pane not empty? b parse b [some integer!]] [f/cols: b]
232
+ ; rows items [...]: the fields of a row; a second block is the action
233
+ all [f/style = 'rows none? f/template] [f/template: b]
234
+ find container-styles f/style [
235
+ ; --- in a toolbar is a separator
236
+ f/pane: build-faces either f/style = 'toolbar [replace/all copy b '--- 'divider] [b] labels
237
+ if all [f/style = 'split 2 <> length? f/pane] [
238
+ do make error! "layout: split takes two faces, one per pane"
239
+ ]
240
+ ]
241
+ find data-styles f/style [f/columns: columns-of b]
242
+ ; the first block holds the items, the second is the action
243
+ all [f/style = 'breadcrumb none? f/data] [f/data: crumbs-of b]
244
+ f/style = 'canvas [f/draw: b]
245
+ f/style = 'tabs [f/pane: tabs-of b labels]
246
+ f/style = 'screens [f/data: screens-of b]
247
+ find [choice radio] f/style [f/options: options-of b]
248
+ true [f/action: b]
249
+ ]
250
+ )
251
+ | set i issue! (f/icon: i)
252
+ | set v [file! | url! | percent!] (f/value: v)
253
+ ; a link to a screen: link "Clients" 'clients, link "Ada" 'client/42
254
+ | set v lit-word! (f/value: to word! v)
255
+ | set v lit-path! (f/value: to path! v)
256
+ | set n integer! (f/cols: n)
257
+ | set p pair! (f/extent: p)
258
+ | set w word! if (all [f/style = 'chart find chart-kinds w]) (f/kind: w)
259
+ | 'min set n [number! | money! | date!] (add-rule f reduce ['min n])
260
+ | 'max set n [number! | money! | date!] (add-rule f reduce ['max n])
261
+ | 'length set a integer! set n integer! (add-rule f reduce ['length a n])
262
+ | 'pattern set b block! (add-rule f reduce ['pattern b])
263
+ | 'accept set b block! (f/accept: b)
264
+ | 'poster set v [file! | url!] (f/poster: v)
265
+ | 'captions set v [file! | url! | block!] (f/captions: v)
266
+ ; muted names a role elsewhere, and loop a function, so a video keeps them in fields of its own
267
+ | set w word! if (all [find media-flags w find media-styles f/style]) (
268
+ poke f select [muted silent loop looping autoplay autoplay] w true
269
+ )
270
+ | 'known set v map! (f/known: v)
271
+ | 'links set v map! (f/links: v)
272
+ | 'match set w word! (add-rule f reduce ['match w])
273
+ | 'check set b block! (add-rule f reduce ['check b])
274
+ | 'check-async set b block! (add-rule f reduce ['check-async b])
275
+ | 'calc set b block! (f/calc: b f/readonly: true)
276
+ | 'width set w word! (f/size: w)
277
+ | set w ['pad | 'gap] [set n integer! | 'none (n: 0) | 'wide (n: 6)] (poke f w spacing n)
278
+ | 'menu set b block! (f/menu: menu-of b)
279
+ | 'key set s string! (f/shortcut: s)
280
+ | 'key set w word! (f/key: w)
281
+ | 'sorted set b block! (f/sorted: sort-order b)
282
+ | set w word! if (find roles w) (f/role: w)
283
+ | set w word! if (find sizes w) (f/size: w)
284
+ | set w word! if (find aligns w) (f/align: w)
285
+ | set w word! if (all [f/style = 'row find valigns w]) (f/valign: w)
286
+ | set w word! if (find flags w) (poke f w true)
287
+ | set w word! if (find simple-rules w) (add-rule f reduce [w])
288
+ ; money price "Price" EUR
289
+ | set w word! if (all [f/style = 'money parse form w [3 letter]]) (f/currency: to word! uppercase form w)
290
+ ]
291
+ unless parse spec [
292
+ any [
293
+ (label: none global?: false)
294
+ opt ['global (global?: true)]
295
+ opt [set label set-word!]
296
+ set style-word word! (
297
+ proto: select styles style-word
298
+ unless proto [do make error! rejoin ["layout: unknown style " style-word]]
299
+ f: make proto []
300
+ append out f
301
+ if label [label-face label labels global? f]
302
+ )
303
+ opt [if (find input-styles f/style) set n word! if (field-name? n) (f/name: n)]
304
+ ; chart bar income [...]: the kind comes before the label
305
+ opt [if (f/style = 'chart) set n word! if (find chart-kinds n) (f/kind: n)]
306
+ ; table clients [...] is the same as clients: table [...]
307
+ opt [if (find data-styles f/style) set n word! if (all [field-name? n n <> 'key]) (label-face n labels global? f)]
308
+ any modifier
309
+ (if f/style = 'rows [rows-fill f])
310
+ ]
311
+ ] [
312
+ do make error! "layout: cannot read the layout"
313
+ ]
314
+ out
315
+ ]
316
+
317
+ spacing: func [n [integer!]] [
318
+ unless all [n >= 0 n <= 7] [do make error! "layout: pad and gap take a step from 0 to 7"]
319
+ n
320
+ ]
321
+
322
+ ; [name "Name" email] -> [name "Name" email "email"]
323
+ ; A column of images keeps what it is with its label: [photo "" image round] -> [photo ["" image round]]
324
+ columns-of: function [spec [block!] /local w label kind] [
325
+ out: copy []
326
+ unless parse spec [any [
327
+ set w word! (label: form w kind: none) opt [set label text-rule]
328
+ opt ['image (kind: copy [image]) opt ['round (append kind 'round)]]
329
+ (
330
+ label: text-of label
331
+ append out w
332
+ append/only out either kind [head insert/only kind label] [label]
333
+ )
334
+ ]] [
335
+ do make error! "layout: table columns are words, each with an optional label, then image or image round"
336
+ ]
337
+ out
338
+ ]
339
+
340
+ ; [name desc] -> [name desc], checked
341
+ sort-order: function [b [block!]] [
342
+ unless parse b [word! ['asc | 'desc]] [
343
+ do make error! "layout: sorted takes a column and asc or desc"
344
+ ]
345
+ copy b
346
+ ]
347
+
348
+ ; ["General" [...] "Advanced" [...]] -> a tab face per pair
349
+ tabs-of: function [spec [block!] labels [block!]] [
350
+ out: copy []
351
+ unless parse spec [any [set label text-rule set b block! (
352
+ label: text-of label
353
+ append out make select styles 'tab [text: label pane: build-faces b labels]
354
+ )]] [
355
+ do make error! "layout: tabs take a label and a block per tab"
356
+ ]
357
+ out
358
+ ]
359
+
360
+ ; [home "Home" [...] client "Client" [id] [...]] -> [name title args body ...]
361
+ screens-of: function [spec [block!] /local n t a b] [
362
+ out: copy []
363
+ unless parse spec [some [
364
+ set n word! set t text-rule (a: copy []) opt [ahead [block! block!] set a block!] set b block! (
365
+ foreach w a [unless word? w [do make error! "layout: the arguments of a screen are words"]]
366
+ append out reduce [n text-of t a b]
367
+ )
368
+ ]] [
369
+ do make error! "layout: screens take a name, a title, an optional block of arguments and a block per screen"
370
+ ]
371
+ out
372
+ ]
373
+
374
+ menu-item: make object! [
375
+ text: none
376
+ icon: none
377
+ id: none ; the word on-menu gets in event/picked
378
+ items: none ; a submenu
379
+ divider: false
380
+ ]
381
+
382
+ ; As in Red: ["Edit" edit #trash "Delete" delete --- "Move to" ["Top" top]] -> menu items
383
+ menu-of: function [spec [block!] /local i label w b] [
384
+ out: copy []
385
+ unless parse spec [any [
386
+ '--- (append out make menu-item [divider: true])
387
+ | (i: none) opt [set i issue!] set label text-rule (label: text-of label) [
388
+ set b block! (append out make menu-item [text: label icon: i items: menu-of b])
389
+ | set w word! (append out make menu-item [text: label icon: i id: w])
390
+ ]
391
+ ]] [
392
+ do make error! "layout: a menu takes a label and a word per item, a block for a submenu, or ---"
393
+ ]
394
+ out
395
+ ]
396
+
397
+ ; ["Home" home "Clients" clients "Ada"]: a label and a word per item; the last one may have no word
398
+ crumbs-of: function [spec [block!] /local label w] [
399
+ unless parse spec [any [set label [string! | map!] set w [word! | path!]] opt [string! | map!]] [
400
+ do make error! "layout: a breadcrumb takes a label and a word per item; only the last may have no word"
401
+ ]
402
+ copy spec
403
+ ]
404
+
405
+ ; The options of a choice or radio: each reduced, and so are those of a group,
406
+ ; a title followed by a block: ["Bikes" ["Road" "City"] "Other"].
407
+ options-of: function [spec [block!]] [
408
+ opts: reduce spec
409
+ forall opts [if block? first opts [change/only opts reduce first opts]]
410
+ opts
411
+ ]
412
+
413
+ ; Links each face to its view and form, so handlers can find `values`.
414
+ attach: function [f root form] [
415
+ f/root: root
416
+ f/form: form
417
+ if f/pane [
418
+ inner: either find [form entry] f/style [f] [form]
419
+ foreach c f/pane [attach c root inner]
420
+ ]
421
+ ]
422
+
423
+ view: function [
424
+ "Shows a layout (a block of the dialect, or a face from layout) as the page."
425
+ spec [block! object!]
426
+ ] [
427
+ root: either block? spec [layout spec] [spec]
428
+ attach root root none
429
+ set 'page root
430
+ entered: copy []
431
+ foreach f all-faces root [
432
+ if f/style = 'screens [
433
+ set 'router f
434
+ append entered follow-link f route
435
+ ]
436
+ ]
437
+ foreach f all-faces root [refresh-form f]
438
+ show root
439
+ foreach s entered [run-enter s]
440
+ root
441
+ ]
442
+
443
+ ; --- screens -------------------------------------------------------------------
444
+ ;
445
+ ; A screens face shows one screen at a time and keeps the page's link in step:
446
+ ; the screen `client [id]` showing id "42" is #/client/42. A route is a block
447
+ ; of the screen name and its arguments as strings, as they come in the link.
448
+
449
+ router: none ; the screens face of the page
450
+ page: none ; the root face of the view on show
451
+
452
+ ; The [name title args body] of the screen a route names, or none.
453
+ find-screen: function [f route [block!] /local n t a b] [
454
+ if any [empty? route not word? first route] [return none]
455
+ foreach [n t a b] f/data [
456
+ if all [n = first route (length? a) = ((length? route) - 1)] [return reduce [n t a b]]
457
+ ]
458
+ none
459
+ ]
460
+
461
+ ; Builds the screen a route names as the only child of f; returns the screen face.
462
+ enter-screen: function [f route [block!] /local ev b] [
463
+ def: find-screen f route
464
+ ; a screen's arguments and labels are local to it
465
+ spec: copy/deep def/4
466
+ labels: copy []
467
+ ; on-xxx handlers before the first face belong to the screen
468
+ events: copy []
469
+ parse spec [any [set ev word! if (event-word? ev) set b block! (append events reduce [ev b])] body:]
470
+ s: make select styles 'screen [text: def/2 pane: build-faces body labels]
471
+ unless empty? events [s/events: events]
472
+ words: copy []
473
+ repeat i length? def/3 [append words reduce [to set-word! pick def/3 i pick route i + 1]]
474
+ bind-labels spec labels words
475
+ f/value: copy route
476
+ f/entering: true
477
+ f/pane: reduce [s]
478
+ ui-pane f
479
+ s
480
+ ]
481
+
482
+ ; Runs what a screen does on entering, and only then lets the host show it: the
483
+ ; page keeps the screen it had until the new one has its data, so nothing paints
484
+ ; half made. If on-enter went elsewhere, the screen it went to decides.
485
+ run-enter: function [s] [
486
+ r: none
487
+ if h: handler-for s 'on-enter [r: try [run-handler s h none]]
488
+ if same? s first router/pane [
489
+ router/entering: false
490
+ ; the screen on show changed, and so may whether back and forward can be used
491
+ if h: handler-for router 'on-change [run-handler router h]
492
+ ]
493
+ if error? :r [do :r]
494
+ ]
495
+
496
+ ; Shows the screen of a link given as a block of strings; a link that names no
497
+ ; screen shows the first one and is replaced by its link.
498
+ follow-link: function [f parts [block!]] [
499
+ route: copy parts
500
+ unless empty? route [route/1: attempt [to word! route/1]]
501
+ unless find-screen f route [
502
+ route: reduce [first f/data]
503
+ set-route route true
504
+ ]
505
+ enter-screen f route
506
+ ]
507
+
508
+ go: function [
509
+ "Shows a screen and gives the page its link: go 'clients, go [client row/id] (the name is not evaluated, the arguments are)."
510
+ target [word! block! path!]
511
+ ] [
512
+ unless router [do make error! "go: the view has no screens"]
513
+ route: reduce [either word? target [target] [first target]]
514
+ if block? target [foreach a reduce next target [append route form a]]
515
+ if path? target [foreach a next to block! target [append route form a]]
516
+ unless find-screen router route [do make error! rejoin ["go: no screen for " mold route]]
517
+ set-route route false
518
+ run-enter enter-screen router route
519
+ ]
520
+
521
+ go-back: function [
522
+ "Returns to the screen the user came from; if they came from outside the page, goes to target."
523
+ target [word! block! path!]
524
+ ] [
525
+ unless route-back [go target]
526
+ ]
527
+
528
+ go-forward: function [
529
+ "Goes to the screen the user came back from, if there is one; true if it went."
530
+ ] [
531
+ route-forward
532
+ ]
533
+
534
+ can-go-back?: function [
535
+ "True if a screen of this page lies behind the current one in the browser's history."
536
+ ] [
537
+ 0 < first route-steps
538
+ ]
539
+
540
+ can-go-forward?: function [
541
+ "True if a screen of this page lies ahead of the current one: the user went back and has not gone elsewhere since."
542
+ ] [
543
+ 0 < second route-steps
544
+ ]
545
+
546
+ ui-route: function [
547
+ "Called by the host when the page's link changes: back, forward or a link."
548
+ f parts [block!]
549
+ ] [
550
+ route: copy parts
551
+ unless empty? route [route/1: attempt [to word! route/1]]
552
+ if route = f/value [exit]
553
+ run-enter follow-link f parts
554
+ ]
555
+
556
+ ui-pane: function [
557
+ "Called by the host when code gave a face a new pane: links the new faces to their view and form."
558
+ f
559
+ ] [
560
+ inner: either find [form entry] f/style [f] [f/form]
561
+ foreach c any [f/pane []] [attach c f/root inner]
562
+ if f/root [refresh-form any [inner f/root]]
563
+ ]
564
+
565
+ open: func [
566
+ "Shows a dialog."
567
+ dialog [object!]
568
+ ] [
569
+ dialog/hidden: false
570
+ ]
571
+
572
+ close: func [
573
+ "Hides a dialog; closes a stream."
574
+ target [object! handle!]
575
+ ] [
576
+ either object? target [target/hidden: true] [close-stream target]
577
+ ]
578
+
579
+ toast: func [
580
+ "Shows a short notice that goes away by itself."
581
+ message [string! map! word!] "A text, a text per language, or a word of the dictionary"
582
+ /role "primary, success, warning or danger"
583
+ kind [word!]
584
+ ] [
585
+ show-toast tr message either role [kind] [none]
586
+ ]
587
+
588
+ confirm: func [
589
+ "Asks a question in a small dialog and waits: true when the user says yes."
590
+ question [string! map! word!] "A text, a text per language, or a word of the dictionary"
591
+ /with "The answers, yes first"
592
+ choices [block!]
593
+ /role "primary (the default) or danger, for what cannot be undone"
594
+ kind [word!]
595
+ ] [
596
+ show-confirm tr question
597
+ either with [tr first choices] [none]
598
+ either with [tr second choices] [none]
599
+ either role [kind] [none]
600
+ ]
601
+
602
+ print-page: func [
603
+ "Opens the browser's print dialog for the screen."
604
+ /only "Print just this face and what is inside it"
605
+ face [object!]
606
+ ] [
607
+ print-faces either only [face] [none]
608
+ ]
609
+
610
+ set-language: func [
611
+ "Changes the active language and shows every face again in it."
612
+ language [word! string!]
613
+ ] [
614
+ i18n/language: either string? language [to word! language] [language]
615
+ ; the messages under the fields were written in the language before
616
+ if page [foreach f all-faces page [refresh-form f]]
617
+ repaint
618
+ ]
619
+
620
+ ; The looks of src/host/theme.ts, with their names: colors, shapes, fonts, shadows and
621
+ ; how the icons are drawn go together. As they are, they make the items of a menu.
622
+ looks: [
623
+ #(en: "Minimal" es: "Minimalista") minimal
624
+ #(en: "Paper" es: "Papel") paper
625
+ #(en: "Civic" es: "Institucional") civic
626
+ #(en: "Soft" es: "Amable") soft
627
+ #(en: "Ink" es: "Tinta") ink
628
+ #(en: "Classic" es: "Clásico") classic
629
+ #(en: "Cupertino" es: "Cupertino") cupertino
630
+ #(en: "Fusion" es: "Fusion") fusion
631
+ #(en: "Fluent" es: "Fluent") fluent
632
+ ]
633
+
634
+ theme: func [
635
+ "Sets the theme: a look by name (theme 'civic), or parameters (theme [look: civic base: 200 mode: dark])."
636
+ spec [block! word!]
637
+ ] [
638
+ apply-theme construct either word? spec [reduce [to set-word! 'look spec]] [spec]
639
+ ]
640
+
641
+ ; --- walking faces -------------------------------------------------------------
642
+
643
+ all-faces: function [f] [
644
+ out: copy []
645
+ collect-faces f out
646
+ out
647
+ ]
648
+
649
+ collect-faces: function [f out] [
650
+ append out f
651
+ if f/pane [foreach c f/pane [collect-faces c out]]
652
+ ]
653
+
654
+ ; Faces that belong to a scope: nested forms keep their own fields and submits.
655
+ own-faces: function [scope] [
656
+ out: reduce [scope]
657
+ if scope/pane [foreach c scope/pane [collect-own c out]]
658
+ out
659
+ ]
660
+
661
+ collect-own: function [f out] [
662
+ append out f
663
+ if all [f/pane f/style <> 'form] [foreach c f/pane [collect-own c out]]
664
+ ]
665
+
666
+ ; The fields of a row are not fields of the form: rows holds their values.
667
+ named-faces: function [scope] [
668
+ out: copy []
669
+ collect-named scope out
670
+ out
671
+ ]
672
+
673
+ collect-named: function [f out] [
674
+ if f/name [append out f]
675
+ if all [f/pane f/style <> 'rows] [foreach c f/pane [collect-named c out]]
676
+ ]
677
+
678
+ form-values: function [
679
+ "The values of the named fields of a form (or view), as a map."
680
+ scope
681
+ ] [
682
+ m: make map! []
683
+ foreach f named-faces scope [poke m f/name f/value]
684
+ m
685
+ ]
686
+
687
+ set-values: function [
688
+ "Fills the named fields of a form (or view) from a map or object; none clears them."
689
+ scope data [map! object! none!]
690
+ ] [
691
+ foreach f named-faces scope [
692
+ v: either data [select data f/name] [none]
693
+ f/value: either logic-face? f [true = v] [v]
694
+ f/touched: false
695
+ f/typed: none
696
+ f/async-error: none
697
+ if f/style = 'rows [
698
+ f/built: none
699
+ rows-fill f
700
+ ]
701
+ ]
702
+ refresh-form scope
703
+ ]
704
+
705
+ field-error: function [
706
+ "Shows a message on a named field until the user changes it: what a server refused. False if there is no such field."
707
+ scope name [word! string!] text [string!]
708
+ ] [
709
+ name: to word! name
710
+ foreach f named-faces scope [
711
+ if f/name = name [
712
+ f/async-error: text
713
+ f/error: text
714
+ f/touched: true
715
+ refresh-form scope
716
+ return true
717
+ ]
718
+ ]
719
+ false
720
+ ]
721
+
722
+ ; --- rows ------------------------------------------------------------------------
723
+ ;
724
+ ; A field made of rows the user adds and removes. Its pane is an entry per row
725
+ ; and the add button last; an entry holds the fields of the template and its
726
+ ; remove button. Each entry is a scope of its own, so `values` in a handler or
727
+ ; a formula of a row is that row. The value is a block with a map per row, or
728
+ ; none when there are no rows; code changes the rows by assigning it.
729
+
730
+ last-entry: 0
731
+
732
+ rule-of: function [f name [word!]] [
733
+ foreach r any [f/rules []] [if r/1 = name [return r/2]]
734
+ none
735
+ ]
736
+
737
+ entries-of: function [f] [
738
+ collect [foreach c any [f/pane []] [if c/style = 'entry [keep c]]]
739
+ ]
740
+
741
+ rows-entry: function [f data] [
742
+ spec: copy/deep f/template
743
+ labels: copy []
744
+ set 'last-entry last-entry + 1
745
+ e: make select styles 'entry [owner: f value: last-entry]
746
+ e/pane: build-faces spec labels
747
+ bind-labels spec labels copy []
748
+ foreach g named-faces e [
749
+ if f/readonly [g/readonly: true]
750
+ if f/disabled [g/disabled: true]
751
+ ]
752
+ if data [set-values e data]
753
+ remove-button: make select styles 'button [icon: #trash]
754
+ remove-button/hint: select i18n/ui 'remove-row
755
+ remove-button/action: compose [rows-remove (f) (e/value)]
756
+ append e/pane remove-button
757
+ e
758
+ ]
759
+
760
+ ; Makes the rows again from the value, if it or the flags changed since they were made.
761
+ rows-fill: function [f] [
762
+ state: reduce [f/value f/readonly f/disabled]
763
+ if all [f/pane state = f/built] [exit]
764
+ entries: map-each d any [f/value []] [rows-entry f d]
765
+ ; `min 2` starts with two empty rows
766
+ while [(length? entries) < any [rule-of f 'min 0]] [append entries rows-entry f none]
767
+ add-button: either f/pane [last f/pane] [
768
+ make select styles 'button [icon: #plus]
769
+ ]
770
+ add-button/text: select i18n/ui 'add-row
771
+ add-button/action: compose [rows-add (f)]
772
+ f/pane: append entries add-button
773
+ if f/root [foreach e entries [attach e f/root f/form]]
774
+ rows-store f
775
+ ]
776
+
777
+ ; Takes the value from the rows and shows only the buttons that can be used.
778
+ rows-store: function [f] [
779
+ entries: entries-of f
780
+ n: length? entries
781
+ f/value: either empty? entries [none] [map-each e entries [form-values e]]
782
+ f/built: reduce [f/value f/readonly f/disabled]
783
+ add-button: last f/pane
784
+ add-button/hidden: f/readonly
785
+ add-button/disabled: any [f/disabled n >= any [rule-of f 'max n + 1]]
786
+ foreach e entries [
787
+ remove-button: last e/pane
788
+ remove-button/hidden: f/readonly
789
+ remove-button/disabled: any [f/disabled n <= any [rule-of f 'min 0]]
790
+ ]
791
+ ]
792
+
793
+ ; A field of a row changed: so did the value of its rows, and of the rows those are in.
794
+ store-around: function [f] [
795
+ e: f/form
796
+ if all [e e/style = 'entry] [
797
+ rows-store e/owner
798
+ store-around e/owner
799
+ ]
800
+ ]
801
+
802
+ rows-add: function [f] [
803
+ e: rows-entry f none
804
+ attach e f/root f/form
805
+ faces: copy f/pane
806
+ insert back tail faces e
807
+ f/pane: faces
808
+ rows-store f
809
+ f/touched: true
810
+ changed f
811
+ ]
812
+
813
+ rows-remove: function [f id] [
814
+ faces: copy f/pane
815
+ ; faces compare by content, so a row is found by its number
816
+ forall faces [
817
+ if faces/1/value = id [
818
+ remove faces
819
+ break
820
+ ]
821
+ ]
822
+ f/pane: head faces
823
+ rows-store f
824
+ f/touched: true
825
+ changed f
826
+ ]
827
+
828
+ ui-rows: function [
829
+ "Called by the host when code gave a rows face a new value or changed its flags."
830
+ f
831
+ ] [
832
+ rows-fill f
833
+ refresh-form any [f/form f/root]
834
+ ]
835
+
836
+ ; Gives calculated fields their value, again while one depends on another.
837
+ recalc: function [scope] [
838
+ faces: own-faces scope
839
+ loop 5 [
840
+ again: false
841
+ foreach f faces [
842
+ if f/calc [
843
+ v: attempt [run-handler f f/calc]
844
+ unless :v = f/value [
845
+ f/value: :v
846
+ store-around f
847
+ again: true
848
+ ]
849
+ ]
850
+ ]
851
+ unless again [break]
852
+ ]
853
+ ]
854
+
855
+ ; --- validation ------------------------------------------------------------------
856
+
857
+ message: function [key args [block!]] [
858
+ out: copy tr select i18n/ui key
859
+ repeat i length? args [replace out rejoin ["#" i] localize pick args i]
860
+ out
861
+ ]
862
+
863
+ digit: charset "0123456789"
864
+ word-char: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9" "._%+-"]
865
+ host-char: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9" "-"]
866
+ email-rule: [some word-char "@" some host-char some ["." some host-char] end]
867
+ url-rule: [["http://" | "https://"] some host-char any ["." some host-char] opt [":" some digit] opt ["/" to end] end]
868
+
869
+ ; check and toggle hold true or false
870
+ logic-face?: func [f] [to logic! find [check toggle] f/style]
871
+
872
+ blank?: func [v] [
873
+ any [none? :v all [string? :v empty? trim copy v]]
874
+ ]
875
+
876
+ label-of: func [f] [either f/text [tr f/text] [form f/name]]
877
+
878
+ list-of: function [items [block!]] [
879
+ out: copy ""
880
+ foreach x items [unless empty? out [append out ", "] append out form x]
881
+ out
882
+ ]
883
+
884
+ ; The message for the first rule a face breaks, or none. Asynchronous checks
885
+ ; are skipped here: their last result is kept in async-error.
886
+ validate-face: function [
887
+ "Checks a face's rules; returns a message or none."
888
+ f
889
+ ] [
890
+ if f/readonly [return none]
891
+ v: f/value
892
+ rules: any [f/rules []]
893
+ if f/style = 'rows [
894
+ n: either v [length? v] [0]
895
+ return case [
896
+ all [n = 0 find/only rules [required]] [message 'required []]
897
+ n < any [rule-of f 'min 0] [message 'rows-min reduce [rule-of f 'min]]
898
+ n > any [rule-of f 'max n] [message 'rows-max reduce [rule-of f 'max]]
899
+ true [none]
900
+ ]
901
+ ]
902
+ if f/typed [
903
+ if f/style = 'upload [return message 'accept reduce [list-of f/accept]]
904
+ if f/style = 'photo [return either f/accept [message 'accept reduce [list-of f/accept]] [message 'images []]]
905
+ return message select [number number money amount date date] f/style []
906
+ ]
907
+ if blank? :v [
908
+ return either find/only rules [required] [message 'required []] [f/async-error]
909
+ ]
910
+ if all [logic-face? f find/only rules [required] not v] [return message 'required []]
911
+ foreach r rules [
912
+ problem: switch first r [
913
+ email [unless parse v email-rule [message 'email []]]
914
+ url [unless parse v url-rule [message 'url []]]
915
+ integer [unless parse form v [opt "-" some digit] [message 'integer []]]
916
+ min [if all [any [number? v money? v date? v] v < r/2] [message 'min reduce [r/2]]]
917
+ max [if all [any [number? v money? v date? v] v > r/2] [message 'max reduce [r/2]]]
918
+ length [
919
+ n: length? form v
920
+ if any [n < r/2 n > r/3] [
921
+ either r/2 = r/3 [message 'length-exact reduce [r/2]] [message 'length reduce [r/2 r/3]]
922
+ ]
923
+ ]
924
+ pattern [unless parse form v r/2 [message 'pattern []]]
925
+ match [
926
+ other: find-named f r/2
927
+ if all [other v <> other/value] [message 'match reduce [label-of other]]
928
+ ]
929
+ check [check-result run-handler f r/2]
930
+ ]
931
+ if problem [return problem]
932
+ ]
933
+ f/async-error
934
+ ]
935
+
936
+ check-result: func [result] [
937
+ case [
938
+ string? :result [result]
939
+ any [none? :result false = :result] [message 'check []]
940
+ true [none]
941
+ ]
942
+ ]
943
+
944
+ find-named: function [f name] [
945
+ scope: any [f/form f/root]
946
+ foreach g named-faces scope [if g/name = name [return g]]
947
+ none
948
+ ]
949
+
950
+ ; Runs asynchronous checks of a face (they may fetch).
951
+ check-async: function [f] [
952
+ foreach r any [f/rules []] [
953
+ if all [r/1 = 'check-async not blank? f/value] [
954
+ f/pending: true
955
+ f/async-error: check-result run-handler f r/2
956
+ f/pending: false
957
+ ]
958
+ ]
959
+ ]
960
+
961
+ ; Updates the errors of touched faces and enables submit buttons only when
962
+ ; every field of the form is valid. A view checks only the faces outside its forms.
963
+ refresh-form: function [scope] [
964
+ ; the fields of a row count for the form its rows are in
965
+ while [all [scope scope/style = 'entry]] [scope: any [scope/form scope/root]]
966
+ unless all [scope find [form view] scope/style] [exit]
967
+ recalc scope
968
+ ok: true
969
+ faces: own-faces scope
970
+ foreach f faces [
971
+ if any [f/rules f/typed f/error] [
972
+ problem: validate-face f
973
+ f/error: either f/touched [problem] [none]
974
+ if problem [ok: false]
975
+ ]
976
+ ]
977
+ foreach f faces [if f/submit [f/disabled: not ok]]
978
+ ]
979
+
980
+ ; --- events --------------------------------------------------------------------
981
+ ;
982
+ ; The host calls these from DOM events, each in its own task.
983
+
984
+ ; A handler block runs with face, value, values and, as in Red, event bound;
985
+ ; event is an object (event/key, event/away? ...) or none. Named fields are
986
+ ; not bound: their values are in values, their faces reached through labels.
987
+ run-handler: function [f body [block!] /with ev [object!] /on node "row is this node, not the selected one"] [
988
+ scope: any [f/form f/root]
989
+ ; /only: a value that is a block stays one block
990
+ words: compose/only [
991
+ face: (f)
992
+ value: (quoted f/value)
993
+ values: (form-values any [scope f])
994
+ ]
995
+ if find data-styles f/style [append words compose [row: (either on [node] [f/selected])]]
996
+ append words compose [event: (either with [ev] [none])]
997
+ ctx: make object! words
998
+ do bind copy/deep body ctx
999
+ ]
1000
+
1001
+ ; A value as it must appear in a block given to make object!
1002
+ quoted: func [v] [
1003
+ case [
1004
+ word? :v [to lit-word! :v]
1005
+ path? :v [to lit-path! :v]
1006
+ true [:v]
1007
+ ]
1008
+ ]
1009
+
1010
+ handler-for: function [f name] [
1011
+ all [f/events select f/events name]
1012
+ ]
1013
+
1014
+ ui-input: function [
1015
+ "Called by the host when the user changes a field."
1016
+ f new
1017
+ ] [
1018
+ f/value: switch/default f/style [
1019
+ number [any [attempt [to decimal! new] none]]
1020
+ ; the host gives the amount as 1234.5, whatever the language
1021
+ money [all [new attempt [to money! rejoin [any [f/currency ""] "$" new]]]]
1022
+ ; and a date as 2026-11-03
1023
+ date [all [new attempt [to date! new]]]
1024
+ check [to logic! new]
1025
+ toggle [to logic! new]
1026
+ ] [new]
1027
+ if f/style = 'number [
1028
+ if all [decimal? f/value f/value = to integer! f/value] [f/value: to integer! f/value]
1029
+ ]
1030
+ ; text that is not a number, amount or date stays on screen, with its message
1031
+ f/typed: all [find [number money date] f/style new none? f/value new]
1032
+ changed f
1033
+ ]
1034
+
1035
+ changed: function [f] [
1036
+ f/async-error: none
1037
+ store-around f
1038
+ refresh-form any [f/form f/root]
1039
+ if f/action [run-handler f f/action]
1040
+ if h: handler-for f 'on-change [run-handler f h]
1041
+ if all [f/form f/form/style = 'entry] [changed f/form/owner]
1042
+ ]
1043
+
1044
+ ; An upload's value is none or a block of objects [name size type file];
1045
+ ; file is a handle the host keeps, read with read-file or sent with multipart.
1046
+ ; A photo's is none, the object of the image chosen, or the address of one kept elsewhere.
1047
+ ui-files: function [
1048
+ "Called by the host when the user chooses or drops files: name size type file for each, and the names it refused."
1049
+ f files [block!] refused [block!]
1050
+ ] [
1051
+ chosen: collect [
1052
+ foreach [n s t h] files [keep make object! [name: n size: s type: t file: h]]
1053
+ ]
1054
+ unless empty? chosen [
1055
+ f/value: case [
1056
+ f/style = 'photo [first chosen]
1057
+ f/multiple [append copy any [f/value []] chosen]
1058
+ true [copy/part chosen 1]
1059
+ ]
1060
+ ]
1061
+ f/typed: either empty? refused [none] [refused]
1062
+ f/touched: true
1063
+ changed f
1064
+ ]
1065
+
1066
+ ui-remove-file: function [
1067
+ "Called by the host when the user removes the file at a position of an upload, or a photo's."
1068
+ f index [integer!]
1069
+ ] [
1070
+ f/value: either f/style = 'photo [none] [
1071
+ files: remove at copy f/value index
1072
+ either empty? head files [none] [head files]
1073
+ ]
1074
+ f/typed: none
1075
+ f/touched: true
1076
+ changed f
1077
+ ]
1078
+
1079
+ ; A lookup's value is none, one value, or with `multiple` a block of them. Its on-search
1080
+ ; answers the options for event/query: maps with value and text, as a choice takes them.
1081
+ ui-lookup-search: function [
1082
+ "Called by the host when the user opens a lookup or types in it."
1083
+ f text [string!]
1084
+ ] [
1085
+ f/query: text
1086
+ found: fire-event f 'on-search compose [query: (text)]
1087
+ ; the user may have typed on while this was answering
1088
+ if f/query = text [f/options: either block? :found [found] [copy []]]
1089
+ ]
1090
+
1091
+ ui-lookup-pick: function [
1092
+ "Called by the host when the user chooses an option of a lookup."
1093
+ f option [map!]
1094
+ ] [
1095
+ unless f/known [f/known: make map! []]
1096
+ poke f/known option/value option/text
1097
+ f/value: either f/multiple [
1098
+ any [
1099
+ all [f/value find f/value option/value f/value]
1100
+ append copy any [f/value []] option/value
1101
+ ]
1102
+ ] [option/value]
1103
+ changed f
1104
+ ]
1105
+
1106
+ ui-lookup-remove: function [
1107
+ "Called by the host when the user removes the value at a position of a multiple lookup."
1108
+ f index [integer!]
1109
+ ] [
1110
+ chosen: head remove at copy f/value index
1111
+ f/value: either empty? chosen [none] [chosen]
1112
+ changed f
1113
+ ]
1114
+
1115
+ ui-blur: function [
1116
+ "Called by the host when the user leaves a field."
1117
+ f
1118
+ ] [
1119
+ f/touched: true
1120
+ check-async f
1121
+ refresh-form any [f/form f/root]
1122
+ ]
1123
+
1124
+ ui-click: function [
1125
+ "Called by the host when the user activates a button."
1126
+ f
1127
+ ] [
1128
+ if f/submit [
1129
+ scope: any [f/form f/root]
1130
+ faces: own-faces scope
1131
+ foreach g faces [if g/rules [g/touched: true]]
1132
+ foreach g faces [check-async g]
1133
+ refresh-form scope
1134
+ if f/disabled [exit]
1135
+ ]
1136
+ if f/action [run-handler f f/action]
1137
+ ]
1138
+
1139
+ ui-select: function [
1140
+ "Called by the host when the user chooses a row of a table or list."
1141
+ f row
1142
+ ] [
1143
+ f/selected: row
1144
+ if h: handler-for f 'on-select [run-handler f h]
1145
+ ]
1146
+
1147
+ ui-sort: function [
1148
+ "Called by the host when the user chooses the header of a column of a table."
1149
+ f column [word!]
1150
+ ] [
1151
+ order: either all [block? f/sorted f/sorted/1 = column f/sorted/2 = 'asc] ['desc] ['asc]
1152
+ f/sorted: reduce [column order]
1153
+ fire-event f 'on-sort reduce [quote column: to lit-word! column quote order: to lit-word! order]
1154
+ ]
1155
+
1156
+ ui-close: function [
1157
+ "Called by the host when the user dismisses a dialog."
1158
+ f
1159
+ ] [
1160
+ f/hidden: true
1161
+ if h: handler-for f 'on-close [run-handler f h]
1162
+ ]
1163
+
1164
+ ; Runs the on-xxx handler of an event with event bound to an object made of
1165
+ ; type (the event name without on-) and the given [word: value ...] fields.
1166
+ fire-event: function [f name [word!] fields [block!]] [
1167
+ if h: handler-for f name [
1168
+ type: to word! skip form name 3
1169
+ run-handler/with f h make object! append reduce [quote type: to lit-word! type] fields
1170
+ ]
1171
+ ]
1172
+
1173
+ ui-event: function [
1174
+ "Called by the host for on-focus, on-unfocus, on-over, on-dbl-click and on-key."
1175
+ f name [word!] fields [block!]
1176
+ ] [
1177
+ fire-event f name fields
1178
+ ]
1179
+
1180
+ ui-crumb: function [
1181
+ "Called by the host when the user chooses an item of a breadcrumb."
1182
+ f item [word! path!]
1183
+ ] [
1184
+ f/value: item
1185
+ if f/action [run-handler f f/action]
1186
+ ]
1187
+
1188
+ ui-menu: function [
1189
+ "Called by the host when the user chooses a menu item; row is the row it was opened on."
1190
+ f item row
1191
+ ] [
1192
+ if row [f/selected: row]
1193
+ fire-event f 'on-menu reduce [quote picked: quoted item/id]
1194
+ ]
1195
+
1196
+ ui-drop: function [
1197
+ "Called by the host when something is dropped on a face; index is where it goes (1-based), parent the tree node."
1198
+ f dropped source index [integer!] parent
1199
+ ] [
1200
+ fields: reduce [quote dropped: quoted :dropped quote source: source quote index: index]
1201
+ if f/style = 'tree [append fields reduce [quote parent: parent]]
1202
+ fire-event f 'on-drop fields
1203
+ ]
1204
+
1205
+ ui-reorder: function [
1206
+ "Called by the host when the user moves a child or row of a face; in a tree, among the children of parent."
1207
+ f from [integer!] to [integer!] parent
1208
+ ] [
1209
+ fields: reduce [quote from: from quote to: to]
1210
+ case [
1211
+ f/style = 'tree [
1212
+ either parent [parent/children: move-item parent/children from to] [f/data: move-item f/data from to]
1213
+ f/data: f/data ; repaint the tree
1214
+ append fields reduce [quote parent: parent]
1215
+ ]
1216
+ find data-styles f/style [f/data: move-item f/data from to]
1217
+ true [f/pane: move-item f/pane from to]
1218
+ ]
1219
+ fire-event f 'on-reorder fields
1220
+ ]
1221
+
1222
+ ui-expand: function [
1223
+ "Called by the host when the user opens a node of a tree."
1224
+ f node
1225
+ ] [
1226
+ if h: handler-for f 'on-expand [
1227
+ run-handler/with/on f h make object! [type: 'expand] node
1228
+ ]
1229
+ ]
1230
+
1231
+ move-item: function [items [block!] from [integer!] to [integer!]] [
1232
+ items: copy items
1233
+ x: pick items from
1234
+ remove at items from
1235
+ insert/only at items to :x
1236
+ items
1237
+ ]