1- import { ChangeDetectionStrategy , Component , ElementRef , inject } from '@angular/core' ;
1+ import {
2+ AfterViewInit ,
3+ ChangeDetectionStrategy ,
4+ Component ,
5+ DestroyRef ,
6+ ElementRef ,
7+ inject ,
8+ output
9+ } from '@angular/core' ;
10+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
11+ import { ResizeObserverService } from '@fundamental-ngx/cdk/utils' ;
12+ import { isEqual , sortBy } from 'lodash-es' ;
213import { FD_SHELLBAR_COMPONENT } from '../tokens' ;
314
415/**
@@ -22,24 +33,56 @@ import { FD_SHELLBAR_COMPONENT } from '../tokens';
2233 `
2334 ]
2435} )
25- export class ShellbarContextAreaComponent {
36+ export class ShellbarContextAreaComponent implements AfterViewInit {
37+ /** Event emitted when items in the context area are hidden or shown. Event parameter is an array of all the items that are hidden. */
38+ contentItemVisibilityChange = output < HTMLElement [ ] > ( ) ;
39+
40+ /** @hidden */
41+ el : ElementRef = inject ( ElementRef ) ;
42+
2643 /** @hidden */
2744 private readonly _shellbar = inject ( FD_SHELLBAR_COMPONENT ) ;
2845
2946 /** @hidden */
30- constructor ( public el : ElementRef ) { }
47+ private _resizeObserverService = inject ( ResizeObserverService ) ;
48+
49+ /** @hidden */
50+ private readonly _destroyRef = inject ( DestroyRef ) ;
51+
52+ /** @hidden */
53+ private _hiddenItems : HTMLElement [ ] ;
54+
55+ /** @hidden */
56+ ngAfterViewInit ( ) : void {
57+ this . _resizeObserverService
58+ . observe ( this . el . nativeElement )
59+ . pipe ( takeUntilDestroyed ( this . _destroyRef ) )
60+ . subscribe ( ( ) => {
61+ this . showElements ( ) ;
62+ this . hideElementsIfNeeded ( ) ;
63+ } ) ;
64+ }
3165
3266 /**
3367 * Iteratively hides elements if the end of the actions exceed the end of the shellbar.
3468 */
3569 hideElementsIfNeeded ( ) : void {
36- const elements : { el : HTMLElement ; priority : number } [ ] = this . _getElementsWithPriority ( ) ;
70+ const newHiddenItems : HTMLElement [ ] = [ ] ;
71+ const contextAreaItems : { el : HTMLElement ; priority : number } [ ] = this . _getContextAreaItemsWithPriority ( ) ;
3772 while ( this . _shellbar . _actionsExceedShellbarWidth ( ) ) {
38- const shownElements = elements . filter ( ( el ) => el . el . style . display !== 'none' ) ;
73+ const shownElements = contextAreaItems . filter ( ( item ) => item ? .el ? .style ? .display !== 'none' ) ;
3974 if ( shownElements . length === 0 ) {
4075 break ;
4176 }
42- shownElements [ shownElements . length - 1 ] . el . style . display = 'none' ;
77+ const lastItem = shownElements [ shownElements . length - 1 ] ;
78+ if ( lastItem ?. el ?. style ) {
79+ newHiddenItems . push ( lastItem . el ) ;
80+ lastItem . el . style . display = 'none' ;
81+ }
82+ }
83+ if ( ! isEqual ( sortBy ( this . _hiddenItems ) , sortBy ( newHiddenItems ) ) ) {
84+ this . _hiddenItems = newHiddenItems ;
85+ this . contentItemVisibilityChange . emit ( newHiddenItems ) ;
4386 }
4487 }
4588
@@ -48,8 +91,10 @@ export class ShellbarContextAreaComponent {
4891 * to exceed the end of th eshellbar.
4992 */
5093 showElements ( ) : void {
51- this . _getElementsWithPriority ( ) . forEach ( ( el ) => {
52- el . el . style . display = '' ;
94+ this . _getContextAreaItemsWithPriority ( ) . forEach ( ( item ) => {
95+ if ( item ?. el ?. style ) {
96+ item . el . style . display = '' ;
97+ }
5398 } ) ;
5499 }
55100
@@ -58,8 +103,8 @@ export class ShellbarContextAreaComponent {
58103 * The elements are sorted based on their priority, with elements having
59104 * higher priority shown first.
60105 */
61- private _getElementsWithPriority ( ) : { el : HTMLElement ; priority : number } [ ] {
62- return [ ...this . el . nativeElement . childNodes ]
106+ private _getContextAreaItemsWithPriority ( ) : { el : HTMLElement ; priority : number } [ ] {
107+ return [ ...this . el . nativeElement . children ]
63108 . map ( ( element : HTMLElement , index ) => {
64109 const hasPriorityAttribute = element . hasAttribute && element . hasAttribute ( 'fdShellbarHidePriority' ) ;
65110 const priority = hasPriorityAttribute
0 commit comments