@@ -9,7 +9,6 @@ import {ElementRef, NgZone} from '@angular/core';
9
9
import { Platform , supportsPassiveEventListeners } from '@angular/cdk/platform' ;
10
10
import { RippleRef , RippleState } from './ripple-ref' ;
11
11
12
-
13
12
/** Fade-in duration for the ripples. Can be modified with the speedFactor option. */
14
13
export const RIPPLE_FADE_IN_DURATION = 450 ;
15
14
@@ -30,6 +29,19 @@ export type RippleConfig = {
30
29
persistent ?: boolean ;
31
30
} ;
32
31
32
+ /**
33
+ * Interface that describes the target for launching ripples.
34
+ * It defines the ripple configuration and disabled state for interaction ripples.
35
+ * @docs -private
36
+ */
37
+ export interface RippleTarget {
38
+ /** Configuration for ripples that are launched on pointer down. */
39
+ rippleConfig : RippleConfig ;
40
+
41
+ /** Whether ripples on pointer down should be disabled. */
42
+ rippleDisabled : boolean ;
43
+ }
44
+
33
45
/**
34
46
* Helper service that performs DOM manipulations. Not intended to be used outside this module.
35
47
* The constructor takes a reference to the ripple directive's host element and a map of DOM
@@ -60,13 +72,11 @@ export class RippleRenderer {
60
72
/** Options that apply to all the event listeners that are bound by the renderer. */
61
73
private _eventOptions = supportsPassiveEventListeners ( ) ? ( { passive : true } as any ) : false ;
62
74
63
- /** Ripple config for all ripples created by events. */
64
- rippleConfig : RippleConfig = { } ;
65
-
66
- /** Whether mouse ripples should be created or not. */
67
- rippleDisabled : boolean = false ;
75
+ constructor ( private _target : RippleTarget ,
76
+ private _ngZone : NgZone ,
77
+ elementRef : ElementRef ,
78
+ platform : Platform ) {
68
79
69
- constructor ( elementRef : ElementRef , private _ngZone : NgZone , platform : Platform ) {
70
80
// Only do anything if we're on the browser.
71
81
if ( platform . isBrowser ) {
72
82
this . _containerElement = elementRef . nativeElement ;
@@ -78,9 +88,6 @@ export class RippleRenderer {
78
88
79
89
this . _triggerEvents . set ( 'touchstart' , this . onTouchStart ) ;
80
90
this . _triggerEvents . set ( 'touchend' , this . onPointerUp ) ;
81
-
82
- // By default use the host element as trigger element.
83
- this . setTriggerElement ( this . _containerElement ) ;
84
91
}
85
92
}
86
93
@@ -170,22 +177,19 @@ export class RippleRenderer {
170
177
this . _activeRipples . forEach ( ripple => ripple . fadeOut ( ) ) ;
171
178
}
172
179
173
- /** Sets the trigger element and registers the mouse events. */
174
- setTriggerElement ( element : HTMLElement | null ) {
175
- // Remove all previously register event listeners from the trigger element.
176
- if ( this . _triggerElement ) {
177
- this . _triggerEvents . forEach ( ( fn , type ) => {
178
- this . _triggerElement ! . removeEventListener ( type , fn , this . _eventOptions ) ;
179
- } ) ;
180
+ /** Sets up the trigger event listeners */
181
+ setupTriggerEvents ( element : HTMLElement ) {
182
+ if ( ! element || element === this . _triggerElement ) {
183
+ return ;
180
184
}
181
185
182
- if ( element ) {
183
- // If the element is not null, register all event listeners on the trigger element.
184
- this . _ngZone . runOutsideAngular ( ( ) => {
185
- this . _triggerEvents . forEach ( ( fn , type ) =>
186
- element . addEventListener ( type , fn , this . _eventOptions ) ) ;
187
- } ) ;
188
- }
186
+ // Remove all previously registered event listeners from the trigger element.
187
+ this . _removeTriggerEvents ( ) ;
188
+
189
+ this . _ngZone . runOutsideAngular ( ( ) => {
190
+ this . _triggerEvents . forEach ( ( fn , type ) =>
191
+ element . addEventListener ( type , fn , this . _eventOptions ) ) ;
192
+ } ) ;
189
193
190
194
this . _triggerElement = element ;
191
195
}
@@ -195,22 +199,23 @@ export class RippleRenderer {
195
199
const isSyntheticEvent = this . _lastTouchStartEvent &&
196
200
Date . now ( ) < this . _lastTouchStartEvent + IGNORE_MOUSE_EVENTS_TIMEOUT ;
197
201
198
- if ( ! this . rippleDisabled && ! isSyntheticEvent ) {
202
+ if ( ! this . _target . rippleDisabled && ! isSyntheticEvent ) {
199
203
this . _isPointerDown = true ;
200
- this . fadeInRipple ( event . clientX , event . clientY , this . rippleConfig ) ;
204
+ this . fadeInRipple ( event . clientX , event . clientY , this . _target . rippleConfig ) ;
201
205
}
202
206
}
203
207
204
208
/** Function being called whenever the trigger is being pressed using touch. */
205
209
private onTouchStart = ( event : TouchEvent ) => {
206
- if ( ! this . rippleDisabled ) {
210
+ if ( ! this . _target . rippleDisabled ) {
207
211
// Some browsers fire mouse events after a `touchstart` event. Those synthetic mouse
208
212
// events will launch a second ripple if we don't ignore mouse events for a specific
209
213
// time after a touchstart event.
210
214
this . _lastTouchStartEvent = Date . now ( ) ;
211
215
this . _isPointerDown = true ;
212
216
213
- this . fadeInRipple ( event . touches [ 0 ] . clientX , event . touches [ 0 ] . clientY , this . rippleConfig ) ;
217
+ this . fadeInRipple (
218
+ event . touches [ 0 ] . clientX , event . touches [ 0 ] . clientY , this . _target . rippleConfig ) ;
214
219
}
215
220
}
216
221
@@ -235,10 +240,17 @@ export class RippleRenderer {
235
240
this . _ngZone . runOutsideAngular ( ( ) => setTimeout ( fn , delay ) ) ;
236
241
}
237
242
243
+ /** Removes previously registered event listeners from the trigger element. */
244
+ _removeTriggerEvents ( ) {
245
+ if ( this . _triggerElement ) {
246
+ this . _triggerEvents . forEach ( ( fn , type ) => {
247
+ this . _triggerElement ! . removeEventListener ( type , fn , this . _eventOptions ) ;
248
+ } ) ;
249
+ }
250
+ }
238
251
}
239
252
240
253
/** Enforces a style recalculation of a DOM element by computing its styles. */
241
- // TODO(devversion): Move into global utility function.
242
254
function enforceStyleRecalculation ( element : HTMLElement ) {
243
255
// Enforce a style recalculation by calling `getComputedStyle` and accessing any property.
244
256
// Calling `getPropertyValue` is important to let optimizers know that this is not a noop.
0 commit comments