mdb-ui-kit/js/ripples.js

333 lines
8.9 KiB
JavaScript
Raw Normal View History

2017-08-11 17:59:31 +03:00
import Util from "./util";
2017-08-11 17:59:31 +03:00
const Ripples = ($ => {
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2017-08-11 17:59:31 +03:00
const NAME = "ripples";
const DATA_KEY = `bmd.${NAME}`;
const JQUERY_NAME = `bmd${NAME.charAt(0).toUpperCase() + NAME.slice(1)}`;
const JQUERY_NO_CONFLICT = $.fn[JQUERY_NAME];
const ClassName = {
2017-08-11 17:59:31 +03:00
CONTAINER: "ripple-container",
DECORATOR: "ripple-decorator"
};
const Selector = {
CONTAINER: `.${ClassName.CONTAINER}`,
DECORATOR: `.${ClassName.DECORATOR}` //,
2017-08-11 17:59:31 +03:00
};
const Default = {
container: {
template: `<div class='${ClassName.CONTAINER}'></div>`
},
decorator: {
template: `<div class='${ClassName.DECORATOR}'></div>`
},
trigger: {
2017-08-11 17:59:31 +03:00
start: "mousedown touchstart",
end: "mouseup mouseleave touchend"
},
touchUserAgentRegex: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i,
duration: 500
2017-08-11 17:59:31 +03:00
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Ripples {
constructor($element, config) {
2017-08-11 17:59:31 +03:00
this.$element = $element;
// console.log(`Adding ripples to ${Util.describe(this.$element)}`) // eslint-disable-line no-console
2017-08-11 17:59:31 +03:00
this.config = $.extend(true, {}, Default, config);
// attach initial listener
2017-08-11 17:59:31 +03:00
this.$element.on(this.config.trigger.start, event => {
this._onStartRipple(event);
});
}
dispose() {
2017-08-11 17:59:31 +03:00
this.$element.data(DATA_KEY, null);
this.$element = null;
this.$container = null;
this.$decorator = null;
this.config = null;
}
// ------------------------------------------------------------------------
// private
_onStartRipple(event) {
// Verify if the user is just touching on a device and return if so
2017-08-11 17:59:31 +03:00
if (this._isTouch() && event.type === "mousedown") {
return;
}
// Find or create the ripple container element
2017-08-11 17:59:31 +03:00
this._findOrCreateContainer();
// Get relY and relX positions of the container element
2017-08-11 17:59:31 +03:00
let relY = this._getRelY(event);
let relX = this._getRelX(event);
// If relY and/or relX are false, return the event
if (!relY && !relX) {
2017-08-11 17:59:31 +03:00
return;
}
// set the location and color each time (even if element is cached)
this.$decorator.css({
left: relX,
top: relY,
2017-08-11 17:59:31 +03:00
"background-color": this._getRipplesColor()
});
// Make sure the ripple has the styles applied (ugly hack but it works)
2017-08-11 17:59:31 +03:00
this._forceStyleApplication();
// Turn on the ripple animation
2017-08-11 17:59:31 +03:00
this.rippleOn();
// Call the rippleEnd function when the transition 'on' ends
setTimeout(() => {
2017-08-11 17:59:31 +03:00
this.rippleEnd();
}, this.config.duration);
// Detect when the user leaves the element to cleanup if not already done?
this.$element.on(this.config.trigger.end, () => {
2017-08-11 17:59:31 +03:00
if (this.$decorator) {
// guard against race condition/mouse attack
this.$decorator.data("mousedown", "off");
2017-08-11 17:59:31 +03:00
if (this.$decorator.data("animating") === "off") {
this.rippleOut();
}
}
2017-08-11 17:59:31 +03:00
});
}
_findOrCreateContainer() {
if (!this.$container || !this.$container.length > 0) {
2017-08-11 17:59:31 +03:00
this.$element.append(this.config.container.template);
this.$container = this.$element.find(Selector.CONTAINER);
}
// always add the rippleElement, it is always removed
2017-08-11 17:59:31 +03:00
this.$container.append(this.config.decorator.template);
this.$decorator = this.$container.find(Selector.DECORATOR);
}
// Make sure the ripple has the styles applied (ugly hack but it works)
_forceStyleApplication() {
2017-08-11 17:59:31 +03:00
return window.getComputedStyle(this.$decorator[0]).opacity;
}
/**
* Get the relX
*/
_getRelX(event) {
2017-08-11 17:59:31 +03:00
let wrapperOffset = this.$container.offset();
2017-08-11 17:59:31 +03:00
let result = null;
if (!this._isTouch()) {
// Get the mouse position relative to the ripple wrapper
2017-08-11 17:59:31 +03:00
result = event.pageX - wrapperOffset.left;
} else {
// Make sure the user is using only one finger and then get the touch
// position relative to the ripple wrapper
2017-08-11 17:59:31 +03:00
event = event.originalEvent;
if (event.touches.length === 1) {
2017-08-11 17:59:31 +03:00
result = event.touches[0].pageX - wrapperOffset.left;
} else {
2017-08-11 17:59:31 +03:00
result = false;
}
}
2017-08-11 17:59:31 +03:00
return result;
}
/**
* Get the relY
*/
_getRelY(event) {
2017-08-11 17:59:31 +03:00
let containerOffset = this.$container.offset();
let result = null;
if (!this._isTouch()) {
/**
* Get the mouse position relative to the ripple wrapper
*/
2017-08-11 17:59:31 +03:00
result = event.pageY - containerOffset.top;
} else {
/**
* Make sure the user is using only one finger and then get the touch
* position relative to the ripple wrapper
*/
2017-08-11 17:59:31 +03:00
event = event.originalEvent;
if (event.touches.length === 1) {
2017-08-11 17:59:31 +03:00
result = event.touches[0].pageY - containerOffset.top;
} else {
2017-08-11 17:59:31 +03:00
result = false;
}
}
2017-08-11 17:59:31 +03:00
return result;
}
/**
* Get the ripple color
*/
_getRipplesColor() {
2017-08-11 17:59:31 +03:00
let color = this.$element.data("ripple-color")
? this.$element.data("ripple-color")
: window.getComputedStyle(this.$element[0]).color;
return color;
}
/**
* Verify if the client is using a mobile device
*/
_isTouch() {
2017-08-11 17:59:31 +03:00
return this.config.touchUserAgentRegex.test(navigator.userAgent);
}
/**
* End the animation of the ripple
*/
rippleEnd() {
2017-08-11 17:59:31 +03:00
if (this.$decorator) {
// guard against race condition/mouse attack
this.$decorator.data("animating", "off");
2017-08-11 17:59:31 +03:00
if (this.$decorator.data("mousedown") === "off") {
this.rippleOut(this.$decorator);
}
}
}
/**
* Turn off the ripple effect
*/
rippleOut() {
2017-08-11 17:59:31 +03:00
this.$decorator.off();
if (Util.transitionEndSupported()) {
2017-08-11 17:59:31 +03:00
this.$decorator.addClass("ripple-out");
} else {
2017-08-11 17:59:31 +03:00
this.$decorator.animate({ opacity: 0 }, 100, () => {
this.$decorator.trigger("transitionend");
});
}
this.$decorator.on(Util.transitionEndSelector(), () => {
if (this.$decorator) {
2017-08-11 17:59:31 +03:00
this.$decorator.remove();
this.$decorator = null;
}
2017-08-11 17:59:31 +03:00
});
}
/**
* Turn on the ripple effect
*/
rippleOn() {
2017-08-11 17:59:31 +03:00
let size = this._getNewSize();
if (Util.transitionEndSupported()) {
this.$decorator
.css({
2017-08-11 17:59:31 +03:00
"-ms-transform": `scale(${size})`,
"-moz-transform": `scale(${size})`,
"-webkit-transform": `scale(${size})`,
transform: `scale(${size})`
})
2017-08-11 17:59:31 +03:00
.addClass("ripple-on")
.data("animating", "on")
.data("mousedown", "on");
} else {
2017-08-11 17:59:31 +03:00
this.$decorator.animate(
{
width:
Math.max(
this.$element.outerWidth(),
this.$element.outerHeight()
) * 2,
height:
Math.max(
this.$element.outerWidth(),
this.$element.outerHeight()
) * 2,
"margin-left":
Math.max(
this.$element.outerWidth(),
this.$element.outerHeight()
) * -1,
"margin-top":
Math.max(
this.$element.outerWidth(),
this.$element.outerHeight()
) * -1,
opacity: 0.2
},
this.config.duration,
() => {
this.$decorator.trigger("transitionend");
}
);
}
}
/**
* Get the new size based on the element height/width and the ripple width
*/
_getNewSize() {
2017-08-11 17:59:31 +03:00
return (
Math.max(this.$element.outerWidth(), this.$element.outerHeight()) /
this.$decorator.outerWidth() *
2.5
);
}
// ------------------------------------------------------------------------
// static
static _jQueryInterface(config) {
2017-08-11 17:59:31 +03:00
return this.each(function() {
let $element = $(this);
let data = $element.data(DATA_KEY);
if (!data) {
2017-08-11 17:59:31 +03:00
data = new Ripples($element, config);
$element.data(DATA_KEY, data);
}
2017-08-11 17:59:31 +03:00
});
}
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2017-08-11 17:59:31 +03:00
$.fn[JQUERY_NAME] = Ripples._jQueryInterface;
$.fn[JQUERY_NAME].Constructor = Ripples;
$.fn[JQUERY_NAME].noConflict = () => {
2017-08-11 17:59:31 +03:00
$.fn[JQUERY_NAME] = JQUERY_NO_CONFLICT;
return Ripples._jQueryInterface;
};
2017-08-11 17:59:31 +03:00
return Ripples;
})(jQuery);
2017-08-11 17:59:31 +03:00
export default Ripples;