-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTreeView.hx
More file actions
3001 lines (2687 loc) · 99.8 KB
/
TreeView.hx
File metadata and controls
3001 lines (2687 loc) · 99.8 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
/*
Feathers UI
Copyright 2026 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.controls;
import feathers.controls.dataRenderers.HierarchicalItemRenderer;
import feathers.controls.dataRenderers.IDataRenderer;
import feathers.controls.dataRenderers.IHierarchicalDepthItemRenderer;
import feathers.controls.dataRenderers.IHierarchicalItemRenderer;
import feathers.controls.dataRenderers.ITreeViewItemRenderer;
import feathers.controls.supportClasses.AdvancedLayoutViewPort;
import feathers.controls.supportClasses.BaseScrollContainer;
import feathers.core.IDataSelector;
import feathers.core.IFocusContainer;
import feathers.core.IMeasureObject;
import feathers.core.IOpenCloseToggle;
import feathers.core.ITextControl;
import feathers.core.IUIControl;
import feathers.core.InvalidationFlag;
import feathers.data.IHierarchicalCollection;
import feathers.data.TreeViewItemState;
import feathers.events.FeathersEvent;
import feathers.events.HierarchicalCollectionEvent;
import feathers.events.TreeViewEvent;
import feathers.events.TriggerEvent;
import feathers.layout.IKeyboardNavigationLayout;
import feathers.layout.ILayout;
import feathers.layout.ILayoutIndexObject;
import feathers.layout.IScrollLayout;
import feathers.layout.ITypicalItemLayout;
import feathers.layout.IVirtualLayout;
import feathers.layout.Measurements;
import feathers.style.IVariantStyleObject;
import feathers.utils.AbstractDisplayObjectRecycler;
import feathers.utils.DisplayObjectRecycler;
import haxe.ds.ObjectMap;
import haxe.ds.StringMap;
import openfl.display.DisplayObject;
import openfl.errors.ArgumentError;
import openfl.errors.IllegalOperationError;
import openfl.events.Event;
import openfl.events.KeyboardEvent;
import openfl.events.MouseEvent;
import openfl.events.TouchEvent;
import openfl.text.TextField;
import openfl.ui.Keyboard;
#if air
import openfl.ui.Multitouch;
#end
#if (openfl >= "9.1.0")
import openfl.utils.ObjectPool;
#else
import openfl._internal.utils.ObjectPool;
#end
/**
Displays a hierarchical tree of items. Supports scrolling, custom item
renderers, and custom layouts.
The following example creates a tree, gives it a data provider, tells
the item renderer how to interpret the data, and listens for when the
selection changes:
```haxe
var treeView = new TreeView();
treeView.dataProvider = new ArrayHierarchicalCollection<TreeItemData>([
{
text: "Node 1",
children: [
{
text: "Node 1A",
children: [
{text: "Node 1A-I"},
{text: "Node 1A-II"},
{text: "Node 1A-III"},
{text: "Node 1A-IV"}
]
},
{text: "Node 1B"},
{text: "Node 1C"}
]
},
{
text: "Node 2",
children: [
{text: "Node 2A"},
{text: "Node 2B"},
{text: "Node 2C"}
]
},
{text: "Node 3"},
{
text: "Node 4",
children: [
{text: "Node 4A"},
{text: "Node 4B"},
{text: "Node 4C"},
{text: "Node 4D"},
{text: "Node 4E"}
]
}
], (item:TreeItemData) -> item.children);
treeView.itemToText = (item:TreeItemData) -> {
return item.text;
};
treeView.addEventListener(Event.CHANGE, (event:Event) -> {
var treeView = cast(event.currentTarget, TreeView);
trace("TreeView changed: " + treeView.selectedLocation + " " + treeView.selectedItem.text);
});
this.addChild(treeView);
```
The example above uses the following custom [Haxe typedef](https://haxe.org/manual/type-system-typedef.html).
```haxe
typedef TreeItemData = {
text:String,
?children:Array<TreeItemData>
};
```
@event openfl.events.Event.CHANGE Dispatched when either
`TreeView.selectedItem` or `TreeView.selectedLocation` changes.
@event feathers.events.TreeViewEvent.ITEM_TRIGGER Dispatched when the user
taps or clicks an item renderer in the tree view. The pointer must remain
within the bounds of the item renderer on release, and the tree view cannot
scroll before release, or the gesture will be ignored.
@event feathers.events.TreeViewEvent.ITEM_DOUBLE_CLICK Dispatched when the
user double-clicks an item renderer in the tree view with a mouse. The item
renderer's `doubleClickEnabled` property must be set to true, and in some
cases the same property must be set on its children.
@event feathers.events.TreeViewEvent.BRANCH_OPEN Dispatched when a branch
is opened.
@event feathers.events.TreeViewEvent.BRANCH_CLOSE Dispatched when a branch
is closed.
@event feathers.events.TreeViewEvent.BRANCH_OPENING Dispatched before a
branch opens.
@event feathers.events.TreeViewEvent.BRANCH_CLOSING Dispatched before a
branch closes.
@see [Tutorial: How to use the TreeView component](https://feathersui.com/learn/haxe-openfl/tree-view/)
@since 1.0.0
**/
@:event(openfl.events.Event.CHANGE)
@:event(feathers.events.TreeViewEvent.ITEM_TRIGGER)
@:event(feathers.events.TreeViewEvent.ITEM_DOUBLE_CLICK)
@:event(feathers.events.TreeViewEvent.BRANCH_OPEN)
@:event(feathers.events.TreeViewEvent.BRANCH_CLOSE)
@:event(feathers.events.TreeViewEvent.BRANCH_OPENING)
@:event(feathers.events.TreeViewEvent.BRANCH_CLOSING)
@:access(feathers.data.TreeViewItemState)
@defaultXmlProperty("dataProvider")
@:styleContext
class TreeView extends BaseScrollContainer implements IDataSelector<Dynamic> implements IFocusContainer {
/**
A variant used to style the tree view without a border. The variant is
used by default on mobile.
The following example uses this variant:
```haxe
var treeView = new TreeView();
treeView.variant = TreeView.VARIANT_BORDERLESS;
```
@see `feathers.style.IVariantStyleObject.variant`
@see [Feathers UI User Manual: Themes](https://feathersui.com/learn/haxe-openfl/themes/)
@since 1.0.0
**/
public static final VARIANT_BORDERLESS = "borderless";
/**
A variant used to style the tree view with a border. This variant is
used by default on desktop.
The following example uses this variant:
```haxe
var treeView = new TreeView();
treeView.variant = TreeView.VARIANT_BORDER;
```
@see `feathers.style.IVariantStyleObject.variant`
@see [Feathers UI User Manual: Themes](https://feathersui.com/learn/haxe-openfl/themes/)
@since 1.0.0
**/
public static final VARIANT_BORDER = "border";
/**
The variant used to style the tree view's item renderers in a theme.
To override this default variant, set the
`TreeView.customItemRendererVariant` property.
@see `TreeView.customItemRendererVariant`
@see `feathers.style.IVariantStyleObject.variant`
@see [Feathers UI User Manual: Themes](https://feathersui.com/learn/haxe-openfl/themes/)
@since 1.0.0
**/
public static final CHILD_VARIANT_ITEM_RENDERER = "treeView_itemRenderer";
private static final INVALIDATION_FLAG_ITEM_RENDERER_FACTORY = InvalidationFlag.CUSTOM("itemRendererFactory");
private static final RESET_ITEM_STATE = new TreeViewItemState();
private static function defaultItemToText(data:Dynamic):String {
return Std.string(data);
}
private static function defaultItemToEnabled(data:Dynamic):Bool {
return true;
}
private static function defaultUpdateItemRenderer(itemRenderer:DisplayObject, state:TreeViewItemState):Void {
if ((itemRenderer is ITextControl)) {
var textControl:ITextControl = cast itemRenderer;
textControl.text = state.text;
}
}
private static function defaultResetItemRenderer(itemRenderer:DisplayObject, state:TreeViewItemState):Void {
if ((itemRenderer is ITextControl)) {
var textControl:ITextControl = cast itemRenderer;
textControl.text = null;
}
}
/**
Creates a new `TreeView` object.
@since 1.0.0
**/
public function new(?dataProvider:IHierarchicalCollection<Dynamic>, ?changeListener:(Event) -> Void) {
initializeTreeViewTheme();
super();
this.dataProvider = dataProvider;
this.tabEnabled = true;
this.focusRect = null;
if (this.viewPort == null) {
this.treeViewPort = new AdvancedLayoutViewPort();
this.addChild(this.treeViewPort);
this.viewPort = this.treeViewPort;
}
if (changeListener != null) {
this.addEventListener(Event.CHANGE, changeListener);
}
}
private var treeViewPort:AdvancedLayoutViewPort;
#if (flash && haxe_ver < 4.3) @:getter(tabEnabled) #end
override private function get_tabEnabled():Bool {
return (this._selectable || this.maxScrollY != this.minScrollY || this.maxScrollX != this.minScrollX)
&& this._enabled
&& this.rawTabEnabled;
}
private var _childFocusEnabled:Bool = true;
/**
@see `feathers.core.IFocusContainer.childFocusEnabled`
**/
public var childFocusEnabled(get, set):Bool;
private function get_childFocusEnabled():Bool {
return this._enabled && this._childFocusEnabled;
}
private function set_childFocusEnabled(value:Bool):Bool {
if (this._childFocusEnabled == value) {
return this._childFocusEnabled;
}
this._childFocusEnabled = value;
return this._childFocusEnabled;
}
private var _openBranches:Array<Dynamic> = [];
private var _dataProvider:IHierarchicalCollection<Dynamic>;
/**
The collection of data displayed by the tree view.
Items in the collection must be class instances or anonymous structures.
Do not add primitive values (such as strings, booleans, or numeric
values) directly to the collection.
Additionally, all items in the collection must be unique object
instances. Do not add the same instance to the collection more than
once because a runtime exception will be thrown.
The following example passes in a data provider and tells the item
renderer how to interpret the data:
```haxe
var collection = new ArrayHierarchicalCollection([
{
text: "Node 1",
children: [
{
text: "Node 1A",
children: [
{text: "Node 1A-I"},
{text: "Node 1A-II"},
{text: "Node 1A-III"},
{text: "Node 1A-IV"}
]
},
{text: "Node 1B"},
{text: "Node 1C"}
]
},
{
text: "Node 2",
children: [
{text: "Node 2A"},
{text: "Node 2B"},
{text: "Node 2C"}
]
},
{text: "Node 3"},
{
text: "Node 4",
children: [
{text: "Node 4A"},
{text: "Node 4B"},
{text: "Node 4C"},
{text: "Node 4D"},
{text: "Node 4E"}
]
}
]);
collection.itemToChildren = (item:Dynamic) -> {
return item.children;
};
treeView.dataProvider = collection;
treeView.itemToText = (item:Dynamic) -> {
return item.text;
};
```
@default null
@see `feathers.data.ArrayHierarchicalCollection`
@since 1.0.0
**/
@:bindable("dataChange")
public var dataProvider(get, set):IHierarchicalCollection<Dynamic>;
private function get_dataProvider():IHierarchicalCollection<Dynamic> {
return this._dataProvider;
}
private function set_dataProvider(value:IHierarchicalCollection<Dynamic>):IHierarchicalCollection<Dynamic> {
if (this._dataProvider == value) {
return this._dataProvider;
}
// clear the entire layout cache when we receive a new data provider.
// we should not assume it contains the same set of items, and even if
// it does, they may not be in the same order. checking if the new data
// provider matches the old data provider would be overly expensive and
// likely to be error prone.
#if (hl && haxe_ver < 4.3)
this._virtualCache.splice(0, this._virtualCache.length);
#else
this._virtualCache.resize(0);
#end
this._totalLayoutCount = 0;
#if (hl && haxe_ver < 4.3)
this._openBranches.splice(0, this._openBranches.length);
#else
this._openBranches.resize(0);
#end
if (this._dataProvider != null) {
this._dataProvider.removeEventListener(Event.CHANGE, treeView_dataProvider_changeHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.ADD_ITEM, treeView_dataProvider_addItemHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.REMOVE_ITEM, treeView_dataProvider_removeItemHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.REPLACE_ITEM, treeView_dataProvider_replaceItemHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.REMOVE_ALL, treeView_dataProvider_removeAllHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.RESET, treeView_dataProvider_resetHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.FILTER_CHANGE, treeView_dataProvider_filterChangeHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.SORT_CHANGE, treeView_dataProvider_sortChangeHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.UPDATE_ITEM, treeView_dataProvider_updateItemHandler);
this._dataProvider.removeEventListener(HierarchicalCollectionEvent.UPDATE_ALL, treeView_dataProvider_updateAllHandler);
}
this._dataProvider = value;
if (this._dataProvider != null) {
this._totalLayoutCount = this.calculateTotalLayoutCount([]);
this._virtualCache.resize(this._totalLayoutCount);
this._dataProvider.addEventListener(Event.CHANGE, treeView_dataProvider_changeHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.ADD_ITEM, treeView_dataProvider_addItemHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.REMOVE_ITEM, treeView_dataProvider_removeItemHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.REPLACE_ITEM, treeView_dataProvider_replaceItemHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.REMOVE_ALL, treeView_dataProvider_removeAllHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.RESET, treeView_dataProvider_resetHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.FILTER_CHANGE, treeView_dataProvider_filterChangeHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.SORT_CHANGE, treeView_dataProvider_sortChangeHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.UPDATE_ITEM, treeView_dataProvider_updateItemHandler);
this._dataProvider.addEventListener(HierarchicalCollectionEvent.UPDATE_ALL, treeView_dataProvider_updateAllHandler);
}
// reset the scroll position because this is a drastic change and
// the data is probably completely different
this.scrollX = 0.0;
this.scrollY = 0.0;
// clear the selection for the same reason
this.selectedLocation = null;
this.setInvalid(DATA);
FeathersEvent.dispatch(this, "dataChange");
return this._dataProvider;
}
private var _selectedLocation:Array<Int> = null;
/**
The currently selected location. Returns `null` if no location is
selected.
The following example selects a specific location:
```haxe
treeView.selectedLocation = [2, 0];
```
The following example clears the currently selected location:
```haxe
treeView.selectedLocation = null;
```
The following example listens for when the selection changes, and it
prints the new selected location to the debug console:
```haxe
var treeView = new TreeView();
function changeHandler(event:Event):Void
{
var treeView = cast(event.currentTarget, TreeView);
trace("selection change: " + treeView.selectedLocation);
}
treeView.addEventListener(Event.CHANGE, changeHandler);
```
@default null
@since 1.0.0
**/
@:bindable("change")
public var selectedLocation(get, set):Array<Int>;
private function get_selectedLocation():Array<Int> {
return this._selectedLocation;
}
private function set_selectedLocation(value:Array<Int>):Array<Int> {
if (!this._selectable || this._dataProvider == null) {
value = null;
}
if (this._selectedLocation == value || this.compareLocations(this._selectedLocation, value) == 0) {
return this._selectedLocation;
}
if (value == null) {
this._selectionAnchorIndex = -1;
this._selectedItem = null;
this._selectedLocation = null;
#if (hl && haxe_ver < 4.3)
this._selectedLocations.splice(0, this._selectedLocations.length);
this._selectedItems.splice(0, this._selectedItems.length);
#else
this._selectedLocations.resize(0);
this._selectedItems.resize(0);
#end
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._selectedLocation;
}
this._selectedLocation = value;
this._selectedItem = this._dataProvider.get(this._selectedLocation);
this._selectedLocations.resize(1);
this._selectedLocations[0] = this._selectedLocation.copy();
this._selectedItems.resize(1);
this._selectedItems[0] = this._selectedItem;
this._selectionAnchorIndex = this.locationToDisplayIndex(this._selectedLocation, false);
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._selectedLocation;
}
private var _selectedItem:Dynamic = null;
/**
@see `feathers.core.IDataSelector.selectedItem`
**/
@:bindable("change")
public var selectedItem(get, set):Dynamic;
private function get_selectedItem():Dynamic {
return this._selectedItem;
}
private function set_selectedItem(value:Dynamic):Dynamic {
if (value == null || !this._selectable || this._dataProvider == null) {
// use the setter
this.selectedLocation = null;
return this._selectedItem;
}
var location = this._dataProvider.locationOf(value);
if (location == null) {
// use the setter
this.selectedLocation = null;
return this._selectedItem;
}
if (this._selectedItem == value && this.compareLocations(this._selectedLocation, location) == 0) {
return this._selectedItem;
}
this._selectedItem = value;
this._selectedLocation = location;
this._selectedLocations.resize(1);
this._selectedLocations[0] = this._selectedLocation;
this._selectedItems.resize(1);
this._selectedItems[0] = this._selectedItem;
this._selectionAnchorIndex = this.locationToDisplayIndex(this._selectedLocation, false);
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._selectedItem;
}
private var _allowMultipleSelection:Bool = false;
/**
Determines if multiple items may be selected at the same time. Has no
effect if `selectable` is `false`.
In the following example, multiple selection is enabled:
```haxe
treeView.allowMultipleSelection = true;
```
@see `TreeView.selectable`
@see `TreeView.selectedLocations`
@see `TreeView.selectedItems`
@since 1.4.0
**/
public var allowMultipleSelection(get, set):Bool;
private function get_allowMultipleSelection():Bool {
return this._allowMultipleSelection;
}
private function set_allowMultipleSelection(value:Bool):Bool {
if (this._allowMultipleSelection == value) {
return this._allowMultipleSelection;
}
this._allowMultipleSelection = value;
this.setInvalid(SELECTION);
return this._allowMultipleSelection;
}
private var _selectionAnchorIndex:Int = -1;
private var _selectedLocations:Array<Array<Int>> = [];
/**
Contains all of the locations that are currently selected. The most
recently selected location will appear at the beginning of the array. In
other words, the locations are in the reverse order that they were
selected by the user.
When the `selectedLocations` array contains multiple items, the
`selectedLocation` property will return the first item from
`selectedLocations`.
@see `TreeView.allowMultipleSelection`
@see `TreeView.selectedItems`
@since 1.4.0
**/
@:bindable("change")
public var selectedLocations(get, set):Array<Array<Int>>;
private function get_selectedLocations():Array<Array<Int>> {
return this._selectedLocations;
}
private function set_selectedLocations(value:Array<Array<Int>>):Array<Array<Int>> {
if (value == null || value.length == 0 || !this._selectable || this._dataProvider == null) {
// use the setter
this.selectedLocation = null;
return this._selectedLocations;
}
if (this._selectedLocations == value) {
return this._selectedLocations;
}
if (!this._allowMultipleSelection && value.length > 1) {
value.resize(1);
}
this._selectedLocations = value;
this._selectedLocation = this._selectedLocations[0];
this._selectedItems.resize(this._selectedLocations.length);
for (i in 0...this._selectedLocations.length) {
var location = this._selectedLocations[i];
this._selectedItems[i] = this._dataProvider.get(location);
}
this._selectedItem = this._selectedItems[0];
this._selectionAnchorIndex = this.locationToDisplayIndex(this._selectedLocation, false);
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._selectedLocations;
}
private var _selectedItems:Array<Dynamic> = [];
/**
Contains all of the items that are currently selected. The most
recently selected item will appear at the beginning of the array. In
other words, the items are in the reverse order that they were
selected by the user.
When the `selectedItems` array contains multiple items, the
`selectedItem` property will return the first item from `selectedItems`.
@see `TreeView.allowMultipleSelection`
@see `TreeView.selectedLocations`
@since 1.4.0
**/
@:bindable("change")
public var selectedItems(get, set):Array<Dynamic>;
private function get_selectedItems():Array<Dynamic> {
return this._selectedItems;
}
private function set_selectedItems(value:Array<Dynamic>):Array<Dynamic> {
if (value == null || value.length == 0 || !this._selectable || this._dataProvider == null) {
// use the setter
this.selectedLocation = null;
return this._selectedItems;
}
if (this._selectedItems == value) {
return this._selectedItems;
}
if (!this._allowMultipleSelection && value.length > 1) {
value.resize(1);
}
var locations:Array<Array<Int>> = [];
var i = 0;
while (i < value.length) {
var item = value[i];
var location = this._dataProvider.locationOf(item);
if (location == null) {
value.splice(i, 1);
continue;
}
locations.push(location);
i++;
}
this._selectedLocations = locations;
this._selectedItems = value;
if (value.length == 0) {
this._selectedLocation = null;
this._selectedItem = null;
this._selectionAnchorIndex = -1;
} else {
this._selectedLocation = this._selectedLocations[0];
this._selectedItem = this._selectedItems[0];
this._selectionAnchorIndex = this.locationToDisplayIndex(this._selectedLocation, false);
}
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._selectedLocations;
}
private var _previousLayout:ILayout;
/**
The layout algorithm used to position and size the tree view's items.
By default, if no layout is provided by the time that the tree view
initializes, a default layout that displays items vertically will be
created.
The following example tells the tree view to use a horizontal layout:
```haxe
var layout = new HorizontalListLayout();
layout.gap = 20.0;
layout.padding = 20.0;
treeView.layout = layout;
```
@since 1.0.0
**/
@:style
public var layout:ILayout = null;
private var _previousCustomItemRendererVariant:String = null;
/**
A custom variant to set on all item renderers, instead of
`TreeView.CHILD_VARIANT_ITEM_RENDERER`.
The `customItemRendererVariant` will be not be used if the result of
`itemRendererRecycler.create()` already has a variant set.
@see `TreeView.CHILD_VARIANT_ITEM_RENDERER`
@see `feathers.style.IVariantStyleObject.variant`
@since 1.0.0
**/
@:style
public var customItemRendererVariant:String = null;
/**
Manages item renderers used by the tree view.
In the following example, the tree view uses a custom item renderer
class:
```haxe
treeView.itemRendererRecycler = DisplayObjectRecycler.withClass(CustomItemRenderer);
```
@see `feathers.controls.dataRenderers.HierarchicalItemRenderer`
@see `feathers.controls.dataRenderers.LayoutGroupItemRenderer`
@since 1.0.0
**/
public var itemRendererRecycler(get, set):AbstractDisplayObjectRecycler<Dynamic, TreeViewItemState, DisplayObject>;
private function get_itemRendererRecycler():AbstractDisplayObjectRecycler<Dynamic, TreeViewItemState, DisplayObject> {
return this._defaultStorage.itemRendererRecycler;
}
private function set_itemRendererRecycler(value:AbstractDisplayObjectRecycler<Dynamic, TreeViewItemState,
DisplayObject>):AbstractDisplayObjectRecycler<Dynamic, TreeViewItemState, DisplayObject> {
if (this._defaultStorage.itemRendererRecycler == value) {
return this._defaultStorage.itemRendererRecycler;
}
this._defaultStorage.oldItemRendererRecycler = this._defaultStorage.itemRendererRecycler;
this._defaultStorage.itemRendererRecycler = value;
this._defaultStorage.measurements = null;
this.setInvalid(INVALIDATION_FLAG_ITEM_RENDERER_FACTORY);
return this._defaultStorage.itemRendererRecycler;
}
private var _forceItemStateUpdate:Bool = false;
/**
Forces the `itemRendererRecycler.update()` method to be called with the
`TreeViewItemState` when the tree view validates, even if the item's
state has not changed since the previous validation.
Before Feathers UI 1.2, `update()` was called more frequently, and this
property is provided to enable backwards compatibility, temporarily, to
assist in migration from earlier versions of Feathers UI.
In general, when this property needs to be enabled, its often because of
a missed call to `dataProvider.updateAt()` (preferred) or
`dataProvider.updateAll()` (less common).
The `forceItemStateUpdate` property may be removed in a future major
version, so it is best to avoid relying on it as a long-term solution.
@since 1.2.0
**/
public var forceItemStateUpdate(get, set):Bool;
private function get_forceItemStateUpdate():Bool {
return this._forceItemStateUpdate;
}
private function set_forceItemStateUpdate(value:Bool):Bool {
if (this._forceItemStateUpdate == value) {
return this._forceItemStateUpdate;
}
this._forceItemStateUpdate = value;
this.setInvalid(DATA);
return this._forceItemStateUpdate;
}
private var _recyclerMap:Map<String, DisplayObjectRecycler<Dynamic, TreeViewItemState, DisplayObject>> = null;
private var _itemRendererRecyclerIDFunction:(state:TreeViewItemState) -> String;
/**
When a tree view requires multiple item renderer types, this function is
used to determine which type of item renderer is required for a specific
item. Returns the ID of the item renderer recycler to use for the item,
or `null` if the default `itemRendererRecycler` should be used.
The following example provides an `itemRendererRecyclerIDFunction`:
```haxe
var regularItemRecycler = DisplayObjectRecycler.withClass(HierarchicalItemRenderer);
var firstItemRecycler = DisplayObjectRecycler.withClass(MyCustomItemRenderer);
treeView.setItemRendererRecycler("regular-item", regularItemRecycler);
treeView.setItemRendererRecycler("first-item", firstItemRecycler);
treeView.itemRendererRecyclerIDFunction = function(state:TreeViewItemState):String {
if(state.location.length == 1 && state.location[0] == 0) {
return "first-item";
}
return "regular-item";
};
```
@default null
@see `TreeView.setItemRendererRecycler()`
@see `TreeView.itemRendererRecycler
@since 1.0.0
**/
public var itemRendererRecyclerIDFunction(get, set):(state:TreeViewItemState) -> String;
private function get_itemRendererRecyclerIDFunction():(state:TreeViewItemState) -> String {
return this._itemRendererRecyclerIDFunction;
}
private function set_itemRendererRecyclerIDFunction(value:(state:TreeViewItemState) -> String):(state:TreeViewItemState) -> String {
if (this._itemRendererRecyclerIDFunction == value) {
return this._itemRendererRecyclerIDFunction;
}
this._itemRendererRecyclerIDFunction = value;
this.setInvalid(INVALIDATION_FLAG_ITEM_RENDERER_FACTORY);
return this._itemRendererRecyclerIDFunction;
}
private var _defaultStorage = new ItemRendererStorage(null, DisplayObjectRecycler.withClass(HierarchicalItemRenderer));
private var _additionalStorage:Array<ItemRendererStorage> = null;
private var objectDataToItemRenderer = new ObjectMap<Dynamic, DisplayObject>();
private var stringDataToItemRenderer = new StringMap<DisplayObject>();
private var itemRendererToItemState = new ObjectMap<DisplayObject, TreeViewItemState>();
private var itemStatePool = new ObjectPool(() -> new TreeViewItemState());
private var _unrenderedLocations:Array<Array<Int>> = [];
private var _unrenderedLayoutIndices:Array<Int> = [];
private var _virtualCache:Array<Dynamic> = [];
private var _visibleIndices:VirtualLayoutRange = new VirtualLayoutRange(0, 0);
private var _tempVisibleIndices:VirtualLayoutRange = new VirtualLayoutRange(0, 0);
private var _layoutItems:Array<DisplayObject> = [];
private var _totalLayoutCount:Int = 0;
private var _typicalItemRenderer:DisplayObject;
private var _selectable:Bool = true;
/**
Determines if items in the tree view may be selected. By default only a
single item may be selected at any given time. In other words, if item
_A_ is already selected, and the user selects item _B_, item _A_ will be
deselected automatically.
The following example disables selection of items in the tree view:
```haxe
treeView.selectable = false;
```
@default true
@see `TreeView.selectedItem`
@see `TreeView.selectedIndex`
@since 1.0.0
**/
public var selectable(get, set):Bool;
private function get_selectable():Bool {
return this._selectable;
}
private function set_selectable(value:Bool):Bool {
if (this._selectable == value) {
return this._selectable;
}
this._selectable = value;
if (!this._selectable) {
// use the setter
this.selectedLocation = null;
}
return this._selectable;
}
private var _virtualLayout:Bool = true;
/**
Indicates if the tree view's layout is allowed to virtualize items or
not.
The following example disables virtual layouts:
```haxe
treeView.virtualLayout = false;
```
@since 1.0.0
**/
public var virtualLayout(get, set):Bool;
private function get_virtualLayout():Bool {
return this._virtualLayout;
}
private function set_virtualLayout(value:Bool):Bool {
if (this._virtualLayout = value) {
return this._virtualLayout;
}
this._virtualLayout = value;
this.setInvalid(LAYOUT);
return this._virtualLayout;
}
/**
Indicates if selection is changed with `MouseEvent.CLICK` or
`TouchEvent.TOUCH_TAP` when the item renderer does not implement the
`IToggle` interface. If set to `false`, all item renderers must control
their own selection manually (not only ones that implement `IToggle`).
The following example disables pointer selection:
```haxe
treeView.pointerSelectionEnabled = false;
```
@since 1.0.0
**/
public var pointerSelectionEnabled:Bool = true;
private var _ignoreSelectionChange = false;
private var _ignoreOpenedChange = false;
private var _ignoreLayoutChanges = false;
private var _currentDisplayIndex:Int;
private var _pendingScrollLocation:Array<Int> = null;
private var _pendingScrollDuration:Null<Float> = null;
private var _itemToText:(Dynamic) -> String = defaultItemToText;
/**
Converts an item to text to display within tree view. By default, the
`toString()` method is called to convert an item to text. This method
may be replaced to provide custom text.
For example, consider the following item:
```haxe
{ text: "Example Item" }
```
If the `TreeView` should display the text "Example Item", a custom
implementation of `itemToText()` might look like this:
```haxe
treeView.itemToText = (item:Dynamic) -> {
return item.text;
};
```
@since 1.0.0
**/
public var itemToText(get, set):(Dynamic) -> String;
private function get_itemToText():(Dynamic) -> String {
return this._itemToText;
}
private function set_itemToText(value:(Dynamic) -> String):(Dynamic) -> String {
if (value == null) {
value = defaultItemToText;
}