@@ -7,12 +7,11 @@ import {
77import {
88 AfterViewChecked ,
99 Component ,
10- ElementRef ,
1110 HostListener ,
1211 Inject ,
1312 Injector ,
13+ OnDestroy ,
1414 OnInit ,
15- ViewChild ,
1615} from '@angular/core' ;
1716import { RouterLinkActive } from '@angular/router' ;
1817import { Observable } from 'rxjs' ;
@@ -24,7 +23,6 @@ import { MenuService } from '../../shared/menu/menu.service';
2423import { MenuID } from '../../shared/menu/menu-id.model' ;
2524import { MenuSection } from '../../shared/menu/menu-section.model' ;
2625import { HoverOutsideDirective } from '../../shared/utils/hover-outside.directive' ;
27- import { VarDirective } from '../../shared/utils/var.directive' ;
2826import { NavbarSectionComponent } from '../navbar-section/navbar-section.component' ;
2927
3028/**
@@ -43,12 +41,9 @@ import { NavbarSectionComponent } from '../navbar-section/navbar-section.compone
4341 NgFor ,
4442 NgIf ,
4543 RouterLinkActive ,
46- VarDirective ,
4744 ] ,
4845} )
49- export class ExpandableNavbarSectionComponent extends NavbarSectionComponent implements AfterViewChecked , OnInit {
50-
51- @ViewChild ( 'expandableNavbarSectionContainer' ) expandableNavbarSection : ElementRef ;
46+ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent implements AfterViewChecked , OnInit , OnDestroy {
5247
5348 /**
5449 * This section resides in the Public Navbar
@@ -77,6 +72,11 @@ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent imp
7772 */
7873 addArrowEventListeners = false ;
7974
75+ /**
76+ * List of current dropdown items who have event listeners
77+ */
78+ private dropdownItems : NodeListOf < HTMLElement > ;
79+
8080 @HostListener ( 'window:resize' , [ '$event' ] )
8181 onResize ( ) {
8282 this . isMobile$ . pipe (
@@ -106,23 +106,43 @@ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent imp
106106 this . subs . push ( this . active$ . subscribe ( ( active : boolean ) => {
107107 if ( active === true ) {
108108 this . addArrowEventListeners = true ;
109+ } else {
110+ this . unsubscribeFromEventListeners ( ) ;
109111 }
110112 } ) ) ;
111113 }
112114
113115 ngAfterViewChecked ( ) : void {
114116 if ( this . addArrowEventListeners ) {
115- const dropdownItems : NodeListOf < HTMLElement > = document . querySelectorAll ( `#${ this . expandableNavbarSectionId ( ) } *[role="menuitem"]` ) ;
116- dropdownItems . forEach ( ( item : HTMLElement ) => {
117+ this . dropdownItems = document . querySelectorAll ( `#${ this . expandableNavbarSectionId ( ) } *[role="menuitem"]` ) ;
118+ this . dropdownItems . forEach ( ( item : HTMLElement ) => {
117119 item . addEventListener ( 'keydown' , this . navigateDropdown . bind ( this ) ) ;
118120 } ) ;
119- if ( dropdownItems . length > 0 ) {
120- dropdownItems . item ( 0 ) . focus ( ) ;
121+ if ( this . dropdownItems . length > 0 ) {
122+ this . dropdownItems . item ( 0 ) . focus ( ) ;
121123 }
122124 this . addArrowEventListeners = false ;
123125 }
124126 }
125127
128+ ngOnDestroy ( ) : void {
129+ super . ngOnDestroy ( ) ;
130+ this . unsubscribeFromEventListeners ( ) ;
131+ }
132+
133+ /**
134+ * Removes all the current event listeners on the dropdown items (called when the menu is closed & on component
135+ * destruction)
136+ */
137+ unsubscribeFromEventListeners ( ) : void {
138+ if ( this . dropdownItems ) {
139+ this . dropdownItems . forEach ( ( item : HTMLElement ) => {
140+ item . removeEventListener ( 'keydown' , this . navigateDropdown . bind ( this ) ) ;
141+ } ) ;
142+ this . dropdownItems = undefined ;
143+ }
144+ }
145+
126146 /**
127147 * When the mouse enters the section toggler activate the menu section
128148 * @param $event
0 commit comments