UNPKG

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