@@ -320,15 +320,65 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
320
320
/**
321
321
* If the item is already expanded, we collapse the item. Otherwise, we will focus the parent.
322
322
*/
323
- private _collapseCurrentItem ( ) { }
323
+ private _collapseCurrentItem ( ) {
324
+ if ( ! this . _activeItem ) {
325
+ return ;
326
+ }
327
+
328
+ if ( ! this . _isCurrentItemExpanded ( ) ) {
329
+ this . _activeItem . collapse ( ) ;
330
+ } else {
331
+ const parent = this . _activeItem . getParent ( ) ;
332
+ if ( ! parent ) {
333
+ return ;
334
+ }
335
+ this . _setActiveItem ( parent as T ) ;
336
+ }
337
+ }
324
338
325
339
/**
326
340
* If the item is already collapsed, we expand the item. Otherwise, we will focus the first child.
327
341
*/
328
- private _expandCurrentItem ( ) { }
342
+ private _expandCurrentItem ( ) {
343
+ if ( ! this . _activeItem ) {
344
+ return ;
345
+ }
346
+
347
+ if ( ! this . _isCurrentItemExpanded ( ) ) {
348
+ this . _activeItem . expand ( ) ;
349
+ } else {
350
+ coerceObservable ( this . _activeItem . getChildren ( ) )
351
+ . pipe ( take ( 1 ) )
352
+ . subscribe ( children => {
353
+ const firstChild = children [ 0 ] ;
354
+ if ( ! firstChild ) {
355
+ return ;
356
+ }
357
+ this . _setActiveItem ( firstChild as T ) ;
358
+ } ) ;
359
+ }
360
+ }
329
361
330
362
/** For all items that are the same level as the current item, we expand those items. */
331
- private _expandAllItemsAtCurrentItemLevel ( ) { }
363
+ private _expandAllItemsAtCurrentItemLevel ( ) {
364
+ if ( ! this . _activeItem ) {
365
+ return ;
366
+ }
367
+
368
+ const parent = this . _activeItem . getParent ( ) ;
369
+ let itemsToExpand ;
370
+ if ( ! parent ) {
371
+ itemsToExpand = observableOf ( this . _items . filter ( item => item . getParent ( ) === null ) ) ;
372
+ } else {
373
+ itemsToExpand = coerceObservable ( parent . getChildren ( ) ) ;
374
+ }
375
+
376
+ itemsToExpand . pipe ( take ( 1 ) ) . subscribe ( items => {
377
+ for ( const item of items ) {
378
+ item . expand ( ) ;
379
+ }
380
+ } ) ;
381
+ }
332
382
333
383
private _activateCurrentItem ( ) {
334
384
this . _activeItem ?. activate ( ) ;
0 commit comments