You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
9.3 KiB
216 lines
9.3 KiB
import { coerceElement, coerceBooleanProperty, coerceNumberProperty } from '@angular/cdk/coercion';
|
|
import * as i0 from '@angular/core';
|
|
import { Injectable, EventEmitter, Directive, ElementRef, NgZone, Output, Input, NgModule } from '@angular/core';
|
|
import { Observable, Subject } from 'rxjs';
|
|
import { debounceTime } from 'rxjs/operators';
|
|
|
|
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
/**
|
|
* Factory that creates a new MutationObserver and allows us to stub it out in unit tests.
|
|
* @docs-private
|
|
*/
|
|
import * as ɵngcc0 from '@angular/core';
|
|
class MutationObserverFactory {
|
|
create(callback) {
|
|
return typeof MutationObserver === 'undefined' ? null : new MutationObserver(callback);
|
|
}
|
|
}
|
|
MutationObserverFactory.ɵfac = function MutationObserverFactory_Factory(t) { return new (t || MutationObserverFactory)(); };
|
|
MutationObserverFactory.ɵprov = i0.ɵɵdefineInjectable({ factory: function MutationObserverFactory_Factory() { return new MutationObserverFactory(); }, token: MutationObserverFactory, providedIn: "root" });
|
|
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(MutationObserverFactory, [{
|
|
type: Injectable,
|
|
args: [{ providedIn: 'root' }]
|
|
}], null, null); })();
|
|
/** An injectable service that allows watching elements for changes to their content. */
|
|
class ContentObserver {
|
|
constructor(_mutationObserverFactory) {
|
|
this._mutationObserverFactory = _mutationObserverFactory;
|
|
/** Keeps track of the existing MutationObservers so they can be reused. */
|
|
this._observedElements = new Map();
|
|
}
|
|
ngOnDestroy() {
|
|
this._observedElements.forEach((_, element) => this._cleanupObserver(element));
|
|
}
|
|
observe(elementOrRef) {
|
|
const element = coerceElement(elementOrRef);
|
|
return new Observable((observer) => {
|
|
const stream = this._observeElement(element);
|
|
const subscription = stream.subscribe(observer);
|
|
return () => {
|
|
subscription.unsubscribe();
|
|
this._unobserveElement(element);
|
|
};
|
|
});
|
|
}
|
|
/**
|
|
* Observes the given element by using the existing MutationObserver if available, or creating a
|
|
* new one if not.
|
|
*/
|
|
_observeElement(element) {
|
|
if (!this._observedElements.has(element)) {
|
|
const stream = new Subject();
|
|
const observer = this._mutationObserverFactory.create(mutations => stream.next(mutations));
|
|
if (observer) {
|
|
observer.observe(element, {
|
|
characterData: true,
|
|
childList: true,
|
|
subtree: true
|
|
});
|
|
}
|
|
this._observedElements.set(element, { observer, stream, count: 1 });
|
|
}
|
|
else {
|
|
this._observedElements.get(element).count++;
|
|
}
|
|
return this._observedElements.get(element).stream;
|
|
}
|
|
/**
|
|
* Un-observes the given element and cleans up the underlying MutationObserver if nobody else is
|
|
* observing this element.
|
|
*/
|
|
_unobserveElement(element) {
|
|
if (this._observedElements.has(element)) {
|
|
this._observedElements.get(element).count--;
|
|
if (!this._observedElements.get(element).count) {
|
|
this._cleanupObserver(element);
|
|
}
|
|
}
|
|
}
|
|
/** Clean up the underlying MutationObserver for the specified element. */
|
|
_cleanupObserver(element) {
|
|
if (this._observedElements.has(element)) {
|
|
const { observer, stream } = this._observedElements.get(element);
|
|
if (observer) {
|
|
observer.disconnect();
|
|
}
|
|
stream.complete();
|
|
this._observedElements.delete(element);
|
|
}
|
|
}
|
|
}
|
|
ContentObserver.ɵfac = function ContentObserver_Factory(t) { return new (t || ContentObserver)(ɵngcc0.ɵɵinject(MutationObserverFactory)); };
|
|
ContentObserver.ɵprov = i0.ɵɵdefineInjectable({ factory: function ContentObserver_Factory() { return new ContentObserver(i0.ɵɵinject(MutationObserverFactory)); }, token: ContentObserver, providedIn: "root" });
|
|
ContentObserver.ctorParameters = () => [
|
|
{ type: MutationObserverFactory }
|
|
];
|
|
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(ContentObserver, [{
|
|
type: Injectable,
|
|
args: [{ providedIn: 'root' }]
|
|
}], function () { return [{ type: MutationObserverFactory }]; }, null); })();
|
|
/**
|
|
* Directive that triggers a callback whenever the content of
|
|
* its associated element has changed.
|
|
*/
|
|
class CdkObserveContent {
|
|
constructor(_contentObserver, _elementRef, _ngZone) {
|
|
this._contentObserver = _contentObserver;
|
|
this._elementRef = _elementRef;
|
|
this._ngZone = _ngZone;
|
|
/** Event emitted for each change in the element's content. */
|
|
this.event = new EventEmitter();
|
|
this._disabled = false;
|
|
this._currentSubscription = null;
|
|
}
|
|
/**
|
|
* Whether observing content is disabled. This option can be used
|
|
* to disconnect the underlying MutationObserver until it is needed.
|
|
*/
|
|
get disabled() { return this._disabled; }
|
|
set disabled(value) {
|
|
this._disabled = coerceBooleanProperty(value);
|
|
this._disabled ? this._unsubscribe() : this._subscribe();
|
|
}
|
|
/** Debounce interval for emitting the changes. */
|
|
get debounce() { return this._debounce; }
|
|
set debounce(value) {
|
|
this._debounce = coerceNumberProperty(value);
|
|
this._subscribe();
|
|
}
|
|
ngAfterContentInit() {
|
|
if (!this._currentSubscription && !this.disabled) {
|
|
this._subscribe();
|
|
}
|
|
}
|
|
ngOnDestroy() {
|
|
this._unsubscribe();
|
|
}
|
|
_subscribe() {
|
|
this._unsubscribe();
|
|
const stream = this._contentObserver.observe(this._elementRef);
|
|
// TODO(mmalerba): We shouldn't be emitting on this @Output() outside the zone.
|
|
// Consider brining it back inside the zone next time we're making breaking changes.
|
|
// Bringing it back inside can cause things like infinite change detection loops and changed
|
|
// after checked errors if people's code isn't handling it properly.
|
|
this._ngZone.runOutsideAngular(() => {
|
|
this._currentSubscription =
|
|
(this.debounce ? stream.pipe(debounceTime(this.debounce)) : stream).subscribe(this.event);
|
|
});
|
|
}
|
|
_unsubscribe() {
|
|
var _a;
|
|
(_a = this._currentSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
}
|
|
}
|
|
CdkObserveContent.ɵfac = function CdkObserveContent_Factory(t) { return new (t || CdkObserveContent)(ɵngcc0.ɵɵdirectiveInject(ContentObserver), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ElementRef), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.NgZone)); };
|
|
CdkObserveContent.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: CdkObserveContent, selectors: [["", "cdkObserveContent", ""]], inputs: { disabled: ["cdkObserveContentDisabled", "disabled"], debounce: "debounce" }, outputs: { event: "cdkObserveContent" }, exportAs: ["cdkObserveContent"] });
|
|
CdkObserveContent.ctorParameters = () => [
|
|
{ type: ContentObserver },
|
|
{ type: ElementRef },
|
|
{ type: NgZone }
|
|
];
|
|
CdkObserveContent.propDecorators = {
|
|
event: [{ type: Output, args: ['cdkObserveContent',] }],
|
|
disabled: [{ type: Input, args: ['cdkObserveContentDisabled',] }],
|
|
debounce: [{ type: Input }]
|
|
};
|
|
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(CdkObserveContent, [{
|
|
type: Directive,
|
|
args: [{
|
|
selector: '[cdkObserveContent]',
|
|
exportAs: 'cdkObserveContent'
|
|
}]
|
|
}], function () { return [{ type: ContentObserver }, { type: ɵngcc0.ElementRef }, { type: ɵngcc0.NgZone }]; }, { event: [{
|
|
type: Output,
|
|
args: ['cdkObserveContent']
|
|
}], disabled: [{
|
|
type: Input,
|
|
args: ['cdkObserveContentDisabled']
|
|
}], debounce: [{
|
|
type: Input
|
|
}] }); })();
|
|
class ObserversModule {
|
|
}
|
|
ObserversModule.ɵfac = function ObserversModule_Factory(t) { return new (t || ObserversModule)(); };
|
|
ObserversModule.ɵmod = /*@__PURE__*/ ɵngcc0.ɵɵdefineNgModule({ type: ObserversModule });
|
|
ObserversModule.ɵinj = /*@__PURE__*/ ɵngcc0.ɵɵdefineInjector({ providers: [MutationObserverFactory] });
|
|
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(ObserversModule, [{
|
|
type: NgModule,
|
|
args: [{
|
|
exports: [CdkObserveContent],
|
|
declarations: [CdkObserveContent],
|
|
providers: [MutationObserverFactory]
|
|
}]
|
|
}], null, null); })();
|
|
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵngcc0.ɵɵsetNgModuleScope(ObserversModule, { declarations: [CdkObserveContent], exports: [CdkObserveContent] }); })();
|
|
|
|
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { CdkObserveContent, ContentObserver, MutationObserverFactory, ObserversModule };
|
|
|
|
//# sourceMappingURL=observers.js.map
|