UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

1 lines • 9.32 kB
{"version":3,"file":"ng-zorro-antd-cdk-resize-observer.mjs","names":[],"sources":["../../components/cdk/resize-observer/resize-observer.service.ts","../../components/cdk/resize-observer/resize-observer.directive.ts","../../components/cdk/resize-observer/resize-observer.module.ts"],"sourcesContent":["/**\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE\n */\n\nimport { coerceElement } from '@angular/cdk/coercion';\nimport { DestroyRef, ElementRef, inject, Injectable } from '@angular/core';\nimport { Observable, Observer, Subject } from 'rxjs';\n\n/**\n * Factory that creates a new ResizeObserver and allows us to stub it out in unit tests.\n */\n@Injectable({ providedIn: 'root' })\nexport class NzResizeObserverFactory {\n create(callback: ResizeObserverCallback): ResizeObserver | null {\n return typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(callback);\n }\n}\n\n/** An injectable service that allows watching elements for changes to their content. */\n@Injectable({ providedIn: 'root' })\nexport class NzResizeObserver {\n private nzResizeObserverFactory = inject(NzResizeObserverFactory);\n private destroyRef = inject(DestroyRef);\n /** Keeps track of the existing ResizeObservers so they can be reused. */\n private observedElements = new Map<\n Element,\n {\n observer: ResizeObserver | null;\n stream: Subject<ResizeObserverEntry[]>;\n count: number;\n }\n >();\n\n constructor() {\n this.destroyRef.onDestroy(() => this.observedElements.forEach((_, element) => this.cleanupObserver(element)));\n }\n\n observe(elementOrRef: Element | ElementRef<Element>): Observable<ResizeObserverEntry[]> {\n const element = coerceElement(elementOrRef);\n\n return new Observable((observer: Observer<ResizeObserverEntry[]>) => {\n const stream = this.observeElement(element);\n const subscription = stream.subscribe(observer);\n\n return () => {\n subscription.unsubscribe();\n this.unobserveElement(element);\n };\n });\n }\n\n /**\n * Observes the given element by using the existing ResizeObserver if available, or creating a\n * new one if not.\n */\n private observeElement(element: Element): Subject<ResizeObserverEntry[]> {\n if (!this.observedElements.has(element)) {\n const stream = new Subject<ResizeObserverEntry[]>();\n const observer = this.nzResizeObserverFactory.create((mutations: ResizeObserverEntry[]) =>\n stream.next(mutations)\n );\n if (observer) {\n observer.observe(element);\n }\n this.observedElements.set(element, { observer, stream, count: 1 });\n } else {\n this.observedElements.get(element)!.count++;\n }\n return this.observedElements.get(element)!.stream;\n }\n\n /**\n * Un-observes the given element and cleans up the underlying ResizeObserver if nobody else is\n * observing this element.\n */\n private unobserveElement(element: Element): void {\n if (this.observedElements.has(element)) {\n this.observedElements.get(element)!.count--;\n if (!this.observedElements.get(element)!.count) {\n this.cleanupObserver(element);\n }\n }\n }\n\n /** Clean up the underlying ResizeObserver for the specified element. */\n private cleanupObserver(element: Element): void {\n if (this.observedElements.has(element)) {\n const { observer, stream } = this.observedElements.get(element)!;\n if (observer) {\n observer.disconnect();\n }\n stream.complete();\n this.observedElements.delete(element);\n }\n }\n}\n","/**\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE\n */\n\nimport {\n AfterContentInit,\n Directive,\n ElementRef,\n EventEmitter,\n Input,\n OnChanges,\n Output,\n SimpleChanges,\n booleanAttribute,\n inject,\n DestroyRef\n} from '@angular/core';\nimport { Subscription } from 'rxjs';\n\nimport { NzResizeObserver } from './resize-observer.service';\n\n@Directive({\n selector: '[nzResizeObserver]'\n})\nexport class NzResizeObserverDirective implements AfterContentInit, OnChanges {\n private readonly resizeObserver = inject(NzResizeObserver);\n private readonly elementRef = inject(ElementRef<HTMLElement>);\n private readonly destroyRef = inject(DestroyRef);\n\n @Input({ transform: booleanAttribute }) nzResizeObserverDisabled = false;\n @Output() readonly nzResizeObserve = new EventEmitter<ResizeObserverEntry[]>();\n\n private currentSubscription: Subscription | null = null;\n\n private subscribe(): void {\n this.unsubscribe();\n this.currentSubscription = this.resizeObserver.observe(this.elementRef).subscribe(this.nzResizeObserve);\n }\n\n private unsubscribe(): void {\n this.currentSubscription?.unsubscribe();\n }\n\n constructor() {\n this.destroyRef.onDestroy(() => this.unsubscribe());\n }\n\n ngAfterContentInit(): void {\n if (!this.currentSubscription && !this.nzResizeObserverDisabled) {\n this.subscribe();\n }\n }\n\n ngOnChanges(changes: SimpleChanges): void {\n const { nzResizeObserverDisabled } = changes;\n if (nzResizeObserverDisabled) {\n if (this.nzResizeObserverDisabled) {\n this.unsubscribe();\n } else {\n this.subscribe();\n }\n }\n }\n}\n","/**\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE\n */\n\nimport { NgModule } from '@angular/core';\n\nimport { NzResizeObserverDirective } from './resize-observer.directive';\n\n@NgModule({\n imports: [NzResizeObserverDirective],\n exports: [NzResizeObserverDirective]\n})\nexport class NzResizeObserverModule {}\n"],"mappings":";;;;AAaA,IAAa,0BAAb,MAAa,wBAAuB;CAClC,OAAO,UAAgC;EACrC,OAAO,OAAO,mBAAmB,cAAc,OAAO,IAAI,eAAe,QAAQ;CACnF;CAHW,OAAA,OAAA,GAAA,mBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAuB,MAAA,CAAA;EAAA,QAAA,GAAA,gBAAA;CAAA,CAAA;CAAvB,OAAA,QAAA,GAAA,sBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAuB,YADV;CAAM,CAAA;;AACnB,GAAA,yBAAA;CAAA,YAAA;CAAA,SAAA;CAAA,UAAA;CAAA,MAAA;CAAuB,YAAA,CAAA;EADnC,MAAA;EAAW,MAAA,CAAA,EAAE,YAAY,OAAM,CAAE;;;AASlC,IAAa,mBAAb,MAAa,iBAAgB;CACnB,0BAA0B,OAAO,uBAAuB;CACxD,aAAa,OAAO,UAAU;CAE9B,mCAAmB,IAAI,IAAG;CASlC,cAAA;EACE,KAAK,WAAW,gBAAgB,KAAK,iBAAiB,SAAS,GAAG,YAAY,KAAK,gBAAgB,OAAO,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