blob: 3d29eb2bdae95dc2fffbe297b7f35c1804fe5f8a [file] [log] [blame]
{"version":3,"file":"cdk-observers.umd.js","sources":["../../../../../src/cdk/observers/observe-content.ts","../../../../../src/cdk/observers/public-api.ts","../../../../../src/cdk/observers/index.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\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://angular.io/license\n */\n\nimport {\n coerceBooleanProperty,\n coerceNumberProperty,\n coerceElement,\n BooleanInput,\n NumberInput\n} from '@angular/cdk/coercion';\nimport {\n AfterContentInit,\n Directive,\n ElementRef,\n EventEmitter,\n Injectable,\n Input,\n NgModule,\n NgZone,\n OnDestroy,\n Output,\n} from '@angular/core';\nimport {Observable, Subject, Subscription, Observer} from 'rxjs';\nimport {debounceTime} from 'rxjs/operators';\n\n/**\n * Factory that creates a new MutationObserver and allows us to stub it out in unit tests.\n * @docs-private\n */\n@Injectable({providedIn: 'root'})\nexport class MutationObserverFactory {\n create(callback: MutationCallback): MutationObserver | null {\n return typeof MutationObserver === 'undefined' ? null : new MutationObserver(callback);\n }\n}\n\n\n/** An injectable service that allows watching elements for changes to their content. */\n@Injectable({providedIn: 'root'})\nexport class ContentObserver implements OnDestroy {\n /** Keeps track of the existing MutationObservers so they can be reused. */\n private _observedElements = new Map<Element, {\n observer: MutationObserver | null,\n stream: Subject<MutationRecord[]>,\n count: number\n }>();\n\n constructor(private _mutationObserverFactory: MutationObserverFactory) {}\n\n ngOnDestroy() {\n this._observedElements.forEach((_, element) => this._cleanupObserver(element));\n }\n\n /**\n * Observe content changes on an element.\n * @param element The element to observe for content changes.\n */\n observe(element: Element): Observable<MutationRecord[]>;\n\n /**\n * Observe content changes on an element.\n * @param element The element to observe for content changes.\n */\n observe(element: ElementRef<Element>): Observable<MutationRecord[]>;\n\n observe(elementOrRef: Element | ElementRef<Element>): Observable<MutationRecord[]> {\n const element = coerceElement(elementOrRef);\n\n return new Observable((observer: Observer<MutationRecord[]>) => {\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 MutationObserver if available, or creating a\n * new one if not.\n */\n private _observeElement(element: Element): Subject<MutationRecord[]> {\n if (!this._observedElements.has(element)) {\n const stream = new Subject<MutationRecord[]>();\n const observer = this._mutationObserverFactory.create(mutations => stream.next(mutations));\n if (observer) {\n observer.observe(element, {\n characterData: true,\n childList: true,\n subtree: true\n });\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 MutationObserver if nobody else is\n * observing this element.\n */\n private _unobserveElement(element: Element) {\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 MutationObserver for the specified element. */\n private _cleanupObserver(element: Element) {\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\n/**\n * Directive that triggers a callback whenever the content of\n * its associated element has changed.\n */\n@Directive({\n selector: '[cdkObserveContent]',\n exportAs: 'cdkObserveContent',\n})\nexport class CdkObserveContent implements AfterContentInit, OnDestroy {\n /** Event emitted for each change in the element's content. */\n @Output('cdkObserveContent') event = new EventEmitter<MutationRecord[]>();\n\n /**\n * Whether observing content is disabled. This option can be used\n * to disconnect the underlying MutationObserver until it is needed.\n */\n @Input('cdkObserveContentDisabled')\n get disabled() { return this._disabled; }\n set disabled(value: any) {\n this._disabled = coerceBooleanProperty(value);\n this._disabled ? this._unsubscribe() : this._subscribe();\n }\n private _disabled = false;\n\n /** Debounce interval for emitting the changes. */\n @Input()\n get debounce(): number { return this._debounce; }\n set debounce(value: number) {\n this._debounce = coerceNumberProperty(value);\n this._subscribe();\n }\n private _debounce: number;\n\n private _currentSubscription: Subscription | null = null;\n\n constructor(private _contentObserver: ContentObserver,\n private _elementRef: ElementRef<HTMLElement>,\n private _ngZone: NgZone) {}\n\n ngAfterContentInit() {\n if (!this._currentSubscription && !this.disabled) {\n this._subscribe();\n }\n }\n\n ngOnDestroy() {\n this._unsubscribe();\n }\n\n private _subscribe() {\n this._unsubscribe();\n const stream = this._contentObserver.observe(this._elementRef);\n\n // TODO(mmalerba): We shouldn't be emitting on this @Output() outside the zone.\n // Consider brining it back inside the zone next time we're making breaking changes.\n // Bringing it back inside can cause things like infinite change detection loops and changed\n // after checked errors if people's code isn't handling it properly.\n this._ngZone.runOutsideAngular(() => {\n this._currentSubscription =\n (this.debounce ? stream.pipe(debounceTime(this.debounce)) : stream).subscribe(this.event);\n });\n }\n\n private _unsubscribe() {\n this._currentSubscription?.unsubscribe();\n }\n\n static ngAcceptInputType_disabled: BooleanInput;\n static ngAcceptInputType_debounce: NumberInput;\n}\n\n\n@NgModule({\n exports: [CdkObserveContent],\n declarations: [CdkObserveContent],\n providers: [MutationObserverFactory]\n})\nexport class ObserversModule {}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\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://angular.io/license\n */\n\nexport * from './observe-content';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":["Injectable","coerceElement","Observable","Subject","EventEmitter","coerceBooleanProperty","coerceNumberProperty","debounceTime","Directive","ElementRef","NgZone","Output","Input","NgModule"],"mappings":";;;;;;IAAA;;;;;;;AAQA,IAsBA;;;;AAKA;QAAA;;QACE,wCAAM,GAAN,UAAO,QAA0B;YAC/B,OAAO,OAAO,gBAAgB,KAAK,WAAW,GAAG,IAAI,GAAG,IAAI,gBAAgB,CAAC,QAAQ,CAAC,CAAC;SACxF;;;;;gBAJFA,aAAU,SAAC,EAAC,UAAU,EAAE,MAAM,EA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