gsap
Version:
GSAP is a robust JavaScript toolset that turns developers into animation superheroes. Build high-performance animations that work in **every** major browser. Animate CSS, SVG, canvas, React, Vue, WebGL, colors, strings, motion paths, generic objects...any
1,430 lines (1,229 loc) • 99.3 kB
JavaScript
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
/*!
* Draggable 3.8.0
* https://greensock.com
*
* @license Copyright 2008-2021, GreenSock. All rights reserved.
* Subject to the terms at https://greensock.com/standard-license or for
* Club GreenSock members, the agreement issued with that membership.
* @author: Jack Doyle, jack@greensock.com
*/
/* eslint-disable */
import { getGlobalMatrix, Matrix2D } from "./utils/matrix.js";
var gsap,
_win,
_doc,
_docElement,
_body,
_tempDiv,
_placeholderDiv,
_coreInitted,
_checkPrefix,
_toArray,
_supportsPassive,
_isTouchDevice,
_touchEventLookup,
_dragCount,
_isMultiTouching,
_isAndroid,
InertiaPlugin,
_defaultCursor,
_supportsPointer,
_windowExists = function _windowExists() {
return typeof window !== "undefined";
},
_getGSAP = function _getGSAP() {
return gsap || _windowExists() && (gsap = window.gsap) && gsap.registerPlugin && gsap;
},
_isFunction = function _isFunction(value) {
return typeof value === "function";
},
_isObject = function _isObject(value) {
return typeof value === "object";
},
_isUndefined = function _isUndefined(value) {
return typeof value === "undefined";
},
_emptyFunc = function _emptyFunc() {
return false;
},
_transformProp = "transform",
_transformOriginProp = "transformOrigin",
_round = function _round(value) {
return Math.round(value * 10000) / 10000;
},
_isArray = Array.isArray,
_createElement = function _createElement(type, ns) {
var e = _doc.createElementNS ? _doc.createElementNS((ns || "http://www.w3.org/1999/xhtml").replace(/^https/, "http"), type) : _doc.createElement(type); //some servers swap in https for http in the namespace which can break things, making "style" inaccessible.
return e.style ? e : _doc.createElement(type); //some environments won't allow access to the element's style when created with a namespace in which case we default to the standard createElement() to work around the issue. Also note that when GSAP is embedded directly inside an SVG file, createElement() won't allow access to the style object in Firefox (see https://greensock.com/forums/topic/20215-problem-using-tweenmax-in-standalone-self-containing-svg-file-err-cannot-set-property-csstext-of-undefined/).
},
_RAD2DEG = 180 / Math.PI,
_bigNum = 1e20,
_identityMatrix = new Matrix2D(),
_getTime = Date.now || function () {
return new Date().getTime();
},
_renderQueue = [],
_lookup = {},
//when a Draggable is created, the target gets a unique _gsDragID property that allows gets associated with the Draggable instance for quick lookups in Draggable.get(). This avoids circular references that could cause gc problems.
_lookupCount = 0,
_clickableTagExp = /^(?:a|input|textarea|button|select)$/i,
_lastDragTime = 0,
_temp1 = {},
// a simple object we reuse and populate (usually x/y properties) to conserve memory and improve performance.
_windowProxy = {},
//memory/performance optimization - we reuse this object during autoScroll to store window-related bounds/offsets.
_copy = function _copy(obj, factor) {
var copy = {},
p;
for (p in obj) {
copy[p] = factor ? obj[p] * factor : obj[p];
}
return copy;
},
_extend = function _extend(obj, defaults) {
for (var p in defaults) {
if (!(p in obj)) {
obj[p] = defaults[p];
}
}
return obj;
},
_setTouchActionForAllDescendants = function _setTouchActionForAllDescendants(elements, value) {
var i = elements.length,
children;
while (i--) {
value ? elements[i].style.touchAction = value : elements[i].style.removeProperty("touch-action");
children = elements[i].children;
children && children.length && _setTouchActionForAllDescendants(children, value);
}
},
_renderQueueTick = function _renderQueueTick() {
return _renderQueue.forEach(function (func) {
return func();
});
},
_addToRenderQueue = function _addToRenderQueue(func) {
_renderQueue.push(func);
if (_renderQueue.length === 1) {
gsap.ticker.add(_renderQueueTick);
}
},
_renderQueueTimeout = function _renderQueueTimeout() {
return !_renderQueue.length && gsap.ticker.remove(_renderQueueTick);
},
_removeFromRenderQueue = function _removeFromRenderQueue(func) {
var i = _renderQueue.length;
while (i--) {
if (_renderQueue[i] === func) {
_renderQueue.splice(i, 1);
}
}
gsap.to(_renderQueueTimeout, {
overwrite: true,
delay: 15,
duration: 0,
onComplete: _renderQueueTimeout,
data: "_draggable"
}); //remove the "tick" listener only after the render queue is empty for 15 seconds (to improve performance). Adding/removing it constantly for every click/touch wouldn't deliver optimal speed, and we also don't want the ticker to keep calling the render method when things are idle for long periods of time (we want to improve battery life on mobile devices).
},
_setDefaults = function _setDefaults(obj, defaults) {
for (var p in defaults) {
if (!(p in obj)) {
obj[p] = defaults[p];
}
}
return obj;
},
_addListener = function _addListener(element, type, func, capture) {
if (element.addEventListener) {
var touchType = _touchEventLookup[type];
capture = capture || (_supportsPassive ? {
passive: false
} : null);
element.addEventListener(touchType || type, func, capture);
touchType && type !== touchType && element.addEventListener(type, func, capture); //some browsers actually support both, so must we. But pointer events cover all.
}
},
_removeListener = function _removeListener(element, type, func) {
if (element.removeEventListener) {
var touchType = _touchEventLookup[type];
element.removeEventListener(touchType || type, func);
touchType && type !== touchType && element.removeEventListener(type, func);
}
},
_preventDefault = function _preventDefault(event) {
event.preventDefault && event.preventDefault();
event.preventManipulation && event.preventManipulation(); //for some Microsoft browsers
},
_hasTouchID = function _hasTouchID(list, ID) {
var i = list.length;
while (i--) {
if (list[i].identifier === ID) {
return true;
}
}
},
_onMultiTouchDocumentEnd = function _onMultiTouchDocumentEnd(event) {
_isMultiTouching = event.touches && _dragCount < event.touches.length;
_removeListener(event.target, "touchend", _onMultiTouchDocumentEnd);
},
_onMultiTouchDocument = function _onMultiTouchDocument(event) {
_isMultiTouching = event.touches && _dragCount < event.touches.length;
_addListener(event.target, "touchend", _onMultiTouchDocumentEnd);
},
_getDocScrollTop = function _getDocScrollTop(doc) {
return _win.pageYOffset || doc.scrollTop || doc.documentElement.scrollTop || doc.body.scrollTop || 0;
},
_getDocScrollLeft = function _getDocScrollLeft(doc) {
return _win.pageXOffset || doc.scrollLeft || doc.documentElement.scrollLeft || doc.body.scrollLeft || 0;
},
_addScrollListener = function _addScrollListener(e, callback) {
_addListener(e, "scroll", callback);
if (!_isRoot(e.parentNode)) {
_addScrollListener(e.parentNode, callback);
}
},
_removeScrollListener = function _removeScrollListener(e, callback) {
_removeListener(e, "scroll", callback);
if (!_isRoot(e.parentNode)) {
_removeScrollListener(e.parentNode, callback);
}
},
_isRoot = function _isRoot(e) {
return !!(!e || e === _docElement || e.nodeType === 9 || e === _doc.body || e === _win || !e.nodeType || !e.parentNode);
},
_getMaxScroll = function _getMaxScroll(element, axis) {
var dim = axis === "x" ? "Width" : "Height",
scroll = "scroll" + dim,
client = "client" + dim;
return Math.max(0, _isRoot(element) ? Math.max(_docElement[scroll], _body[scroll]) - (_win["inner" + dim] || _docElement[client] || _body[client]) : element[scroll] - element[client]);
},
_recordMaxScrolls = function _recordMaxScrolls(e, skipCurrent) {
//records _gsMaxScrollX and _gsMaxScrollY properties for the element and all ancestors up the chain so that we can cap it, otherwise dragging beyond the edges with autoScroll on can endlessly scroll.
var x = _getMaxScroll(e, "x"),
y = _getMaxScroll(e, "y");
if (_isRoot(e)) {
e = _windowProxy;
} else {
_recordMaxScrolls(e.parentNode, skipCurrent);
}
e._gsMaxScrollX = x;
e._gsMaxScrollY = y;
if (!skipCurrent) {
e._gsScrollX = e.scrollLeft || 0;
e._gsScrollY = e.scrollTop || 0;
}
},
_setStyle = function _setStyle(element, property, value) {
var style = element.style;
if (!style) {
return;
}
if (_isUndefined(style[property])) {
property = _checkPrefix(property, element) || property;
}
if (value == null) {
style.removeProperty && style.removeProperty(property.replace(/([A-Z])/g, "-$1").toLowerCase());
} else {
style[property] = value;
}
},
_getComputedStyle = function _getComputedStyle(element) {
return _win.getComputedStyle(element instanceof Element ? element : element.host || (element.parentNode || {}).host || element);
},
//the "host" stuff helps to accommodate ShadowDom objects.
_tempRect = {},
//reuse to reduce garbage collection tasks
_parseRect = function _parseRect(e) {
//accepts a DOM element, a mouse event, or a rectangle object and returns the corresponding rectangle with left, right, width, height, top, and bottom properties
if (e === _win) {
_tempRect.left = _tempRect.top = 0;
_tempRect.width = _tempRect.right = _docElement.clientWidth || e.innerWidth || _body.clientWidth || 0;
_tempRect.height = _tempRect.bottom = (e.innerHeight || 0) - 20 < _docElement.clientHeight ? _docElement.clientHeight : e.innerHeight || _body.clientHeight || 0;
return _tempRect;
}
var doc = e.ownerDocument || _doc,
r = !_isUndefined(e.pageX) ? {
left: e.pageX - _getDocScrollLeft(doc),
top: e.pageY - _getDocScrollTop(doc),
right: e.pageX - _getDocScrollLeft(doc) + 1,
bottom: e.pageY - _getDocScrollTop(doc) + 1
} : !e.nodeType && !_isUndefined(e.left) && !_isUndefined(e.top) ? e : _toArray(e)[0].getBoundingClientRect();
if (_isUndefined(r.right) && !_isUndefined(r.width)) {
r.right = r.left + r.width;
r.bottom = r.top + r.height;
} else if (_isUndefined(r.width)) {
//some browsers don't include width and height properties. We can't just set them directly on r because some browsers throw errors, so create a new generic object.
r = {
width: r.right - r.left,
height: r.bottom - r.top,
right: r.right,
left: r.left,
bottom: r.bottom,
top: r.top
};
}
return r;
},
_dispatchEvent = function _dispatchEvent(target, type, callbackName) {
var vars = target.vars,
callback = vars[callbackName],
listeners = target._listeners[type],
result;
if (_isFunction(callback)) {
result = callback.apply(vars.callbackScope || target, vars[callbackName + "Params"] || [target.pointerEvent]);
}
if (listeners && target.dispatchEvent(type) === false) {
result = false;
}
return result;
},
_getBounds = function _getBounds(target, context) {
//accepts any of the following: a DOM element, jQuery object, selector text, or an object defining bounds as {top, left, width, height} or {minX, maxX, minY, maxY}. Returns an object with left, top, width, and height properties.
var e = _toArray(target)[0],
top,
left,
offset;
if (!e.nodeType && e !== _win) {
if (!_isUndefined(target.left)) {
offset = {
x: 0,
y: 0
}; //_getOffsetTransformOrigin(context); //the bounds should be relative to the origin
return {
left: target.left - offset.x,
top: target.top - offset.y,
width: target.width,
height: target.height
};
}
left = target.min || target.minX || target.minRotation || 0;
top = target.min || target.minY || 0;
return {
left: left,
top: top,
width: (target.max || target.maxX || target.maxRotation || 0) - left,
height: (target.max || target.maxY || 0) - top
};
}
return _getElementBounds(e, context);
},
_point1 = {},
//we reuse to minimize garbage collection tasks.
_getElementBounds = function _getElementBounds(element, context) {
context = _toArray(context)[0];
var isSVG = element.getBBox && element.ownerSVGElement,
doc = element.ownerDocument || _doc,
left,
right,
top,
bottom,
matrix,
p1,
p2,
p3,
p4,
bbox,
width,
height,
cs,
contextParent;
if (element === _win) {
top = _getDocScrollTop(doc);
left = _getDocScrollLeft(doc);
right = left + (doc.documentElement.clientWidth || element.innerWidth || doc.body.clientWidth || 0);
bottom = top + ((element.innerHeight || 0) - 20 < doc.documentElement.clientHeight ? doc.documentElement.clientHeight : element.innerHeight || doc.body.clientHeight || 0); //some browsers (like Firefox) ignore absolutely positioned elements, and collapse the height of the documentElement, so it could be 8px, for example, if you have just an absolutely positioned div. In that case, we use the innerHeight to resolve this.
} else if (context === _win || _isUndefined(context)) {
return element.getBoundingClientRect();
} else {
left = top = 0;
if (isSVG) {
bbox = element.getBBox();
width = bbox.width;
height = bbox.height;
} else {
if (element.viewBox && (bbox = element.viewBox.baseVal)) {
left = bbox.x || 0;
top = bbox.y || 0;
width = bbox.width;
height = bbox.height;
}
if (!width) {
cs = _getComputedStyle(element);
bbox = cs.boxSizing === "border-box";
width = (parseFloat(cs.width) || element.clientWidth || 0) + (bbox ? 0 : parseFloat(cs.borderLeftWidth) + parseFloat(cs.borderRightWidth));
height = (parseFloat(cs.height) || element.clientHeight || 0) + (bbox ? 0 : parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth));
}
}
right = width;
bottom = height;
}
if (element === context) {
return {
left: left,
top: top,
width: right - left,
height: bottom - top
};
}
matrix = getGlobalMatrix(context, true).multiply(getGlobalMatrix(element));
p1 = matrix.apply({
x: left,
y: top
});
p2 = matrix.apply({
x: right,
y: top
});
p3 = matrix.apply({
x: right,
y: bottom
});
p4 = matrix.apply({
x: left,
y: bottom
});
left = Math.min(p1.x, p2.x, p3.x, p4.x);
top = Math.min(p1.y, p2.y, p3.y, p4.y);
contextParent = context.parentNode || {};
return {
left: left + (contextParent.scrollLeft || 0),
top: top + (contextParent.scrollTop || 0),
width: Math.max(p1.x, p2.x, p3.x, p4.x) - left,
height: Math.max(p1.y, p2.y, p3.y, p4.y) - top
};
},
_parseInertia = function _parseInertia(draggable, snap, max, min, factor, forceZeroVelocity) {
var vars = {},
a,
i,
l;
if (snap) {
if (factor !== 1 && snap instanceof Array) {
//some data must be altered to make sense, like if the user passes in an array of rotational values in degrees, we must convert it to radians. Or for scrollLeft and scrollTop, we invert the values.
vars.end = a = [];
l = snap.length;
if (_isObject(snap[0])) {
//if the array is populated with objects, like points ({x:100, y:200}), make copies before multiplying by the factor, otherwise we'll mess up the originals and the user may reuse it elsewhere.
for (i = 0; i < l; i++) {
a[i] = _copy(snap[i], factor);
}
} else {
for (i = 0; i < l; i++) {
a[i] = snap[i] * factor;
}
}
max += 1.1; //allow 1.1 pixels of wiggle room when snapping in order to work around some browser inconsistencies in the way bounds are reported which can make them roughly a pixel off. For example, if "snap:[-$('#menu').width(), 0]" was defined and #menu had a wrapper that was used as the bounds, some browsers would be one pixel off, making the minimum -752 for example when snap was [-753,0], thus instead of snapping to -753, it would snap to 0 since -753 was below the minimum.
min -= 1.1;
} else if (_isFunction(snap)) {
vars.end = function (value) {
var result = snap.call(draggable, value),
copy,
p;
if (factor !== 1) {
if (_isObject(result)) {
copy = {};
for (p in result) {
copy[p] = result[p] * factor;
}
result = copy;
} else {
result *= factor;
}
}
return result; //we need to ensure that we can scope the function call to the Draggable instance itself so that users can access important values like maxX, minX, maxY, minY, x, and y from within that function.
};
} else {
vars.end = snap;
}
}
if (max || max === 0) {
vars.max = max;
}
if (min || min === 0) {
vars.min = min;
}
if (forceZeroVelocity) {
vars.velocity = 0;
}
return vars;
},
_isClickable = function _isClickable(element) {
//sometimes it's convenient to mark an element as clickable by adding a data-clickable="true" attribute (in which case we won't preventDefault() the mouse/touch event). This method checks if the element is an <a>, <input>, or <button> or has an onclick or has the data-clickable or contentEditable attribute set to true (or any of its parent elements).
var data;
return !element || !element.getAttribute || element === _body ? false : (data = element.getAttribute("data-clickable")) === "true" || data !== "false" && (element.onclick || _clickableTagExp.test(element.nodeName + "") || element.getAttribute("contentEditable") === "true") ? true : _isClickable(element.parentNode);
},
_setSelectable = function _setSelectable(elements, selectable) {
var i = elements.length,
e;
while (i--) {
e = elements[i];
e.ondragstart = e.onselectstart = selectable ? null : _emptyFunc;
gsap.set(e, {
lazy: true,
userSelect: selectable ? "text" : "none"
});
}
},
_isFixed = function _isFixed(element) {
if (_getComputedStyle(element).position === "fixed") {
return true;
}
element = element.parentNode;
if (element && element.nodeType === 1) {
// avoid document fragments which will throw an error.
return _isFixed(element);
}
},
_supports3D,
_addPaddingBR,
//The ScrollProxy class wraps an element's contents into another div (we call it "content") that we either add padding when necessary or apply a translate3d() transform in order to overscroll (scroll past the boundaries). This allows us to simply set the scrollTop/scrollLeft (or top/left for easier reverse-axis orientation, which is what we do in Draggable) and it'll do all the work for us. For example, if we tried setting scrollTop to -100 on a normal DOM element, it wouldn't work - it'd look the same as setting it to 0, but if we set scrollTop of a ScrollProxy to -100, it'll give the correct appearance by either setting paddingTop of the wrapper to 100 or applying a 100-pixel translateY.
ScrollProxy = function ScrollProxy(element, vars) {
element = gsap.utils.toArray(element)[0];
vars = vars || {};
var content = document.createElement("div"),
style = content.style,
node = element.firstChild,
offsetTop = 0,
offsetLeft = 0,
prevTop = element.scrollTop,
prevLeft = element.scrollLeft,
scrollWidth = element.scrollWidth,
scrollHeight = element.scrollHeight,
extraPadRight = 0,
maxLeft = 0,
maxTop = 0,
elementWidth,
elementHeight,
contentHeight,
nextNode,
transformStart,
transformEnd;
if (_supports3D && vars.force3D !== false) {
transformStart = "translate3d(";
transformEnd = "px,0px)";
} else if (_transformProp) {
transformStart = "translate(";
transformEnd = "px)";
}
this.scrollTop = function (value, force) {
if (!arguments.length) {
return -this.top();
}
this.top(-value, force);
};
this.scrollLeft = function (value, force) {
if (!arguments.length) {
return -this.left();
}
this.left(-value, force);
};
this.left = function (value, force) {
if (!arguments.length) {
return -(element.scrollLeft + offsetLeft);
}
var dif = element.scrollLeft - prevLeft,
oldOffset = offsetLeft;
if ((dif > 2 || dif < -2) && !force) {
//if the user interacts with the scrollbar (or something else scrolls it, like the mouse wheel), we should kill any tweens of the ScrollProxy.
prevLeft = element.scrollLeft;
gsap.killTweensOf(this, {
left: 1,
scrollLeft: 1
});
this.left(-prevLeft);
if (vars.onKill) {
vars.onKill();
}
return;
}
value = -value; //invert because scrolling works in the opposite direction
if (value < 0) {
offsetLeft = value - 0.5 | 0;
value = 0;
} else if (value > maxLeft) {
offsetLeft = value - maxLeft | 0;
value = maxLeft;
} else {
offsetLeft = 0;
}
if (offsetLeft || oldOffset) {
if (!this._skip) {
style[_transformProp] = transformStart + -offsetLeft + "px," + -offsetTop + transformEnd;
}
if (offsetLeft + extraPadRight >= 0) {
style.paddingRight = offsetLeft + extraPadRight + "px";
}
}
element.scrollLeft = value | 0;
prevLeft = element.scrollLeft; //don't merge this with the line above because some browsers adjust the scrollLeft after it's set, so in order to be 100% accurate in tracking it, we need to ask the browser to report it.
};
this.top = function (value, force) {
if (!arguments.length) {
return -(element.scrollTop + offsetTop);
}
var dif = element.scrollTop - prevTop,
oldOffset = offsetTop;
if ((dif > 2 || dif < -2) && !force) {
//if the user interacts with the scrollbar (or something else scrolls it, like the mouse wheel), we should kill any tweens of the ScrollProxy.
prevTop = element.scrollTop;
gsap.killTweensOf(this, {
top: 1,
scrollTop: 1
});
this.top(-prevTop);
if (vars.onKill) {
vars.onKill();
}
return;
}
value = -value; //invert because scrolling works in the opposite direction
if (value < 0) {
offsetTop = value - 0.5 | 0;
value = 0;
} else if (value > maxTop) {
offsetTop = value - maxTop | 0;
value = maxTop;
} else {
offsetTop = 0;
}
if (offsetTop || oldOffset) {
if (!this._skip) {
style[_transformProp] = transformStart + -offsetLeft + "px," + -offsetTop + transformEnd;
}
}
element.scrollTop = value | 0;
prevTop = element.scrollTop;
};
this.maxScrollTop = function () {
return maxTop;
};
this.maxScrollLeft = function () {
return maxLeft;
};
this.disable = function () {
node = content.firstChild;
while (node) {
nextNode = node.nextSibling;
element.appendChild(node);
node = nextNode;
}
if (element === content.parentNode) {
//in case disable() is called when it's already disabled.
element.removeChild(content);
}
};
this.enable = function () {
node = element.firstChild;
if (node === content) {
return;
}
while (node) {
nextNode = node.nextSibling;
content.appendChild(node);
node = nextNode;
}
element.appendChild(content);
this.calibrate();
};
this.calibrate = function (force) {
var widthMatches = element.clientWidth === elementWidth,
cs,
x,
y;
prevTop = element.scrollTop;
prevLeft = element.scrollLeft;
if (widthMatches && element.clientHeight === elementHeight && content.offsetHeight === contentHeight && scrollWidth === element.scrollWidth && scrollHeight === element.scrollHeight && !force) {
return; //no need to recalculate things if the width and height haven't changed.
}
if (offsetTop || offsetLeft) {
x = this.left();
y = this.top();
this.left(-element.scrollLeft);
this.top(-element.scrollTop);
}
cs = _getComputedStyle(element); //first, we need to remove any width constraints to see how the content naturally flows so that we can see if it's wider than the containing element. If so, we've got to record the amount of overage so that we can apply that as padding in order for browsers to correctly handle things. Then we switch back to a width of 100% (without that, some browsers don't flow the content correctly)
if (!widthMatches || force) {
style.display = "block";
style.width = "auto";
style.paddingRight = "0px";
extraPadRight = Math.max(0, element.scrollWidth - element.clientWidth); //if the content is wider than the container, we need to add the paddingLeft and paddingRight in order for things to behave correctly.
if (extraPadRight) {
extraPadRight += parseFloat(cs.paddingLeft) + (_addPaddingBR ? parseFloat(cs.paddingRight) : 0);
}
}
style.display = "inline-block";
style.position = "relative";
style.overflow = "visible";
style.verticalAlign = "top";
style.boxSizing = "content-box";
style.width = "100%";
style.paddingRight = extraPadRight + "px"; //some browsers neglect to factor in the bottom padding when calculating the scrollHeight, so we need to add that padding to the content when that happens. Allow a 2px margin for error
if (_addPaddingBR) {
style.paddingBottom = cs.paddingBottom;
}
elementWidth = element.clientWidth;
elementHeight = element.clientHeight;
scrollWidth = element.scrollWidth;
scrollHeight = element.scrollHeight;
maxLeft = element.scrollWidth - elementWidth;
maxTop = element.scrollHeight - elementHeight;
contentHeight = content.offsetHeight;
style.display = "block";
if (x || y) {
this.left(x);
this.top(y);
}
};
this.content = content;
this.element = element;
this._skip = false;
this.enable();
},
_initCore = function _initCore(required) {
if (_windowExists() && document.body) {
var nav = window && window.navigator;
_win = window;
_doc = document;
_docElement = _doc.documentElement;
_body = _doc.body;
_tempDiv = _createElement("div");
_supportsPointer = !!window.PointerEvent;
_placeholderDiv = _createElement("div");
_placeholderDiv.style.cssText = "visibility:hidden;height:1px;top:-1px;pointer-events:none;position:relative;clear:both;cursor:grab";
_defaultCursor = _placeholderDiv.style.cursor === "grab" ? "grab" : "move";
_isAndroid = nav && nav.userAgent.toLowerCase().indexOf("android") !== -1; //Android handles touch events in an odd way and it's virtually impossible to "feature test" so we resort to UA sniffing
_isTouchDevice = "ontouchstart" in _docElement && "orientation" in _win || nav && (nav.MaxTouchPoints > 0 || nav.msMaxTouchPoints > 0);
_addPaddingBR = function () {
//this function is in charge of analyzing browser behavior related to padding. It sets the _addPaddingBR to true if the browser doesn't normally factor in the bottom or right padding on the element inside the scrolling area, and it sets _addPaddingLeft to true if it's a browser that requires the extra offset (offsetLeft) to be added to the paddingRight (like Opera).
var div = _createElement("div"),
child = _createElement("div"),
childStyle = child.style,
parent = _body,
val;
childStyle.display = "inline-block";
childStyle.position = "relative";
div.style.cssText = child.innerHTML = "width:90px;height:40px;padding:10px;overflow:auto;visibility:hidden";
div.appendChild(child);
parent.appendChild(div);
val = child.offsetHeight + 18 > div.scrollHeight; //div.scrollHeight should be child.offsetHeight + 20 because of the 10px of padding on each side, but some browsers ignore one side. We allow a 2px margin of error.
parent.removeChild(div);
return val;
}();
_touchEventLookup = function (types) {
//we create an object that makes it easy to translate touch event types into their "pointer" counterparts if we're in a browser that uses those instead. Like IE10 uses "MSPointerDown" instead of "touchstart", for example.
var standard = types.split(","),
converted = ("onpointerdown" in _tempDiv ? "pointerdown,pointermove,pointerup,pointercancel" : "onmspointerdown" in _tempDiv ? "MSPointerDown,MSPointerMove,MSPointerUp,MSPointerCancel" : types).split(","),
obj = {},
i = 4;
while (--i > -1) {
obj[standard[i]] = converted[i];
obj[converted[i]] = standard[i];
} //to avoid problems in iOS 9, test to see if the browser supports the "passive" option on addEventListener().
try {
_docElement.addEventListener("test", null, Object.defineProperty({}, "passive", {
get: function get() {
_supportsPassive = 1;
}
}));
} catch (e) {}
return obj;
}("touchstart,touchmove,touchend,touchcancel");
_addListener(_doc, "touchcancel", _emptyFunc); //some older Android devices intermittently stop dispatching "touchmove" events if we don't listen for "touchcancel" on the document. Very strange indeed.
_addListener(_win, "touchmove", _emptyFunc); //works around Safari bugs that still allow the page to scroll even when we preventDefault() on the touchmove event.
_body && _body.addEventListener("touchstart", _emptyFunc); //works around Safari bug: https://greensock.com/forums/topic/21450-draggable-in-iframe-on-mobile-is-buggy/
_addListener(_doc, "contextmenu", function () {
for (var p in _lookup) {
if (_lookup[p].isPressed) {
_lookup[p].endDrag();
}
}
});
gsap = _coreInitted = _getGSAP();
}
if (gsap) {
InertiaPlugin = gsap.plugins.inertia;
_checkPrefix = gsap.utils.checkPrefix;
_transformProp = _checkPrefix(_transformProp);
_transformOriginProp = _checkPrefix(_transformOriginProp);
_toArray = gsap.utils.toArray;
_supports3D = !!_checkPrefix("perspective");
} else if (required) {
console.warn("Please gsap.registerPlugin(Draggable)");
}
};
var EventDispatcher = /*#__PURE__*/function () {
function EventDispatcher(target) {
this._listeners = {};
this.target = target || this;
}
var _proto = EventDispatcher.prototype;
_proto.addEventListener = function addEventListener(type, callback) {
var list = this._listeners[type] || (this._listeners[type] = []);
if (!~list.indexOf(callback)) {
list.push(callback);
}
};
_proto.removeEventListener = function removeEventListener(type, callback) {
var list = this._listeners[type],
i = list && list.indexOf(callback) || -1;
i > -1 && list.splice(i, 1);
};
_proto.dispatchEvent = function dispatchEvent(type) {
var _this = this;
var result;
(this._listeners[type] || []).forEach(function (callback) {
return callback.call(_this, {
type: type,
target: _this.target
}) === false && (result = false);
});
return result; //if any of the callbacks return false, pass that along.
};
return EventDispatcher;
}();
export var Draggable = /*#__PURE__*/function (_EventDispatcher) {
_inheritsLoose(Draggable, _EventDispatcher);
function Draggable(target, vars) {
var _this2;
_this2 = _EventDispatcher.call(this) || this;
_coreInitted || _initCore(1);
target = _toArray(target)[0]; //in case the target is a selector object or selector text
if (!InertiaPlugin) {
InertiaPlugin = gsap.plugins.inertia;
}
_this2.vars = vars = _copy(vars || {});
_this2.target = target;
_this2.x = _this2.y = _this2.rotation = 0;
_this2.dragResistance = parseFloat(vars.dragResistance) || 0;
_this2.edgeResistance = isNaN(vars.edgeResistance) ? 1 : parseFloat(vars.edgeResistance) || 0;
_this2.lockAxis = vars.lockAxis;
_this2.autoScroll = vars.autoScroll || 0;
_this2.lockedAxis = null;
_this2.allowEventDefault = !!vars.allowEventDefault;
gsap.getProperty(target, "x"); // to ensure that transforms are instantiated.
var type = (vars.type || "x,y").toLowerCase(),
xyMode = ~type.indexOf("x") || ~type.indexOf("y"),
rotationMode = type.indexOf("rotation") !== -1,
xProp = rotationMode ? "rotation" : xyMode ? "x" : "left",
yProp = xyMode ? "y" : "top",
allowX = !!(~type.indexOf("x") || ~type.indexOf("left") || type === "scroll"),
allowY = !!(~type.indexOf("y") || ~type.indexOf("top") || type === "scroll"),
minimumMovement = vars.minimumMovement || 2,
self = _assertThisInitialized(_this2),
triggers = _toArray(vars.trigger || vars.handle || target),
killProps = {},
dragEndTime = 0,
checkAutoScrollBounds = false,
autoScrollMarginTop = vars.autoScrollMarginTop || 40,
autoScrollMarginRight = vars.autoScrollMarginRight || 40,
autoScrollMarginBottom = vars.autoScrollMarginBottom || 40,
autoScrollMarginLeft = vars.autoScrollMarginLeft || 40,
isClickable = vars.clickableTest || _isClickable,
clickTime = 0,
gsCache = target._gsap || gsap.core.getCache(target),
isFixed = _isFixed(target),
getPropAsNum = function getPropAsNum(property, unit) {
return parseFloat(gsCache.get(target, property, unit));
},
ownerDoc = target.ownerDocument || _doc,
enabled,
scrollProxy,
startPointerX,
startPointerY,
startElementX,
startElementY,
hasBounds,
hasDragCallback,
hasMoveCallback,
maxX,
minX,
maxY,
minY,
touch,
touchID,
rotationOrigin,
dirty,
old,
snapX,
snapY,
snapXY,
isClicking,
touchEventTarget,
matrix,
interrupted,
allowNativeTouchScrolling,
touchDragAxis,
isDispatching,
clickDispatch,
trustedClickDispatch,
isPreventingDefault,
innerMatrix,
onContextMenu = function onContextMenu(e) {
//used to prevent long-touch from triggering a context menu.
// (self.isPressed && e.which < 2) && self.endDrag() // previously ended drag when context menu was triggered, but instead we should just stop propagation and prevent the default event behavior.
_preventDefault(e);
e.stopImmediatePropagation && e.stopImmediatePropagation();
return false;
},
//this method gets called on every tick of TweenLite.ticker which allows us to synchronize the renders to the core engine (which is typically synchronized with the display refresh via requestAnimationFrame). This is an optimization - it's better than applying the values inside the "mousemove" or "touchmove" event handler which may get called many times inbetween refreshes.
render = function render(suppressEvents) {
if (self.autoScroll && self.isDragging && (checkAutoScrollBounds || dirty)) {
var e = target,
autoScrollFactor = self.autoScroll * 15,
//multiplying by 15 just gives us a better "feel" speed-wise.
parent,
isRoot,
rect,
pointerX,
pointerY,
changeX,
changeY,
gap;
checkAutoScrollBounds = false;
_windowProxy.scrollTop = _win.pageYOffset != null ? _win.pageYOffset : ownerDoc.documentElement.scrollTop != null ? ownerDoc.documentElement.scrollTop : ownerDoc.body.scrollTop;
_windowProxy.scrollLeft = _win.pageXOffset != null ? _win.pageXOffset : ownerDoc.documentElement.scrollLeft != null ? ownerDoc.documentElement.scrollLeft : ownerDoc.body.scrollLeft;
pointerX = self.pointerX - _windowProxy.scrollLeft;
pointerY = self.pointerY - _windowProxy.scrollTop;
while (e && !isRoot) {
//walk up the chain and sense wherever the pointer is within 40px of an edge that's scrollable.
isRoot = _isRoot(e.parentNode);
parent = isRoot ? _windowProxy : e.parentNode;
rect = isRoot ? {
bottom: Math.max(_docElement.clientHeight, _win.innerHeight || 0),
right: Math.max(_docElement.clientWidth, _win.innerWidth || 0),
left: 0,
top: 0
} : parent.getBoundingClientRect();
changeX = changeY = 0;
if (allowY) {
gap = parent._gsMaxScrollY - parent.scrollTop;
if (gap < 0) {
changeY = gap;
} else if (pointerY > rect.bottom - autoScrollMarginBottom && gap) {
checkAutoScrollBounds = true;
changeY = Math.min(gap, autoScrollFactor * (1 - Math.max(0, rect.bottom - pointerY) / autoScrollMarginBottom) | 0);
} else if (pointerY < rect.top + autoScrollMarginTop && parent.scrollTop) {
checkAutoScrollBounds = true;
changeY = -Math.min(parent.scrollTop, autoScrollFactor * (1 - Math.max(0, pointerY - rect.top) / autoScrollMarginTop) | 0);
}
if (changeY) {
parent.scrollTop += changeY;
}
}
if (allowX) {
gap = parent._gsMaxScrollX - parent.scrollLeft;
if (gap < 0) {
changeX = gap;
} else if (pointerX > rect.right - autoScrollMarginRight && gap) {
checkAutoScrollBounds = true;
changeX = Math.min(gap, autoScrollFactor * (1 - Math.max(0, rect.right - pointerX) / autoScrollMarginRight) | 0);
} else if (pointerX < rect.left + autoScrollMarginLeft && parent.scrollLeft) {
checkAutoScrollBounds = true;
changeX = -Math.min(parent.scrollLeft, autoScrollFactor * (1 - Math.max(0, pointerX - rect.left) / autoScrollMarginLeft) | 0);
}
if (changeX) {
parent.scrollLeft += changeX;
}
}
if (isRoot && (changeX || changeY)) {
_win.scrollTo(parent.scrollLeft, parent.scrollTop);
setPointerPosition(self.pointerX + changeX, self.pointerY + changeY);
}
e = parent;
}
}
if (dirty) {
var x = self.x,
y = self.y;
if (rotationMode) {
self.deltaX = x - parseFloat(gsCache.rotation);
self.rotation = x;
gsCache.rotation = x + "deg";
gsCache.renderTransform(1, gsCache);
} else {
if (scrollProxy) {
if (allowY) {
self.deltaY = y - scrollProxy.top();
scrollProxy.top(y);
}
if (allowX) {
self.deltaX = x - scrollProxy.left();
scrollProxy.left(x);
}
} else if (xyMode) {
if (allowY) {
self.deltaY = y - parseFloat(gsCache.y);
gsCache.y = y + "px";
}
if (allowX) {
self.deltaX = x - parseFloat(gsCache.x);
gsCache.x = x + "px";
}
gsCache.renderTransform(1, gsCache);
} else {
if (allowY) {
self.deltaY = y - parseFloat(target.style.top || 0);
target.style.top = y + "px";
}
if (allowX) {
self.deltaX = x - parseFloat(target.style.left || 0);
target.style.left = x + "px";
}
}
}
if (hasDragCallback && !suppressEvents && !isDispatching) {
isDispatching = true; //in case onDrag has an update() call (avoid endless loop)
if (_dispatchEvent(self, "drag", "onDrag") === false) {
if (allowX) {
self.x -= self.deltaX;
}
if (allowY) {
self.y -= self.deltaY;
}
render(true);
}
isDispatching = false;
}
}
dirty = false;
},
//copies the x/y from the element (whether that be transforms, top/left, or ScrollProxy's top/left) to the Draggable's x and y (and rotation if necessary) properties so that they reflect reality and it also (optionally) applies any snapping necessary. This is used by the InertiaPlugin tween in an onUpdate to ensure things are synced and snapped.
syncXY = function syncXY(skipOnUpdate, skipSnap) {
var x = self.x,
y = self.y,
snappedValue,
cs;
if (!target._gsap) {
//just in case the _gsap cache got wiped, like if the user called clearProps on the transform or something (very rare).
gsCache = gsap.core.getCache(target);
}
gsCache.uncache && gsap.getProperty(target, "x"); // trigger a re-cache
if (xyMode) {
self.x = parseFloat(gsCache.x);
self.y = parseFloat(gsCache.y);
} else if (rotationMode) {
self.x = self.rotation = parseFloat(gsCache.rotation);
} else if (scrollProxy) {
self.y = scrollProxy.top();
self.x = scrollProxy.left();
} else {
self.y = parseFloat(target.style.top || (cs = _getComputedStyle(target)) && cs.top) || 0;
self.x = parseFloat(target.style.left || (cs || {}).left) || 0;
}
if ((snapX || snapY || snapXY) && !skipSnap && (self.isDragging || self.isThrowing)) {
if (snapXY) {
_temp1.x = self.x;
_temp1.y = self.y;
snappedValue = snapXY(_temp1);
if (snappedValue.x !== self.x) {
self.x = snappedValue.x;
dirty = true;
}
if (snappedValue.y !== self.y) {
self.y = snappedValue.y;
dirty = true;
}
}
if (snapX) {
snappedValue = snapX(self.x);
if (snappedValue !== self.x) {
self.x = snappedValue;
if (rotationMode) {
self.rotation = snappedValue;
}
dirty = true;
}
}
if (snapY) {
snappedValue = snapY(self.y);
if (snappedValue !== self.y) {
self.y = snappedValue;
}
dirty = true;
}
}
dirty && render(true);
if (!skipOnUpdate) {
self.deltaX = self.x - x;
self.deltaY = self.y - y;
_dispatchEvent(self, "throwupdate", "onThrowUpdate");
}
},
buildSnapFunc = function buildSnapFunc(snap, min, max, factor) {
if (min == null) {
min = -_bigNum;
}
if (max == null) {
max = _bigNum;
}
if (_isFunction(snap)) {
return function (n) {
var edgeTolerance = !self.isPressed ? 1 : 1 - self.edgeResistance; //if we're tweening, disable the edgeTolerance because it's already factored into the tweening values (we don't want to apply it multiple times)
return snap.call(self, n > max ? max + (n - max) * edgeTolerance : n < min ? min + (n - min) * edgeTolerance : n) * factor;
};
}
if (_isArray(snap)) {
return function (n) {
var i = snap.length,
closest = 0,
absDif = _bigNum,
val,
dif;
while (--i > -1) {
val = snap[i];
dif = val - n;
if (dif < 0) {
dif = -dif;
}
if (dif < absDif && val >= min && val <= max) {
closest = i;
absDif = dif;
}
}
return snap[closest];
};
}
return isNaN(snap) ? function (n) {
return n;
} : function () {
return snap * factor;
};
},
buildPointSnapFunc = function buildPointSnapFunc(snap, minX, maxX, minY, maxY, radius, factor) {
radius = radius && radius < _bigNum ? radius * radius : _bigNum; //so we don't have to Math.sqrt() in the functions. Performance optimization.
if (_isFunction(snap)) {
return function (point) {
var edgeTolerance = !self.isPressed ? 1 : 1 - self.edgeResistance,
x = point.x,
y = point.y,
result,
dx,
dy; //if we're tweening, disable the edgeTolerance because it's already factored into the tweening values (we don't want to apply it multiple times)
point.x = x = x > maxX ? maxX + (x - maxX) * edgeTolerance : x < minX ? minX + (x - minX) * edgeTolerance : x;
point.y = y = y > maxY ? maxY + (y - maxY) * edgeTolerance : y < minY ? minY + (y - minY) * edgeTolerance : y;
result = snap.call(self, point);
if (result !== point) {
point.x = result.x;
point.y = result.y;
}
if (factor !== 1) {
point.x *= factor;
point.y *= factor;
}
if (radius < _bigNum) {
dx = point.x - x;
dy = point.y - y;
if (dx * dx + dy * dy > radius) {
point.x = x;
point.y = y;
}
}
return point;
};
}
if (_isArray(snap)) {
return function (p) {
var i = snap.length,
closest = 0,
minDist = _bigNum,
x,
y,
point,
dist;
while (--i > -1) {
point = snap[i];
x = point.x - p.x;
y = point.y - p.y;
dist = x * x + y * y;
if (dist < minDist) {
closest = i;
minDist = dist;
}
}
return minDist <= radius ? snap[closest] : p;
};
}
return function (n) {
return n;
};
},
calculateBounds = function calculateBounds() {
var bounds, targetBounds, snap, snapIsRaw;
hasBounds = false;
if (scrollProxy) {
scrollProxy.calibrate();
self.minX = minX = -scrollProxy.maxScrollLeft();
self.minY = minY = -scrollProxy.maxScrollTop();
self.maxX = maxX = self.maxY = maxY = 0;
hasBounds = true;
} else if (!!vars.bounds) {
bounds = _getBounds(vars.bounds, target.parentNode); //could be a selector/jQuery object or a DOM element or a generic object like {top:0, left:100, width:1000, height:800} or {minX:100, maxX:1100, minY:0, maxY:800}
if (rotationMode) {
self.minX = minX = bounds.left;
self.maxX = maxX = bounds.left + bounds.width;
self.minY = minY = self.maxY = maxY = 0;
} else if (!_isUndefined(vars.bounds.maxX) || !_isUndefined(vars.bounds.maxY)) {
bounds = vars.bounds;
self.minX = minX = bounds.minX;
self.minY = minY = bounds.minY;
self.maxX = maxX = bounds.maxX;
self.maxY = maxY = bounds.maxY;
} else {
targetBounds = _getBounds(target, target.parentNode);
self.minX = minX = Math.round(getPropAsNum(xProp, "px") + bounds.left - targetBounds.left - 0.5);
self.minY = minY = Math.round(getPropAsNum(yProp, "px") + bounds.top - targetBounds.top - 0.5);
self.maxX = maxX = Math.round(minX + (bounds.width - targetBounds.width));
self.maxY = maxY = Math.round(minY + (bounds.height - targetBounds.height));
}
if (minX > maxX) {
self.minX = maxX;
self.maxX = maxX = minX;
minX = self.minX;
}
if (minY > maxY) {
self.minY = maxY;
self.maxY = maxY = minY;
minY = self.minY;
}
if (rotationMode) {
self.minRotation = minX;
self.maxRotation = maxX;
}
hasBounds = true;
}
if (vars.liveSnap) {
snap = vars.liveSnap === true ? vars.snap || {} : vars.liveSnap;
snapIsRaw = _isArray(snap) || _isFunction(snap);
if (rotationMode) {
snapX = buildSnapFunc(snapIsRaw ? snap : snap.rotation, minX, maxX, 1);
snapY = null;
} else {
if (snap.points) {
snapXY = buildPointSnapFunc(snapIsRaw ? snap : snap.points, minX, maxX, minY, maxY, snap.radius, scrollProxy ? -1 : 1);
} else {
if (allowX) {
snapX = buildSnapFunc(snapIsRaw ? snap : snap.x || snap.left || snap.scrollLeft, minX, maxX, scrollProxy ? -1 : 1);
}
if (allowY) {
snapY = buildSnapFunc(snapIsRaw ? snap : snap.y || snap.top || snap.scrollTop, minY, maxY, scrollProxy ? -1 : 1);
}
}
}
}
},
onThrowComplete = function onThrowComplete() {
self.isThrowing = false;
_dispatchEvent(self, "throwcomplete", "onThrowComplete");
},
onThrowInterrupt = function onThrowInterrupt() {
self.isThrowing = false;
},
animate = function animate(inertia, forceZeroVelocity) {
var snap, snapIsRaw, tween, overshootTolerance;
if (inertia && InertiaPlugin) {
if (inertia === true) {
snap = vars.snap || vars.liveSnap || {};
snapIsRaw = _isArray(snap) || _isFunct