mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
116404: Replaced ViewChild logic with a CSS selector
This change avoids triggering an additional change detection cycle
(cherry picked from commit f7bb83013a
)
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
<div #expandableNavbarSectionContainer class="ds-menu-item-wrapper text-md-center"
|
<div class="ds-menu-item-wrapper text-md-center"
|
||||||
[id]="'expandable-navbar-section-' + section.id"
|
[id]="'expandable-navbar-section-' + section.id"
|
||||||
(mouseenter)="onMouseEnter($event)"
|
(mouseenter)="onMouseEnter($event)"
|
||||||
(mouseleave)="onMouseLeave($event)"
|
(mouseleave)="onMouseLeave($event)"
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
</a>
|
</a>
|
||||||
<div *ngIf="(active$ | async).valueOf() === true" (click)="deactivateSection($event)"
|
<div *ngIf="(active$ | async).valueOf() === true" (click)="deactivateSection($event)"
|
||||||
[id]="expandableNavbarSectionId()"
|
[id]="expandableNavbarSectionId()"
|
||||||
[dsHoverOutsideOfElement]="expandableNavbarSection"
|
[dsHoverOutsideOfParentSelector]="'#expandable-navbar-section-' + section.id"
|
||||||
(dsHoverOutside)="deactivateSection($event, false)"
|
(dsHoverOutside)="deactivateSection($event, false)"
|
||||||
role="menu"
|
role="menu"
|
||||||
class="dropdown-menu show nav-dropdown-menu m-0 shadow-none border-top-0 px-3 px-md-0 pt-0 pt-md-1">
|
class="dropdown-menu show nav-dropdown-menu m-0 shadow-none border-top-0 px-3 px-md-0 pt-0 pt-md-1">
|
||||||
|
@@ -9,7 +9,7 @@ import { HostWindowService } from '../../shared/host-window.service';
|
|||||||
import { MenuService } from '../../shared/menu/menu.service';
|
import { MenuService } from '../../shared/menu/menu.service';
|
||||||
import { HostWindowServiceStub } from '../../shared/testing/host-window-service.stub';
|
import { HostWindowServiceStub } from '../../shared/testing/host-window-service.stub';
|
||||||
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
|
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
|
||||||
import { VarDirective } from '../../shared/utils/var.directive';
|
import { HoverOutsideDirective } from '../../shared/utils/hover-outside.directive';
|
||||||
|
|
||||||
describe('ExpandableNavbarSectionComponent', () => {
|
describe('ExpandableNavbarSectionComponent', () => {
|
||||||
let component: ExpandableNavbarSectionComponent;
|
let component: ExpandableNavbarSectionComponent;
|
||||||
@@ -20,7 +20,11 @@ describe('ExpandableNavbarSectionComponent', () => {
|
|||||||
beforeEach(waitForAsync(() => {
|
beforeEach(waitForAsync(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
imports: [NoopAnimationsModule],
|
imports: [NoopAnimationsModule],
|
||||||
declarations: [ExpandableNavbarSectionComponent, TestComponent, VarDirective],
|
declarations: [
|
||||||
|
ExpandableNavbarSectionComponent,
|
||||||
|
HoverOutsideDirective,
|
||||||
|
TestComponent,
|
||||||
|
],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: 'sectionDataProvider', useValue: {} },
|
{ provide: 'sectionDataProvider', useValue: {} },
|
||||||
{ provide: MenuService, useValue: menuService },
|
{ provide: MenuService, useValue: menuService },
|
||||||
@@ -43,10 +47,6 @@ describe('ExpandableNavbarSectionComponent', () => {
|
|||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create', () => {
|
|
||||||
expect(component).toBeTruthy();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('when the mouse enters the section header (while inactive)', () => {
|
describe('when the mouse enters the section header (while inactive)', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
spyOn(component, 'onMouseEnter').and.callThrough();
|
spyOn(component, 'onMouseEnter').and.callThrough();
|
||||||
@@ -187,7 +187,11 @@ describe('ExpandableNavbarSectionComponent', () => {
|
|||||||
beforeEach(waitForAsync(() => {
|
beforeEach(waitForAsync(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
imports: [NoopAnimationsModule],
|
imports: [NoopAnimationsModule],
|
||||||
declarations: [ExpandableNavbarSectionComponent, TestComponent, VarDirective],
|
declarations: [
|
||||||
|
ExpandableNavbarSectionComponent,
|
||||||
|
HoverOutsideDirective,
|
||||||
|
TestComponent,
|
||||||
|
],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: 'sectionDataProvider', useValue: {} },
|
{ provide: 'sectionDataProvider', useValue: {} },
|
||||||
{ provide: MenuService, useValue: menuService },
|
{ provide: MenuService, useValue: menuService },
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { Component, HostListener, Inject, Injector, OnInit, ViewChild, AfterViewChecked, ElementRef } from '@angular/core';
|
import { Component, HostListener, Inject, Injector, OnInit, AfterViewChecked, OnDestroy } from '@angular/core';
|
||||||
import { NavbarSectionComponent } from '../navbar-section/navbar-section.component';
|
import { NavbarSectionComponent } from '../navbar-section/navbar-section.component';
|
||||||
import { MenuService } from '../../shared/menu/menu.service';
|
import { MenuService } from '../../shared/menu/menu.service';
|
||||||
import { slide } from '../../shared/animations/slide';
|
import { slide } from '../../shared/animations/slide';
|
||||||
@@ -17,9 +17,7 @@ import { MenuSection } from '../../shared/menu/menu-section.model';
|
|||||||
styleUrls: ['./expandable-navbar-section.component.scss'],
|
styleUrls: ['./expandable-navbar-section.component.scss'],
|
||||||
animations: [slide]
|
animations: [slide]
|
||||||
})
|
})
|
||||||
export class ExpandableNavbarSectionComponent extends NavbarSectionComponent implements AfterViewChecked, OnInit {
|
export class ExpandableNavbarSectionComponent extends NavbarSectionComponent implements AfterViewChecked, OnInit, OnDestroy {
|
||||||
|
|
||||||
@ViewChild('expandableNavbarSectionContainer') expandableNavbarSection: ElementRef;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This section resides in the Public Navbar
|
* This section resides in the Public Navbar
|
||||||
@@ -48,6 +46,11 @@ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent imp
|
|||||||
*/
|
*/
|
||||||
addArrowEventListeners = false;
|
addArrowEventListeners = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of current dropdown items who have event listeners
|
||||||
|
*/
|
||||||
|
private dropdownItems: NodeListOf<HTMLElement>;
|
||||||
|
|
||||||
@HostListener('window:resize', ['$event'])
|
@HostListener('window:resize', ['$event'])
|
||||||
onResize() {
|
onResize() {
|
||||||
this.isMobile$.pipe(
|
this.isMobile$.pipe(
|
||||||
@@ -77,23 +80,43 @@ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent imp
|
|||||||
this.subs.push(this.active$.subscribe((active: boolean) => {
|
this.subs.push(this.active$.subscribe((active: boolean) => {
|
||||||
if (active === true) {
|
if (active === true) {
|
||||||
this.addArrowEventListeners = true;
|
this.addArrowEventListeners = true;
|
||||||
|
} else {
|
||||||
|
this.unsubscribeFromEventListeners();
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
ngAfterViewChecked(): void {
|
ngAfterViewChecked(): void {
|
||||||
if (this.addArrowEventListeners) {
|
if (this.addArrowEventListeners) {
|
||||||
const dropdownItems: NodeListOf<HTMLElement> = document.querySelectorAll(`#${this.expandableNavbarSectionId()} *[role="menuitem"]`);
|
this.dropdownItems = document.querySelectorAll(`#${this.expandableNavbarSectionId()} *[role="menuitem"]`);
|
||||||
dropdownItems.forEach((item: HTMLElement) => {
|
this.dropdownItems.forEach((item: HTMLElement) => {
|
||||||
item.addEventListener('keydown', this.navigateDropdown.bind(this));
|
item.addEventListener('keydown', this.navigateDropdown.bind(this));
|
||||||
});
|
});
|
||||||
if (dropdownItems.length > 0) {
|
if (this.dropdownItems.length > 0) {
|
||||||
dropdownItems.item(0).focus();
|
this.dropdownItems.item(0).focus();
|
||||||
}
|
}
|
||||||
this.addArrowEventListeners = false;
|
this.addArrowEventListeners = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ngOnDestroy(): void {
|
||||||
|
super.ngOnDestroy();
|
||||||
|
this.unsubscribeFromEventListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all the current event listeners on the dropdown items (called when the menu is closed & on component
|
||||||
|
* destruction)
|
||||||
|
*/
|
||||||
|
unsubscribeFromEventListeners(): void {
|
||||||
|
if (this.dropdownItems) {
|
||||||
|
this.dropdownItems.forEach((item: HTMLElement) => {
|
||||||
|
item.removeEventListener('keydown', this.navigateDropdown.bind(this));
|
||||||
|
});
|
||||||
|
this.dropdownItems = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When the mouse enters the section toggler activate the menu section
|
* When the mouse enters the section toggler activate the menu section
|
||||||
* @param $event
|
* @param $event
|
||||||
|
@@ -8,10 +8,11 @@ import {
|
|||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Directive to detect when the user hovers outside of the element the directive was put on
|
* Directive to detect when the user hovers outside the element the directive was put on
|
||||||
*
|
*
|
||||||
* BEWARE: it's probably not good for performance to use this excessively (on {@link ExpandableNavbarSectionComponent}
|
* **Performance Consideration**: it's probably not good for performance to use this excessively (on
|
||||||
* for example, a workaround for this problem was to add an `*ngIf` to prevent this Directive from always being active)
|
* {@link ExpandableNavbarSectionComponent} for example, a workaround for this problem was to add an `*ngIf` to prevent
|
||||||
|
* this Directive from always being active)
|
||||||
*/
|
*/
|
||||||
@Directive({
|
@Directive({
|
||||||
selector: '[dsHoverOutside]',
|
selector: '[dsHoverOutside]',
|
||||||
@@ -25,22 +26,23 @@ export class HoverOutsideDirective {
|
|||||||
public dsHoverOutside = new EventEmitter();
|
public dsHoverOutside = new EventEmitter();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link ElementRef} for which this directive should emit when the mouse leaves it. By default this will be the
|
* CSS selector for the parent element to monitor. If set, the directive will use this
|
||||||
* element the directive was put on.
|
* selector to determine if the hover event originated within the selected parent element.
|
||||||
|
* If left unset, the directive will monitor mouseover hover events for the element it is applied to.
|
||||||
*/
|
*/
|
||||||
@Input()
|
@Input()
|
||||||
public dsHoverOutsideOfElement: ElementRef;
|
public dsHoverOutsideOfParentSelector: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private elementRef: ElementRef,
|
private elementRef: ElementRef,
|
||||||
) {
|
) {
|
||||||
this.dsHoverOutsideOfElement = this.elementRef;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('document:mouseover', ['$event'])
|
@HostListener('document:mouseover', ['$event'])
|
||||||
public onMouseOver(event: MouseEvent): void {
|
public onMouseOver(event: MouseEvent): void {
|
||||||
const targetElement: HTMLElement = event.target as HTMLElement;
|
const targetElement: HTMLElement = event.target as HTMLElement;
|
||||||
const hoveredInside = this.dsHoverOutsideOfElement.nativeElement.contains(targetElement);
|
const element: Element = document.querySelector(this.dsHoverOutsideOfParentSelector);
|
||||||
|
const hoveredInside = (element ? new ElementRef(element) : this.elementRef).nativeElement.contains(targetElement);
|
||||||
|
|
||||||
if (!hoveredInside) {
|
if (!hoveredInside) {
|
||||||
this.dsHoverOutside.emit(null);
|
this.dsHoverOutside.emit(null);
|
||||||
|
Reference in New Issue
Block a user