This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ripple.js
470 lines (414 loc) · 13.6 KB
/
ripple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* @ngdoc module
* @name material.core.ripple
* @description
* Ripple
*/
angular.module('material.core')
.provider('$mdInkRipple', InkRippleProvider)
.directive('mdInkRipple', InkRippleDirective)
.directive('mdNoInk', attrNoDirective)
.directive('mdNoBar', attrNoDirective)
.directive('mdNoStretch', attrNoDirective);
var DURATION = 450;
/**
* @ngdoc directive
* @name mdInkRipple
* @module material.core.ripple
*
* @description
* The `md-ink-ripple` directive allows you to specify the ripple color or if a ripple is allowed.
*
* @param {string|boolean} md-ink-ripple A color string `#FF0000` or boolean (`false` or `0`) for
* preventing ripple
*
* @usage
* ### String values
* <hljs lang="html">
* <ANY md-ink-ripple="#FF0000">
* Ripples in red
* </ANY>
*
* <ANY md-ink-ripple="false">
* Not rippling
* </ANY>
* </hljs>
*
* ### Interpolated values
* <hljs lang="html">
* <ANY md-ink-ripple="{{ randomColor() }}">
* Ripples with the return value of 'randomColor' function
* </ANY>
*
* <ANY md-ink-ripple="{{ canRipple() }}">
* Ripples if 'canRipple' function return value is not 'false' or '0'
* </ANY>
* </hljs>
*/
function InkRippleDirective ($mdButtonInkRipple, $mdCheckboxInkRipple) {
return {
controller: angular.noop,
link: function (scope, element, attr) {
attr.hasOwnProperty('mdInkRippleCheckbox')
? $mdCheckboxInkRipple.attach(scope, element)
: $mdButtonInkRipple.attach(scope, element);
}
};
}
/**
* @ngdoc service
* @name $mdInkRipple
* @module material.core.ripple
*
* @description
* `$mdInkRipple` is a service for adding ripples to any element.
*
* @usage
* <hljs lang="js">
* app.factory('$myElementInkRipple', function($mdInkRipple) {
* return {
* attach: function (scope, element, options) {
* return $mdInkRipple.attach(scope, element, angular.extend({
* center: false,
* dimBackground: true
* }, options));
* }
* };
* });
*
* app.controller('myController', function ($scope, $element, $myElementInkRipple) {
* $scope.onClick = function (ev) {
* $myElementInkRipple.attach($scope, angular.element(ev.target), { center: true });
* }
* });
* </hljs>
*/
/**
* @ngdoc service
* @name $mdInkRippleProvider
* @module material.core.ripple
*
* @description
* If you want to disable ink ripples globally, for all components, you can call the
* `disableInkRipple` method in your app's config.
*
*
* @usage
* <hljs lang="js">
* app.config(function ($mdInkRippleProvider) {
* $mdInkRippleProvider.disableInkRipple();
* });
* </hljs>
*/
function InkRippleProvider () {
var isDisabledGlobally = false;
return {
disableInkRipple: disableInkRipple,
$get: function($injector) {
return { attach: attach };
/**
* @ngdoc method
* @name $mdInkRipple#attach
*
* @description
* Attaching given scope, element and options to inkRipple controller
*
* @param {object=} scope Scope within the current context
* @param {object=} element The element the ripple effect should be applied to
* @param {object=} options (Optional) Configuration options to override the defaultRipple configuration
* * `center` - Whether the ripple should start from the center of the container element
* * `dimBackground` - Whether the background should be dimmed with the ripple color
* * `colorElement` - The element the ripple should take its color from, defined by css property `color`
* * `fitRipple` - Whether the ripple should fill the element
*/
function attach (scope, element, options) {
if (isDisabledGlobally || element.controller('mdNoInk')) return angular.noop;
return $injector.instantiate(InkRippleCtrl, {
$scope: scope,
$element: element,
rippleOptions: options
});
}
}
};
/**
* @ngdoc method
* @name $mdInkRippleProvider#disableInkRipple
*
* @description
* A config-time method that, when called, disables ripples globally.
*/
function disableInkRipple () {
isDisabledGlobally = true;
}
}
/**
* Controller used by the ripple service in order to apply ripples
* @ngInject
*/
function InkRippleCtrl ($scope, $element, rippleOptions, $window, $timeout, $mdUtil, $mdColorUtil) {
this.$window = $window;
this.$timeout = $timeout;
this.$mdUtil = $mdUtil;
this.$mdColorUtil = $mdColorUtil;
this.$scope = $scope;
this.$element = $element;
this.options = rippleOptions;
this.mousedown = false;
this.ripples = [];
this.timeout = null; // Stores a reference to the most-recent ripple timeout
this.lastRipple = null;
$mdUtil.valueOnUse(this, 'container', this.createContainer);
this.$element.addClass('md-ink-ripple');
// attach method for unit tests
($element.controller('mdInkRipple') || {}).createRipple = angular.bind(this, this.createRipple);
($element.controller('mdInkRipple') || {}).setColor = angular.bind(this, this.color);
this.bindEvents();
}
/**
* Either remove or unlock any remaining ripples when the user mouses off of the element (either by
* mouseup or mouseleave event)
*/
function autoCleanup (self, cleanupFn) {
if (self.mousedown || self.lastRipple) {
self.mousedown = false;
self.$mdUtil.nextTick(angular.bind(self, cleanupFn), false);
}
}
/**
* Returns the color that the ripple should be (either based on CSS or hard-coded)
* @returns {string}
*/
InkRippleCtrl.prototype.color = function (value) {
var self = this;
// If assigning a color value, apply it to background and the ripple color
if (angular.isDefined(value)) {
self._color = self._parseColor(value);
}
// If color lookup, use assigned, defined, or inherited
return self._color || self._parseColor(self.inkRipple()) || self._parseColor(getElementColor());
/**
* Finds the color element and returns its text color for use as default ripple color
* @returns {string}
*/
function getElementColor () {
var items = self.options && self.options.colorElement ? self.options.colorElement : [];
var elem = items.length ? items[ 0 ] : self.$element[ 0 ];
return elem ? self.$window.getComputedStyle(elem).color : 'rgb(0,0,0)';
}
};
/**
* Updating the ripple colors based on the current inkRipple value
* or the element's computed style color
*/
InkRippleCtrl.prototype.calculateColor = function () {
return this.color();
};
/**
* Takes a string color and converts it to RGBA format
* @param {string} color
* @param {number} multiplier
* @returns {string}
*/
InkRippleCtrl.prototype._parseColor = function parseColor (color, multiplier) {
multiplier = multiplier || 1;
var colorUtil = this.$mdColorUtil;
if (!color) return;
if (color.indexOf('rgba') === 0) return color.replace(/\d?\.?\d*\s*\)\s*$/, (0.1 * multiplier).toString() + ')');
if (color.indexOf('rgb') === 0) return colorUtil.rgbToRgba(color);
if (color.indexOf('#') === 0) return colorUtil.hexToRgba(color);
};
/**
* Binds events to the root element for
*/
InkRippleCtrl.prototype.bindEvents = function () {
this.$element.on('mousedown', angular.bind(this, this.handleMousedown));
this.$element.on('mouseup touchend', angular.bind(this, this.handleMouseup));
this.$element.on('mouseleave', angular.bind(this, this.handleMouseup));
this.$element.on('touchmove', angular.bind(this, this.handleTouchmove));
};
/**
* Create a new ripple on every mousedown event from the root element
* @param event {MouseEvent}
*/
InkRippleCtrl.prototype.handleMousedown = function (event) {
if (this.mousedown) return;
// When jQuery is loaded, we have to get the original event
if (event.hasOwnProperty('originalEvent')) event = event.originalEvent;
this.mousedown = true;
if (this.options.center) {
this.createRipple(this.container.prop('clientWidth') / 2, this.container.prop('clientWidth') / 2);
} else {
// We need to calculate the relative coordinates if the target is a sublayer of the ripple element
if (event.srcElement !== this.$element[0]) {
var layerRect = this.$element[0].getBoundingClientRect();
var layerX = event.clientX - layerRect.left;
var layerY = event.clientY - layerRect.top;
this.createRipple(layerX, layerY);
} else {
this.createRipple(event.offsetX, event.offsetY);
}
}
};
/**
* Either remove or unlock any remaining ripples when the user mouses off of the element (either by
* mouseup, touchend or mouseleave event)
*/
InkRippleCtrl.prototype.handleMouseup = function () {
this.$timeout(function () {
autoCleanup(this, this.clearRipples);
}.bind(this));
};
/**
* Either remove or unlock any remaining ripples when the user mouses off of the element (by
* touchmove)
*/
InkRippleCtrl.prototype.handleTouchmove = function () {
autoCleanup(this, this.deleteRipples);
};
/**
* Cycles through all ripples and attempts to remove them.
*/
InkRippleCtrl.prototype.deleteRipples = function () {
for (var i = 0; i < this.ripples.length; i++) {
this.ripples[ i ].remove();
}
};
/**
* Cycles through all ripples and attempts to remove them with fade.
* Depending on logic within `fadeInComplete`, some removals will be postponed.
*/
InkRippleCtrl.prototype.clearRipples = function () {
for (var i = 0; i < this.ripples.length; i++) {
this.fadeInComplete(this.ripples[ i ]);
}
};
/**
* Creates the ripple container element
* @returns {*}
*/
InkRippleCtrl.prototype.createContainer = function () {
var container = angular.element('<div class="md-ripple-container"></div>');
this.$element.append(container);
return container;
};
InkRippleCtrl.prototype.clearTimeout = function () {
if (this.timeout) {
this.$timeout.cancel(this.timeout);
this.timeout = null;
}
};
InkRippleCtrl.prototype.isRippleAllowed = function () {
var element = this.$element[0];
do {
if (!element.tagName || element.tagName === 'BODY') break;
if (element && angular.isFunction(element.hasAttribute)) {
if (element.hasAttribute('disabled')) return false;
if (this.inkRipple() === 'false' || this.inkRipple() === '0') return false;
}
} while (element = element.parentNode);
return true;
};
/**
* The attribute `md-ink-ripple` may be a static or interpolated
* color value OR a boolean indicator (used to disable ripples)
*/
InkRippleCtrl.prototype.inkRipple = function () {
return this.$element.attr('md-ink-ripple');
};
/**
* Creates a new ripple and adds it to the container. Also tracks ripple in `this.ripples`.
* @param left
* @param top
*/
InkRippleCtrl.prototype.createRipple = function (left, top) {
if (!this.isRippleAllowed()) return;
var ctrl = this;
var colorUtil = ctrl.$mdColorUtil;
var ripple = angular.element('<div class="md-ripple"></div>');
var width = this.$element.prop('clientWidth');
var height = this.$element.prop('clientHeight');
var x = Math.max(Math.abs(width - left), left) * 2;
var y = Math.max(Math.abs(height - top), top) * 2;
var size = getSize(this.options.fitRipple, x, y);
var color = this.calculateColor();
ripple.css({
left: left + 'px',
top: top + 'px',
background: 'black',
width: size + 'px',
height: size + 'px',
backgroundColor: colorUtil.rgbaToRgb(color),
borderColor: colorUtil.rgbaToRgb(color)
});
this.lastRipple = ripple;
// we only want one timeout to be running at a time
this.clearTimeout();
this.timeout = this.$timeout(function () {
ctrl.clearTimeout();
if (!ctrl.mousedown) ctrl.fadeInComplete(ripple);
}, DURATION * 0.35, false);
if (this.options.dimBackground) this.container.css({ backgroundColor: color });
this.container.append(ripple);
this.ripples.push(ripple);
ripple.addClass('md-ripple-placed');
this.$mdUtil.nextTick(function () {
ripple.addClass('md-ripple-scaled md-ripple-active');
ctrl.$timeout(function () {
ctrl.clearRipples();
}, DURATION, false);
}, false);
function getSize (fit, x, y) {
return fit
? Math.max(x, y)
: Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
};
/**
* After fadeIn finishes, either kicks off the fade-out animation or queues the element for removal on mouseup
* @param ripple
*/
InkRippleCtrl.prototype.fadeInComplete = function (ripple) {
if (this.lastRipple === ripple) {
if (!this.timeout && !this.mousedown) {
this.removeRipple(ripple);
}
} else {
this.removeRipple(ripple);
}
};
/**
* Kicks off the animation for removing a ripple
* @param ripple {Element}
*/
InkRippleCtrl.prototype.removeRipple = function (ripple) {
var ctrl = this;
var index = this.ripples.indexOf(ripple);
if (index < 0) return;
this.ripples.splice(this.ripples.indexOf(ripple), 1);
ripple.removeClass('md-ripple-active');
ripple.addClass('md-ripple-remove');
if (this.ripples.length === 0) this.container.css({ backgroundColor: '' });
// use a 2-second timeout in order to allow for the animation to finish
// we don't actually care how long the animation takes
this.$timeout(function () {
ctrl.fadeOutComplete(ripple);
}, DURATION, false);
};
/**
* Removes the provided ripple from the DOM
* @param ripple
*/
InkRippleCtrl.prototype.fadeOutComplete = function (ripple) {
ripple.remove();
this.lastRipple = null;
};
/**
* Used to create an empty directive. This is used to track flag-directives whose children may have
* functionality based on them.
*
* Example: `md-no-ink` will potentially be used by all child directives.
*/
function attrNoDirective () {
return { controller: angular.noop };
}