Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 | 1x 1x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 79x 103x 79x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 2214x 2214x 103x 97x 103x 103x 103x 103x 1349x 1349x 1953x 103x 103x 103x 35744x 184x 131x 80x 131x 90x 131x 79x 79x 131x 79x 79x 131x 81x 10x 131x 80x 5x 131x 80x 80x 4x 80x 131x 107x 131x 85x 103x 97x 97x 95x 97x 23x 97x 25x 97x 5x 97x 3x 97x 15x 97x 2x 1x 1x 1x 97x 1x 97x 123x 123x 123x 97x 97x 97x 97x 97x 97x 361x 99x 99x 97x 2x 99x 95x 95x 94x 94x 94x 94x 361x 361x 361x 358x 3x 361x 4x 4x 4x 4x 4x 4x 103x 968x 968x 103x 262x 262x 98x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 101x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 15x 6x 6x 2x 4x 1x 1x 1x 4x 103x 2x 2x 2x 5x 5x 5x 5x 4x 3x 3x 1x 3x 4x 1x 1x 1x 1x 3x 7x 3x 5x 4x 1x 5x 1x 1x 410x 410x 377x 33x 47x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 16x 23x 23x 1x 22x 22x 7x 7x 7x 1x 7x 7x 6x 1x 1x 1x 15x 2x 2x 2x 2x 2x 20x 2x 2x 2x 2x 1x 13x 13x 41x 13x 13x 22x 95x 22x 21x 22x 22x 13x 22x 22x 15x 15x 15x 187x 183x 187x 16x 16x 4x 2x 4x 12x 3x 9x 4x 5x 5x 16x 16x 3x 171x 171x 187x 42x 39x 3x 103x 25x 25x 25x 25x 25x 21x 4x 3x 3x 48x 25x 23x 23x 23x 23x 5x 5x 3x 1x 42x 3x 3x 3x 3x 6x 6x 6x 6x 4x 4x 4x 188x 12x 12x 12x 12x 12x 12x 12x 12x 15x 15x 15x 15x 410x 410x 410x 380x 30x 410x 10x 670x 354x 128x 128x 125x 125x 542x 542x 124x 97x 1x 1x 1x 1x 97x 97x 97x 97x 24x 25x 25x 25x 2x 23x 23x 1x 85x 2x 83x 107x 107x 107x 107x 107x 107x 2099x 107x 107x 85x 85x 85x 79x 85x 33x 52x 106x 52x 52x 7x 7x 45x 42x 42x 42x 42x 41x 42x 42x 42x 42x 42x 3x 3x 8x 8x 8x 3x 3x 3x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ /* * WARNING: Parts of this component are made to be used in conjunction with Bootstrap. * Bootstrap is not included with this package and has to be installed separately. */ import { FocusKeyManager } from '@angular/cdk/a11y'; import { CdkScrollableModule } from '@angular/cdk/scrolling'; import { NgClass } from '@angular/common'; import { AfterViewChecked, AfterViewInit, booleanAttribute, ChangeDetectorRef, Component, computed, contentChild, contentChildren, ElementRef, HostListener, inject, INJECTOR, input, OnChanges, OnDestroy, OnInit, output, signal, Signal, SimpleChanges, TemplateRef, viewChild, viewChildren } from '@angular/core'; import { MenuItem as MenuItemLegacy } from '@siemens/element-ng/common'; import { MenuItem } from '@siemens/element-ng/menu'; import { ElementDimensions, ResizeObserverService } from '@siemens/element-ng/resize-observer'; import { SiTranslatePipe, t, TranslatableString } from '@siemens/element-translate-ng/translate'; import { asyncScheduler, defer, fromEvent, merge, Observable, Subject, Subscription } from 'rxjs'; import { map, withLatestFrom } from 'rxjs/operators'; import { SiTreeViewConverterService } from './si-tree-view-converter.service'; import { SiTreeViewItemHeightService } from './si-tree-view-item-height.service'; import { SiTreeViewItemTemplateDirective } from './si-tree-view-item-template.directive'; import { SiTreeViewItemComponent } from './si-tree-view-item/si-tree-view-item.component'; import { SiTreeViewItemDirective } from './si-tree-view-item/si-tree-view-item.directive'; import { SiTreeViewVirtualizationService } from './si-tree-view-virtualization.service'; import { CheckboxClickEventArgs, ClickEventArgs, DEFAULT_CHILDREN_INDENTATION, DEFAULT_TREE_ICON_SET, FolderStateEventArgs, ItemsVirtualizedArgs, LoadChildrenEventArgs, MenuItemsProvider, TreeItem, TreeViewIconSet } from './si-tree-view.model'; import { SiTreeViewService } from './si-tree-view.service'; import { addChildItems, childrenLoaded, collapse, enableCheckboxRecursive, enableOptionboxRecursive, expand, hasChildren, initializeTreeItemsRecursive, removeUndefinedState, resetActive, ROOT_LEVEL, selectItemsBetween, selectRecursive, setActive, setTreeItemDefaults } from './si-tree-view.utils'; const rootDefaults: TreeItem = { label: 'root', level: ROOT_LEVEL, state: 'expanded', selectable: false }; /** * Implements a tree view with the following main capabilities: * Showing multiple trees. The data of the trees is set via the input property 'items'. * Showing the trees in a flat mode with navigation/breadcrumb on top. * A tree/list item is composed of a 'folder' icon, any item icon, the label, two additional datafields and a context menu button. * Children of parent nodes are lazy loaded upon request (in case they are not yet available in model.) * Context menu support via context menu item: the context menu items can be set via input properties. * Multi selection and focus support. * Checkboxes and / or option boxes per tree node. * Virtualization support: Input properties allow to set the page size and the number of pages to be virtualized. * Current limitation is roughly half a million of nodes. This is due to some 'strange' behavior with flex containers. * Set the input properties 'pageSize' and 'pagesVirtualized' to a reasonable amount of virtualized items. * The number of virtualized items is the product of the pageSize and the pagesVirtualized (number of pages virtualized). * Choose a value in the area of 50 virtualized items, dependent on the screen size. Check at runtime if appropriate! * Grouped List support: The component does also support grouped lists. * See the test client implementation of this package for all functionality. */ @Component({ selector: 'si-tree-view', imports: [ NgClass, SiTranslatePipe, SiTreeViewItemComponent, CdkScrollableModule, SiTreeViewItemDirective ], templateUrl: './si-tree-view.component.html', styleUrl: './si-tree-view.component.scss', providers: [ SiTreeViewConverterService, SiTreeViewItemHeightService, SiTreeViewService, SiTreeViewVirtualizationService ] }) export class SiTreeViewComponent implements OnInit, OnChanges, OnDestroy, AfterViewInit, AfterViewChecked { /** * The context menu items which are bound to the context menu of all tree items, * or a menu item provider function that is invoked for each tree item once. * * @defaultValue [] */ readonly contextMenuItems = input<(MenuItemLegacy | MenuItem)[] | MenuItemsProvider>([]); /** * The indentation in pixel for the children in respect to its parent. * * @defaultValue DEFAULT_CHILDREN_INDENTATION */ readonly childrenIndentation = input<number>(DEFAULT_CHILDREN_INDENTATION); /** * Enable horizontal scrolling. When disabled (the default), an ellipsis is used for overflowing text * * @defaultValue false */ readonly horizontalScrolling = input(false, { transform: booleanAttribute }); /** * Enable the compact mode, making it more vertically compact. * * @defaultValue false */ readonly compactMode = input(false, { transform: booleanAttribute }); /** * Enable buttons for collapse and expand all. * Does not work when `flatTree` is enabled. * * @defaultValue false */ readonly expandCollapseAll = input(false, { transform: booleanAttribute }); /** * Tooltip text shown for the expand all icon. * * @defaultValue * ``` * t(() => $localize`:@@SI_TREE_VIEW.EXPAND_ALL:Expand all`) * ``` */ readonly expandAllTooltip = input(t(() => $localize`:@@SI_TREE_VIEW.EXPAND_ALL:Expand all`)); /** * Tooltip text shown for the collapse all icon. * * @defaultValue * ``` * t(() => $localize`:@@SI_TREE_VIEW.COLLAPSE_ALL:Collapse all`) * ``` */ readonly collapseAllTooltip = input( t(() => $localize`:@@SI_TREE_VIEW.COLLAPSE_ALL:Collapse all`) ); /** * Customize icons for treeview. */ readonly icons = input<TreeViewIconSet>(); /** * Number of rows per page. Used for the virtualization of rows (number of * rows per page). * @defaultValue 10 * @defaultref {@link SiTreeViewVirtualizationService#pageSize} */ readonly pageSize = input(10, { transform: (value: number) => (value < 5 ? 5 : value) }); /** * Number of pages which are virtualized. * @defaultValue 6 * @defaultref {@link SiTreeViewVirtualizationService#pagesVirtualized} */ readonly pagesVirtualized = input(6, { transform: (value: number) => (value < 5 ? 5 : value) }); /** * Sets if the folder state icon shall be shown on the left (in LTR) or on the right (in LTR) side * of the tree item. Per default the icon will be shown on the left (in LTR). Has no * effect if flatTree is enabled. * @defaultValue true */ readonly folderStateStart = input(true, { transform: booleanAttribute }); /** * Sets if the tree list shall virtualize the tree items. * This input field must be set at startup and shall not be changed afterwards. * @defaultValue true */ readonly isVirtualized = input(true, { transform: booleanAttribute }); /** * Sets the root tree items of all the trees (Required). * @defaultValue [] * @defaultref {@link _items} */ readonly items = input<TreeItem[]>([]); /** * Sets the tree item to be selected. */ readonly selectedItem = input<TreeItem | TreeItem[] | undefined>(); /** * Sets if the tree shall force single tree item selection. * @defaultValue false */ readonly singleSelectMode = input(false, { transform: booleanAttribute }); /** * Shows or hides additional information below the label. * @defaultValue false */ readonly enableDataField1 = input(false, { transform: booleanAttribute }); /** * Shows or hides additional information below the label. * @defaultValue false * @defaultref {@link SiTreeViewService#enableDataField2} */ readonly enableDataField2 = input(false, { transform: booleanAttribute }); /** * Shows or hides state pipe. * @defaultValue true */ readonly enableStateIndicator = input(true, { transform: booleanAttribute }); /** * Shows or hides icon * @defaultValue true */ readonly enableIcon = input(true, { transform: booleanAttribute }); /** * Shows or hides context menu button and also controls context menu visibility on right click. * @defaultValue true * @defaultref {@link SiTreeViewService#enableContextMenuButton} */ readonly enableContextMenuButton = input(true, { transform: booleanAttribute }); /** * Allows / disabled selecting of tree items by clicking on them. * @defaultValue false */ readonly enableSelection = input(false, { transform: booleanAttribute }); /** * Sets if children are deleted upon collapsing tree items. This feature might * be used, if children shall be lazy loaded always upon expanding a tree * item. * @defaultValue false */ readonly deleteChildrenOnCollapse = input(false, { transform: booleanAttribute }); /** * Sets if the tree view shall be displayed as flat tree list with a * breadcrumb. A flat tree only shows the one level at the time and lists the * tree items of the current level as a list. * @defaultValue false * @defaultref {@link SiTreeViewService#flatTree} */ readonly flatTree = input(false); /** * Sets if the tree view is displayed as a grouped list. * Important: In this mode, only the first two hierarchies of the tree model * are considered! * @defaultValue false * @defaultref {@link SiTreeViewService#groupedList} */ readonly groupedList = input(false, { transform: booleanAttribute }); /** * Sets if the tree items shall show a checkbox. * @defaultValue false * @defaultref {@link SiTreeViewService#enableCheckbox} */ readonly enableCheckbox = input(false, { transform: booleanAttribute }); /** * Sets if the tree items shall show an optionbox. * @defaultValue false */ readonly enableOptionbox = input(false, { transform: booleanAttribute }); /** * Sets if the tree items should expand/collapse when clicking on them. * Does not work when `flatTree` is enabled or the tree item is not selectable. * @defaultValue false */ readonly expandOnClick = input(false, { transform: booleanAttribute }); /** * Sets if the checkbox state of a tree item is inherited to its children/parent. * @defaultValue true */ readonly inheritChecked = input(true, { transform: booleanAttribute }); /** * String to be shown when there are no content actions. * * @defaultValue 'No actions available' */ readonly noActionsString = input('No actions available'); /** Aria label for the tree view. Needed for a11y, alternatively use {@link ariaLabelledBy}. */ readonly ariaLabel = input<TranslatableString>(); /** Aria labelled by for the tree view. Needed for a11y, alternatively use {@link ariaLabel}. */ readonly ariaLabelledBy = input<string>(); /** * Triggered upon virtualization (or unvirtualization) of a tree item. */ readonly itemsVirtualizedChanged = output<ItemsVirtualizedArgs>(); /** * Triggered upon clicking the folder of a tree item. */ readonly treeItemFolderClicked = output<FolderStateEventArgs>(); /** * Triggered upon a state change of the folder of a tree item. */ readonly treeItemFolderStateChanged = output<FolderStateEventArgs>(); /** * Triggered upon clicking the label of a tree item. */ readonly treeItemClicked = output<TreeItem>(); /** * Triggered upon clicking the checkbox of a tree item. */ readonly treeItemCheckboxClicked = output<CheckboxClickEventArgs>(); /** * Triggered upon the request of loading the children of a tree item. */ readonly loadChildren = output<LoadChildrenEventArgs>(); /** * Triggered upon the selection of a tree item (multi selection supported). */ readonly treeItemsSelected = output<TreeItem[]>(); /** * The injected content with included SiTreeViewItemTemplateDirective. */ readonly templates = contentChildren(SiTreeViewItemTemplateDirective); private initialized = false; private updateTreeItemHeightRequired = true; // must be set to true so the height is initially calculated once at least private updateGroupItemHeightRequired = true; // must be set to true so the height is initially calculated once at least private selectedTreeItems: TreeItem[] = []; private manuallySelectedTreeItems = false; private latestFolderChanged?: TreeItem; private breadCrumbTreeItems: TreeItem[] = []; private subscriptions: Subscription[] = []; private multiSelectionStart!: TreeItem; private _multiSelectionActive = false; private domChangeObserver?: MutationObserver; private scroll$!: Observable<Event>; private element = inject(ElementRef); private siTreeViewService = inject(SiTreeViewService); private siTreeViewConverterService = inject(SiTreeViewConverterService); private siTreeViewVirtualizationService = inject(SiTreeViewVirtualizationService); private siTreeViewItemHeightService = inject(SiTreeViewItemHeightService); private cdRef = inject(ChangeDetectorRef); private resizeObserver = inject(ResizeObserverService); private injector = inject(INJECTOR); /** * Create a virtual root node so there is just a single root node. This makes sure the tree * can be fully traversed starting from any node. This is needed e.g. for recursively * removing the "active" state from other nodes. */ private virtualRoot: TreeItem = { ...rootDefaults }; private readonly _items = signal(this.items()); private updateTreeItemDefaults(item: TreeItem): void { item.showCheckbox ??= this.enableCheckbox(); item.showOptionbox ??= this.enableOptionbox(); } /** * @internal */ readonly computedIcons = computed(() => { return { ...DEFAULT_TREE_ICON_SET, ...this.icons() }; }); /** @internal */ scrollChildIntoView: Subject<TreeItem> = new Subject<TreeItem>(); /** @internal */ childrenLoaded: Subject<TreeItem> = new Subject<TreeItem>(); /** * @internal * Used internally by `SiTreeViewItemDirective` to determine if the items have changed, * and render the new items. */ evaluateForTreeItemsDiffer = new Subject<void>(); /** @internal */ hasAnyChildren = true; /** * Gets the height of the div above the virtualized rows. */ protected get heightBefore(): string { return this.siTreeViewVirtualizationService.heightBefore + 'px'; } /** * Gets the height of the div below the virtualized rows. */ protected get heightAfter(): string { return this.siTreeViewVirtualizationService.heightAfter + 'px'; } /** * @internal * This is meant to be used for internal purpose. * Returns the virtualized items in the tree list uses virtualization, otherwise all items are returned as a flat list. * Bind these to the HTML. */ get itemsVirtualized(): TreeItem[] { return this.isVirtualized() ? this.siTreeViewVirtualizationService.itemsVirtualized : this.siTreeViewConverterService.flattenedTrees; } protected readonly children = viewChildren(SiTreeViewItemComponent); protected readonly treeItemContentTemplate = contentChild(SiTreeViewItemDirective, { read: TemplateRef }); private readonly nextItems = contentChildren(SiTreeViewItemComponent, { descendants: true }); private keyManager!: FocusKeyManager<SiTreeViewItemComponent>; /** * @internal * Returns index of the currently active Tree Item element in DOM * or null if there is no active tree item element */ get activeIndex(): number | null { // This method is called before afterViewInit. Everywhere else keymanager can be expected to exist. return this.keyManager?.activeItemIndex; } /** Can be used for [cdkDropListData] when using cdk drag and drop. */ get dropListItems(): TreeItem[] { return this.itemsVirtualized; } ngOnChanges(changes: SimpleChanges): void { // Compact mode change the CSS margins so it is necessary to // re-calc the tree item height if (changes.compactMode || changes.enableDataField1 || changes.enableDataField2) { this.updateTreeItemHeightRequired = true; } if (changes.flatTree) { this.handleTreeMode(); } if (changes.pageSize) { this.siTreeViewVirtualizationService.pageSize = this.pageSize(); this.resetVirtualizedItemList(); } if (changes.pagesVirtualized) { this.siTreeViewVirtualizationService.pagesVirtualized = this.pagesVirtualized(); this.resetVirtualizedItemList(); } if (changes.enableCheckbox) { for (const element of this._items()) { enableCheckboxRecursive(element, this.enableCheckbox()); } } if (changes.enableOptionbox) { for (const element of this._items()) { enableOptionboxRecursive(element, this.enableOptionbox()); } } if (changes.groupedList) { this.siTreeViewService.groupedList = this.groupedList(); if (this.groupedList()) { this.updateGroupItemHeightRequired = true; } this.fillFlattenedTree(this._items(), true); } if (changes.items) { this.treeItemsUpdated(); } if (changes.selectedItem) { this.selectTreeItems(this.selectedItem()); } } ngOnInit(): void { this.scroll$ = defer(() => fromEvent(this.treeViewInnerElement().nativeElement, 'scroll')); this.siTreeViewService.scroll$ = this.scroll$; if (this.isVirtualized()) { this.subscriptions.push(this.scroll$.subscribe(event => this.onScroll(event))); } this.subscriptions.push( this.siTreeViewService.clickEvent.subscribe(event => this.onItemClicked(event)) ); this.subscriptions.push( this.siTreeViewService.folderClickEvent.subscribe(event => this.onItemFolderClicked(event)) ); this.subscriptions.push( this.siTreeViewService.checkboxClickEvent.subscribe(event => this.onItemCheckboxClicked(event) ) ); this.subscriptions.push( this.siTreeViewService.loadChildrenEvent.subscribe(event => this.onLoadChildren(event)) ); this.subscriptions.push( this.siTreeViewService.scrollIntoViewEvent.subscribe(event => this.onScrollIntoView(event)) ); this.subscriptions.push( this.siTreeViewService.focusParentEvent.subscribe(event => { if (!this.flatTree()) { this.focusParentItem(event); return; } this.onFlatTreeNavigateUp(); }) ); this.subscriptions.push( this.siTreeViewService.focusFirstChildEvent.subscribe(event => this.focusFirstChildItem(event) ) ); this.subscriptions.push( this.siTreeViewVirtualizationService.itemsVirtualizedChanged.subscribe( (event: ItemsVirtualizedArgs) => { this.itemsVirtualizedChanged.emit(event); this.evaluateForTreeItemsDiffer.next(); this.cdRef.markForCheck(); } ) ); this.initialized = true; this.handleTreeMode(); } ngAfterViewInit(): void { this.addClassObserver(); this.subscriptions.push(this.monitorTreeSizeChanges().subscribe(d => this.updatePageSize(d))); this.keyManager = new FocusKeyManager(this.getChildren(), this.injector) .withWrap() .withAllowedModifierKeys(['shiftKey']) .withHomeAndEnd() .withTypeAhead(); queueMicrotask(() => this.keyManager.updateActiveItem(0)); } ngAfterViewChecked(): void { if ( this.updateTreeItemHeightRequired && this.siTreeViewConverterService.flattenedTrees.length > 0 ) { const oldHeight = this.siTreeViewItemHeightService.itemHeight; let newHeight: number | undefined; if (this.isVirtualized()) { newHeight = this.siTreeViewItemHeightService.updateItemHeight( this.treeViewInnerElement().nativeElement, this.siTreeViewConverterService.flattenedTrees, this.siTreeViewVirtualizationService.itemBaseIdx, this.siTreeViewVirtualizationService.itemsVirtualizedCount ); } else { newHeight = this.siTreeViewItemHeightService.updateItemHeight( this.treeViewInnerElement().nativeElement, this.siTreeViewConverterService.flattenedTrees, 0, this.siTreeViewConverterService.flattenedTrees.length ); } if (newHeight !== undefined && newHeight > 0) { this.updateTreeItemHeightRequired = false; if (oldHeight !== newHeight) { const task: () => void = () => { this.updateVirtualizedItemList(); this.cdRef.markForCheck(); }; asyncScheduler.schedule(task, 0); } } } if ( this.updateGroupItemHeightRequired && this.siTreeViewConverterService.flattenedTrees.length > 0 ) { const oldHeight = this.siTreeViewItemHeightService.groupItemHeight; let newHeight: number | undefined; if (this.isVirtualized()) { newHeight = this.siTreeViewItemHeightService.updateGroupedItemHeight( this.treeViewInnerElement().nativeElement, this.siTreeViewConverterService.flattenedTrees, this.siTreeViewVirtualizationService.itemBaseIdx, this.siTreeViewVirtualizationService.itemsVirtualizedCount ); } else { newHeight = this.siTreeViewItemHeightService.updateGroupedItemHeight( this.treeViewInnerElement().nativeElement, this.siTreeViewConverterService.flattenedTrees, 0, this.siTreeViewConverterService.flattenedTrees.length ); } if (newHeight !== undefined) { this.updateGroupItemHeightRequired = false; if (oldHeight !== newHeight) { const task: () => void = () => { this.updateVirtualizedItemList(); this.cdRef.markForCheck(); }; asyncScheduler.schedule(task, 0); } } } } ngOnDestroy(): void { this.subscriptions .filter(subscription => !!subscription) .forEach(subscription => subscription.unsubscribe()); this.domChangeObserver?.disconnect(); } /** * Returns the last breadcrumb tree item. Can be null in case the flat tree * shows the root. */ protected get lastBreadCrumbItem(): TreeItem | null { const item = this.breadCrumbTreeItems[this.breadCrumbTreeItems.length - 1]; return item ? item : null; } /** * Returns true if the header shows the root/home of the tree. */ protected get headerShowsRoot(): boolean { return this.lastBreadCrumbItem == null; } /** * Called by the owner of the tree upon returning the retrieved children of a * 'load children' request. */ private childrenLoadingDone(item: TreeItem, children: TreeItem[]): void { const oldState = item.state ?? 'collapsed'; if (item?.state === 'expanding') { initializeTreeItemsRecursive(children, item, itemToUpdate => this.updateTreeItemDefaults(itemToUpdate) ); item.children = children; item.state = 'expanded'; } const newState = item.state ?? 'collapsed'; this.updateBreadCrumb(this.evalLowestBreadCrumbNode()); Iif (this.flatTree()) { this.fillFlattenedTree(children); this.focusFirstItemFlattened(); } else { this.fillFlattenedTree(this._items()); } if (oldState !== newState) { this.treeItemFolderStateChanged.emit(new FolderStateEventArgs(item, oldState, newState)); } this.childrenLoaded.next(item); this.cdRef.markForCheck(); } protected onFlatTreeNavigateUp(): void { if (this.lastBreadCrumbItem) { const last = this.lastBreadCrumbItem; this.onBreadCrumbItemClicked(last.parent); setTimeout(() => this.focusItem(last)); } } protected onFlatTreeNavigateHome(): void { this.onBreadCrumbItemClicked(); this.focusFirstItemFlattened(); } private getChildren(): Signal<readonly SiTreeViewItemComponent[]> { return !this.treeItemContentTemplate() ? this.children : this.nextItems; } private focusItem(item: TreeItem): void { const children = this.getChildren(); const component = children()?.find(child => child.treeItem === item); if (component) { this.keyManager.setActiveItem(component); } } private focusParentItem(item: TreeItem): void { const parent = item.parent; if (parent && parent.level !== ROOT_LEVEL) { this.focusItem(parent); } } private focusFirstChildItem(item: TreeItem): void { const firstChild = item.children?.[0]; if (firstChild) { const children = this.getChildren(); const firstChildComponent = children()?.find(child => child.treeItem === firstChild); if (firstChildComponent) { this.keyManager.setActiveItem(firstChildComponent); } } } /** * Updates the tree to visualize the provided tree item by expanding all its parent items, collapsing all * other first level tree items and scroll to the provided item. * @param item - The tree item that shall be visible. */ showTreeItem(item: TreeItem): void { this.doShowTreeItem(item); const task: () => void = () => { this.scrollChildIntoView.next(item); this.cdRef.markForCheck(); }; asyncScheduler.schedule(task, 0); } private doShowTreeItem(item: TreeItem, collapseOtherNodes = true): void { if (item.parent && item.parent.level !== ROOT_LEVEL) { this.expandTreeItem(item.parent); if (item.parent.parent && item.parent.parent.level !== ROOT_LEVEL) { this.doShowTreeItem(item.parent, collapseOtherNodes); } else if (collapseOtherNodes) { const myRoot = item.parent; for (const root of this._items()) { Iif (myRoot !== root) { this.collapseTreeItem(root); } } } } } /** * Expands all tree items. * @param items - Optional param for recursion, will expand these items. If empty, all tree items will be expanded. */ expandAll(items?: TreeItem[]): void { (items ?? this._items()).forEach(item => { if (hasChildren(item) && childrenLoaded(item)) { if (item.state === 'collapsed') { this.expandTreeItem(item); } this.expandAll(item.children); } }); } /** * Expands the provided tree item. * @param item - The tree item to be expanded. */ expandTreeItem(item: TreeItem): void { const oldState = removeUndefinedState(item.state); expand(item); this.latestFolderChanged = item; this.siTreeViewService.folderClickEvent.next( new FolderStateEventArgs(item, oldState, removeUndefinedState(item.state)) ); } /** * Collapses all tree items. * @param items - Optional param for recursion, will collapse these items. If empty, all tree items will be collapsed. */ collapseAll(items?: TreeItem[]): void { (items ?? this._items()).forEach(item => { if (hasChildren(item)) { if (item.state === 'expanded' || item.state === 'expanding') { this.collapseTreeItem(item); } this.collapseAll(item.children); } }); this.resetVirtualizedItemList(); } /** * Collapses the provided tree item. * @param item - The tree item to be collapsed. */ collapseTreeItem(item: TreeItem): void { const oldState = item.state ?? 'collapsed'; collapse(item); this.latestFolderChanged = item; this.siTreeViewService.folderClickEvent.next( new FolderStateEventArgs(item, oldState, removeUndefinedState(item.state)) ); } /** * Add tree items as root elements or as children of an existing tree item. * @param children - Tree items to add. * @param parent - Optional parent tree item to which the items shall be added. * @param position - Optional position where to insert the tree items among their siblings. */ addChildItems(children: TreeItem[], parent?: TreeItem, position?: number): void { addChildItems(parent ?? this.virtualRoot, children, position, item => this.updateTreeItemDefaults(item) ); this.refresh(); } /** * Forces a refresh of the view considering the current tree item model. Needs * to be called when tree items have been added, removed, or updated via model * in ways that do not trigger an automatic update of the view. */ refresh(): void { if (!this.flatTree()) { this.fillFlattenedTree(this._items()); } else { this.fillFlattenedTree(this.evalListItemsForFlatTreeMode()); } this.cdRef.markForCheck(); } /** * Scrolls the specified tree item into view. Tree items will be expanded if * required. */ scrollItemIntoView(treeItem: TreeItem): void { this.onScrollIntoViewByConsumer(treeItem); } private evalListItemsForFlatTreeMode(): TreeItem[] { return this.lastBreadCrumbItem ? (this.lastBreadCrumbItem.children ?? []) : this._items(); } private collapseChildrenFlatTreeModeOnly(items: TreeItem[]): void { this.collapseChildren(items, true); } private collapseChildren(items: TreeItem[], flatModeOnly: boolean = false): void { if (flatModeOnly && !this.flatTree()) { return; } for (const item of items) { if (item.state === 'expanded' || item.state === 'expanding') { const oldState = item.state; collapse(item); this.latestFolderChanged = item; this.treeItemFolderStateChanged.emit(new FolderStateEventArgs(item, oldState, item.state)); } } } @HostListener('document:keyup.shift') protected onKeyUpShift(): void { if (this._multiSelectionActive) { this._multiSelectionActive = false; this.emitSelectedItems(); } } @HostListener('document:keyup.control') protected onKeyUpCtrl(): void { if (this._multiSelectionActive) { this._multiSelectionActive = false; this.emitSelectedItems(); } } @HostListener('document:keyup.meta') protected onKeyUpMeta(): void { Iif (this._multiSelectionActive) { this._multiSelectionActive = false; this.emitSelectedItems(); } } @HostListener('document:mouseleave') protected onMouseLeave(): void { Iif (this._multiSelectionActive) { this._multiSelectionActive = false; this.emitSelectedItems(); } } @HostListener('keydown', ['$event']) protected handleKeydown(event: KeyboardEvent): void { this.keyManager.onKeydown(event); } private onItemClicked(event: ClickEventArgs): void { let skipFocus = false; if (!event.target.selectable) { return; } this.manuallySelectedTreeItems = true; if ( (event.mouseEvent.ctrlKey || (navigator.userAgent.includes('Mac') && event.mouseEvent.metaKey)) && !this.singleSelectMode() ) { this._multiSelectionActive = true; this.multiSelectionStart = event.target; if (event.target.selected) { skipFocus = true; } event.target.selected = !event.target.selected; if (event.target.selected && !this.singleSelectMode()) { this.selectedTreeItems.push(event.target); } else { const idx: number = this.selectedTreeItems.indexOf(event.target); if (idx !== -1) { this.selectedTreeItems.splice(idx, 1); } } } else if (event.mouseEvent.shiftKey && !this.singleSelectMode()) { this._multiSelectionActive = true; // If the anchor has not been established, set it to the first item in the selected // items list if defined ELSE the first item in the tree if (!this.multiSelectionStart) { this.multiSelectionStart = this.selectedTreeItems?.[0] ?? this._items()?.[0]; } // If the tree is in flat mode, ensure the anchor is inside the currently selected folder Iif (this.flatTree()) { const treeItems: TreeItem[] = (event.target.parent ? event.target.parent.children : this._items()) ?? []; Iif (treeItems.findIndex(ti => ti === this.multiSelectionStart) < 0) { // Move anchor to top-level item in current folder! this.multiSelectionStart = treeItems[0]; } } for (const element of this._items()) { selectRecursive(element, false); } this.multiSelectionStart.selected = true; const selectedItems = selectItemsBetween( this._items(), this.multiSelectionStart, event.target ); this.selectedTreeItems = [this.multiSelectionStart]; if (selectedItems) { this.selectedTreeItems.push(...selectedItems); } } else { this.multiSelectionStart = event.target; for (const element of this._items()) { selectRecursive(element, false); } event.target.selected = true; this.selectedTreeItems = [event.target]; } for (const element of this._items()) { resetActive(element); } if (!skipFocus) { setActive(event.target, true); } this.treeItemClicked.emit(event.target); if (!this._multiSelectionActive) { this.emitSelectedItems(); // Note, if multi selection would be active, we would emit the selected items only upon 'keyup' event for Shift and Control // and upon document.mouseleave event } this.cdRef.markForCheck(); this.siTreeViewService.triggerMarkForCheck.next(); } private emitSelectedItems(): void { if (this.enableSelection()) { Iif (this.siTreeViewService.groupedList) { const filtered: TreeItem[] = this.selectedTreeItems.filter( item => !this.siTreeViewService.isGroupedItem(item) ); this.treeItemsSelected.emit(filtered); } else { this.treeItemsSelected.emit(this.selectedTreeItems); } } } private handleTreeMode(): void { let ti: TreeItem | undefined; if (this.isVirtualized()) { ti = this.siTreeViewVirtualizationService.calculateFirstVisibleTreeItem(); } if (this.flatTree()) { this.siTreeViewService.childrenIndentation = 0; if (this.selectedTreeItems.length && this.manuallySelectedTreeItems) { // manually selected items => display the (children of) parent of the (highest or first) selected item, // if there is no parent display the root node const sorted = this.selectedTreeItems.sort( (itemA, itemB) => (itemA.level ?? 0) - (itemB.level ?? 0) ); this.fillFlattenedTree(sorted[0].parent?.children ?? this._items(), true); } else if (!this.latestFolderChanged) { // no folder state change of any tree item so far => display the root item in the list this.fillFlattenedTree(this._items(), true); } else if (this.latestFolderChanged.state === 'expanded') { this.fillFlattenedTree(this.latestFolderChanged.children ?? [], true); } else if (this.latestFolderChanged.parent) { this.fillFlattenedTree(this.latestFolderChanged.parent?.children ?? [], true); } else E{ this.fillFlattenedTree(this._items(), true); } this.updateBreadCrumb(this.evalLowestBreadCrumbNode()); if (ti && !this.siTreeViewConverterService.flattenedTrees.includes(ti, 0)) { ti = this.siTreeViewConverterService.flattenedTrees[0]; } } else { this.siTreeViewService.childrenIndentation = this.childrenIndentation(); this.fillFlattenedTree(this._items(), true); } this.onScrollIntoViewByConsumer(ti); } private evalLowestBreadCrumbNode(): TreeItem | undefined { if (this.latestFolderChanged) { return this.latestFolderChanged.state === 'expanded' ? this.latestFolderChanged : this.latestFolderChanged.parent; } return undefined; } protected readonly treeViewInnerElement = viewChild.required<ElementRef<HTMLDivElement>>('treeViewInner'); private onItemFolderClicked(eventArgs: FolderStateEventArgs): void { this.treeItemFolderClicked.emit(eventArgs); this.treeItemFolderStateChanged.emit(eventArgs); this.latestFolderChanged = eventArgs.treeItem; this.updateBreadCrumb(this.evalLowestBreadCrumbNode()); if (!this.flatTree()) { this.fillFlattenedTree(this._items()); } else if (this.latestFolderChanged.state === 'expanded') { this.fillFlattenedTree(this.latestFolderChanged.children ?? []); this.focusFirstItemFlattened(); } } private updateBreadCrumb(treeItem?: TreeItem): void { if (!treeItem || treeItem.level === ROOT_LEVEL) { this.breadCrumbTreeItems = []; } else { this.breadCrumbTreeItems = [treeItem]; this.pushParentItemsIntoBreadCrumbRecursive(treeItem); this.breadCrumbTreeItems = this.breadCrumbTreeItems.reverse(); } } private pushParentItemsIntoBreadCrumbRecursive(treeItem: TreeItem): void { Iif (treeItem?.parent && treeItem?.parent?.level !== ROOT_LEVEL) { this.breadCrumbTreeItems.push(treeItem.parent); this.pushParentItemsIntoBreadCrumbRecursive(treeItem.parent); } } private onItemCheckboxClicked(eventArgs: CheckboxClickEventArgs): void { this.siTreeViewService.triggerMarkForCheck.next(); this.treeItemCheckboxClicked.emit(eventArgs); } private onLoadChildren(treeItem: TreeItem): void { this.loadChildren.emit( new LoadChildrenEventArgs(treeItem, (item, children) => this.childrenLoadingDone(item, children) ) ); } private setFlatTreeSelectedFolder(treeItem?: TreeItem): void { if (this.flatTree()) { this.latestFolderChanged = treeItem; if (treeItem) { expand(treeItem); } this.onBreadCrumbItemClicked(treeItem); } } private onBreadCrumbItemClicked(treeItem?: TreeItem): void { Iif (treeItem && treeItem.level !== ROOT_LEVEL) { this.fillFlattenedTree(treeItem.children ?? [], true); this.focusFirstItemFlattened(); } else { this.fillFlattenedTree(this._items(), true); this.focusFirstItemFlattened(); } this.updateBreadCrumb(treeItem); } private onScroll(event: Event): void { if (this.isVirtualized()) { const scrollTop: number = (event.target as Element).scrollTop; this.siTreeViewVirtualizationService.handleScroll( scrollTop, this.siTreeViewConverterService.flattenedTrees ); } } /** * Called by the consumer when he wants a node to be scrolled into view. */ private onScrollIntoViewByConsumer(treeItem: TreeItem | undefined): void { if (treeItem) { if (this.isVirtualized()) { this.siTreeViewVirtualizationService.virtualizeItem( treeItem, this.siTreeViewConverterService.flattenedTrees ); } // wait one cycle until the new children (in case of page virtualization) are created const task: () => void = () => { this.doShowTreeItem(treeItem, false); // keep previously opened nodes as it is asyncScheduler.schedule(() => { this.scrollChildIntoView.next(treeItem); this.cdRef.markForCheck(); }, 0); }; asyncScheduler.schedule(task, 0); } } private onScrollIntoView(elementTreeItem: ElementRef): void { const clientRectNode = elementTreeItem.nativeElement.getBoundingClientRect(); const clientRect = this.element.nativeElement.getBoundingClientRect(); Iif ( clientRectNode.top < clientRect.top || clientRectNode.top + this.siTreeViewItemHeightService.itemHeight > clientRect.bottom ) { elementTreeItem.nativeElement.scrollIntoView(); } this.cdRef.markForCheck(); } private fillFlattenedTree(items: TreeItem[], resetVirtualization: boolean = false): void { this.collapseChildrenFlatTreeModeOnly(items); this.siTreeViewConverterService.fillFlattenedTree(items, this.flatTree()); if (resetVirtualization) { this.resetVirtualizedItemList(); } else { this.updateVirtualizedItemList(); } this.evaluateForTreeItemsDiffer.next(); } private focusFirstItemFlattened(): void { setTimeout(() => this.keyManager.setFirstItemActive()); } private updateHasChildren(): void { this.hasAnyChildren = !!this.siTreeViewConverterService.flattenedTrees?.some(item => hasChildren(item) ); } protected updateVirtualizedItemList(): void { this.updateHasChildren(); if (this.isVirtualized() && this.initialized) { this.siTreeViewVirtualizationService.updateVirtualizedItemList( this.siTreeViewConverterService.flattenedTrees ); this.cdRef.markForCheck(); } } private resetVirtualizedItemList(): void { this.updateHasChildren(); if (this.isVirtualized() && this.initialized) { this.siTreeViewVirtualizationService.resetVirtualizedItemList( this.siTreeViewConverterService.flattenedTrees ); } } /** * Track class changes since adding classes like `.tree-sm` or `.tree-xs` change the item height. * Changes have to trigger the SiTreeViewItemHeightService otherwise the virtualized items are * calculated incorrectly which cause rendering issues. */ private addClassObserver(): void { this.domChangeObserver = new MutationObserver((mutations: MutationRecord[]) => { mutations.forEach((mutation: MutationRecord) => { this.updateTreeItemHeightRequired = true; Iif (this.groupedList()) { this.updateGroupItemHeightRequired = true; } this.cdRef.markForCheck(); }); }); this.domChangeObserver.observe(this.element.nativeElement, { attributeFilter: ['class', 'style'] }); } /** * Create an observable to track visible dimensions and item height of the tree view. * It's necessary to dynamically increase the `pageSize` to render the correct amount * of visible tree items. * @returns observable with the current tree dimensions. */ private monitorTreeSizeChanges(): Observable<ElementDimensions> { const resize = this.resizeObserver.observe(this.element.nativeElement, 100, true, true); const merged = merge(resize, this.siTreeViewItemHeightService.itemHeightChange); return merged.pipe( withLatestFrom(resize), map(x => x[1]) ); } /** * Dynamically calculate and update the virtual pageSize to render the correct amount of tree items. * @param dimension - current tree dimensions. */ private updatePageSize(dimension: ElementDimensions): void { if (this.isVirtualized()) { // Re-calc number of virtualized items based on the visible component dimensions const itemHeight = this.siTreeViewItemHeightService.itemHeight; // Proceeding with itemHeight==0 leads to an error in // SiTreeViewVirtualizationService.updateListItemsTypeCount. It happens // when the component is created but not yet visible. if (!itemHeight) { return; } // Set page size to half the no. of items visible on the screen following // the recommendations in SiTreeViewVirtualizationService. const pageSize = Math.floor(dimension.height / itemHeight / 2); // Once the pageSize is initialized we are only allowed to increase // the number of virtualized items but do never reduce the size. // Since this would lead to a incorrect calculation of heightAfter // (see calculateBeforeAndAfter) if (pageSize > 0 && pageSize > this.siTreeViewVirtualizationService.pageSize) { this.siTreeViewVirtualizationService.pageSize = pageSize; } } } /** * Make sure to always have a single item for single selection or if there is only one item in the array. */ private coerceToSelectionType( treeItem: TreeItem[] | TreeItem | undefined ): TreeItem[] | TreeItem | undefined { if (Array.isArray(treeItem) && (treeItem.length <= 1 || this.singleSelectMode())) { // If array is empty, this will set it to undefined return treeItem[0]; } return treeItem; } private treeItemsUpdated(): void { this.selectedTreeItems = []; const items = this.items(); this._items.set(items); this.virtualRoot = { ...rootDefaults, children: items }; setTreeItemDefaults(this.virtualRoot, item => this.updateTreeItemDefaults(item)); initializeTreeItemsRecursive(items, this.virtualRoot, item => this.updateTreeItemDefaults(item) ); this.fillFlattenedTree(items, true); this.latestFolderChanged = items[0]; } private selectTreeItems(treeItem: TreeItem | TreeItem[] | undefined): void { const items = this.coerceToSelectionType(treeItem); const isMultipleTreeItems = Array.isArray(items); if ( (isMultipleTreeItems && items.length) || (!isMultipleTreeItems && items) || this.selectedTreeItems.length ) { this.manuallySelectedTreeItems = false; } if (!isMultipleTreeItems && items && !items.selectable) { return; } for (const element of this._items()) { selectRecursive(element, false); } this.selectedTreeItems = []; if (!items) { this.siTreeViewService.triggerMarkForCheck.next(); return; } // If this is single select or multi select with only one item if (!isMultipleTreeItems) { items.selected = true; this.selectedTreeItems.push(items); this.setFlatTreeSelectedFolder(items.parent); if (this.isVirtualized()) { this.siTreeViewVirtualizationService.virtualizeItem( items, this.siTreeViewConverterService.flattenedTrees ); } // wait one cycle until the new children (in case of page virtualization) are created const task: () => void = () => { this.scrollChildIntoView.next(items); this.cdRef.markForCheck(); this.siTreeViewService.triggerMarkForCheck.next(); }; asyncScheduler.schedule(task, 0); } else { // Multi select with multiple items const task: () => void = () => { for (const item of items) { Iif (item.selectable === false) { continue; } item.selected = true; this.selectedTreeItems.push(item); } this.cdRef.markForCheck(); this.siTreeViewService.triggerMarkForCheck.next(); }; asyncScheduler.schedule(task, 0); } } } |