1- import { Component , HostListener , Inject , Injector , OnInit , ViewChild , AfterViewChecked , ElementRef } from '@angular/core' ;
1+ import { Component , HostListener , Inject , Injector , OnInit , AfterViewChecked , OnDestroy } from '@angular/core' ;
22import { NavbarSectionComponent } from '../navbar-section/navbar-section.component' ;
33import { MenuService } from '../../shared/menu/menu.service' ;
44import { slide } from '../../shared/animations/slide' ;
@@ -17,9 +17,7 @@ import { MenuSection } from '../../shared/menu/menu-section.model';
1717 styleUrls : [ './expandable-navbar-section.component.scss' ] ,
1818 animations : [ slide ]
1919} )
20- export class ExpandableNavbarSectionComponent extends NavbarSectionComponent implements AfterViewChecked , OnInit {
21-
22- @ViewChild ( 'expandableNavbarSectionContainer' ) expandableNavbarSection : ElementRef ;
20+ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent implements AfterViewChecked , OnInit , OnDestroy {
2321
2422 /**
2523 * This section resides in the Public Navbar
@@ -48,6 +46,11 @@ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent imp
4846 */
4947 addArrowEventListeners = false ;
5048
49+ /**
50+ * List of current dropdown items who have event listeners
51+ */
52+ private dropdownItems : NodeListOf < HTMLElement > ;
53+
5154 @HostListener ( 'window:resize' , [ '$event' ] )
5255 onResize ( ) {
5356 this . isMobile$ . pipe (
@@ -77,23 +80,43 @@ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent imp
7780 this . subs . push ( this . active$ . subscribe ( ( active : boolean ) => {
7881 if ( active === true ) {
7982 this . addArrowEventListeners = true ;
83+ } else {
84+ this . unsubscribeFromEventListeners ( ) ;
8085 }
8186 } ) ) ;
8287 }
8388
8489 ngAfterViewChecked ( ) : void {
8590 if ( this . addArrowEventListeners ) {
86- const dropdownItems : NodeListOf < HTMLElement > = document . querySelectorAll ( `#${ this . expandableNavbarSectionId ( ) } *[role="menuitem"]` ) ;
87- dropdownItems . forEach ( ( item : HTMLElement ) => {
91+ this . dropdownItems = document . querySelectorAll ( `#${ this . expandableNavbarSectionId ( ) } *[role="menuitem"]` ) ;
92+ this . dropdownItems . forEach ( ( item : HTMLElement ) => {
8893 item . addEventListener ( 'keydown' , this . navigateDropdown . bind ( this ) ) ;
8994 } ) ;
90- if ( dropdownItems . length > 0 ) {
91- dropdownItems . item ( 0 ) . focus ( ) ;
95+ if ( this . dropdownItems . length > 0 ) {
96+ this . dropdownItems . item ( 0 ) . focus ( ) ;
9297 }
9398 this . addArrowEventListeners = false ;
9499 }
95100 }
96101
102+ ngOnDestroy ( ) : void {
103+ super . ngOnDestroy ( ) ;
104+ this . unsubscribeFromEventListeners ( ) ;
105+ }
106+
107+ /**
108+ * Removes all the current event listeners on the dropdown items (called when the menu is closed & on component
109+ * destruction)
110+ */
111+ unsubscribeFromEventListeners ( ) : void {
112+ if ( this . dropdownItems ) {
113+ this . dropdownItems . forEach ( ( item : HTMLElement ) => {
114+ item . removeEventListener ( 'keydown' , this . navigateDropdown . bind ( this ) ) ;
115+ } ) ;
116+ this . dropdownItems = undefined ;
117+ }
118+ }
119+
97120 /**
98121 * When the mouse enters the section toggler activate the menu section
99122 * @param $event
0 commit comments