trebol 0.1.1 → 0.1.2

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 (84) hide show
  1. package/README.md +10 -1
  2. package/dist/cli.js +13747 -11409
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/boot.js +2 -1
  12. package/dist/lib/core/func.d.ts +4 -0
  13. package/dist/lib/core/natives/control.js +1 -1
  14. package/dist/lib/core/natives/series.js +11 -1
  15. package/dist/lib/core/natives/text.js +3 -0
  16. package/dist/lib/host/browser.js +951 -58
  17. package/dist/lib/host/devlog.d.ts +11 -0
  18. package/dist/lib/host/devlog.js +13 -0
  19. package/dist/lib/host/devtools.d.ts +23 -0
  20. package/dist/lib/host/devtools.js +626 -0
  21. package/dist/lib/host/fonts/OFL.txt +93 -0
  22. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  23. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  24. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  25. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  26. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  27. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  28. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  29. package/dist/lib/host/fonts.d.ts +11 -0
  30. package/dist/lib/host/fonts.js +29 -0
  31. package/dist/lib/host/index.d.ts +2 -0
  32. package/dist/lib/host/index.js +3 -1
  33. package/dist/lib/host/js.d.ts +5 -0
  34. package/dist/lib/host/js.js +130 -0
  35. package/dist/lib/host/markdown.d.ts +2 -0
  36. package/dist/lib/host/markdown.js +10 -0
  37. package/dist/lib/host/modules.d.ts +4 -0
  38. package/dist/lib/host/modules.js +39 -1
  39. package/dist/lib/host/net.js +9 -1
  40. package/dist/lib/host/orbit.d.ts +121 -0
  41. package/dist/lib/host/orbit.js +374 -0
  42. package/dist/lib/host/rows.d.ts +14 -0
  43. package/dist/lib/host/rows.js +29 -0
  44. package/dist/lib/host/scheduler.d.ts +6 -0
  45. package/dist/lib/host/scheduler.js +60 -36
  46. package/dist/lib/host/theme.js +207 -2
  47. package/dist/lib/ui/i18n.trb +6 -0
  48. package/dist/lib/ui/test.trb +565 -0
  49. package/dist/lib/ui/ui.trb +523 -82
  50. package/dist/trebol.js +1346 -124
  51. package/editors/vscode/README.md +15 -0
  52. package/editors/vscode/language-configuration.json +28 -0
  53. package/editors/vscode/package.json +42 -0
  54. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  55. package/package.json +7 -3
  56. package/src/core/boot.ts +1 -1
  57. package/src/core/func.ts +4 -0
  58. package/src/core/natives/control.ts +1 -1
  59. package/src/core/natives/series.ts +9 -1
  60. package/src/core/natives/text.ts +2 -0
  61. package/src/host/browser.ts +962 -51
  62. package/src/host/devlog.ts +16 -0
  63. package/src/host/devtools.ts +641 -0
  64. package/src/host/fonts/OFL.txt +93 -0
  65. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  66. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  67. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  68. package/src/host/fonts/inter-greek.woff2 +0 -0
  69. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  70. package/src/host/fonts/inter-latin.woff2 +0 -0
  71. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  72. package/src/host/fonts.ts +39 -0
  73. package/src/host/index.ts +3 -1
  74. package/src/host/js.ts +127 -0
  75. package/src/host/markdown.ts +9 -0
  76. package/src/host/modules.ts +33 -1
  77. package/src/host/net.ts +9 -1
  78. package/src/host/orbit.ts +432 -0
  79. package/src/host/rows.ts +30 -0
  80. package/src/host/scheduler.ts +62 -36
  81. package/src/host/theme.ts +207 -2
  82. package/src/ui/i18n.trb +6 -0
  83. package/src/ui/test.trb +565 -0
  84. package/src/ui/ui.trb +523 -82
package/src/ui/ui.trb CHANGED
@@ -2,8 +2,8 @@ Trebol [
2
2
  title: "UI dialect: faces, styles, layout and validation"
3
3
  exports: [
4
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
5
+ validate-face field-error form-values set-values insert-text 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 ui-orbit
6
+ go go-back go-forward can-go-back? can-go-forward? unsaved unsaved? ui-route set-language ui-rows ui-check ui-react
7
7
  ]
8
8
  ]
9
9
 
@@ -37,6 +37,8 @@ face: make object! [
37
37
  inert: false ; shown as it is, but neither it nor its children can be used
38
38
  linked: false ; a tile that stands for something kept elsewhere: marked with an arrow
39
39
  round: false ; an image: cut to a circle, as for a person's photo
40
+ tag: none ; a widget: the name of the element it is, as the page's JavaScript defines it
41
+ props: none ; a widget: the element's properties, an object
40
42
  poster: none ; a video: the image shown before it plays
41
43
  captions: none ; a video: its captions, a .vtt file, or a block of one per language [en %a.vtt es %b.vtt]
42
44
  looping: false ; a video or a sound starts again when it ends (loop in the dialect)
@@ -60,7 +62,7 @@ face: make object! [
60
62
  links: none ; a lookup: the screen each value links to when readonly, a map
61
63
  query: none ; a lookup: the text last searched for
62
64
  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
65
+ reactions: none ; react, is and calc: a block of reaction objects
64
66
  template: none ; rows: the block of the fields of one row
65
67
  built: none ; rows: what its rows were last made from
66
68
  owner: none ; a row of rows: the rows face it belongs to
@@ -71,7 +73,7 @@ face: make object! [
71
73
  pane: none
72
74
  columns: none ; table, list and tree: [field "Label" ...]; a column of images, [field ["Label" image round]]
73
75
  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
76
+ ; have their own in `children`); breadcrumb: its items; orbit: its focus, nodes, edges and trail
75
77
  key: 'id ; the row field that identifies a row
76
78
  selected: none ; the row last chosen
77
79
  sortable: false ; a table the host sorts, from the rows it has, when a header is chosen
@@ -83,6 +85,7 @@ face: make object! [
83
85
  extent: none ; a canvas: the size of its drawing space, a pair
84
86
  form: none ; the enclosing form, if any
85
87
  root: none ; the view this face belongs to
88
+ place: none ; where the face was written, file:line:col; none if made at run time
86
89
  submit: false
87
90
  ]
88
91
 
@@ -90,7 +93,7 @@ face: make object! [
90
93
  styles: make map! []
91
94
 
92
95
  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]
96
+ input-styles: [field password number money date area rich check toggle choice radio upload photo lookup rows widget]
94
97
  data-styles: [table list tree chart]
95
98
 
96
99
  foreach [name base] [
@@ -98,10 +101,10 @@ foreach [name base] [
98
101
  title title heading heading text text divider divider
99
102
  field field password password number number money money date date area area rich rich check check choice choice
100
103
  toggle toggle radio radio upload upload photo photo lookup lookup progress progress image image backdrop backdrop icon icon
101
- video video audio audio
104
+ video video audio audio widget widget
102
105
  button button link link tile tile table table list list tabs tabs tab tab
103
106
  toolbar toolbar breadcrumb breadcrumb tree tree screens screens screen screen rows rows entry entry
104
- canvas canvas chart chart
107
+ canvas canvas chart chart orbit orbit
105
108
  ] [
106
109
  poke styles name make face [style: base]
107
110
  ]
@@ -121,7 +124,7 @@ stylize: function [
121
124
  parse spec [some [
122
125
  set name set-word! set base word! set defaults block! (
123
126
  proto: select styles base
124
- unless proto [do make error! rejoin ["stylize: unknown style " base]]
127
+ unless proto [do make error! rejoin ["stylize: unknown style " base ui-suggest base words-of styles]]
125
128
  poke styles to word! name make proto defaults
126
129
  )
127
130
  ]]
@@ -137,7 +140,7 @@ flags: [fill fit hidden disabled readonly inert narrow-only wide-only linked rou
137
140
  chart-kinds: [bar line area pie]
138
141
  simple-rules: [required email url integer]
139
142
  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]
143
+ rule-words: [min max length pattern match check check-async calc react is width pad gap menu accept known links sorted poster captions]
141
144
  media-styles: [video audio]
142
145
  media-flags: [loop muted autoplay] ; of a video or a sound
143
146
 
@@ -159,6 +162,189 @@ event-word?: func [w [word!]] [
159
162
  to logic! find/match form w "on-"
160
163
  ]
161
164
 
165
+ ; --- problems in a layout -------------------------------------------------------
166
+ ;
167
+ ; layout reads the whole block and makes one error of every problem it finds,
168
+ ; each with the place it was loaded from (loaded-at) and a word it may mean.
169
+
170
+ ; [place message ...] while a block is laid out; none otherwise, and then a problem is an error at once.
171
+ ui-problems: none
172
+
173
+ ui-problem: func [place [block!] message [string!]] [
174
+ either ui-problems [append ui-problems reduce [place message]] [ui-fail reduce [place message]]
175
+ ]
176
+
177
+ ; The faces of a block, as build-faces gives them, or an error with every problem in it.
178
+ ui-build: function [spec [block!] labels [block!]] [
179
+ outer: ui-problems
180
+ set 'ui-problems copy []
181
+ out: try [build-faces spec labels]
182
+ found: ui-problems
183
+ set 'ui-problems outer
184
+ if error? out [do out]
185
+ unless empty? found [either ui-checking [append ui-checking found] [ui-fail found]]
186
+ out
187
+ ]
188
+
189
+ ; While ui-check runs: the problems of every block, and no code of the program is run.
190
+ ui-checking: none
191
+
192
+ ; It lays out each block after view or layout (styles of stylize first), and the
193
+ ; faces of each screen and row of rows, which a running app lays out only when shown.
194
+ ui-check: function [
195
+ "Every problem in the layouts of a program, without running it: [place message ...]"
196
+ code [block!]
197
+ /local b p
198
+ ] [
199
+ set 'ui-checking copy []
200
+ blocks: copy []
201
+ ; the blocks after view, layout and stylize, in code and in the blocks inside it
202
+ walk: function [code] [
203
+ parse code [any [
204
+ p: ['view | 'layout | 'stylize | path! if (find [view layout] first first p)]
205
+ ; view compose [...]: its parens stand for values check cannot know
206
+ opt ['compose | path! if ('compose = first first next p)]
207
+ set b block! (
208
+ append/only blocks reduce [first p b]
209
+ walk b
210
+ )
211
+ | set b [block! | paren!] (walk b)
212
+ | skip
213
+ ]]
214
+ ]
215
+ walk code
216
+ attempt-at: func [place body] [
217
+ if error? r: try body [append ui-checking reduce [place form r/message]]
218
+ ]
219
+ foreach p blocks [if 'stylize = first p [attempt-at next p [stylize copy/deep second p]]]
220
+ foreach p blocks [
221
+ unless 'stylize = first p [
222
+ attempt-at next p [
223
+ faces: all-faces layout second p
224
+ ; screens and rows lay these out when shown
225
+ while [not empty? faces] [
226
+ f: take faces
227
+ later: copy []
228
+ if f/style = 'screens [foreach [n t a b] f/data [append/only later b]]
229
+ if all [f/style = 'rows f/template] [append/only later f/template]
230
+ foreach b later [
231
+ spec: copy/deep b
232
+ parse spec [any [set w word! if (event-word? w) block!] body:]
233
+ foreach g ui-build body copy [] [append faces all-faces g]
234
+ ]
235
+ ]
236
+ ]
237
+ ]
238
+ ]
239
+ ; a screen or rows laid out twice (rows with min) gives its problems twice
240
+ found: copy []
241
+ seen: copy []
242
+ foreach [p m] ui-checking [
243
+ k: rejoin [any [loaded-at p ""] " " m]
244
+ unless find seen k [append seen k append found reduce [p m]]
245
+ ]
246
+ set 'ui-checking none
247
+ found
248
+ ]
249
+
250
+ ui-fail: function [found [block!] /local place m] [
251
+ either 2 = length? found [
252
+ message: rejoin ["layout: " second found]
253
+ ] [
254
+ places: collect [foreach [place m] found [keep any [loaded-at place ""]]]
255
+ wide: 0
256
+ foreach p places [wide: max wide length? p]
257
+ message: rejoin ["layout: " (length? found) / 2 " problems"]
258
+ i: 0
259
+ foreach [place m] found [
260
+ p: pick places i: i + 1
261
+ append message rejoin ["^/ " p append/dup copy "" #" " wide + 1 - length? p m]
262
+ ]
263
+ ]
264
+ e: make error! message
265
+ ; the first values from the place, one after another as near shows them
266
+ e/near: copy []
267
+ foreach v copy/part first found 3 [append/only e/near :v]
268
+ e/at: loaded-at first found
269
+ do e
270
+ ]
271
+
272
+ ; What a rule word takes, for a problem when what follows it is something else.
273
+ ui-takes: [
274
+ min "a number, an amount or a date" max "a number, an amount or a date" length "two integers"
275
+ pattern "a block" check "a block" check-async "a block" calc "a block" react "a block, after later or not" is "a block" accept "a block"
276
+ menu "a block" sorted "a block" known "a map" links "a map" match "the name of a field"
277
+ poster "a file or a url" captions "a file, a url or a block" width "small, medium, large or fill"
278
+ pad "a step from 0 to 7, none or wide" gap "a step from 0 to 7, none or wide" key "a text or a word"
279
+ ]
280
+
281
+ ; Every word the dialect knows, for ui-suggest.
282
+ ui-dialect-words: does [
283
+ unique compose [
284
+ (words-of styles) (roles) (sizes) (aligns) (valigns) (flags) (chart-kinds) (simple-rules)
285
+ (rule-words) (media-flags) required global key
286
+ ]
287
+ ]
288
+
289
+ ; The word near enough to w to be what it meant (buton: button), or none.
290
+ ui-nearest: function [w [word!] words [block!]] [
291
+ t: form w
292
+ most: either 4 < length? t [2] [1]
293
+ best: none
294
+ foreach c words [
295
+ d: ui-distance t form c
296
+ if all [d > 0 d <= most] [best: c most: d - 1]
297
+ ]
298
+ best
299
+ ]
300
+
301
+ ; "; did you mean button?" for buton, when one of the words is near enough; "" otherwise.
302
+ ui-suggest: function [w [word!] words [block!]] [
303
+ either best: ui-nearest w words [rejoin ["; did you mean " best "?"]] [""]
304
+ ]
305
+
306
+ ; Optimal string alignment distance: letters put in, taken out, changed, or two neighbours swapped.
307
+ ui-distance: function [a [string!] b [string!]] [
308
+ n: length? b
309
+ before: none
310
+ prev: copy []
311
+ repeat j n + 1 [append prev j - 1]
312
+ repeat i length? a [
313
+ row: reduce [i]
314
+ repeat j n [
315
+ d: min min (pick prev j + 1) + 1 (last row) + 1 (pick prev j) + either (pick a i) = (pick b j) [0] [1]
316
+ if all [i > 1 j > 1 (pick a i) = (pick b j - 1) (pick a i - 1) = (pick b j)] [
317
+ d: min d (pick before j - 1) + 1
318
+ ]
319
+ append row d
320
+ ]
321
+ before: prev
322
+ prev: row
323
+ ]
324
+ last prev
325
+ ]
326
+
327
+ ; A value as a problem shows it: its mold, cut when long.
328
+ ui-shown: function [v] [
329
+ t: mold :v
330
+ either 30 < length? t [append copy/part t 27 "..."] [t]
331
+ ]
332
+
333
+ ; The style a word after a face means when it is a mistyped one (feild: field), or none.
334
+ ui-meant-style: function [v] [
335
+ all [word? :v s: ui-nearest v ui-dialect-words select styles s s]
336
+ ]
337
+
338
+ ; The problem with a value that follows a face and means nothing to it.
339
+ ui-stray: function [f v] [
340
+ case [
341
+ all [word? :v t: select ui-takes v] [rejoin [v " takes " t]]
342
+ s: ui-meant-style :v [rejoin [v " is not a style; did you mean " s "?"]]
343
+ word? :v [rejoin [v " is not a word of " f/style ui-suggest v ui-dialect-words]]
344
+ true [rejoin [f/style " does not take " ui-shown :v]]
345
+ ]
346
+ ]
347
+
162
348
  ; Labels (`search: field`, `table clients`) are local to the layout, as R3
163
349
  ; planned for its VID: each layout gets an object holding them, and the
164
350
  ; layout's blocks (its handlers too) are bound to it. `global search: field`
@@ -174,7 +360,7 @@ layout: function [
174
360
  spec: copy/deep spec
175
361
  labels: copy []
176
362
  either only [
177
- out: build-faces spec labels
363
+ out: ui-build spec labels
178
364
  ] [
179
365
  ; on-xxx handlers and its width before the first face belong to the view itself
180
366
  events: copy []
@@ -183,7 +369,7 @@ layout: function [
183
369
  set ev word! if (event-word? ev) set b block! (append events reduce [ev b])
184
370
  | 'width set w ['small | 'medium | 'large | 'fill] (width: w)
185
371
  ] body:]
186
- out: make select styles 'view [pane: build-faces body labels]
372
+ out: make select styles 'view [pane: ui-build body labels]
187
373
  unless empty? events [out/events: events]
188
374
  out/size: width
189
375
  ]
@@ -214,18 +400,22 @@ text-of: func [t] [either lit-word? :t [to word! t] [t]]
214
400
  ; The faces of a block of the dialect; labels collects [word face ...].
215
401
  build-faces: function [
216
402
  spec [block!] labels [block!]
217
- /local label global? style-word f proto n s b w a i v ev p
403
+ /local label global? style-word f proto n s b w a i v ev p pos later
218
404
  ] [
219
405
  out: copy []
406
+ ; what starts the next face
407
+ face-start: [set-word! | 'global | set w word! if (select styles w)]
220
408
  modifier: [
221
- set s [string! | map!] (either f/text [f/hint: s] [f/text: s])
409
+ ; ui-check reads view compose [...] with its parens in it: menu (looks)
410
+ if (ui-checking) opt [set w word! if (select ui-takes w)] some paren!
411
+ | set s [string! | map!] (either f/text [f/hint: s] [f/text: s])
222
412
  ; a word of the dictionary: button 'save (in a link, a lit-word is the screen)
223
413
  | if (f/style <> 'link) set v lit-word! (v: to word! v either f/text [f/hint: v] [f/text: v])
224
414
  | set ev word! if (event-word? ev) set b block! (
225
415
  unless f/events [f/events: copy []]
226
416
  append f/events reduce [ev b]
227
417
  )
228
- | set b block! (
418
+ | pos: set b block! (
229
419
  case [
230
420
  ; grid [1 4 1] [...]: the weight of each column, then the children
231
421
  all [f/style = 'grid none? f/pane not empty? b parse b [some integer!]] [f/cols: b]
@@ -235,16 +425,20 @@ build-faces: function [
235
425
  ; --- in a toolbar is a separator
236
426
  f/pane: build-faces either f/style = 'toolbar [replace/all copy b '--- 'divider] [b] labels
237
427
  if all [f/style = 'split 2 <> length? f/pane] [
238
- do make error! "layout: split takes two faces, one per pane"
428
+ ui-problem pos "split takes two faces, one per pane"
239
429
  ]
240
430
  ]
241
431
  find data-styles f/style [f/columns: columns-of b]
242
432
  ; the first block holds the items, the second is the action
243
433
  all [f/style = 'breadcrumb none? f/data] [f/data: crumbs-of b]
434
+ ; orbit [focus ... nodes [...]]: what it shows; a second block is the action
435
+ all [f/style = 'orbit none? f/data] [f/data: orbit-of b]
436
+ ; widget <geo-map> [zoom: 12]: its properties; a second block is the action
437
+ all [f/style = 'widget none? f/props] [f/props: either ui-checking [make object! []] [make object! b]]
244
438
  f/style = 'canvas [f/draw: b]
245
439
  f/style = 'tabs [f/pane: tabs-of b labels]
246
440
  f/style = 'screens [f/data: screens-of b]
247
- find [choice radio] f/style [f/options: options-of b]
441
+ all [find [choice radio] f/style none? f/options] [f/options: options-of b]
248
442
  true [f/action: b]
249
443
  ]
250
444
  )
@@ -272,9 +466,11 @@ build-faces: function [
272
466
  | 'match set w word! (add-rule f reduce ['match w])
273
467
  | 'check set b block! (add-rule f reduce ['check b])
274
468
  | 'check-async set b block! (add-rule f reduce ['check-async b])
275
- | 'calc set b block! (f/calc: b f/readonly: true)
469
+ | 'calc set b block! (add-reaction f 'calc b false f/readonly: true)
470
+ | 'is set b block! (add-reaction f 'is b false)
471
+ | 'react (later: false) opt ['later (later: true)] set b block! (add-reaction f 'react b later)
276
472
  | '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)
473
+ | pos: set w ['pad | 'gap] [set n integer! | 'none (n: 0) | 'wide (n: 6)] (poke f w spacing n pos)
278
474
  | 'menu set b block! (f/menu: menu-of b)
279
475
  | 'key set s string! (f/shortcut: s)
280
476
  | 'key set w word! (f/key: w)
@@ -288,42 +484,59 @@ build-faces: function [
288
484
  ; money price "Price" EUR
289
485
  | set w word! if (all [f/style = 'money parse form w [3 letter]]) (f/currency: to word! uppercase form w)
290
486
  ]
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]]
487
+ parse spec [any [
488
+ (label: none global?: false)
489
+ opt ['global (global?: true)]
490
+ opt [set label set-word!]
491
+ [
492
+ ; a face that compose puts in: check takes what follows as words of a face it cannot know
493
+ if (ui-checking) paren! (f: make face []) any modifier
494
+ | pos: set style-word word! if (proto: select styles style-word) (
299
495
  f: make proto []
496
+ f/place: loaded-at pos
300
497
  append out f
301
498
  if label [label-face label labels global? f]
302
499
  )
500
+ ; widget <geo-map> place "Place": the element comes before the name
501
+ opt [if (f/style = 'widget) set v tag! (f/tag: to string! v)]
303
502
  opt [if (find input-styles f/style) set n word! if (field-name? n) (f/name: n)]
304
503
  ; chart bar income [...]: the kind comes before the label
305
504
  opt [if (f/style = 'chart) set n word! if (find chart-kinds n) (f/kind: n)]
306
505
  ; 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
506
+ opt [if (any [find data-styles f/style f/style = 'orbit]) set n word! if (all [field-name? n n <> 'key]) (label-face n labels global? f)]
507
+ any [
508
+ modifier
509
+ ; a mistyped style starts a face of its own: what follows it is that face's
510
+ | pos: not face-start skip (ui-problem pos ui-stray f first pos) opt [
511
+ if (ui-meant-style first pos) any [not face-start skip]
512
+ ]
513
+ ]
309
514
  (if f/style = 'rows [rows-fill f])
515
+ ; not a style: a problem, then on to the next face
516
+ | pos: skip (
517
+ w: first pos
518
+ ui-problem pos case [
519
+ not word? :w [rejoin ["a face starts with a style, not " ui-shown :w]]
520
+ find ui-dialect-words w [rejoin [w " is not a style; it goes after one"]]
521
+ true [rejoin [w " is not a style" ui-suggest w words-of styles]]
522
+ ]
523
+ ) any [not face-start skip]
310
524
  ]
311
- ] [
312
- do make error! "layout: cannot read the layout"
313
- ]
525
+ ]]
314
526
  out
315
527
  ]
316
528
 
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
529
+ spacing: func [n [integer!] place [block!]] [
530
+ if all [n >= 0 n <= 7] [return n]
531
+ ui-problem place rejoin [first place " takes a step from 0 to 7"]
532
+ 0
320
533
  ]
321
534
 
322
535
  ; [name "Name" email] -> [name "Name" email "email"]
323
536
  ; 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] [
537
+ columns-of: function [spec [block!] /local w label kind p] [
325
538
  out: copy []
326
- unless parse spec [any [
539
+ parse spec [any [
327
540
  set w word! (label: form w kind: none) opt [set label text-rule]
328
541
  opt ['image (kind: copy [image]) opt ['round (append kind 'round)]]
329
542
  (
@@ -331,43 +544,41 @@ columns-of: function [spec [block!] /local w label kind] [
331
544
  append out w
332
545
  append/only out either kind [head insert/only kind label] [label]
333
546
  )
334
- ]] [
335
- do make error! "layout: table columns are words, each with an optional label, then image or image round"
336
- ]
547
+ | p: skip (ui-problem p "table columns are words, each with an optional label, then image or image round") to end
548
+ ]]
337
549
  out
338
550
  ]
339
551
 
340
552
  ; [name desc] -> [name desc], checked
341
553
  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
- ]
554
+ unless parse b [word! ['asc | 'desc]] [ui-problem b "sorted takes a column and asc or desc"]
345
555
  copy b
346
556
  ]
347
557
 
348
558
  ; ["General" [...] "Advanced" [...]] -> a tab face per pair
349
- tabs-of: function [spec [block!] labels [block!]] [
559
+ tabs-of: function [spec [block!] labels [block!] /local p] [
350
560
  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
- ]
561
+ parse spec [any [
562
+ set label text-rule set b block! (
563
+ label: text-of label
564
+ append out make select styles 'tab [text: label pane: build-faces b labels]
565
+ )
566
+ | p: skip (ui-problem p "tabs take a label and a block per tab") to end
567
+ ]]
357
568
  out
358
569
  ]
359
570
 
360
571
  ; [home "Home" [...] client "Client" [id] [...]] -> [name title args body ...]
361
- screens-of: function [spec [block!] /local n t a b] [
572
+ screens-of: function [spec [block!] /local n t a b p] [
362
573
  out: copy []
363
- unless parse spec [some [
574
+ if empty? spec [ui-problem spec "screens take a name, a title, an optional block of arguments and a block per screen"]
575
+ parse spec [any [
364
576
  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]
577
+ forall a [unless word? first a [ui-problem a "the arguments of a screen are words"]]
578
+ append out reduce [n text-of t head a b]
367
579
  )
368
- ]] [
369
- do make error! "layout: screens take a name, a title, an optional block of arguments and a block per screen"
370
- ]
580
+ | p: skip (ui-problem p "screens take a name, a title, an optional block of arguments and a block per screen") to end
581
+ ]]
371
582
  out
372
583
  ]
373
584
 
@@ -380,24 +591,46 @@ menu-item: make object! [
380
591
  ]
381
592
 
382
593
  ; 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] [
594
+ menu-of: function [spec [block!] /local i label w b p] [
384
595
  out: copy []
385
- unless parse spec [any [
596
+ parse spec [any [
386
597
  '--- (append out make menu-item [divider: true])
387
598
  | (i: none) opt [set i issue!] set label text-rule (label: text-of label) [
388
599
  set b block! (append out make menu-item [text: label icon: i items: menu-of b])
389
600
  | set w word! (append out make menu-item [text: label icon: i id: w])
390
601
  ]
391
- ]] [
392
- do make error! "layout: a menu takes a label and a word per item, a block for a submenu, or ---"
393
- ]
602
+ | p: skip (ui-problem p "a menu takes a label and a word per item, a block for a submenu, or ---") to end
603
+ ]]
394
604
  out
395
605
  ]
396
606
 
607
+ ; [focus id nodes [id "Title" [hints] ...] edges [from to "Label" [dashed] ...] trail [id ...]]
608
+ ; The host reads the hints (a role or color, many, dashed, count n, steps n at k); this checks the shape.
609
+ orbit-id: [word! | integer! | string! | issue!]
610
+ orbit-of: function [spec [block!] /local b p] [
611
+ parse spec [any [
612
+ 'focus orbit-id
613
+ | 'nodes set b block! (parse b [
614
+ any [orbit-id text-rule opt block!]
615
+ [end | p: (ui-problem p "nodes takes an id and a title for each, then a block of hints if it has any")]
616
+ ])
617
+ | 'edges set b block! (parse b [
618
+ any [orbit-id orbit-id opt text-rule opt [into ['solid | 'dashed | 'faint]]]
619
+ [end | p: (ui-problem p "edges takes two ids for each, then a label and [solid], [dashed] or [faint] if it has them")]
620
+ ])
621
+ | 'trail set b block! (parse b [
622
+ any orbit-id [end | p: (ui-problem p "trail takes the ids of nodes, the most recent last")]
623
+ ])
624
+ | p: skip (ui-problem p "an orbit takes focus id, nodes [...], edges [...] and trail [...]") to end
625
+ ]]
626
+ copy spec
627
+ ]
628
+
397
629
  ; ["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"
630
+ crumbs-of: function [spec [block!] /local label w p] [
631
+ parse spec [
632
+ any [set label [string! | map!] set w [word! | path!]] opt [string! | map!]
633
+ [end | p: (ui-problem p "a breadcrumb takes a label and a word per item; only the last may have no word")]
401
634
  ]
402
635
  copy spec
403
636
  ]
@@ -405,6 +638,7 @@ crumbs-of: function [spec [block!] /local label w] [
405
638
  ; The options of a choice or radio: each reduced, and so are those of a group,
406
639
  ; a title followed by a block: ["Bikes" ["Road" "City"] "Other"].
407
640
  options-of: function [spec [block!]] [
641
+ if ui-checking [return copy spec]
408
642
  opts: reduce spec
409
643
  forall opts [if block? first opts [change/only opts reduce first opts]]
410
644
  opts
@@ -447,6 +681,7 @@ view: function [
447
681
  ; of the screen name and its arguments as strings, as they come in the link.
448
682
 
449
683
  router: none ; the screens face of the page
684
+ marked: false ; the screen on show has changes not saved (unsaved)
450
685
  page: none ; the root face of the view on show
451
686
 
452
687
  ; The [name title args body] of the screen a route names, or none.
@@ -467,13 +702,16 @@ enter-screen: function [f route [block!] /local ev b] [
467
702
  ; on-xxx handlers before the first face belong to the screen
468
703
  events: copy []
469
704
  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]
705
+ s: make select styles 'screen [text: def/2 pane: ui-build body labels]
471
706
  unless empty? events [s/events: events]
472
707
  words: copy []
473
708
  repeat i length? def/3 [append words reduce [to set-word! pick def/3 i pick route i + 1]]
474
709
  bind-labels spec labels words
475
710
  f/value: copy route
476
711
  f/entering: true
712
+ ; a screen starts with nothing to save
713
+ set 'marked false
714
+ set-unsaved false
477
715
  f/pane: reduce [s]
478
716
  ui-pane f
479
717
  s
@@ -493,6 +731,21 @@ run-enter: function [s] [
493
731
  if error? :r [do :r]
494
732
  ]
495
733
 
734
+ ; Whether the screen on show lets the page leave it: its on-leave says, false or
735
+ ; 'continue (as Red's on-close) to stay; with no on-leave, a screen with unsaved
736
+ ; changes asks the user. Both may wait for a dialog.
737
+ may-leave?: function [/local s h r] [
738
+ unless all [router s: first router/pane] [return true]
739
+ if h: handler-for s 'on-leave [
740
+ r: run-handler s h
741
+ return not any [false = :r 'continue = :r]
742
+ ]
743
+ any [
744
+ not marked
745
+ confirm/with/role select i18n/ui 'leave-unsaved reduce [select i18n/ui 'leave select i18n/ui 'stay] 'danger
746
+ ]
747
+ ]
748
+
496
749
  ; Shows the screen of a link given as a block of strings; a link that names no
497
750
  ; screen shows the first one and is replaced by its link.
498
751
  follow-link: function [f parts [block!]] [
@@ -514,6 +767,7 @@ go: function [
514
767
  if block? target [foreach a reduce next target [append route form a]]
515
768
  if path? target [foreach a next to block! target [append route form a]]
516
769
  unless find-screen router route [do make error! rejoin ["go: no screen for " mold route]]
770
+ unless may-leave? [exit]
517
771
  set-route route false
518
772
  run-enter enter-screen router route
519
773
  ]
@@ -543,6 +797,20 @@ can-go-forward?: function [
543
797
  0 < second route-steps
544
798
  ]
545
799
 
800
+ unsaved: function [
801
+ "Marks the screen on show as having changes not saved, or not: leaving it asks first, and so does closing the page."
802
+ on [logic!]
803
+ ] [
804
+ set 'marked on
805
+ set-unsaved on
806
+ ]
807
+
808
+ unsaved?: function [
809
+ "True if the screen on show has changes not saved (unsaved)."
810
+ ] [
811
+ marked
812
+ ]
813
+
546
814
  ui-route: function [
547
815
  "Called by the host when the page's link changes: back, forward or a link."
548
816
  f parts [block!]
@@ -550,6 +818,8 @@ ui-route: function [
550
818
  route: copy parts
551
819
  unless empty? route [route/1: attempt [to word! route/1]]
552
820
  if route = f/value [exit]
821
+ ; the link has changed already: staying puts the screen's link back
822
+ unless may-leave? [route-undo exit]
553
823
  run-enter follow-link f parts
554
824
  ]
555
825
 
@@ -559,7 +829,10 @@ ui-pane: function [
559
829
  ] [
560
830
  inner: either find [form entry] f/style [f] [f/form]
561
831
  foreach c any [f/pane []] [attach c f/root inner]
562
- if f/root [refresh-form any [inner f/root]]
832
+ unless f/root [exit]
833
+ refresh-form any [inner f/root]
834
+ ; and the forms among the new faces, whose submits start disabled until their fields are valid
835
+ foreach c any [f/pane []] [foreach g all-faces c [if g/style = 'form [refresh-form g]]]
563
836
  ]
564
837
 
565
838
  open: func [
@@ -702,6 +975,20 @@ set-values: function [
702
975
  refresh-form scope
703
976
  ]
704
977
 
978
+ insert-text: function [
979
+ "Puts text where the cursor of a field, area or rich text was last, in place of what was selected, as if the user typed it."
980
+ face [object!]
981
+ text [string!]
982
+ ] [
983
+ unless find [field area rich] face/style [
984
+ do make error! rejoin ["insert-text: a " face/style " has no cursor"]
985
+ ]
986
+ if any [face/readonly face/disabled empty? text] [exit]
987
+ ; the host gives the value with the text in; at its end when the face has had no cursor
988
+ ui-input face insert-at-caret face text
989
+ face
990
+ ]
991
+
705
992
  field-error: function [
706
993
  "Shows a message on a named field until the user changes it: what a server refused. False if there is no such field."
707
994
  scope name [word! string!] text [string!]
@@ -743,7 +1030,7 @@ rows-entry: function [f data] [
743
1030
  labels: copy []
744
1031
  set 'last-entry last-entry + 1
745
1032
  e: make select styles 'entry [owner: f value: last-entry]
746
- e/pane: build-faces spec labels
1033
+ e/pane: ui-build spec labels
747
1034
  bind-labels spec labels copy []
748
1035
  foreach g named-faces e [
749
1036
  if f/readonly [g/readonly: true]
@@ -833,23 +1120,169 @@ ui-rows: function [
833
1120
  refresh-form any [f/form f/root]
834
1121
  ]
835
1122
 
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
- ]
1123
+ ; --- reactions -------------------------------------------------------------------
1124
+ ;
1125
+ ; `react [...]` after a face runs the block when the face is shown, and again
1126
+ ; whenever a facet it reads changes: `total/text`, `face/value`, `values/qty`.
1127
+ ; Which facets it reads is found by reading the block, not by running it. The
1128
+ ; host checks them at the end of each round, before it repaints, against what
1129
+ ; they were when the block last ran. `is [...]` gives the face's main facet what
1130
+ ; the block gives, and `calc [...]` its value, which the user cannot change.
1131
+
1132
+ reaction: make object! [
1133
+ kind: 'react ; react, is or calc
1134
+ body: none
1135
+ later: false ; runs on a change only, not when the face is shown
1136
+ sources: none ; the [face facet ...] it reads, found the first time it is checked
1137
+ targets: none ; the [face facet ...] it sets, so that it runs before what reads them
1138
+ last: none ; what its sources were when it last ran
1139
+ ]
1140
+
1141
+ add-reaction: function [f k b l] [
1142
+ unless f/reactions [f/reactions: copy []]
1143
+ append f/reactions make reaction [kind: k body: b later: l]
1144
+ ]
1145
+
1146
+ value-styles: append copy input-styles [progress image video audio]
1147
+
1148
+ ; The facet `is` sets: data for a table, list, tree or chart, a canvas's draw,
1149
+ ; an input's value, and text for the rest.
1150
+ main-facet: func [f] [
1151
+ case [
1152
+ find data-styles f/style ['data]
1153
+ f/style = 'orbit ['data]
1154
+ f/style = 'canvas ['draw]
1155
+ find value-styles f/style ['value]
1156
+ true ['text]
1157
+ ]
1158
+ ]
1159
+
1160
+ face?: func [v] [all [object? :v in v 'style in v 'pane]]
1161
+
1162
+ ; Puts in out the face and facet a path names: name/facet (a label), face/facet
1163
+ ; (the face itself), values/field (the value of the field of that name).
1164
+ react-path: function [f p out [block!]] [
1165
+ w: first p
1166
+ unless all [word? :w word? pick p 2] [exit]
1167
+ facet: second p
1168
+ g: case [
1169
+ w = 'face [f]
1170
+ w = 'values [facet: 'value attempt [find-named f second p]]
1171
+ true [attempt [get w]]
1172
+ ]
1173
+ if all [face? :g in g facet] [append out reduce [g facet]]
1174
+ ]
1175
+
1176
+ react-scan: function [f b [block! paren!] sources [block!] targets [block!] /local v] [
1177
+ foreach v b [
1178
+ case [
1179
+ any [block? :v paren? :v] [react-scan f v sources targets]
1180
+ any [path? :v get-path? :v] [react-path f v sources]
1181
+ set-path? :v [react-path f v targets]
1182
+ ]
1183
+ ]
1184
+ ]
1185
+
1186
+ react-now: function [r /local g facet] [
1187
+ out: copy []
1188
+ foreach [g facet] r/sources [append/only out select g facet]
1189
+ out
1190
+ ]
1191
+
1192
+ ; Faces compare by content, so finding one in a block needs same?.
1193
+ find-same: function [b [block!] v] [
1194
+ foreach x b [if same? x v [return true]]
1195
+ false
1196
+ ]
1197
+
1198
+ react-due?: func [r] [not strict-equal? r/last react-now r]
1199
+
1200
+ ; True if a reaction sets something b reads.
1201
+ feeds?: function [a b /local g m h n] [
1202
+ foreach [g m] a/targets [
1203
+ foreach [h n] b/sources [if all [same? g h m = n] [return true]]
1204
+ ]
1205
+ false
1206
+ ]
1207
+
1208
+ react-run: function [f r] [
1209
+ if r/kind = 'react [
1210
+ run-handler f r/body
1211
+ exit
1212
+ ]
1213
+ m: either r/kind = 'calc ['value] [main-facet f]
1214
+ ; a formula that fails, or lacks a value, leaves the field empty
1215
+ v: either r/kind = 'calc [attempt [run-handler f r/body]] [run-handler f r/body]
1216
+ unless strict-equal? :v select f m [
1217
+ poke f m :v
1218
+ if m = 'value [store-around f]
1219
+ ]
1220
+ ]
1221
+
1222
+ ui-react: function [
1223
+ "Called by the host at the end of each round: runs the reactions of the faces whose facets they read changed. Gives the cycles it cut, as texts."
1224
+ faces [block! object!] "The faces, or a face to take every face in it"
1225
+ /local f r
1226
+ ] [
1227
+ if object? faces [faces: all-faces faces]
1228
+ fs: copy []
1229
+ rs: copy []
1230
+ foreach f faces [
1231
+ foreach r any [select f 'reactions []] [
1232
+ unless r/sources [
1233
+ r/sources: copy []
1234
+ r/targets: copy []
1235
+ react-scan f r/body r/sources r/targets
1236
+ if r/kind <> 'react [append r/targets reduce [f either r/kind = 'calc ['value] [main-facet f]]]
1237
+ ; none is never a list of values: due, so it runs when shown
1238
+ r/last: either r/later [react-now r] [none]
849
1239
  ]
1240
+ append fs f
1241
+ append rs r
850
1242
  ]
851
- unless again [break]
852
1243
  ]
1244
+ cut: copy []
1245
+ failed: none
1246
+ ; a second cascade, for what the forms changed after the first (an error, a disabled submit)
1247
+ loop 2 [
1248
+ ran: copy []
1249
+ forever [
1250
+ due: copy []
1251
+ repeat i length? rs [if all [not find ran i react-due? pick rs i] [append due i]]
1252
+ if empty? due [break]
1253
+ ; the first that no other one due feeds; if they all feed each other, the first
1254
+ i: first due
1255
+ foreach j due [
1256
+ ok: true
1257
+ foreach k due [if all [k <> j feeds? pick rs k pick rs j] [ok: false break]]
1258
+ if ok [i: j break]
1259
+ ]
1260
+ r: pick rs i
1261
+ e: try [react-run pick fs i r none]
1262
+ if all [error? :e not failed] [failed: :e]
1263
+ r/last: react-now r
1264
+ append ran i
1265
+ ]
1266
+ if empty? ran [break]
1267
+ ; what reads again what changed after it ran is in a cycle: it waits for the next change
1268
+ foreach i ran [
1269
+ r: pick rs i
1270
+ if react-due? r [
1271
+ where: loaded-at r/body
1272
+ append cut rejoin [either where [rejoin [where " "]] [""] r/kind ": left out, what it reads changed again after it ran (a cycle)"]
1273
+ r/last: react-now r
1274
+ ]
1275
+ ]
1276
+ scopes: copy []
1277
+ foreach i ran [
1278
+ f: pick fs i
1279
+ s: any [f/form f/root]
1280
+ if all [s not find-same scopes s] [append scopes s]
1281
+ ]
1282
+ foreach s scopes [refresh-form s]
1283
+ ]
1284
+ if failed [do :failed]
1285
+ cut
853
1286
  ]
854
1287
 
855
1288
  ; --- validation ------------------------------------------------------------------
@@ -964,7 +1397,6 @@ refresh-form: function [scope] [
964
1397
  ; the fields of a row count for the form its rows are in
965
1398
  while [all [scope scope/style = 'entry]] [scope: any [scope/form scope/root]]
966
1399
  unless all [scope find [form view] scope/style] [exit]
967
- recalc scope
968
1400
  ok: true
969
1401
  faces: own-faces scope
970
1402
  foreach f faces [
@@ -1219,6 +1651,15 @@ ui-reorder: function [
1219
1651
  fire-event f 'on-reorder fields
1220
1652
  ]
1221
1653
 
1654
+ ui-orbit: function [
1655
+ "Called by the host when the user picks a node of an orbit: a neighbour, or one of the trail when back is true."
1656
+ f id back [logic!]
1657
+ ] [
1658
+ f/value: id
1659
+ ; with no on-back, stepping back is one more pick
1660
+ either all [back handler-for f 'on-back] [fire-event f 'on-back []] [if f/action [run-handler f f/action]]
1661
+ ]
1662
+
1222
1663
  ui-expand: function [
1223
1664
  "Called by the host when the user opens a node of a tree."
1224
1665
  f node