2015-11-30 23:57:40 +03:00
|
|
|
const Ripples = (($) => {
|
2015-11-21 02:50:50 +03:00
|
|
|
|
|
|
|
/**
|
2015-11-30 23:57:40 +03:00
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
2015-11-21 02:50:50 +03:00
|
|
|
*/
|
2015-11-30 23:57:40 +03:00
|
|
|
const NAME = 'ripples'
|
|
|
|
const DATA_KEY = `bmd.${NAME}`
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
const DEFAULT_OPTIONS = {}
|
|
|
|
|
2015-11-21 02:50:50 +03:00
|
|
|
/**
|
2015-11-30 23:57:40 +03:00
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
2015-11-21 02:50:50 +03:00
|
|
|
*/
|
2015-11-30 23:57:40 +03:00
|
|
|
class Ripples {
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
constructor(element, options) {
|
|
|
|
this._element = $(element)
|
|
|
|
this._options = $.extend({}, DEFAULT_OPTIONS, options)
|
2015-12-01 00:47:47 +03:00
|
|
|
this._element.on("mousedown touchstart", this._onStartRipple)
|
2015-11-30 23:57:40 +03:00
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
dispose() {
|
|
|
|
$.removeData(this._element, DATA_KEY)
|
|
|
|
this._element = null
|
2015-12-01 00:47:47 +03:00
|
|
|
this._containerElement = null
|
|
|
|
this._rippleElement = null
|
|
|
|
this._options = null
|
2015-11-30 23:57:40 +03:00
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// private
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
_onStartRipple(event) {
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
// Verify if the user is just touching on a device and return if so
|
|
|
|
if (this.isTouch() && event.type === "mousedown") {
|
|
|
|
return
|
2015-11-21 02:50:50 +03:00
|
|
|
}
|
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
// Find or create the ripple container element
|
|
|
|
this._findOrCreateContainer()
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
// Get relY and relX positions of the container element
|
|
|
|
let relY = this._getRelY(event)
|
|
|
|
let relX = this._getRelX(event)
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
// If relY and/or relX are false, return the event
|
|
|
|
if (!relY && !relX) {
|
|
|
|
return
|
2015-11-21 02:50:50 +03:00
|
|
|
}
|
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
// set the location and color each time (even if element is cached)
|
|
|
|
this._rippleElement.addClass("ripple").css({
|
2015-11-30 23:57:40 +03:00
|
|
|
"left": relX,
|
|
|
|
"top": relY,
|
2015-12-01 00:47:47 +03:00
|
|
|
"background-color": this._getRipplesColor()
|
2015-11-30 23:57:40 +03:00
|
|
|
})
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
// Make sure the ripple has the styles applied (ugly hack but it works)
|
2015-12-01 00:47:47 +03:00
|
|
|
this._forceStyleApplication()
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
// Turn on the ripple animation
|
2015-12-01 00:47:47 +03:00
|
|
|
this.rippleOn()
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
// Call the rippleEnd function when the transition "on" ends
|
|
|
|
setTimeout(() => {
|
2015-12-01 00:47:47 +03:00
|
|
|
this.rippleEnd()
|
2015-11-30 23:57:40 +03:00
|
|
|
}, 500)
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
// Detect when the user leaves the element (attach only when necessary for performance)
|
2015-11-30 23:57:40 +03:00
|
|
|
this._element.on("mouseup mouseleave touchend", () => {
|
2015-12-01 00:47:47 +03:00
|
|
|
this._rippleElement.data("mousedown", "off")
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
if (this._rippleElement.data("animating") === "off") {
|
|
|
|
this.rippleOut()
|
2015-11-30 23:57:40 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
_findOrCreateContainer() {
|
|
|
|
if (!this._containerElement || !this._containerElement.length > 0) {
|
|
|
|
this._element.append("<div class='ripple-container'><div class='ripple'></div></div>")
|
|
|
|
this._containerElement = this._element.find(".ripple-container")
|
|
|
|
this._rippleElement = this._containerElement.find("div.ripple")
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._containerElement
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the ripple has the styles applied (ugly hack but it works)
|
|
|
|
_forceStyleApplication() {
|
|
|
|
return window.getComputedStyle(this._rippleElement[0]).opacity
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* Get the relX
|
|
|
|
*/
|
2015-12-01 00:47:47 +03:00
|
|
|
_getRelX(event) {
|
|
|
|
let wrapperOffset = this._containerElement.offset()
|
2015-11-30 23:57:40 +03:00
|
|
|
|
|
|
|
let result = null
|
|
|
|
if (!this.isTouch()) {
|
|
|
|
// Get the mouse position relative to the ripple wrapper
|
|
|
|
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
|
|
|
|
event = event.originalEvent
|
|
|
|
|
|
|
|
if (event.touches.length === 1) {
|
|
|
|
result = event.touches[0].pageX - wrapperOffset.left
|
|
|
|
} else {
|
|
|
|
result = false
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
return result
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* Get the relY
|
|
|
|
*/
|
2015-12-01 00:47:47 +03:00
|
|
|
_getRelY(event) {
|
|
|
|
let containerOffset = this._containerElement.offset()
|
2015-11-30 23:57:40 +03:00
|
|
|
let result = null
|
|
|
|
|
|
|
|
if (!this.isTouch()) {
|
|
|
|
/**
|
|
|
|
* Get the mouse position relative to the ripple wrapper
|
|
|
|
*/
|
2015-12-01 00:47:47 +03:00
|
|
|
result = event.pageY - containerOffset.top
|
2015-11-30 23:57:40 +03:00
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Make sure the user is using only one finger and then get the touch
|
|
|
|
* position relative to the ripple wrapper
|
|
|
|
*/
|
|
|
|
event = event.originalEvent
|
|
|
|
|
|
|
|
if (event.touches.length === 1) {
|
2015-12-01 00:47:47 +03:00
|
|
|
result = event.touches[0].pageY - containerOffset.top
|
2015-11-30 23:57:40 +03:00
|
|
|
} else {
|
|
|
|
result = false
|
2015-11-21 02:50:50 +03:00
|
|
|
}
|
2015-11-30 23:57:40 +03:00
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
return result
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* Get the ripple color
|
|
|
|
*/
|
2015-12-01 00:47:47 +03:00
|
|
|
_getRipplesColor() {
|
|
|
|
let color = this._element.data("ripple-color") ? this._element.data("ripple-color") : window.getComputedStyle(this._element[0]).color
|
2015-11-30 23:57:40 +03:00
|
|
|
return color
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* Verify if the client browser has transistion support
|
|
|
|
*/
|
|
|
|
_hasTransitionSupport() {
|
|
|
|
let thisBody = document.body || document.documentElement
|
|
|
|
let thisStyle = thisBody.style
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
let support = (
|
|
|
|
thisStyle.transition !== undefined ||
|
|
|
|
thisStyle.WebkitTransition !== undefined ||
|
|
|
|
thisStyle.MozTransition !== undefined ||
|
|
|
|
thisStyle.MsTransition !== undefined ||
|
|
|
|
thisStyle.OTransition !== undefined
|
|
|
|
)
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
return support
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* Verify if the client is using a mobile device
|
|
|
|
*/
|
|
|
|
isTouch() {
|
|
|
|
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
|
2015-11-21 02:50:50 +03:00
|
|
|
}
|
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* End the animation of the ripple
|
|
|
|
*/
|
2015-12-01 00:47:47 +03:00
|
|
|
rippleEnd() {
|
|
|
|
this._rippleElement.data("animating", "off")
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
if (this._rippleElement.data("mousedown") === "off") {
|
|
|
|
this.rippleOut(this._rippleElement)
|
2015-11-21 02:50:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* Turn off the ripple effect
|
|
|
|
*/
|
2015-12-01 00:47:47 +03:00
|
|
|
rippleOut() {
|
|
|
|
this._rippleElement.off()
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
if (this._hasTransitionSupport()) {
|
2015-12-01 00:47:47 +03:00
|
|
|
this._rippleElement.addClass("ripple-out")
|
2015-11-30 23:57:40 +03:00
|
|
|
} else {
|
2015-12-01 00:47:47 +03:00
|
|
|
this._rippleElement.animate({ "opacity": 0 }, 100, () => {
|
|
|
|
this._rippleElement.trigger("transitionend")
|
2015-11-30 23:57:40 +03:00
|
|
|
})
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
this._rippleElement.on("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", () => {
|
|
|
|
this._rippleElement.remove()
|
2015-11-30 23:57:40 +03:00
|
|
|
})
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* Turn on the ripple effect
|
|
|
|
*/
|
2015-12-01 00:47:47 +03:00
|
|
|
rippleOn() {
|
|
|
|
let size = this._getNewSize()
|
2015-11-30 23:57:40 +03:00
|
|
|
|
|
|
|
if (this._hasTransitionSupport()) {
|
2015-12-01 00:47:47 +03:00
|
|
|
this._rippleElement
|
2015-11-30 23:57:40 +03:00
|
|
|
.css({
|
|
|
|
"-ms-transform": `scale(${size})`,
|
|
|
|
"-moz-transform": `scale(${size})`,
|
|
|
|
"-webkit-transform": `scale(${size})`,
|
|
|
|
"transform": `scale(${size})`
|
|
|
|
})
|
|
|
|
.addClass("ripple-on")
|
|
|
|
.data("animating", "on")
|
|
|
|
.data("mousedown", "on")
|
|
|
|
} else {
|
2015-12-01 00:47:47 +03:00
|
|
|
this._rippleElement.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),
|
2015-11-30 23:57:40 +03:00
|
|
|
"opacity": 0.2
|
|
|
|
}, 500, () => {
|
2015-12-01 00:47:47 +03:00
|
|
|
this._rippleElement.trigger("transitionend")
|
2015-11-30 23:57:40 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
/**
|
|
|
|
* Get the new size based on the element height/width and the ripple width
|
|
|
|
*/
|
2015-12-01 00:47:47 +03:00
|
|
|
_getNewSize() {
|
|
|
|
return (Math.max(this._element.outerWidth(), this._element.outerHeight()) / this._rippleElement.outerWidth()) * 2.5
|
2015-11-30 23:57:40 +03:00
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-12-01 00:47:47 +03:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// static
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
static _jQueryInterface(options) {
|
|
|
|
return this.each(() => {
|
|
|
|
let element = $(this)
|
|
|
|
let data = element.data(DATA_KEY)
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
if (!data) {
|
|
|
|
data = new Ripples(this, options)
|
|
|
|
element.data(DATA_KEY, data)
|
|
|
|
}
|
|
|
|
})
|
2015-11-21 02:50:50 +03:00
|
|
|
}
|
2015-11-30 23:57:40 +03:00
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
|
|
|
/**
|
2015-11-30 23:57:40 +03:00
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
2015-11-21 02:50:50 +03:00
|
|
|
*/
|
2015-11-30 23:57:40 +03:00
|
|
|
$.fn[NAME] = Ripples._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = Ripples
|
|
|
|
$.fn[NAME].noConflict = () => {
|
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return Ripples._jQueryInterface
|
|
|
|
}
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
return Ripples
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
})(jQuery)
|
2015-11-21 02:50:50 +03:00
|
|
|
|
2015-11-30 23:57:40 +03:00
|
|
|
export default Ripples
|