-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ripple.js
463 lines (391 loc) · 13.1 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
import { element, getjQuery, typeCheckConfig, onDOMContentLoaded } from '../mdb/util/index';
import Data from '../mdb/dom/data';
import EventHandler from '../mdb/dom/event-handler';
import Manipulator from '../mdb/dom/manipulator';
import SelectorEngine from '../mdb/dom/selector-engine';
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'ripple';
const DATA_KEY = 'mdb.ripple';
const CLASSNAME_RIPPLE = 'ripple-surface';
const CLASSNAME_RIPPLE_WAVE = 'ripple-wave';
const CLASSNAME_RIPPLE_WRAPPER = 'input-wrapper';
const SELECTOR_COMPONENT = ['.btn', '.ripple'];
const CLASSNAME_UNBOUND = 'ripple-surface-unbound';
const GRADIENT =
'rgba({{color}}, 0.2) 0, rgba({{color}}, 0.3) 40%, rgba({{color}}, 0.4) 50%, rgba({{color}}, 0.5) 60%, rgba({{color}}, 0) 70%';
const DEFAULT_RIPPLE_COLOR = [0, 0, 0];
const BOOTSTRAP_COLORS = [
'primary',
'secondary',
'success',
'danger',
'warning',
'info',
'light',
'dark',
];
// Sets value when run opacity transition
// Hide element after 50% (0.5) time of animation and finish on 100%
const TRANSITION_BREAK_OPACITY = 0.5;
const Default = {
rippleCentered: false,
rippleColor: '',
rippleDuration: '500ms',
rippleRadius: 0,
rippleUnbound: false,
};
const DefaultType = {
rippleCentered: 'boolean',
rippleColor: 'string',
rippleDuration: 'string',
rippleRadius: 'number',
rippleUnbound: 'boolean',
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Ripple {
constructor(element, options) {
this._element = element;
this._options = this._getConfig(options);
if (this._element) {
Data.setData(element, DATA_KEY, this);
Manipulator.addClass(this._element, CLASSNAME_RIPPLE);
}
this._clickHandler = this._createRipple.bind(this);
this._rippleTimer = null;
this._isMinWidthSet = false;
this._rippleInSpan = false;
this.init();
}
// Getters
static get NAME() {
return NAME;
}
// Public
init() {
this._addClickEvent(this._element);
}
dispose() {
Data.removeData(this._element, DATA_KEY);
EventHandler.off(this._element, 'click', this._clickHandler);
this._element = null;
this._options = null;
}
// Private
_autoInit(event) {
SELECTOR_COMPONENT.forEach((selector) => {
const target = SelectorEngine.closest(event.target, selector);
if (target) {
this._element = SelectorEngine.closest(event.target, selector);
}
});
this._options = this._getConfig();
if (this._element.tagName.toLowerCase() === 'input') {
const parent = this._element.parentNode;
this._rippleInSpan = true;
if (parent.tagName.toLowerCase() === 'span' && parent.classList.contains(CLASSNAME_RIPPLE)) {
this._element = parent;
} else {
const shadow = getComputedStyle(this._element).boxShadow;
const btn = this._element;
const wrapper = document.createElement('span');
if (btn.classList.contains('btn-block')) {
wrapper.style.display = 'block';
}
EventHandler.one(wrapper, 'mouseup', (e) => {
// prevent submit on click other than LMB, ripple still triggered, but submit is blocked
if (e.button === 0) {
btn.click();
}
});
wrapper.classList.add(CLASSNAME_RIPPLE, CLASSNAME_RIPPLE_WRAPPER);
Manipulator.addStyle(wrapper, {
border: 0,
'box-shadow': shadow,
});
// Put element as child
parent.replaceChild(wrapper, this._element);
wrapper.appendChild(this._element);
this._element = wrapper;
}
this._element.focus();
}
if (!this._element.style.minWidth) {
Manipulator.style(this._element, { 'min-width': `${getComputedStyle(this._element).width}` });
this._isMinWidthSet = true;
}
Manipulator.addClass(this._element, CLASSNAME_RIPPLE);
this._createRipple(event);
}
_addClickEvent(target) {
EventHandler.on(target, 'mousedown', this._clickHandler);
}
_getEventLayer(event) {
const x = Math.round(event.clientX - event.target.getBoundingClientRect().x);
const y = Math.round(event.clientY - event.target.getBoundingClientRect().y);
return { layerX: x, layerY: y };
}
_createRipple(event) {
if (!Manipulator.hasClass(this._element, CLASSNAME_RIPPLE)) {
Manipulator.addClass(this._element, CLASSNAME_RIPPLE);
}
const { layerX, layerY } = this._getEventLayer(event);
const offsetX = layerX;
const offsetY = layerY;
const height = this._element.offsetHeight;
const width = this._element.offsetWidth;
const duration = this._durationToMsNumber(this._options.rippleDuration);
const diameterOptions = {
offsetX: this._options.rippleCentered ? height / 2 : offsetX,
offsetY: this._options.rippleCentered ? width / 2 : offsetY,
height,
width,
};
const diameter = this._getDiameter(diameterOptions);
const radiusValue = this._options.rippleRadius || diameter / 2;
const opacity = {
delay: duration * TRANSITION_BREAK_OPACITY,
duration: duration - duration * TRANSITION_BREAK_OPACITY,
};
const styles = {
left: this._options.rippleCentered
? `${width / 2 - radiusValue}px`
: `${offsetX - radiusValue}px`,
top: this._options.rippleCentered
? `${height / 2 - radiusValue}px`
: `${offsetY - radiusValue}px`,
height: `${this._options.rippleRadius * 2 || diameter}px`,
width: `${this._options.rippleRadius * 2 || diameter}px`,
transitionDelay: `0s, ${opacity.delay}ms`,
transitionDuration: `${duration}ms, ${opacity.duration}ms`,
};
const rippleHTML = element('div');
this._createHTMLRipple({ wrapper: this._element, ripple: rippleHTML, styles });
this._removeHTMLRipple({ ripple: rippleHTML, duration });
}
_createHTMLRipple({ wrapper, ripple, styles }) {
Object.keys(styles).forEach((property) => (ripple.style[property] = styles[property]));
ripple.classList.add(CLASSNAME_RIPPLE_WAVE);
if (this._options.rippleColor !== '') {
this._removeOldColorClasses(wrapper);
this._addColor(ripple, wrapper);
}
this._toggleUnbound(wrapper);
this._appendRipple(ripple, wrapper);
}
_removeHTMLRipple({ ripple, duration }) {
if (this._rippleTimer) {
clearTimeout(this._rippleTimer);
this._rippleTimer = null;
}
this._rippleTimer = setTimeout(() => {
if (ripple) {
ripple.remove();
if (this._element) {
SelectorEngine.find(`.${CLASSNAME_RIPPLE_WAVE}`, this._element).forEach((rippleEl) => {
rippleEl.remove();
});
if (this._isMinWidthSet) {
Manipulator.style(this._element, { 'min-width': '' });
this._isMinWidthSet = false;
}
if (this._rippleInSpan && this._element.classList.contains(CLASSNAME_RIPPLE_WRAPPER)) {
this._removeWrapperSpan();
} else {
Manipulator.removeClass(this._element, CLASSNAME_RIPPLE);
}
}
}
}, duration);
}
_removeWrapperSpan() {
const child = this._element.firstChild;
this._element.replaceWith(child);
this._element = child;
this._element.focus();
this._rippleInSpan = false;
}
_durationToMsNumber(time) {
return Number(time.replace('ms', '').replace('s', '000'));
}
_getConfig(config = {}) {
const dataAttributes = Manipulator.getDataAttributes(this._element);
config = {
...Default,
...dataAttributes,
...config,
};
typeCheckConfig(NAME, config, DefaultType);
return config;
}
_getDiameter({ offsetX, offsetY, height, width }) {
const top = offsetY <= height / 2;
const left = offsetX <= width / 2;
const pythagorean = (sideA, sideB) => Math.sqrt(sideA ** 2 + sideB ** 2);
const positionCenter = offsetY === height / 2 && offsetX === width / 2;
// mouse position on the quadrants of the coordinate system
const quadrant = {
first: top === true && left === false,
second: top === true && left === true,
third: top === false && left === true,
fourth: top === false && left === false,
};
const getCorner = {
topLeft: pythagorean(offsetX, offsetY),
topRight: pythagorean(width - offsetX, offsetY),
bottomLeft: pythagorean(offsetX, height - offsetY),
bottomRight: pythagorean(width - offsetX, height - offsetY),
};
let diameter = 0;
if (positionCenter || quadrant.fourth) {
diameter = getCorner.topLeft;
} else if (quadrant.third) {
diameter = getCorner.topRight;
} else if (quadrant.second) {
diameter = getCorner.bottomRight;
} else if (quadrant.first) {
diameter = getCorner.bottomLeft;
}
return diameter * 2;
}
_appendRipple(target, parent) {
const FIX_ADD_RIPPLE_EFFECT = 50; // delay for active animations
parent.appendChild(target);
setTimeout(() => {
Manipulator.addClass(target, 'active');
}, FIX_ADD_RIPPLE_EFFECT);
}
_toggleUnbound(target) {
if (this._options.rippleUnbound === true) {
Manipulator.addClass(target, CLASSNAME_UNBOUND);
} else {
target.classList.remove(CLASSNAME_UNBOUND);
}
}
_addColor(target, parent) {
const IS_BOOTSTRAP_COLOR = BOOTSTRAP_COLORS.find(
(color) => color === this._options.rippleColor.toLowerCase()
);
if (IS_BOOTSTRAP_COLOR) {
Manipulator.addClass(
parent,
`${CLASSNAME_RIPPLE}-${this._options.rippleColor.toLowerCase()}`
);
} else {
const rgbValue = this._colorToRGB(this._options.rippleColor).join(',');
const gradientImage = GRADIENT.split('{{color}}').join(`${rgbValue}`);
target.style.backgroundImage = `radial-gradient(circle, ${gradientImage})`;
}
}
_removeOldColorClasses(target) {
const REGEXP_CLASS_COLOR = new RegExp(`${CLASSNAME_RIPPLE}-[a-z]+`, 'gi');
const PARENT_CLASSS_COLOR = target.classList.value.match(REGEXP_CLASS_COLOR) || [];
PARENT_CLASSS_COLOR.forEach((className) => {
target.classList.remove(className);
});
}
_colorToRGB(color) {
function hexToRgb(color) {
const HEX_COLOR_LENGTH = 7;
const IS_SHORT_HEX = color.length < HEX_COLOR_LENGTH;
if (IS_SHORT_HEX) {
color = `#${color[1]}${color[1]}${color[2]}${color[2]}${color[3]}${color[3]}`;
}
return [
parseInt(color.substr(1, 2), 16),
parseInt(color.substr(3, 2), 16),
parseInt(color.substr(5, 2), 16),
];
}
function namedColorsToRgba(color) {
const tempElem = document.body.appendChild(document.createElement('fictum'));
const flag = 'rgb(1, 2, 3)';
tempElem.style.color = flag;
if (tempElem.style.color !== flag) {
return DEFAULT_RIPPLE_COLOR;
}
tempElem.style.color = color;
if (tempElem.style.color === flag || tempElem.style.color === '') {
return DEFAULT_RIPPLE_COLOR;
} // color parse failed
color = getComputedStyle(tempElem).color;
document.body.removeChild(tempElem);
return color;
}
function rgbaToRgb(color) {
color = color.match(/[.\d]+/g).map((a) => +Number(a));
color.length = 3;
return color;
}
if (color.toLowerCase() === 'transparent') {
return DEFAULT_RIPPLE_COLOR;
}
if (color[0] === '#') {
return hexToRgb(color);
}
if (color.indexOf('rgb') === -1) {
color = namedColorsToRgba(color);
}
if (color.indexOf('rgb') === 0) {
return rgbaToRgb(color);
}
return DEFAULT_RIPPLE_COLOR;
}
// Static
static autoInitial(instance) {
return function (event) {
instance._autoInit(event);
};
}
static jQueryInterface(options) {
return this.each(function () {
const data = Data.getData(this, DATA_KEY);
if (!data) {
return new Ripple(this, options);
}
return null;
});
}
static getInstance(element) {
return Data.getData(element, DATA_KEY);
}
static getOrCreateInstance(element, config = {}) {
return (
this.getInstance(element) || new this(element, typeof config === 'object' ? config : null)
);
}
}
/**
* ------------------------------------------------------------------------
* Data Api implementation - auto initialization
* ------------------------------------------------------------------------
*/
SELECTOR_COMPONENT.forEach((selector) => {
EventHandler.one(document, 'mousedown', selector, Ripple.autoInitial(new Ripple()));
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
* add .ripple to jQuery only if jQuery is present
*/
onDOMContentLoaded(() => {
const $ = getjQuery();
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME];
$.fn[NAME] = Ripple.jQueryInterface;
$.fn[NAME].Constructor = Ripple;
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Ripple.jQueryInterface;
};
}
});
export default Ripple;