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
/
Copy pathripple.js
395 lines (357 loc) · 11.8 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
(function() {
'use strict';
angular.module('material.core')
.factory('$mdInkRipple', InkRippleService)
.directive('mdInkRipple', InkRippleDirective)
.directive('mdNoInk', attrNoDirective())
.directive('mdNoBar', attrNoDirective())
.directive('mdNoStretch', attrNoDirective());
function InkRippleDirective($mdInkRipple) {
return {
controller: angular.noop,
link: function (scope, element, attr) {
if (attr.hasOwnProperty('mdInkRippleCheckbox')) {
$mdInkRipple.attachCheckboxBehavior(scope, element);
} else {
$mdInkRipple.attachButtonBehavior(scope, element);
}
}
};
}
function InkRippleService($window, $timeout) {
return {
attachButtonBehavior: attachButtonBehavior,
attachCheckboxBehavior: attachCheckboxBehavior,
attachTabBehavior: attachTabBehavior,
attach: attach
};
function attachButtonBehavior(scope, element, options) {
return attach(scope, element, angular.extend({
isFAB: element.hasClass('md-fab'),
isMenuItem: element.hasClass('md-menu-item'),
center: false,
dimBackground: true
}, options));
}
function attachCheckboxBehavior(scope, element, options) {
return attach(scope, element, angular.extend({
center: true,
dimBackground: false
}, options));
}
function attachTabBehavior(scope, element, options) {
return attach(scope, element, angular.extend({
center: false,
dimBackground: true,
outline: true
}, options));
}
function attach(scope, element, options) {
if (element.controller('mdNoInk')) return angular.noop;
options = angular.extend({
colorElement: element,
mousedown: true,
hover: true,
focus: true,
center: false,
mousedownPauseTime: 150,
dimBackground: false,
outline: false,
isFAB: false,
isMenuItem: false
}, options);
var rippleContainer, rippleSize,
controller = element.controller('mdInkRipple') || {},
counter = 0,
ripples = [],
states = [],
isActiveExpr = element.attr('md-highlight'),
isActive = false,
isHeld = false,
node = element[0],
hammertime = new Hammer(node),
color = parseColor(element.attr('md-ink-ripple')) || parseColor($window.getComputedStyle(options.colorElement[0]).color || 'rgb(0, 0, 0)');
options.mousedown && hammertime.on('hammer.input', onInput);
controller.createRipple = createRipple;
if (isActiveExpr) {
scope.$watch(isActiveExpr, function watchActive(newValue) {
isActive = newValue;
if (isActive && !ripples.length) {
$timeout(function () { createRipple(0, 0); }, 0, false);
}
angular.forEach(ripples, updateElement);
});
}
// Publish self-detach method if desired...
return function detach() {
hammertime.destroy();
rippleContainer && rippleContainer.remove();
};
function parseColor(color) {
if (!color) return;
if (color.indexOf('rgba') === 0) return color.replace(/\d?\.?\d*\s*\)\s*$/, '0.1)');
if (color.indexOf('rgb') === 0) return rgbToRGBA(color);
if (color.indexOf('#') === 0) return hexToRGBA(color);
/**
* Converts a hex value to an rgba string
*
* @param {string} hex value (3 or 6 digits) to be converted
*
* @returns {string} rgba color with 0.1 alpha
*/
function hexToRGBA(color) {
var hex = color.charAt(0) === '#' ? color.substr(1) : color,
dig = hex.length / 3,
red = hex.substr(0, dig),
grn = hex.substr(dig, dig),
blu = hex.substr(dig * 2);
if (dig === 1) {
red += red;
grn += grn;
blu += blu;
}
return 'rgba(' + parseInt(red, 16) + ',' + parseInt(grn, 16) + ',' + parseInt(blu, 16) + ',0.1)';
}
/**
* Converts rgb value to rgba string
*
* @param {string} rgb color string
*
* @returns {string} rgba color with 0.1 alpha
*/
function rgbToRGBA(color) {
return color.replace(')', ', 0.1)').replace('(', 'a(')
}
}
function removeElement(elem, wait) {
ripples.splice(ripples.indexOf(elem), 1);
if (ripples.length === 0) {
rippleContainer && rippleContainer.css({ backgroundColor: '' });
}
$timeout(function () { elem.remove(); }, wait, false);
}
function updateElement(elem) {
var index = ripples.indexOf(elem),
state = states[index] || {},
elemIsActive = ripples.length > 1 ? false : isActive,
elemIsHeld = ripples.length > 1 ? false : isHeld;
if (elemIsActive || state.animating || elemIsHeld) {
elem.addClass('md-ripple-visible');
} else if (elem) {
elem.removeClass('md-ripple-visible');
if (options.outline) {
elem.css({
width: rippleSize + 'px',
height: rippleSize + 'px',
marginLeft: (rippleSize * -1) + 'px',
marginTop: (rippleSize * -1) + 'px'
});
}
removeElement(elem, options.outline ? 450 : 650);
}
}
/**
* Creates a ripple at the provided coordinates
*
* @param {number} left cursor position
* @param {number} top cursor position
*
* @returns {angular.element} the generated ripple element
*/
function createRipple(left, top) {
color = parseColor(element.attr('md-ink-ripple')) || parseColor($window.getComputedStyle(options.colorElement[0]).color || 'rgb(0, 0, 0)');
var container = getRippleContainer(),
size = getRippleSize(left, top),
css = getRippleCss(size, left, top),
elem = getRippleElement(css),
index = ripples.indexOf(elem),
state = states[index] || {};
rippleSize = size;
state.animating = true;
$timeout(function () {
if (options.dimBackground) {
container.css({ backgroundColor: color });
}
elem.addClass('md-ripple-placed md-ripple-scaled');
if (options.outline) {
elem.css({
borderWidth: (size * 0.5) + 'px',
marginLeft: (size * -0.5) + 'px',
marginTop: (size * -0.5) + 'px'
});
} else {
elem.css({ left: '50%', top: '50%' });
}
updateElement(elem);
$timeout(function () {
state.animating = false;
updateElement(elem);
}, (options.outline ? 450 : 225), false);
}, 0, false);
return elem;
/**
* Creates the ripple element with the provided css
*
* @param {object} css properties to be applied
*
* @returns {angular.element} the generated ripple element
*/
function getRippleElement(css) {
var elem = angular.element('<div class="md-ripple" data-counter="' + counter++ + '">');
ripples.unshift(elem);
states.unshift({ animating: true });
container.append(elem);
css && elem.css(css);
return elem;
}
/**
* Calculate the ripple size
*
* @returns {number} calculated ripple diameter
*/
function getRippleSize(left, top) {
var width = container.prop('offsetWidth'),
height = container.prop('offsetHeight'),
multiplier, size, rect;
if (options.isMenuItem) {
size = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
} else if (options.outline) {
rect = node.getBoundingClientRect();
left -= rect.left;
top -= rect.top;
width = Math.max(left, width - left);
height = Math.max(top, height - top);
size = 2 * Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
} else {
multiplier = options.isFAB ? 1.1 : 0.8;
size = Math.max(width, height) * multiplier;
}
return size;
}
/**
* Generates the ripple css
*
* @param {number} the diameter of the ripple
* @param {number} the left cursor offset
* @param {number} the top cursor offset
*
* @returns {{backgroundColor: *, width: string, height: string, marginLeft: string, marginTop: string}}
*/
function getRippleCss(size, left, top) {
var rect,
css = {
backgroundColor: rgbaToRGB(color),
borderColor: rgbaToRGB(color),
width: size + 'px',
height: size + 'px'
};
if (options.outline) {
css.width = 0;
css.height = 0;
} else {
css.marginLeft = css.marginTop = (size * -0.5) + 'px';
}
if (options.center) {
css.left = css.top = '50%';
} else {
rect = node.getBoundingClientRect();
css.left = Math.round((left - rect.left) / container.prop('offsetWidth') * 100) + '%';
css.top = Math.round((top - rect.top) / container.prop('offsetHeight') * 100) + '%';
}
return css;
/**
* Converts rgba string to rgb, removing the alpha value
*
* @param {string} rgba color
*
* @returns {string} rgb color
*/
function rgbaToRGB(color) {
return color.replace('rgba', 'rgb').replace(/,[^\)\,]+\)/, ')');
}
}
/**
* Gets the current ripple container
* If there is no ripple container, it creates one and returns it
*
* @returns {angular.element} ripple container element
*/
function getRippleContainer() {
if (rippleContainer) return rippleContainer;
var container = rippleContainer = angular.element('<div class="md-ripple-container">');
element.append(container);
return container;
}
}
/**
* Handles user input start and stop events
*
* @param {event} event fired by hammer.js
*/
function onInput(ev) {
var ripple, index;
if (ev.eventType === Hammer.INPUT_START && ev.isFirst && isRippleAllowed()) {
ripple = createRipple(ev.center.x, ev.center.y);
isHeld = true;
} else if (ev.eventType === Hammer.INPUT_END && ev.isFinal) {
isHeld = false;
index = ripples.length - 1;
ripple = ripples[index];
$timeout(function () { updateElement(ripple); }, 0, false);
}
/**
* Determines if the ripple is allowed
*
* @returns {boolean} true if the ripple is allowed, false if not
*/
function isRippleAllowed() {
var parent = node.parentNode;
var grandparent = parent && parent.parentNode;
var ancestor = grandparent && grandparent.parentNode;
return !node.hasAttribute('disabled') &&
!(parent && parent.hasAttribute('disabled')) &&
!(grandparent && grandparent.hasAttribute('disabled')) &&
!(ancestor && ancestor.hasAttribute('disabled'));
}
}
}
}
/**
* noink/nobar/nostretch directive: make any element that has one of
* these attributes be given a controller, so that other directives can
* `require:` these and see if there is a `no<xxx>` parent attribute.
*
* @usage
* <hljs lang="html">
* <parent md-no-ink>
* <child detect-no>
* </child>
* </parent>
* </hljs>
*
* <hljs lang="js">
* myApp.directive('detectNo', function() {
* return {
* require: ['^?mdNoInk', ^?mdNoBar'],
* link: function(scope, element, attr, ctrls) {
* var noinkCtrl = ctrls[0];
* var nobarCtrl = ctrls[1];
* if (noInkCtrl) {
* alert("the md-no-ink flag has been specified on an ancestor!");
* }
* if (nobarCtrl) {
* alert("the md-no-bar flag has been specified on an ancestor!");
* }
* }
* };
* });
* </hljs>
*/
function attrNoDirective() {
return function() {
return {
controller: angular.noop
};
};
}
})();