From a5ce9559e5576ece471ec94d8df33ac9f273fb54 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Fri, 20 Sep 2019 10:23:32 +0300 Subject: [PATCH] feature(core): add new input visibility directive for no listen to dom --- .../lib/directives/visibility.directive.ts | 55 +++++++++++++------ .../packages/theme-shared/package.json | 2 +- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/directives/visibility.directive.ts b/npm/ng-packs/packages/core/src/lib/directives/visibility.directive.ts index da379b250a..56b49f23ee 100644 --- a/npm/ng-packs/packages/core/src/lib/directives/visibility.directive.ts +++ b/npm/ng-packs/packages/core/src/lib/directives/visibility.directive.ts @@ -9,34 +9,49 @@ export class VisibilityDirective implements AfterViewInit { @Input('abpVisibility') focusedElement: HTMLElement; + @Input() + mutationObserverEnabled: boolean = true; + completed$ = new Subject(); constructor(@Optional() private elRef: ElementRef, private renderer: Renderer2) {} ngAfterViewInit() { - const observer = new MutationObserver(mutations => { - mutations.forEach(mutation => { - if (!mutation.target) return; + let observer: MutationObserver; + if (this.mutationObserverEnabled) { + observer = new MutationObserver(mutations => { + mutations.forEach(mutation => { + if (!mutation.target) return; - const htmlNodes = snq( - () => Array.from(mutation.target.childNodes).filter(node => node instanceof HTMLElement), - [], - ); + const htmlNodes = snq( + () => Array.from(mutation.target.childNodes).filter(node => node instanceof HTMLElement), + [], + ); - if (!htmlNodes.length) { - this.renderer.removeChild(this.elRef.nativeElement.parentElement, this.elRef.nativeElement); - this.disconnect(); - } else { - setTimeout(() => { + if (!htmlNodes.length) { + this.removeFromDOM(); this.disconnect(); - }, 0); - } + } else { + setTimeout(() => { + this.disconnect(); + }, 0); + } + }); }); - }); - observer.observe(this.focusedElement, { - childList: true, - }); + observer.observe(this.focusedElement, { + childList: true, + }); + } else { + setTimeout(() => { + const htmlNodes = snq( + () => Array.from(this.focusedElement.childNodes).filter(node => node instanceof HTMLElement), + [], + ); + + if (!htmlNodes.length) this.removeFromDOM(); + }, 0); + } this.completed$.subscribe(() => observer.disconnect()); } @@ -45,4 +60,8 @@ export class VisibilityDirective implements AfterViewInit { this.completed$.next(); this.completed$.complete(); } + + removeFromDOM() { + this.renderer.removeChild(this.elRef.nativeElement.parentElement, this.elRef.nativeElement); + } } diff --git a/npm/ng-packs/packages/theme-shared/package.json b/npm/ng-packs/packages/theme-shared/package.json index 867930bf1f..554cbbe105 100644 --- a/npm/ng-packs/packages/theme-shared/package.json +++ b/npm/ng-packs/packages/theme-shared/package.json @@ -5,7 +5,7 @@ "@abp/ng.core": "^0.8.3", "@angular/cdk": "^8.0.1", "@ng-bootstrap/ng-bootstrap": "^5.1.0", - "@ngx-validate/core": "^0.0.4", + "@ngx-validate/core": "^0.0.5", "bootstrap": "^4.3.1", "chart.js": "^2.8.0", "font-awesome": "^4.7.0",