forked from fastn-stack/ftd.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreating-a-tree.ftd
530 lines (327 loc) · 9.89 KB
/
creating-a-tree.ftd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
-- import: ftd.dev/assets
-- import: fpm
-- ft.page-with-toc: How to create a tree using `ftd`?
toc: $config.examples-toc
id: main
-- ft.markdown:
A tree view represents a hierarchical form of items where each item can have
subitems and so on. In this tutorial, we will learn how to create a tree using
`ftd` step by step.
-- ft.h1: Create a component for tree
Let us create a component that would contain all the items and subitems.
The component needs to be of [`column`](/ftd/column/) type which would stack the
items vertically.
-- ft.code:
lang: ftd
\-- ftd.column:
-- ft.image:
src: $assets.files.images.tree.first.jpeg
width: fill
-- ft.markdown:
The above code uses `ftd.column` which is a [kernel component](/ftd/kernel/).
Now let's insert an item.
-- ft.code:
lang: ftd
\-- ftd.column:
\-- item:
name: Beverage
-- ft.image:
src: $assets.files.images.tree.second.jpeg
width: fill
-- ft.markdown:
Bravo! we have successfully inserted an item with the property `name: Beverage`.
You must have noticed that we have called another component with the name `item` to display this item.
This component becomes subsection/child of the `ftd.column` component.
Before we insert another item, let's understand how to create the `item` component first.
-- ft.h1: Create an `item` component
-- ft.code:
lang: ftd
\-- component item:
component: ftd.column
string $name:
padding-left: 10
open: true
-- ft.image:
src: $assets.files.images.tree.third.jpeg
width: fill
-- ft.markdown:
Let's understand the above code in detail. As we need to insert subitem inside
`Beverage` item and it's proper to show subitems vertically. So we have created
[component](/ftd/component/) with type column.
The other important thing to notice here is we have defined `open: true` which
would make the component open. (i.e. any element that comes after its call would
become its child. To understand this better, please check this out
[container-management](/ftd/container-management/)).
We have also defined an argument `$name` of type `string`. So, at the time of
calling this component, we have to pass an argument `name` of type `string`. Now,
We have to consume this argument to show it in frontend.
-- ft.code:
lang: ftd
\-- component item:
component: ftd.column
string $name:
padding-left: 10
open: true
\--- ftd.text: $name
-- ft.image:
src: $assets.files.images.tree.fourth.jpeg
width: fill
-- ft.markdown:
Here, we have inserted [`--- ftd.text: ref $name`](/ftd/text/) as the
child/subsection of `item` component. This has reference to the argument `$name`.
Hence consuming it 😊. Before we proceed further let's call this component and
see the result.
-- ft.code:
lang: ftd
\-- item:
name: Beverage
-- ft.image:
src: $assets.files.images.tree.fifth.jpeg
width: fill
-- ft.markdown:
Output:
-- ftd.column item1:
string name:
padding-left: 10
open: true
--- ftd.text: $name
color if $ftd.dark-mode: $fpm.color.main.text
-- ftd.column:
spacing: 20
border-width: 1
border-radius: 5
border-color if $ftd.dark-mode: $fpm.color.main.text
padding: 10
width: fill
--- item1:
name: Beverage
-- ft.h1: Add items to the tree
Let's get back to adding items.
In this example, we are going to add Water and Juice as children of Bevarage
and Mango Juice as child of Juice.
-- ft.code:
lang: ftd
\-- ftd.column:
\-- item:
name: Beverage
id: beverage-id
\-- item:
name: Water
\-- container: beverage-id
\-- item:
name: Juice
\-- item:
name: Mango Juice
-- ft.markdown:
We are almost done now. Let's understand this in detail. First, we have inserted
our old pal `Beverage` and now item `Beverage` becomes open. (Wait, it's different than the first time when we have inserted.
It has a property `id: beverage-id` but why?? Have patience for answer).
Cool, after that we have inserted item `Water` and it becomes child of item `Beverage`
and now item `Water` becomes open. We need to change the open container/component to item `Bevarage` again.
In order to achieve this, we have added `--- container: beverage-id`
which changes the open container to the one with id `beverage-id` which is item `Beverage`.
(Now, you know why item `Beverage` has `id`). After this, we have inserted item `Juice`,
and now this becomes an open container. At last, we have inserted item `Mango Juice`.
-- ft.image:
src: $assets.files.images.tree.sixth.jpeg
width: fill
-- ft.image:
src: $assets.files.images.tree.seventh.jpeg
width: fill
-- ft.image:
src: $assets.files.images.tree.eighth.jpeg
width: fill
-- ft.image:
src: $assets.files.images.tree.ninth.jpeg
width: fill
-- ft.image:
src: $assets.files.images.tree.tenth.jpeg
width: fill
-- ft.h1: Presenting Tree
Let's combine everything now and see the magic 🪄.
(Note: I have changed component `item` a little
and added a `row` having two elements of type `text`. One with ✌️ emoji and the other with reference to the `$name` argument.)
-- ft.code:
lang: ftd
\-- component item:
component: ftd.column
string $name:
padding-left: 10
open: true
\--- ftd.row:
\--- ftd.text: ✌️
\--- ftd.text: $name
\-- ftd.column:
\-- item:
name: Beverage
id: beverage-id
\-- item:
name: Water
\-- container: beverage-id
\-- item:
name: Juice
\-- item:
name: Mango Juice
-- ft.markdown:
Output
-- ftd.column:
spacing: 20
border-width: 1
border-radius: 5
border-color if $ftd.dark-mode: $fpm.color.main.text
padding: 10
width: fill
-- ftd.column display-item:
string name:
padding-left: 10
open: true
color if $ftd.dark-mode: $fpm.color.main.text
--- ftd.row:
--- ftd.text: ✌️
--- ftd.text: $name
color if $ftd.dark-mode: $fpm.color.main.text
-- ftd.column:
-- display-item:
name: Beverage
id: beverage
-- display-item:
name: Water
-- container: beverage
-- display-item:
name: Juice
-- display-item:
name: Mango Juice
-- container: main
-- ft.h1: Adding Events
We have learnt how to create a tree. Now, we'll learn how to make it more interactive.
As you have guessed, we need to do changes in `-- component item:`.
Let us take a quick look at updated version, then we'll discuss about it line by line.
-- ft.code:
lang: ftd
\-- component item:
component: ftd.column
string $name:
padding-left: 10
open: true
append-at: some-child
$visible: true
\--- ftd.row:
$on-click$: toggle $visible
\--- ftd.text: ✌️
\--- ftd.text: $name
\--- container: ftd.main
\--- ftd.column:
if: $visible
id: some-child
-- ft.markdown:
Thankfully, not many changes. phew 😮💨!! Let's take a closer look at changes in component `item`.
-- ft.code:
lang: ftd
\-- component item:
component: ftd.column
string $name:
padding-left: 10
open: true
append-at: some-child
$visible: true
-- ft.markdown:
Here, we have changed the `open` property value from `true` to `some-child`.
This would make the component `item` open as earlier, but now elements that come after its call
would be inserted inside the child/subsection of component `item` which has `id` as `some-child`.
(If you still have not check out this [container-management](/ftd/container-management/) page.
It's highly recommended to do it now)
And what is that child/subsection which has `id: some-child`? (Remember?? no??
Check out the above code again.)
It is a kernel component `ftd.column`.
We have declared a local variable `@visible` of boolean type with default value as `true`.
(Why?? All I can say, have patience again.)
Now, let us focus on changes in subsections. We have added the below two subsections.
-- ft.code:
lang: ftd
\--- container: ftd.main
\--- ftd.column:
if: $visible
id: some-child
-- ft.markdown:
As `--- ftd.row:` was an open container, we need to change the container to component `item`
having type `column`. This line `--- container: ftd.main` helps in achieving it.
The open container becomes `-- component item:`.
After this, we have defined another kernel component `--- ftd.column:`.
It has a property `if: @visible` which make this component (and its children) visible,
only if the condition satisfies, i.e., if local variable `@visible` is true.
We need something which changes the value of the local variable `@visible` to control its
visibility. As this component `--- ftd.column:` is the open container for `item` (How?? Remember it has `id: some-child`)
and all subsequent components will be placed inside it, so we can control the visibility
of all those components.
FTD has an action `toggle` which toggles a boolean variable.
We can attach it with an event `click`. Check these changes in subsection `--- ftd.row:` of `item`
-- ft.code:
lang: ftd
\--- ftd.row:
$on-click$: toggle $visible
-- ft.h1: Presenting an interactive Tree
Let's combine everything now and see more magic 🪄.
Click on `✌️Beverage` to see everything else disappears, then click it again, and everything is back.
Similarly, click on `✌️Juice` and see `✌️Mango Juice` disappear. You know how to make it appear again.
-- ft.code:
lang: ftd
\-- component item:
component: ftd.column
string $name:
padding-left: 10
open: true
append-at: some-child
$visible: true
\--- ftd.row:
$on-click$: toggle $visible
\--- ftd.text: ✌️
\--- ftd.text: $name
\--- container: ftd.main
\--- ftd.column:
if: $visible
id: some-child
\-- item:
name: Beverage
id: beverage-id
\-- item:
name: Water
\-- container: beverage-id
\-- item:
name: Juice
\-- item:
name: Mango Juice
-- ft.markdown:
Output
-- ftd.column:
spacing: 20
border-width: 1
border-radius: 5
border-color if $ftd.dark-mode: $fpm.color.main.text
padding: 10
width: fill
-- ftd.column display-item1:
string name:
color if $ftd.dark-mode: $fpm.color.main.text
padding-left: 10
open: true
append-at: some-child
boolean visible: true
--- ftd.row:
$on-click$: toggle $visible
--- ftd.text: ✌️
--- ftd.text: $name
color if $ftd.dark-mode: $fpm.color.main.text
--- container: ftd.main
--- ftd.column:
if: $visible
id: some-child
-- display-item1:
name: Beverage
id: beverage
-- display-item1:
name: Water
-- container: beverage
-- display-item1:
name: Juice
-- display-item1:
name: Mango Juice