@angular/material
Version:
Angular Material
145 lines (142 loc) • 4.98 kB
JavaScript
import * as i0 from '@angular/core';
import { inject, DOCUMENT, NgZone, Injector, RendererFactory2, Injectable } from '@angular/core';
import { Platform, _getEventTarget } from '@angular/cdk/platform';
import { _animationsDisabled } from './_animation-chunk.mjs';
import { MAT_RIPPLE_GLOBAL_OPTIONS, RippleRenderer, defaultRippleAnimationConfig } from './_ripple-chunk.mjs';
const eventListenerOptions = {
capture: true
};
const rippleInteractionEvents = ['focus', 'mousedown', 'mouseenter', 'touchstart'];
const matRippleUninitialized = 'mat-ripple-loader-uninitialized';
const matRippleClassName = 'mat-ripple-loader-class-name';
const matRippleCentered = 'mat-ripple-loader-centered';
const matRippleDisabled = 'mat-ripple-loader-disabled';
class MatRippleLoader {
_document = inject(DOCUMENT);
_animationsDisabled = _animationsDisabled();
_globalRippleOptions = inject(MAT_RIPPLE_GLOBAL_OPTIONS, {
optional: true
});
_platform = inject(Platform);
_ngZone = inject(NgZone);
_injector = inject(Injector);
_eventCleanups;
_hosts = new Map();
constructor() {
const renderer = inject(RendererFactory2).createRenderer(null, null);
this._eventCleanups = this._ngZone.runOutsideAngular(() => rippleInteractionEvents.map(name => renderer.listen(this._document, name, this._onInteraction, eventListenerOptions)));
}
ngOnDestroy() {
const hosts = this._hosts.keys();
for (const host of hosts) {
this.destroyRipple(host);
}
this._eventCleanups.forEach(cleanup => cleanup());
}
configureRipple(host, config) {
host.setAttribute(matRippleUninitialized, this._globalRippleOptions?.namespace ?? '');
if (config.className || !host.hasAttribute(matRippleClassName)) {
host.setAttribute(matRippleClassName, config.className || '');
}
if (config.centered) {
host.setAttribute(matRippleCentered, '');
}
if (config.disabled) {
host.setAttribute(matRippleDisabled, '');
}
}
setDisabled(host, disabled) {
const ripple = this._hosts.get(host);
if (ripple) {
ripple.target.rippleDisabled = disabled;
if (!disabled && !ripple.hasSetUpEvents) {
ripple.hasSetUpEvents = true;
ripple.renderer.setupTriggerEvents(host);
}
} else if (disabled) {
host.setAttribute(matRippleDisabled, '');
} else {
host.removeAttribute(matRippleDisabled);
}
}
_onInteraction = event => {
const eventTarget = _getEventTarget(event);
if (eventTarget instanceof HTMLElement) {
const element = eventTarget.closest(`[${matRippleUninitialized}="${this._globalRippleOptions?.namespace ?? ''}"]`);
if (element) {
this._createRipple(element);
}
}
};
_createRipple(host) {
if (!this._document || this._hosts.has(host)) {
return;
}
host.querySelector('.mat-ripple')?.remove();
const rippleEl = this._document.createElement('span');
rippleEl.classList.add('mat-ripple', host.getAttribute(matRippleClassName));
host.append(rippleEl);
const globalOptions = this._globalRippleOptions;
const enterDuration = this._animationsDisabled ? 0 : globalOptions?.animation?.enterDuration ?? defaultRippleAnimationConfig.enterDuration;
const exitDuration = this._animationsDisabled ? 0 : globalOptions?.animation?.exitDuration ?? defaultRippleAnimationConfig.exitDuration;
const target = {
rippleDisabled: this._animationsDisabled || globalOptions?.disabled || host.hasAttribute(matRippleDisabled),
rippleConfig: {
centered: host.hasAttribute(matRippleCentered),
terminateOnPointerUp: globalOptions?.terminateOnPointerUp,
animation: {
enterDuration,
exitDuration
}
}
};
const renderer = new RippleRenderer(target, this._ngZone, rippleEl, this._platform, this._injector);
const hasSetUpEvents = !target.rippleDisabled;
if (hasSetUpEvents) {
renderer.setupTriggerEvents(host);
}
this._hosts.set(host, {
target,
renderer,
hasSetUpEvents
});
host.removeAttribute(matRippleUninitialized);
}
destroyRipple(host) {
const ripple = this._hosts.get(host);
if (ripple) {
ripple.renderer._removeTriggerEvents();
this._hosts.delete(host);
}
}
static ɵfac = i0.ɵɵngDeclareFactory({
minVersion: "12.0.0",
version: "21.0.0",
ngImport: i0,
type: MatRippleLoader,
deps: [],
target: i0.ɵɵFactoryTarget.Injectable
});
static ɵprov = i0.ɵɵngDeclareInjectable({
minVersion: "12.0.0",
version: "21.0.0",
ngImport: i0,
type: MatRippleLoader,
providedIn: 'root'
});
}
i0.ɵɵngDeclareClassMetadata({
minVersion: "12.0.0",
version: "21.0.0",
ngImport: i0,
type: MatRippleLoader,
decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}],
ctorParameters: () => []
});
export { MatRippleLoader };
//# sourceMappingURL=_ripple-loader-chunk.mjs.map