Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit fbcc3ac

Browse files
EladBezalelThomasBurleson
authored andcommitted
feat(ripple): ink-ripple is now getting an interpolated value
Ink ripple attribute supported only colors. Now with interpolated value also boolean values are supported ``` html <div md-ink-ripple="{{myFunc()}}">yay</div> ``` fixes #5438. closes #5580.
1 parent 5ae3d4c commit fbcc3ac

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

src/core/services/ripple/ripple.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ function InkRippleCtrl ($scope, $element, rippleOptions, $window, $timeout, $mdU
5555
this.lastRipple = null;
5656

5757
$mdUtil.valueOnUse(this, 'container', this.createContainer);
58-
$mdUtil.valueOnUse(this, 'background', this.getColor, 0.5);
5958

60-
this.color = this.getColor(1);
6159
this.$element.addClass('md-ink-ripple');
6260

6361
// attach method for unit tests
6462
($element.controller('mdInkRipple') || {}).createRipple = angular.bind(this, this.createRipple);
65-
($element.controller('mdInkRipple') || {}).setColor = angular.bind(this, this.setColor);
63+
($element.controller('mdInkRipple') || {}).setColor = angular.bind(this, this.color);
6664

6765
this.bindEvents();
6866
}
@@ -86,20 +84,37 @@ function autoCleanup (self, cleanupFn) {
8684
* Returns the color that the ripple should be (either based on CSS or hard-coded)
8785
* @returns {string}
8886
*/
89-
InkRippleCtrl.prototype.getColor = function () {
90-
return this._parseColor(this.$element.attr('md-ink-ripple'))
91-
|| this._parseColor(getElementColor.call(this));
87+
InkRippleCtrl.prototype.color = function (value) {
88+
89+
// If assigning a color value, apply it to background and the ripple color
90+
if (angular.isDefined(value)) {
91+
this._color = this._parseColor(value);
92+
}
93+
94+
// If color lookup, use assigned, defined, or inherited
95+
return this._color || this._parseColor( this.inkRipple() ) || this._parseColor( getElementColor(this) );
9296

9397
/**
9498
* Finds the color element and returns its text color for use as default ripple color
9599
* @returns {string}
96100
*/
97-
function getElementColor () {
98-
var colorElement = this.options.colorElement && this.options.colorElement[ 0 ];
99-
colorElement = colorElement || this.$element[ 0 ];
100-
return colorElement ? this.$window.getComputedStyle(colorElement).color : 'rgb(0,0,0)';
101+
function getElementColor (self) {
102+
var items = self.options && self.options.colorElement ? self.options.colorElement : [];
103+
var elem = items.length ? items[ 0 ] : self.$element[ 0 ];
104+
105+
return elem ? self.$window.getComputedStyle(elem).color : 'rgb(0,0,0)';
101106
}
102107
};
108+
109+
/**
110+
* Updating the ripple colors based on the current inkRipple value
111+
* or the element's computed style color
112+
*/
113+
InkRippleCtrl.prototype.calculateColor = function () {
114+
return this.color();
115+
};
116+
117+
103118
/**
104119
* Takes a string color and converts it to RGBA format
105120
* @param color {string}
@@ -161,8 +176,6 @@ InkRippleCtrl.prototype.bindEvents = function () {
161176
InkRippleCtrl.prototype.handleMousedown = function (event) {
162177
if ( this.mousedown ) return;
163178

164-
this.setColor(window.getComputedStyle(this.$element[0])['color']);
165-
166179
// When jQuery is loaded, we have to get the original event
167180
if (event.hasOwnProperty('originalEvent')) event = event.originalEvent;
168181
this.mousedown = true;
@@ -229,11 +242,24 @@ InkRippleCtrl.prototype.isRippleAllowed = function () {
229242
var element = this.$element[0];
230243
do {
231244
if (!element.tagName || element.tagName === 'BODY') break;
232-
if (element && element.hasAttribute && element.hasAttribute('disabled')) return false;
245+
246+
if (element && angular.isFunction(element.hasAttribute)) {
247+
if (element.hasAttribute('disabled')) return false;
248+
if (this.inkRipple() === 'false' || this.inkRipple() === '0') return false;
249+
}
250+
233251
} while (element = element.parentNode);
234252
return true;
235253
};
236254

255+
/**
256+
* The attribute `md-ink-ripple` may be a static or interpolated
257+
* color value OR a boolean indicator (used to disable ripples)
258+
*/
259+
InkRippleCtrl.prototype.inkRipple = function () {
260+
return this.$element.attr('md-ink-ripple');
261+
};
262+
237263
/**
238264
* Creates a new ripple and adds it to the container. Also tracks ripple in `this.ripples`.
239265
* @param left
@@ -249,15 +275,16 @@ InkRippleCtrl.prototype.createRipple = function (left, top) {
249275
var x = Math.max(Math.abs(width - left), left) * 2;
250276
var y = Math.max(Math.abs(height - top), top) * 2;
251277
var size = getSize(this.options.fitRipple, x, y);
278+
var color = this.calculateColor();
252279

253280
ripple.css({
254281
left: left + 'px',
255282
top: top + 'px',
256283
background: 'black',
257284
width: size + 'px',
258285
height: size + 'px',
259-
backgroundColor: rgbaToRGB(this.color),
260-
borderColor: rgbaToRGB(this.color)
286+
backgroundColor: rgbaToRGB(color),
287+
borderColor: rgbaToRGB(color)
261288
});
262289
this.lastRipple = ripple;
263290

@@ -268,7 +295,7 @@ InkRippleCtrl.prototype.createRipple = function (left, top) {
268295
if (!ctrl.mousedown) ctrl.fadeInComplete(ripple);
269296
}, DURATION * 0.35, false);
270297

271-
if (this.options.dimBackground) this.container.css({ backgroundColor: this.background });
298+
if (this.options.dimBackground) this.container.css({ backgroundColor: color });
272299
this.container.append(ripple);
273300
this.ripples.push(ripple);
274301
ripple.addClass('md-ripple-placed');
@@ -295,9 +322,7 @@ InkRippleCtrl.prototype.createRipple = function (left, top) {
295322
}
296323
};
297324

298-
InkRippleCtrl.prototype.setColor = function (color) {
299-
this.color = this._parseColor(color);
300-
};
325+
301326

302327
/**
303328
* After fadeIn finishes, either kicks off the fade-out animation or queues the element for removal on mouseup

0 commit comments

Comments
 (0)