6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
+ import { Platform } from '@angular/cdk/platform' ;
9
10
import {
10
11
Directive ,
11
12
ElementRef ,
12
- Input ,
13
13
Inject ,
14
+ InjectionToken ,
15
+ Input ,
14
16
NgZone ,
15
- OnChanges ,
16
- SimpleChanges ,
17
17
OnDestroy ,
18
- InjectionToken ,
18
+ OnInit ,
19
19
Optional ,
20
20
} from '@angular/core' ;
21
- import { Platform } from '@angular/cdk/platform' ;
22
- import { RippleConfig , RippleRenderer } from './ripple-renderer' ;
23
21
import { RippleRef } from './ripple-ref' ;
22
+ import { RippleConfig , RippleRenderer , RippleTarget } from './ripple-renderer' ;
24
23
25
24
/** Configurable options for `matRipple`. */
26
25
export interface RippleGlobalOptions {
@@ -50,28 +49,20 @@ export const MAT_RIPPLE_GLOBAL_OPTIONS =
50
49
'[class.mat-ripple-unbounded]' : 'unbounded'
51
50
}
52
51
} )
53
- export class MatRipple implements OnChanges , OnDestroy {
52
+ export class MatRipple implements OnInit , OnDestroy , RippleTarget {
54
53
55
- /**
56
- * The element that triggers the ripple when click events are received. Defaults to the
57
- * directive's host element.
58
- */
59
- // Prevent TS metadata emit from referencing HTMLElement in ripple.js
60
- // Otherwise running this code in a Node environment (e.g Universal) will not work.
61
- @Input ( 'matRippleTrigger' ) trigger : HTMLElement | HTMLElement ;
54
+ /** Custom color for all ripples. */
55
+ @Input ( 'matRippleColor' ) color : string ;
56
+
57
+ /** Whether the ripples should be visible outside the component's bounds. */
58
+ @Input ( 'matRippleUnbounded' ) unbounded : boolean ;
62
59
63
60
/**
64
61
* Whether the ripple always originates from the center of the host element's bounds, rather
65
62
* than originating from the location of the click event.
66
63
*/
67
64
@Input ( 'matRippleCentered' ) centered : boolean ;
68
65
69
- /**
70
- * Whether click events will not trigger the ripple. Ripples can be still launched manually
71
- * by using the `launch()` method.
72
- */
73
- @Input ( 'matRippleDisabled' ) disabled : boolean ;
74
-
75
66
/**
76
67
* If set, the radius in pixels of foreground ripples when fully expanded. If unset, the radius
77
68
* will be the distance from the center of the ripple to the furthest corner of the host element's
@@ -86,45 +77,59 @@ export class MatRipple implements OnChanges, OnDestroy {
86
77
*/
87
78
@Input ( 'matRippleSpeedFactor' ) speedFactor : number = 1 ;
88
79
89
- /** Custom color for ripples. */
90
- @Input ( 'matRippleColor' ) color : string ;
80
+ /**
81
+ * Whether click events will not trigger the ripple. Ripples can be still launched manually
82
+ * by using the `launch()` method.
83
+ */
84
+ @Input ( 'matRippleDisabled' )
85
+ get disabled ( ) { return this . _disabled ; }
86
+ set disabled ( value : boolean ) {
87
+ this . _disabled = value ;
88
+ this . _setupTriggerEventsIfEnabled ( ) ;
89
+ }
90
+ private _disabled : boolean = false ;
91
91
92
- /** Whether foreground ripples should be visible outside the component's bounds. */
93
- @Input ( 'matRippleUnbounded' ) unbounded : boolean ;
92
+ /**
93
+ * The element that triggers the ripple when click events are received.
94
+ * Defaults to the directive's host element.
95
+ */
96
+ @Input ( 'matRippleTrigger' )
97
+ get trigger ( ) { return this . _trigger || this . _elementRef . nativeElement ; }
98
+ set trigger ( trigger : HTMLElement ) {
99
+ this . _trigger = trigger ;
100
+ this . _setupTriggerEventsIfEnabled ( ) ;
101
+ }
102
+ private _trigger : HTMLElement ;
94
103
95
104
/** Renderer for the ripple DOM manipulations. */
96
105
private _rippleRenderer : RippleRenderer ;
97
106
98
107
/** Options that are set globally for all ripples. */
99
108
private _globalOptions : RippleGlobalOptions ;
100
109
101
- constructor (
102
- elementRef : ElementRef ,
103
- ngZone : NgZone ,
104
- platform : Platform ,
105
- @Optional ( ) @Inject ( MAT_RIPPLE_GLOBAL_OPTIONS ) globalOptions : RippleGlobalOptions
106
- ) {
107
- this . _rippleRenderer = new RippleRenderer ( elementRef , ngZone , platform ) ;
108
- this . _globalOptions = globalOptions ? globalOptions : { } ;
110
+ /** Whether ripple directive is initialized and the input bindings are set. */
111
+ private _isInitialized : boolean = false ;
109
112
110
- this . _updateRippleRenderer ( ) ;
111
- }
113
+ constructor ( private _elementRef : ElementRef ,
114
+ ngZone : NgZone ,
115
+ platform : Platform ,
116
+ @Optional ( ) @Inject ( MAT_RIPPLE_GLOBAL_OPTIONS ) globalOptions : RippleGlobalOptions ) {
112
117
113
- ngOnChanges ( changes : SimpleChanges ) {
114
- if ( changes [ 'trigger' ] && this . trigger ) {
115
- this . _rippleRenderer . setTriggerElement ( this . trigger ) ;
116
- }
118
+ this . _globalOptions = globalOptions || { } ;
119
+ this . _rippleRenderer = new RippleRenderer ( this , ngZone , _elementRef , platform ) ;
120
+ }
117
121
118
- this . _updateRippleRenderer ( ) ;
122
+ ngOnInit ( ) {
123
+ this . _isInitialized = true ;
124
+ this . _setupTriggerEventsIfEnabled ( ) ;
119
125
}
120
126
121
127
ngOnDestroy ( ) {
122
- // Set the trigger element to null to cleanup all listeners.
123
- this . _rippleRenderer . setTriggerElement ( null ) ;
128
+ this . _rippleRenderer . _removeTriggerEvents ( ) ;
124
129
}
125
130
126
131
/** Launches a manual ripple at the specified position. */
127
- launch ( x : number , y : number , config : RippleConfig = this . rippleConfig ) : RippleRef {
132
+ launch ( x : number , y : number , config : RippleConfig = this ) : RippleRef {
128
133
return this . _rippleRenderer . fadeInRipple ( x , y , config ) ;
129
134
}
130
135
@@ -143,9 +148,16 @@ export class MatRipple implements OnChanges, OnDestroy {
143
148
} ;
144
149
}
145
150
146
- /** Updates the ripple renderer with the latest ripple configuration. */
147
- _updateRippleRenderer ( ) {
148
- this . _rippleRenderer . rippleDisabled = this . _globalOptions . disabled || this . disabled ;
149
- this . _rippleRenderer . rippleConfig = this . rippleConfig ;
151
+ /** Whether ripples on pointer-down are disabled or not. */
152
+ get rippleDisabled ( ) : boolean {
153
+ return this . disabled || ! ! this . _globalOptions . disabled ;
154
+ }
155
+
156
+ /** Sets up the the trigger event listeners if ripples are enabled. */
157
+ private _setupTriggerEventsIfEnabled ( ) {
158
+ if ( ! this . disabled && this . _isInitialized ) {
159
+ this . _rippleRenderer . setupTriggerEvents ( this . trigger ) ;
160
+ }
150
161
}
151
162
}
163
+
0 commit comments