-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resize directive instead of relying on package
- Loading branch information
Showing
5 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { Component, DebugElement } from '@angular/core'; | ||
|
||
import { ResizedDirective } from './resized.directive'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
@Component({ | ||
template: `<div style="width: 10px" (resized)="hasBeenResized()"></div>`, | ||
imports: [ResizedDirective], | ||
}) | ||
class TestComponent { | ||
isResized = false; | ||
hasBeenResized() { | ||
this.isResized = true; | ||
} | ||
} | ||
|
||
describe('resizedDirective', () => { | ||
let fixture: ComponentFixture<TestComponent>; | ||
let component: TestComponent; | ||
let divElement: DebugElement; | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.configureTestingModule({ | ||
declarations: [ResizedDirective, TestComponent], | ||
}).createComponent(TestComponent); | ||
component = fixture.componentInstance; | ||
divElement = fixture.debugElement.query(By.css('div')); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should respond to resize event', () => { | ||
expect(component.isResized).toEqual(false); | ||
divElement.triggerEventHandler('resized'); | ||
expect(component.isResized).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Directive, ElementRef, EventEmitter, NgZone, OnDestroy, OnInit, Output } from '@angular/core'; | ||
|
||
export class ResizedEvent { | ||
public newRect: DOMRectReadOnly; | ||
public oldRect?: DOMRectReadOnly; | ||
public isFirst: boolean; | ||
|
||
public constructor(newRect: DOMRectReadOnly, oldRect: DOMRectReadOnly | undefined) { | ||
this.newRect = newRect; | ||
this.oldRect = oldRect; | ||
this.isFirst = oldRect == null; | ||
} | ||
} | ||
|
||
@Directive({ | ||
selector: '[resized]' | ||
}) | ||
export class ResizedDirective implements OnInit, OnDestroy { | ||
private observer: ResizeObserver; | ||
private oldRect?: DOMRectReadOnly; | ||
|
||
@Output() | ||
public readonly resized: EventEmitter<ResizedEvent>; | ||
|
||
public constructor( | ||
private readonly element: ElementRef, | ||
private readonly zone: NgZone | ||
) | ||
{ | ||
this.resized = new EventEmitter<ResizedEvent>(); | ||
this.observer = new ResizeObserver(entries => this.zone.run(() => this.observe(entries))); | ||
} | ||
|
||
public ngOnInit(): void { | ||
this.observer.observe(this.element.nativeElement) | ||
} | ||
|
||
public ngOnDestroy(): void { | ||
this.observer.disconnect(); | ||
} | ||
|
||
private observe(entries: ResizeObserverEntry[]): void { | ||
const domSize = entries[0]; | ||
const resizedEvent = new ResizedEvent(domSize.contentRect, this.oldRect); | ||
this.oldRect = domSize.contentRect; | ||
this.resized.emit(resizedEvent); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters