2018-07-31 15:46:55 +02:00
|
|
|
import { Directive, Output, EventEmitter, ElementRef, OnDestroy } from '@angular/core';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* detects changes in DOM and emits a signal on changes.
|
|
|
|
*
|
|
|
|
* @example (appDomChange)="onChange($event)"
|
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: '[appDomChange]'
|
|
|
|
})
|
|
|
|
export class DomChangeDirective implements OnDestroy {
|
|
|
|
private changes: MutationObserver;
|
|
|
|
|
|
|
|
@Output() public domChange = new EventEmitter();
|
|
|
|
|
2018-08-29 13:21:25 +02:00
|
|
|
public constructor(private elementRef: ElementRef) {
|
2018-07-31 15:46:55 +02:00
|
|
|
const element = this.elementRef.nativeElement;
|
|
|
|
|
|
|
|
this.changes = new MutationObserver((mutations: MutationRecord[]) => {
|
|
|
|
mutations.forEach((mutation: MutationRecord) => this.domChange.emit(mutation));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.changes.observe(element, {
|
|
|
|
attributes: true,
|
|
|
|
childList: true,
|
|
|
|
characterData: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-29 13:21:25 +02:00
|
|
|
public ngOnDestroy(): void {
|
2018-07-31 15:46:55 +02:00
|
|
|
this.changes.disconnect();
|
|
|
|
}
|
|
|
|
}
|