[6a3a178] | 1 | import { coerceElement, coerceBooleanProperty, coerceNumberProperty } from '@angular/cdk/coercion';
|
---|
| 2 | import * as i0 from '@angular/core';
|
---|
| 3 | import { Injectable, EventEmitter, Directive, ElementRef, NgZone, Output, Input, NgModule } from '@angular/core';
|
---|
| 4 | import { Observable, Subject } from 'rxjs';
|
---|
| 5 | import { debounceTime } from 'rxjs/operators';
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * @license
|
---|
| 9 | * Copyright Google LLC All Rights Reserved.
|
---|
| 10 | *
|
---|
| 11 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 12 | * found in the LICENSE file at https://angular.io/license
|
---|
| 13 | */
|
---|
| 14 | /**
|
---|
| 15 | * Factory that creates a new MutationObserver and allows us to stub it out in unit tests.
|
---|
| 16 | * @docs-private
|
---|
| 17 | */
|
---|
| 18 | class MutationObserverFactory {
|
---|
| 19 | create(callback) {
|
---|
| 20 | return typeof MutationObserver === 'undefined' ? null : new MutationObserver(callback);
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 | MutationObserverFactory.ɵprov = i0.ɵɵdefineInjectable({ factory: function MutationObserverFactory_Factory() { return new MutationObserverFactory(); }, token: MutationObserverFactory, providedIn: "root" });
|
---|
| 24 | MutationObserverFactory.decorators = [
|
---|
| 25 | { type: Injectable, args: [{ providedIn: 'root' },] }
|
---|
| 26 | ];
|
---|
| 27 | /** An injectable service that allows watching elements for changes to their content. */
|
---|
| 28 | class ContentObserver {
|
---|
| 29 | constructor(_mutationObserverFactory) {
|
---|
| 30 | this._mutationObserverFactory = _mutationObserverFactory;
|
---|
| 31 | /** Keeps track of the existing MutationObservers so they can be reused. */
|
---|
| 32 | this._observedElements = new Map();
|
---|
| 33 | }
|
---|
| 34 | ngOnDestroy() {
|
---|
| 35 | this._observedElements.forEach((_, element) => this._cleanupObserver(element));
|
---|
| 36 | }
|
---|
| 37 | observe(elementOrRef) {
|
---|
| 38 | const element = coerceElement(elementOrRef);
|
---|
| 39 | return new Observable((observer) => {
|
---|
| 40 | const stream = this._observeElement(element);
|
---|
| 41 | const subscription = stream.subscribe(observer);
|
---|
| 42 | return () => {
|
---|
| 43 | subscription.unsubscribe();
|
---|
| 44 | this._unobserveElement(element);
|
---|
| 45 | };
|
---|
| 46 | });
|
---|
| 47 | }
|
---|
| 48 | /**
|
---|
| 49 | * Observes the given element by using the existing MutationObserver if available, or creating a
|
---|
| 50 | * new one if not.
|
---|
| 51 | */
|
---|
| 52 | _observeElement(element) {
|
---|
| 53 | if (!this._observedElements.has(element)) {
|
---|
| 54 | const stream = new Subject();
|
---|
| 55 | const observer = this._mutationObserverFactory.create(mutations => stream.next(mutations));
|
---|
| 56 | if (observer) {
|
---|
| 57 | observer.observe(element, {
|
---|
| 58 | characterData: true,
|
---|
| 59 | childList: true,
|
---|
| 60 | subtree: true
|
---|
| 61 | });
|
---|
| 62 | }
|
---|
| 63 | this._observedElements.set(element, { observer, stream, count: 1 });
|
---|
| 64 | }
|
---|
| 65 | else {
|
---|
| 66 | this._observedElements.get(element).count++;
|
---|
| 67 | }
|
---|
| 68 | return this._observedElements.get(element).stream;
|
---|
| 69 | }
|
---|
| 70 | /**
|
---|
| 71 | * Un-observes the given element and cleans up the underlying MutationObserver if nobody else is
|
---|
| 72 | * observing this element.
|
---|
| 73 | */
|
---|
| 74 | _unobserveElement(element) {
|
---|
| 75 | if (this._observedElements.has(element)) {
|
---|
| 76 | this._observedElements.get(element).count--;
|
---|
| 77 | if (!this._observedElements.get(element).count) {
|
---|
| 78 | this._cleanupObserver(element);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | /** Clean up the underlying MutationObserver for the specified element. */
|
---|
| 83 | _cleanupObserver(element) {
|
---|
| 84 | if (this._observedElements.has(element)) {
|
---|
| 85 | const { observer, stream } = this._observedElements.get(element);
|
---|
| 86 | if (observer) {
|
---|
| 87 | observer.disconnect();
|
---|
| 88 | }
|
---|
| 89 | stream.complete();
|
---|
| 90 | this._observedElements.delete(element);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | ContentObserver.ɵprov = i0.ɵɵdefineInjectable({ factory: function ContentObserver_Factory() { return new ContentObserver(i0.ɵɵinject(MutationObserverFactory)); }, token: ContentObserver, providedIn: "root" });
|
---|
| 95 | ContentObserver.decorators = [
|
---|
| 96 | { type: Injectable, args: [{ providedIn: 'root' },] }
|
---|
| 97 | ];
|
---|
| 98 | ContentObserver.ctorParameters = () => [
|
---|
| 99 | { type: MutationObserverFactory }
|
---|
| 100 | ];
|
---|
| 101 | /**
|
---|
| 102 | * Directive that triggers a callback whenever the content of
|
---|
| 103 | * its associated element has changed.
|
---|
| 104 | */
|
---|
| 105 | class CdkObserveContent {
|
---|
| 106 | constructor(_contentObserver, _elementRef, _ngZone) {
|
---|
| 107 | this._contentObserver = _contentObserver;
|
---|
| 108 | this._elementRef = _elementRef;
|
---|
| 109 | this._ngZone = _ngZone;
|
---|
| 110 | /** Event emitted for each change in the element's content. */
|
---|
| 111 | this.event = new EventEmitter();
|
---|
| 112 | this._disabled = false;
|
---|
| 113 | this._currentSubscription = null;
|
---|
| 114 | }
|
---|
| 115 | /**
|
---|
| 116 | * Whether observing content is disabled. This option can be used
|
---|
| 117 | * to disconnect the underlying MutationObserver until it is needed.
|
---|
| 118 | */
|
---|
| 119 | get disabled() { return this._disabled; }
|
---|
| 120 | set disabled(value) {
|
---|
| 121 | this._disabled = coerceBooleanProperty(value);
|
---|
| 122 | this._disabled ? this._unsubscribe() : this._subscribe();
|
---|
| 123 | }
|
---|
| 124 | /** Debounce interval for emitting the changes. */
|
---|
| 125 | get debounce() { return this._debounce; }
|
---|
| 126 | set debounce(value) {
|
---|
| 127 | this._debounce = coerceNumberProperty(value);
|
---|
| 128 | this._subscribe();
|
---|
| 129 | }
|
---|
| 130 | ngAfterContentInit() {
|
---|
| 131 | if (!this._currentSubscription && !this.disabled) {
|
---|
| 132 | this._subscribe();
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | ngOnDestroy() {
|
---|
| 136 | this._unsubscribe();
|
---|
| 137 | }
|
---|
| 138 | _subscribe() {
|
---|
| 139 | this._unsubscribe();
|
---|
| 140 | const stream = this._contentObserver.observe(this._elementRef);
|
---|
| 141 | // TODO(mmalerba): We shouldn't be emitting on this @Output() outside the zone.
|
---|
| 142 | // Consider brining it back inside the zone next time we're making breaking changes.
|
---|
| 143 | // Bringing it back inside can cause things like infinite change detection loops and changed
|
---|
| 144 | // after checked errors if people's code isn't handling it properly.
|
---|
| 145 | this._ngZone.runOutsideAngular(() => {
|
---|
| 146 | this._currentSubscription =
|
---|
| 147 | (this.debounce ? stream.pipe(debounceTime(this.debounce)) : stream).subscribe(this.event);
|
---|
| 148 | });
|
---|
| 149 | }
|
---|
| 150 | _unsubscribe() {
|
---|
| 151 | var _a;
|
---|
| 152 | (_a = this._currentSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | CdkObserveContent.decorators = [
|
---|
| 156 | { type: Directive, args: [{
|
---|
| 157 | selector: '[cdkObserveContent]',
|
---|
| 158 | exportAs: 'cdkObserveContent',
|
---|
| 159 | },] }
|
---|
| 160 | ];
|
---|
| 161 | CdkObserveContent.ctorParameters = () => [
|
---|
| 162 | { type: ContentObserver },
|
---|
| 163 | { type: ElementRef },
|
---|
| 164 | { type: NgZone }
|
---|
| 165 | ];
|
---|
| 166 | CdkObserveContent.propDecorators = {
|
---|
| 167 | event: [{ type: Output, args: ['cdkObserveContent',] }],
|
---|
| 168 | disabled: [{ type: Input, args: ['cdkObserveContentDisabled',] }],
|
---|
| 169 | debounce: [{ type: Input }]
|
---|
| 170 | };
|
---|
| 171 | class ObserversModule {
|
---|
| 172 | }
|
---|
| 173 | ObserversModule.decorators = [
|
---|
| 174 | { type: NgModule, args: [{
|
---|
| 175 | exports: [CdkObserveContent],
|
---|
| 176 | declarations: [CdkObserveContent],
|
---|
| 177 | providers: [MutationObserverFactory]
|
---|
| 178 | },] }
|
---|
| 179 | ];
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * @license
|
---|
| 183 | * Copyright Google LLC All Rights Reserved.
|
---|
| 184 | *
|
---|
| 185 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 186 | * found in the LICENSE file at https://angular.io/license
|
---|
| 187 | */
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * Generated bundle index. Do not edit.
|
---|
| 191 | */
|
---|
| 192 |
|
---|
| 193 | export { CdkObserveContent, ContentObserver, MutationObserverFactory, ObserversModule };
|
---|
| 194 | //# sourceMappingURL=observers.js.map
|
---|