|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {DOCUMENT} from '@angular/common'; |
| 10 | +import { |
| 11 | + ANIMATION_MODULE_TYPE, |
| 12 | + Inject, |
| 13 | + Injectable, |
| 14 | + NgZone, |
| 15 | + OnDestroy, |
| 16 | + Optional, |
| 17 | +} from '@angular/core'; |
| 18 | +import { |
| 19 | + MAT_RIPPLE_GLOBAL_OPTIONS, |
| 20 | + RippleConfig, |
| 21 | + RippleGlobalOptions, |
| 22 | + RippleRenderer, |
| 23 | + RippleTarget, |
| 24 | +} from '@angular/material/core'; |
| 25 | +import {Platform} from '@angular/cdk/platform'; |
| 26 | + |
| 27 | +/** The options for the MatButtonRippleLoader's event listeners. */ |
| 28 | +const OPTIONS = {passive: true, capture: true}; |
| 29 | + |
| 30 | +/** The attribute attached to a mat-button whose ripple has not yet been initialized. */ |
| 31 | +const MAT_BUTTON_RIPPLE_UNINITIALIZED = 'mat-button-ripple-uninitialized'; |
| 32 | + |
| 33 | +/** The attribute attached to a mat-button whose internals (excluding the ripple) have not yet been initialized. */ |
| 34 | +const MAT_BUTTON_INTERNALS_UNINITIALIZED = 'mat-button-internals-uninitialized'; |
| 35 | + |
| 36 | +@Injectable({providedIn: 'root'}) |
| 37 | +export class MatButtonLazyLoader implements OnDestroy { |
| 38 | + private _document: Document; |
| 39 | + |
| 40 | + /** A batch of actions to run. */ |
| 41 | + private _actionQueue: Function[] = []; |
| 42 | + |
| 43 | + /** A timeout for when the action queue will be emptied / ran. */ |
| 44 | + private _runActionsTimeout: any | null = null; |
| 45 | + |
| 46 | + constructor( |
| 47 | + private _platform: Platform, |
| 48 | + private _ngZone: NgZone, |
| 49 | + @Optional() @Inject(DOCUMENT) document: any, |
| 50 | + @Optional() @Inject(ANIMATION_MODULE_TYPE) private _animationMode?: string, |
| 51 | + @Optional() |
| 52 | + @Inject(MAT_RIPPLE_GLOBAL_OPTIONS) |
| 53 | + private _globalRippleOptions?: RippleGlobalOptions, |
| 54 | + ) { |
| 55 | + this._document = document; |
| 56 | + |
| 57 | + this._ngZone.runOutsideAngular(() => { |
| 58 | + this._document.addEventListener('focus', this._onInteraction, OPTIONS); |
| 59 | + this._document.addEventListener('mouseenter', this._onInteraction, OPTIONS); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + ngOnDestroy() { |
| 64 | + this._ngZone.runOutsideAngular(() => { |
| 65 | + this._document.removeEventListener('focus', this._onInteraction, OPTIONS); |
| 66 | + this._document.removeEventListener('mouseenter', this._onInteraction, OPTIONS); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + _renderInternals(button: HTMLButtonElement): void { |
| 71 | + button.removeAttribute(MAT_BUTTON_INTERNALS_UNINITIALIZED); |
| 72 | + this._attachButtonInternals(button); |
| 73 | + } |
| 74 | + |
| 75 | + /** Handles creating and attaching button internals when a button is initially interacted with. */ |
| 76 | + private _onInteraction = (event: Event) => { |
| 77 | + if (!(event.target instanceof Element)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const button = this._closest(event.target); |
| 82 | + if (!button) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + button.removeAttribute(MAT_BUTTON_INTERNALS_UNINITIALIZED); |
| 87 | + this._actionQueue.push(() => this._attachButtonInternals(button as HTMLButtonElement)); |
| 88 | + |
| 89 | + // Immediately run all of the queued actions if a focus event occurs. |
| 90 | + |
| 91 | + if (event.type === 'focus') { |
| 92 | + this._runActions(); |
| 93 | + } else if (event.type === 'mouseenter') { |
| 94 | + this._runActionsTimeout = setTimeout(() => this._runActions(), 50); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + /** Runs all of the actions that have been queued up. */ |
| 99 | + private _runActions(): void { |
| 100 | + if (this._runActionsTimeout !== null) { |
| 101 | + clearTimeout(this._runActionsTimeout); |
| 102 | + this._runActionsTimeout = null; |
| 103 | + } |
| 104 | + for (const callback of this._actionQueue) { |
| 105 | + callback(); |
| 106 | + } |
| 107 | + this._actionQueue = []; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Traverses the element and its parents (heading toward the document root) |
| 112 | + * until it finds a mat-button that has not been initialized. |
| 113 | + */ |
| 114 | + private _closest(element: Element): Element | null { |
| 115 | + let el: Element | null = element; |
| 116 | + while (el) { |
| 117 | + if (el.hasAttribute(MAT_BUTTON_INTERNALS_UNINITIALIZED)) { |
| 118 | + return el; |
| 119 | + } |
| 120 | + el = el.parentElement; |
| 121 | + } |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + private _attachButtonInternals(button: HTMLButtonElement): void { |
| 126 | + button.prepend(this._createSpan(this._getPersistentRippleClassName(button))); |
| 127 | + |
| 128 | + if (button.hasAttribute(MAT_BUTTON_RIPPLE_UNINITIALIZED)) { |
| 129 | + button.removeAttribute(MAT_BUTTON_RIPPLE_UNINITIALIZED); |
| 130 | + button.append(this._createSpan('mat-mdc-focus-indicator')); |
| 131 | + this._appendRipple(button); |
| 132 | + } else { |
| 133 | + const rippleEl = button.querySelector('.mat-mdc-button-ripple'); |
| 134 | + rippleEl!.before(this._createSpan('mat-mdc-focus-indicator')); |
| 135 | + } |
| 136 | + |
| 137 | + // Move the touch target to the correct location in the button. |
| 138 | + const touchTarget = button.querySelector('.mat-mdc-button-touch-target')!; |
| 139 | + button.appendChild(touchTarget); |
| 140 | + } |
| 141 | + |
| 142 | + private _appendRipple(button: HTMLButtonElement): void { |
| 143 | + const ripple = this._document.createElement('span'); |
| 144 | + ripple.classList.add('mat-mdc-button-ripple'); |
| 145 | + |
| 146 | + const target = new MatButtonRippleTarget( |
| 147 | + button, |
| 148 | + this._globalRippleOptions, |
| 149 | + this._animationMode, |
| 150 | + ); |
| 151 | + target.rippleConfig.centered = button.hasAttribute('mat-icon-button'); |
| 152 | + |
| 153 | + const rippleRenderer = new RippleRenderer(target, this._ngZone, ripple, this._platform); |
| 154 | + rippleRenderer.setupTriggerEvents(button); |
| 155 | + |
| 156 | + button.append(ripple); |
| 157 | + } |
| 158 | + |
| 159 | + private _createSpan(className: string): HTMLElement { |
| 160 | + const span = this._document.createElement('span'); |
| 161 | + span.className = className; |
| 162 | + return span; |
| 163 | + } |
| 164 | + |
| 165 | + private _getPersistentRippleClassName(button: Element): string { |
| 166 | + const baseClass = 'mat-mdc-button-persistent-ripple'; |
| 167 | + if (button.getAttribute('data-mat-button-is-fab') === 'true') { |
| 168 | + return baseClass + ' mdc-fab__ripple'; |
| 169 | + } |
| 170 | + if (button.hasAttribute('mat-icon-button')) { |
| 171 | + return baseClass + ' mdc-icon-button__ripple'; |
| 172 | + } |
| 173 | + return baseClass + ' mdc-button__ripple'; |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +class MatButtonRippleTarget implements RippleTarget { |
| 178 | + rippleConfig: RippleConfig & RippleGlobalOptions; |
| 179 | + |
| 180 | + constructor( |
| 181 | + private _button: HTMLButtonElement, |
| 182 | + private _globalRippleOptions?: RippleGlobalOptions, |
| 183 | + animationMode?: string, |
| 184 | + ) { |
| 185 | + this._setRippleConfig(_globalRippleOptions, animationMode); |
| 186 | + } |
| 187 | + |
| 188 | + private _setRippleConfig(globalRippleOptions?: RippleGlobalOptions, animationMode?: string) { |
| 189 | + this.rippleConfig = globalRippleOptions || {}; |
| 190 | + if (animationMode === 'NoopAnimations') { |
| 191 | + this.rippleConfig.animation = {enterDuration: 0, exitDuration: 0}; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + get rippleDisabled(): boolean { |
| 196 | + return this._button.disabled || !!this._globalRippleOptions?.disabled; |
| 197 | + } |
| 198 | +} |
0 commit comments