-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdataview_tree.livecodescript
More file actions
3032 lines (2274 loc) · 80.5 KB
/
dataview_tree.livecodescript
File metadata and controls
3032 lines (2274 loc) · 80.5 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
script "DataView Tree Behavior" with behavior "DataView Behavior"
constant kBuiltInProperties = "id,type,expanded,is leaf,children,level,child count"
constant kDataViewDragType = "dataview tree nodes"
local sTreeA
local sNodeIdNodeLookupA # key = node id, value = node index array in sTreeA
local sRowNodeIdLookupA # key = row, value = node id. Lookup in sNodeIdNodeLookupA.
local sNodeIdRowLookupA # key = node id, value = row number
local sRebuildLookupTable = false
local sDropInfoA
/**
Summary: Sets the tree used to display source tree data.
Parameters:
pRender: By default the tree will render when setting this property. Pass in false to not render the DataView.
pTreeA: An array representing tree used to draw the tree in the DataView.
Description:
The DataView uses an array that is populated with node arrays. A node array has
the following format:
```
pNodeA[type|id|expanded|children|is leaf]
```
A node can have other nodes as `children`.
A tree is made up of a `root` node with a `children` key that contains
one or more nodes.
```
pTreeA[root][children][1][type|id|expanded|children|is leaf]
pTreeA[root][children][n][type|id|expanded|children|is leaf]
```
Here is an example of a tree with three nodes – one node at the root, one node
as a child of the root node, and one node as the child of the child of the root node.
```
pTreeA["root"]["children"][1]["type"]
pTreeA["root"]["children"][1]["id"]
pTreeA["root"]["children"][1]["expanded"]
pTreeA["root"]["children"][1]["is leaf"]
pTreeA["root"]["children"][1]["children"]
pTreeA["root"]["children"][1]["children"][1]["type"]
pTreeA["root"]["children"][1]["children"][1]["id"]
pTreeA["root"]["children"][1]["children"][1]["expanded"]
pTreeA["root"]["children"][1]["children"][1]["is leaf"]
pTreeA["root"]["children"][1]["children"][1]["children"]
pTreeA["root"]["children"][1]["children"][1]["children"][1]["type"]
pTreeA["root"]["children"][1]["children"][1]["children"][1]["id"]
pTreeA["root"]["children"][1]["children"][1]["children"][1]["expanded"]
pTreeA["root"]["children"][1]["children"][1]["children"][1]["is leaf"]
pTreeA["root"]["children"][1]["children"][1]["children"][1]["children"]
```
At a minimum you should define the `type` and `id` properties for each node.
`id` should uniquely identify the node in the entire tree. `type` is used to
distinguish nodes from each other.
`expanded` is a boolean value and defaults to `false` if no value is present.
When setting the `dvTree` you can leave off the `["root"]["children"]` keys and pass
in a numerically indexed array of nodes. The array you pass in will be assigned to the
`["root"]["children"]` array internally.
Returns: nothing
*/
setProp dvTree[pRender] pTreeA
put pRender is not false into pRender
if "root" is among the keys of pTreeA then
put pTreeA into sTreeA
else
put pTreeA into sTreeA["root"]["children"]
end if
_buildRowToNodeLookup
put false into sRebuildLookupTable
if pRender then
lock screen
ResetView
RenderView
unlock screen
end if
end dvTree
/**
Summary: Returns the internal tree array.
Returns: Array
*/
getProp dvTree
return sTreeA
end dvTree
/**
Summary: Returns all node ids in the tree in no particular order.
Description:
This property is useful if you need to look at all of the nodes in a tree
but in no particular order.
Returns: Comma delimited list
*/
getProp dvNodeIds
local tKeys
if sRebuildLookupTable then
_buildRowToNodeLookup
put false into sRebuildLookupTable
end if
put the keys of sNodeIdNodeLookupA into tKeys
replace cr with comma in tKeys
return tKeys
end dvNodeIds
/**
Summary: Adds a node to the tree and marks affected rows as dirty within the DataView.
Parameters:
pNode: An array representing the new node.
pParent: The node id of the parent node.
pPosition: The position within the parent's children. If empty then it will be the last sibling.
pRefreshView: Pass in false to prevent the view being refreshed.
Returns: nothing
*/
command AddNode pNodeA, pParent, pPosition, pRefreshView
local tDirtyIdsA, tId
put pRefreshView is not false into pRefreshView
if pParent is empty then
put "root" into pParent[1]
else if pParent is not an array then
put _findNodeOfId(pParent) into pParent
end if
_addNodeToParent pNodeA, pParent, pPosition, tDirtyIdsA
repeat for each key tId in tDirtyIdsA
_setNodeRowIsDirty tId, true
end repeat
# Brute force
put true into sRebuildLookupTable
if pRefreshView then
_refreshView
end if
return empty
end AddNode
/**
Summary: Moves a node to a new position within the tree and marks affected rows as dirty within the DataView.
Parameters:
pNode: Node id.
pParent: New node parent id. Empty value assumes current parent.
pPosition: New position.
pRefreshView: Pass in false to prevent the view being refreshed.
Returns: nothing
*/
command MoveNode pNode, pParent, pPosition, pRefreshView
local tCurrentParent, tDirtyIdsA, tId
put pRefreshView is not false into pRefreshView
if pNode is not an array then put _findNodeOfId(pNode) into pNode
put _parentNodeIndex(pNode) into tCurrentParent
if pParent is empty then put tCurrentParent into pParent
else if pParent is not an array then put _findNodeOfId(pParent) into pParent
if pParent is tCurrentParent then
_moveNodeWithinParent pNode, pParent, pPosition, tDirtyIdsA
else
_moveNodeToNewParent pNode, pParent, pPosition, tDirtyIdsA
end if
repeat for each key tId in tDirtyIdsA
_setNodeRowIsDirty tId, true
end repeat
# Brute force
put true into sRebuildLookupTable
if pRefreshView then
_refreshView
end if
return empty
end MoveNode
/**
Summary: Deletes a node (and any children) from the tree.
Parameters:
pNodeIds: Comma delimited list of node ids.
pRefreshView: Pass in false to prevent the view being refreshed.
Returns: nothing
*/
command DeleteNodes pNodeIds, pRefreshView
local tDirtyIdsA, tNodeA, tId
put pRefreshView is not false into pRefreshView
repeat for each item tId in pNodeIds
put _findNodeOfId(tId) into tNodeA
if tNodeA is an array then
_deleteNode tNodeA, tDirtyIdsA
end if
end repeat
repeat for each key tId in tDirtyIdsA
_setNodeRowIsDirty tId, true
end repeat
# Brute force
put true into sRebuildLookupTable
if pRefreshView then
_refreshView
end if
return empty
end DeleteNodes
/**
Summary: Adds a node to a parent.
Parameters:
pNodeA: Node array to add to parent.
pParent: Parent index lookup array.
pPosition: Position within parent.
rDirtyIdsA: Array whose keys are ids that are dirty after this handler completes.
Returns: nothing
*/
private command _addNodeToParent pNodeA, pParent, pPosition, @rDirtyIdsA
local tChildrenA, tOldChildCount
put the number of elements of sTreeA[pParent]["children"] into tOldChildCount
if pPosition is empty then
put tOldChildCount + 1 into pPosition
end if
if sTreeA[pParent]["children"][pPosition] is an array then
local i
repeat with i = tOldChildCount down to pPosition
put sTreeA[pParent]["children"][i] into sTreeA[pParent]["children"][i+1]
_updateNodeIndexInLookupTable sTreeA[pParent]["children"][i]["id"], i+1
end repeat
end if
put pNodeA into sTreeA[pParent]["children"][pPosition]
//////////
// Mark any rows that need to change UI based on addition as dirty
//////////
// This may not exist in tree yet. _setNodeRowIsDirty performs checks though.
put empty into rDirtyIdsA[ pNodeA["id"] ]
// If 1st sibling was shifted down then it is dirty
if pPosition is 1 and tOldChildCount > 0 then
put empty into rDirtyIdsA[ sTreeA[pParent]["children"][2]["id"] ]
end if
if pPosition is tOldChildCount+1 then
// previous sibling is dirty as it was previously the last child
put empty into rDirtyIdsA[ sTreeA[pParent]["children"][tOldChildCount]["id"] ]
end if
if tOldChildCount is 0 then
// parent is dirty as it now has children
put empty into rDirtyIdsA[ sTreeA[pParent]["id"] ]
end if
end _addNodeToParent
/**
Summary: Moves a node to a new parent.
Parameters:
pNode: Node lookup array.
pCurrentParent: Current parent index lookup array.
pNewParent: New parent index lookup array.
pPosition: Position within the new parent.
rDirtyIdsA: Array whose keys are ids that are dirty after this handler completes.
Returns: nothing
*/
private command _moveNodeToNewParent pNode, pNewParent, pPosition, @rDirtyIdsA
local tNodeA
put min(max(1, pPosition), the number of elements of sTreeA[pNewParent]["children"]+1) into pPosition
// Store copy of node
put sTreeA[pNode] into tNodeA
// Delete node
_deleteNode pNode, rDirtyIdsA
// Move to new parent
_addNodeToParent tNodeA, pNewParent, pPosition, rDirtyIdsA
end _moveNodeToNewParent
/**
Summary: Deletes a node from the tree along with the associated row controls.
Parameters:
pNode: Node index lookup array.
xDirtyIdsA: Array whose keys are ids of nodes that are dirty after this handler completes.
Returns: nothing
*/
private command _deleteNode pNode, @xDirtyIdsA
local tParent, tCurPosition, tChildCount, i
put _parentNodeIndex(pNode) into tParent
put the number of elements of sTreeA[tParent]["children"] into tChildCount
put _nodePosition(pNode) into tCurPosition
# Delete control if caching is turned on
if the viewProp["cache"] of me is not "none" then
DeleteRowControlFromCache sNodeIdRowLookupA[ sTreeA[pNode]["id"] ]
end if
# Remove all affected nodes from the lookup tables so non-existent index paths
# aren't returned in calls to _findNodeOfId(). The lookup tables will be rebuilt
# after calls to this handler.
_removeNodeFromLookupTables pNode
delete local sTreeA[pNode]
// Adjust siblings
repeat with i = tCurPosition+1 to tChildCount
put sTreeA[tParent]["children"][i] into sTreeA[tParent]["children"][i-1]
_updateNodeIndexInLookupTable sTreeA[tParent]["children"][i]["id"], i-1
end repeat
// Delete last sibling which is no longer valid
if sTreeA[tParent]["children"][tChildCount] is an array then
delete local sTreeA[tParent]["children"][tChildCount]
end if
//////////
// Mark any rows that need to change UI based on addition as dirty
//////////
// If first sibling was affected then it is dirty
if tCurPosition is 1 then
put empty into xDirtyIdsA[ sTreeA[tParent]["children"][1]["id"] ]
end if
// If last sibling was affected then it is dirty
if tCurPosition is tChildCount then
put empty into xDirtyIdsA[ sTreeA[tParent]["children"][tChildCount-1]["id"] ]
end if
// If last child was deleted then mark parent as dirty
if tChildCount is 1 then
put empty into xDirtyIdsA[ sTreeA[tParent]["id"] ]
end if
end _deleteNode
private command _updateNodeIndexInLookupTable pNodeId, pNewIndex
local tIndexA
put sNodeIdNodeLookupA[ pNodeId ] into tIndexA
put pNewIndex into tIndexA[the number of elements of tIndexA]
put tIndexA into sNodeIdNodeLookupA[ pNodeId ]
end _updateNodeIndexInLookupTable
private command _removeNodeFromLookupTables pNode
local tRow, tIndexA, tIndexCount
# Delete node in tables
put sNodeIdRowLookupA[ sTreeA[pNode]["id"] ] into tRow
delete local sRowNodeIdLookupA[ tRow ]
delete local sNodeIdRowLookupA[ sTreeA[pNode]["id"] ]
delete local sNodeIdNodeLookupA[ sTreeA[pNode]["id"] ]
# Delete node children
put the number of elements of pNode into tIndexCount
put pNode into tIndexA
repeat with tIndex = 1 to the number of elements of sTreeA[pNode]["children"]
put "children" into tIndexA[tIndexCount + 1]
put tIndex into tIndexA[tIndexCount + 2]
_removeNodeFromLookupTables tIndexA
end repeat
end _removeNodeFromLookupTables
/**
Summary: Moves a node within current parent and marks affected rows as dirty.
Parameters:
pNode: Node lookup array.
pParent: Parent index lookup array.
pPosition: Position within the new parent.
rDirtyIdsA: Array whose keys are ids that are dirty.
Returns: nothing
*/
private command _moveNodeWithinParent pNode, pParent, pPosition, @rDirtyIdsA
local tChildCount, tCurPosition, i
local tNodeA
put the number of elements of sTreeA[pParent]["children"] into tChildCount
put min(max(1, pPosition), tChildCount) into pPosition
put _nodePosition(pNode) into tCurPosition
if tCurPosition is pPosition then
return empty
end if
put sTreeA[pNode] into tNodeA
if tCurPosition < pPosition then
-- t 1
-- 2 ^
-- 3 ^
-- > 4 ^
-- 5
repeat with i = tCurPosition+1 to pPosition
put sTreeA[pParent]["children"][i] into sTreeA[pParent]["children"][i-1]
_updateNodeIndexInLookupTable sTreeA[pParent]["children"][i]["id"], i-1
end repeat
else if tCurPosition > pPosition then
-- > 1 v
-- 2 v
-- 3 v
-- t 4
-- 5
repeat with i = tCurPosition-1 down to pPosition
put sTreeA[pParent]["children"][i] into sTreeA[pParent]["children"][i+1]
_updateNodeIndexInLookupTable sTreeA[pParent]["children"][i]["id"], i+1
end repeat
end if
put tNodeA into sTreeA[pParent]["children"][pPosition]
//////////
// Mark any rows that need to change UI based on addition as dirty
//////////
// If last sibling was affected then it is dirty
if tCurPosition is tChildCount or pPosition is tChildCount then
put empty into rDirtyIdsA[ sTreeA[pParent]["children"][tChildCount]["id"] ]
end if
// If first sibling was affected then it is dirty
if tCurPosition is 1 or pPosition is 1 then
put empty into rDirtyIdsA[ sTreeA[pParent]["children"][1]["id"] ]
end if
end _moveNodeWithinParent
/**
Summary: Build lookup tables so that `NumberOfRows` can return correct value.
Returns: nothing
*/
command RefreshViewRows
if sRebuildLookupTable then
_buildRowToNodeLookup
put false into sRebuildLookupTable
end if
pass RefreshViewRows
end RefreshViewRows
/**
Summary: Build lookup tables so that `NumberOfRows` can return correct value.
Returns: nothing
*/
command RenderView
if sRebuildLookupTable then
_buildRowToNodeLookup
put false into sRebuildLookupTable
end if
pass RenderView
end RenderView
command DataForRow pRow, @rData, @rTemplateStyle
local tNodeA, tIndexA, tKey
put sNodeIdNodeLookupA[sRowNodeIdLookupA[pRow]] into tIndexA
# id, type, expanded, is leaf, + any custom keys
repeat for each key tKey in sTreeA[ tIndexA ]
if tKey is "children" then next repeat
put sTreeA[ tIndexA ][tKey] into tNodeA[tKey]
end repeat
put _nodeLevel(tIndexA) into tNodeA["level"]
put the number of elements of sTreeA[ tIndexA ]["children"] into tNodeA["child count"]
# Cache for future use
put _nodeTreeLineStyles(tIndexA, tNodeA["level"]) into sTreeA[ tIndexA ]["tree line styles"]
dispatch "DataForNode" with tNodeA, pRow, rData, rTemplateStyle
end DataForRow
function NumberOfRows
return the number of elements of sRowNodeIdLookupA
end NumberOfRows
/**
Summary: Returns the cache key for a given row.
Description:
The Cache Key for each row is a combination of the `id` property and an
internal `@version` property. Whenever a property is changed in the node array
(e.g. the `expanded` state) then the node is marked as dirty and the `@version`
will be updated, thus changing the cache key for the row. When the cache key
changes the DataView will redraw the row with the latest data.
Returns: String
*/
function CacheKeyForRow pRow
return sTreeA[ sNodeIdNodeLookupA[sRowNodeIdLookupA[pRow]] ]["id"]
end CacheKeyForRow
/**
Summary: Refreshes the view after toggling or showing nodes.
Description:
When calling handlers that toggle the expanded state of a node(s) the UI
is not updated. This allows you to update multiple nodes and redraw all at
once. Call this handler when you are ready to redraw the view.
Returns: nothing
*/
command RefreshView
_refreshView
return empty
end RefreshView
/**
Summary: Hide the treeline widget before creating the drag image for a row control.
Returns: nothing
*/
before PreDragImageSnapshot
if there is a control "TreeLines" of the target then
set the visible of control "TreeLines" of the target to false
end if
end PreDragImageSnapshot
/**
Summary: Show the treeline widget after creating the drag image for a row control.
Returns: nothing
*/
before PostDragImageSnapshot
if there is a control "TreeLines" of the target then
set the visible of control "TreeLines" of the target to true
end if
end PostDragImageSnapshot
/**
Summary: Expands all rows in the tree.
Description:
The view will be redrawn when calling this handler.
Returns: nothing
*/
command ExpandAllNodes
local tIndexA
put "root" into tIndexA[1]
put "children" into tIndexA[2]
_setExpandedInTreeBranch sTreeA["root"]["children"], tIndexA, true
put true into sRebuildLookupTable
_refreshView
end ExpandAllNodes
/**
Summary: Contracts all rows in the tree.
Description:
The view will be redrawn when calling this handler.
Returns: nothing
*/
command CollapseAllNodes
local tIndexA
put "root" into tIndexA[1]
put "children" into tIndexA[2]
_setExpandedInTreeBranch sTreeA["root"]["children"], tIndexA, false
put true into sRebuildLookupTable
_refreshView
end CollapseAllNodes
/**
Summary: Sets the expanded state of a row.
Parameters:
pRow: The row to toggle.
pLevelsDown: How many levels to toggle. Empty or 0 only toggles pRow. -1 toggles all. Positive number affects that many levels down.
pExpandedState: Pass in a true/false to force a setting.
pRefreshView: Pass in false to keep the view from being refreshed.
Description:
By default the DataView will be redrawn after calling this handler and the newly displayed
children nodes will be scrolled into view. If you pass in `false` for `pRefreshView`
then call `RefreshView` to redraw the view.
Returns `true` if the expanded state of any node was changed, `false` if not.
Returns: Boolean
*/
command SetRowIsExpanded pRow, pLevelsDown, pExpandedState, pRefreshView
if pRow < 1 then throw "invalid row:" && pRow & cr & the executioncontexts
SetNodeIsExpanded sRowNodeIdLookupA[pRow], pLevelsDown, pExpandedState, pRefreshView
return the result
end SetRowIsExpanded
/**
Summary: Sets the expanded state of a node.
Parameters:
pNode: The node `id` or the node index array.
pLevelsDown: How many levels to toggle. Empty or 0 only toggles pRow. -1 toggles all. Positive number affects that many levels down.
pExpandedState: Pass in a true/false to force a setting.
pRefreshView: Pass in `false` to keep the view from being refreshed.
Description:
Expands the target node, expanding ancestors if need be in order to show the
target node.
See `SetRowIsExpanded` for more information.
Returns `true` if the expanded state of any node was changed, `false` if not.
Returns: Boolean
*/
command SetNodeIsExpanded pNode, pLevelsDown, pExpandedState, pRefreshView
local tRow, tCurrentLevel
local tMadeChange = false
put pRefreshView is not false into pRefreshView
if pNode is not an array then put sNodeIdNodeLookupA[pNode] into pNode
if pNode is not an array then throw "invalid node id" & cr & the executioncontexts
if sTreeA[pNode]["is leaf"] then throw "cannot change expanded setting of a leaf node" & cr & the executioncontexts
if pLevelsDown is empty then
put 0 into pLevelsDown
else if pLevelsDown < 0 then
put -1 into pLevelsDown
end if
if sRebuildLookupTable then
_buildRowToNodeLookup
put false into sRebuildLookupTable
end if
# Toggle
_setNodeExpandedState pNode, pExpandedState
put the result into tMadeChange
# Affect children
if pLevelsDown is not 0 then
put 0 into tCurrentLevel
_setNodeAncestorExpandedState pNode, sTreeA[pNode]["expanded"], \
pLevelsDown, tCurrentLevel
if not tMadeChange then
put the result into tMadeChange
end if
end if
if pRefreshView then
_refreshView
end if
return tMadeChange
end SetNodeIsExpanded
/**
Summary: Toggles the expanded state of a row.
Parameters:
pRow: The row to toggle.
pLevelsDown: How many levels to toggle. Empty or 0 only toggles pRow. -1 toggles all. Positive number affects that many levels down.
pRefreshView: Pass in false to keep the view from being refreshed.
Description:
See `SetRowIsExpanded`.
Returns the new expanded state.
Returns: Boolean
*/
command ToggleRowIsExpanded pRow, pLevelsDown, pRefreshView
local tNodeIndexA, tExpandedState
put sNodeIdNodeLookupA[sRowNodeIdLookupA[pRow]] into tNodeIndexA
put not sTreeA[tNodeIndexA]["expanded"] into tExpandedState
SetRowIsExpanded pRow, pLevelsDown, tExpandedState, pRefreshView
return tExpandedState for value
end ToggleRowIsExpanded
/**
Summary: Toggles the expanded state of a node.
Parameters:
pNode: The node `id` or the node index array.
pLevelsDown: How many levels to toggle. Empty or 0 only toggles pRow. -1 toggles all. Positive number affects that many levels down.
pRefreshView: Pass in false to keep the view from being refreshed.
Description:
See `SetRowIsExpanded`.
Returns the new expanded state.
Returns: Boolean
*/
command ToggleNodeIsExpanded pNode, pLevelsDown, pRefreshView
local tExpandedState
if pNode is not an array then put _findNodeOfId(pNode, true) into pNode
put not sTreeA[pNode]["expanded"] into tExpandedState
SetNodeIsExpanded pNode, pLevelsDown, tExpandedState, pRefreshView
return tExpandedState for value
end ToggleNodeIsExpanded
/**
Summary: Expands any tree nodes necessary so that the node can be seen.
Parameters:
pNode: The node `id` or the node index array.
pRefreshView: Pass in false to keep the view from being refreshed.
Description:
By default the DataView will be redrawn after calling this handler and the node will
be scrolled into view. If you pass in `false` for `pRefreshView`
then call `RefreshView` to redraw the view and `ScrollRowIntoView` to scroll
the row into view.
Returns: empty
*/
command ShowNode pNode, pRefreshView
if pNode is not an array then put sNodeIdNodeLookupA[pNode] into pNode
_showNode pNode
if pRefreshView then
lock screen
_refreshView
ScrollRowIntoView _rowOfNode(pNode)
unlock screen
end if
return empty
end ShowNode
/**
Summary: Scrolls a row and it's descendants into view.
Parameters:
pRow: The target row.
Description:
When expanding a row it may be desirable to scroll the newly exposed children
into view. This handler will scroll the descendants into view without pushing
pRow off the top.
Returns: nothing
*/
command ScrollRowDescendantsIntoView pRow
local tDescendantsA, tRect, tHeight
put the dvRectOfRow[pRow] of me into tRect
add item 4 of tRect - item 2 of tRect to tHeight
put _descendantElements(sNodeIdNodeLookupA[sRowNodeIdLookupA[pRow]], true) into tDescendantsA
if tDescendantsA is an array then
repeat with i = 1 to the number of elements of tDescendantsA
put the dvRectOfRow[ _rowOfNode(tDescendantsA[i]) ] of me into tRect
add item 4 of tRect - item 2 of tRect to tHeight
end repeat
end if
if tHeight > the viewProp["content window height"] of me then
# Descendants are too tall. Scroll anchor row to top.
ScrollRowIntoView pRow, "top"
else
# All descendants fit into view. Scroll last one to bottom.
local tLastRow
if tDescendantsA is an array then
put _rowOfNode(tDescendantsA[the number of elements of tDescendantsA]) into tLastRow
else
put pRow into tLastRow
end if
ScrollRowIntoView tLastRow, "bottom"
end if
end ScrollRowDescendantsIntoView
/**
Summary: See ScrollRowDescendantsIntoView/
Returns: nothing
*/
command ScrollNodeDescendantsIntoView pNode
ScrollRowDescendantsIntoView _rowOfNode(pNode)
end ScrollNodeDescendantsIntoView
/**
Summary: Finds a node by `id` and returns the index array pointing to a node in the tree.
Parameters:
pNodeId: The `id` of the node.
Description:
The index array that is returned by this function can be used in other DataView Tree
handlers. Index array can be used by the LiveCode engine to find nested keys in a
LiveCode array.
An index array is a numerically indexed array that represents the keys pointing
to a specific key in a nested array. For a key that is not nested the index array
would have a single key. For a key that is nested three levels deep the index
array would have three keys.
Assume the following array:
```
tPersonA["name"]
tPersonA["children"]
tPersonA["children"][1]["name"]
tPersonA["children"][2]["name"]
tPersonA["children"][3]["name"]
```
The index array for the third child would be as follows:
```
put "children" into tIndexA[1]
put 3 into tIndexA[2]
```
To get the name of the third child you would use the following syntax:
```
put tPersonA[tIndexA]["name"] into tChildName
```
Returns: Index array
*/
getProp dvNodeOfId[pNodeId]
return _findNodeOfId(pNodeId, false)
end dvNodeOfId
/**
Summary: Same as dvRowOfNodeId, just shorter.
Returns: Integer
*/
getProp dvRowOfId[pNodeId]
return _rowOfNode(pNodeId)
end dvRowOfId
/**
Summary: Returns the row assigned to a node.
Parameters:
pNodeId: The node id to search for.
Description:
If no row is associated with the id passed in then 0 is returned.
A valid id will not have a row if it is the descendant of an
ancestor that is not expanded.
Returns: Integer
*/
getProp dvRowOfNodeId[pNodeId]
return _rowOfNode(pNodeId)
end dvRowOfNodeId
/**
Summary: Returns the node index array of the node associated with a row.
Returns: Array
*/
getProp dvRowNode[pRow]
return sNodeIdNodeLookupA[sRowNodeIdLookupA[pRow]]
end dvRowNode
/**
Summary: Returns the `id` of the row's node.
Parameters:
pRow: The target row
Returns: Mixed
*/
getProp dvRowId[pRow]
return sTreeA[ sNodeIdNodeLookupA[sRowNodeIdLookupA[pRow]] ]["id"]
end dvRowId
/**
Summary: Sets the `type` of a row.
Parameters:
pRow: The target row.
pType: The type.
Description:
Setting this property will not refresh the DataView.
*/
setProp dvRowType[pRow] pType
_setNodeProperty sNodeIdNodeLookupA[sRowNodeIdLookupA[pRow]], "type", pType
end dvRowType
/**
Summary: Returns the `type` of the row's node.
Parameters:
pRow: The target row
Returns: Mixed
*/
getProp dvRowType[pRow]
return sTreeA[ sNodeIdNodeLookupA[sRowNodeIdLookupA[pRow]] ]["type"]
end dvRowType
/**
Summary: Returns the control associated with a node.
Parameters:
pNodeId: The target node id.
Returns: Control reference or empty
*/
getProp dvControlOfNode[pNodeId]
return the dvControlOfRow[ sNodeIdRowLookupA[pNodeId] ] of me
end dvControlOfNode
/**
Summary: Sets the `type` of a node.
Parameters:
pNodeId: The target node id.
pType: The type.