(function (global, factory) { if (typeof define === "function" && define.amd) { define(['exports', 'react', 'prop-types'], factory); } else if (typeof exports !== "undefined") { factory(exports, require('react'), require('prop-types')); } else { var mod = { exports: {} }; factory(mod.exports, global.react, global.propTypes); global.reactSwipe = mod.exports; } })(this, function (exports, _react, _propTypes) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.setHasSupportToCaptureOption = setHasSupportToCaptureOption; var _react2 = _interopRequireDefault(_react); var _propTypes2 = _interopRequireDefault(_propTypes); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var supportsCaptureOption = false; function setHasSupportToCaptureOption(hasSupport) { supportsCaptureOption = hasSupport; } try { addEventListener('test', null, Object.defineProperty({}, 'capture', { get: function get() { setHasSupportToCaptureOption(true); } })); } catch (e) {} // eslint-disable-line no-empty function getSafeEventHandlerOpts() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { capture: true }; return supportsCaptureOption ? options : options.capture; } /** * [getPosition returns a position element that works for mouse or touch events] * @param {[Event]} event [the received event] * @return {[Object]} [x and y coords] */ function getPosition(event) { if ('touches' in event) { var _event$touches$ = event.touches[0], pageX = _event$touches$.pageX, pageY = _event$touches$.pageY; return { x: pageX, y: pageY }; } var screenX = event.screenX, screenY = event.screenY; return { x: screenX, y: screenY }; } var ReactSwipe = function (_Component) { _inherits(ReactSwipe, _Component); function ReactSwipe() { var _ref; _classCallCheck(this, ReactSwipe); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var _this = _possibleConstructorReturn(this, (_ref = ReactSwipe.__proto__ || Object.getPrototypeOf(ReactSwipe)).call.apply(_ref, [this].concat(args))); _this._handleSwipeStart = _this._handleSwipeStart.bind(_this); _this._handleSwipeMove = _this._handleSwipeMove.bind(_this); _this._handleSwipeEnd = _this._handleSwipeEnd.bind(_this); _this._onMouseDown = _this._onMouseDown.bind(_this); _this._onMouseMove = _this._onMouseMove.bind(_this); _this._onMouseUp = _this._onMouseUp.bind(_this); _this._setSwiperRef = _this._setSwiperRef.bind(_this); return _this; } _createClass(ReactSwipe, [{ key: 'componentDidMount', value: function componentDidMount() { if (this.swiper) { this.swiper.addEventListener('touchmove', this._handleSwipeMove, getSafeEventHandlerOpts({ capture: true, passive: false })); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.swiper) { this.swiper.removeEventListener('touchmove', this._handleSwipeMove, getSafeEventHandlerOpts({ capture: true, passive: false })); } } }, { key: '_onMouseDown', value: function _onMouseDown(event) { if (!this.props.allowMouseEvents) { return; } this.mouseDown = true; document.addEventListener('mouseup', this._onMouseUp); document.addEventListener('mousemove', this._onMouseMove); this._handleSwipeStart(event); } }, { key: '_onMouseMove', value: function _onMouseMove(event) { if (!this.mouseDown) { return; } this._handleSwipeMove(event); } }, { key: '_onMouseUp', value: function _onMouseUp(event) { this.mouseDown = false; document.removeEventListener('mouseup', this._onMouseUp); document.removeEventListener('mousemove', this._onMouseMove); this._handleSwipeEnd(event); } }, { key: '_handleSwipeStart', value: function _handleSwipeStart(event) { var _getPosition = getPosition(event), x = _getPosition.x, y = _getPosition.y; this.moveStart = { x: x, y: y }; this.props.onSwipeStart(event); } }, { key: '_handleSwipeMove', value: function _handleSwipeMove(event) { if (!this.moveStart) { return; } var _getPosition2 = getPosition(event), x = _getPosition2.x, y = _getPosition2.y; var deltaX = x - this.moveStart.x; var deltaY = y - this.moveStart.y; this.moving = true; // handling the responsability of cancelling the scroll to // the component handling the event var shouldPreventDefault = this.props.onSwipeMove({ x: deltaX, y: deltaY }, event); if (shouldPreventDefault && event.cancelable) { event.preventDefault(); } this.movePosition = { deltaX: deltaX, deltaY: deltaY }; } }, { key: '_handleSwipeEnd', value: function _handleSwipeEnd(event) { this.props.onSwipeEnd(event); var tolerance = this.props.tolerance; if (this.moving && this.movePosition) { if (this.movePosition.deltaX < -tolerance) { this.props.onSwipeLeft(1, event); } else if (this.movePosition.deltaX > tolerance) { this.props.onSwipeRight(1, event); } if (this.movePosition.deltaY < -tolerance) { this.props.onSwipeUp(1, event); } else if (this.movePosition.deltaY > tolerance) { this.props.onSwipeDown(1, event); } } this.moveStart = null; this.moving = false; this.movePosition = null; } }, { key: '_setSwiperRef', value: function _setSwiperRef(node) { this.swiper = node; this.props.innerRef(node); } }, { key: 'render', value: function render() { var _props = this.props, tagName = _props.tagName, className = _props.className, style = _props.style, children = _props.children, allowMouseEvents = _props.allowMouseEvents, onSwipeUp = _props.onSwipeUp, onSwipeDown = _props.onSwipeDown, onSwipeLeft = _props.onSwipeLeft, onSwipeRight = _props.onSwipeRight, onSwipeStart = _props.onSwipeStart, onSwipeMove = _props.onSwipeMove, onSwipeEnd = _props.onSwipeEnd, innerRef = _props.innerRef, tolerance = _props.tolerance, props = _objectWithoutProperties(_props, ['tagName', 'className', 'style', 'children', 'allowMouseEvents', 'onSwipeUp', 'onSwipeDown', 'onSwipeLeft', 'onSwipeRight', 'onSwipeStart', 'onSwipeMove', 'onSwipeEnd', 'innerRef', 'tolerance']); return _react2.default.createElement( this.props.tagName, _extends({ ref: this._setSwiperRef, onMouseDown: this._onMouseDown, onTouchStart: this._handleSwipeStart, onTouchEnd: this._handleSwipeEnd, className: className, style: style }, props), children ); } }]); return ReactSwipe; }(_react.Component); ReactSwipe.displayName = 'ReactSwipe'; ReactSwipe.propTypes = { tagName: _propTypes2.default.string, className: _propTypes2.default.string, style: _propTypes2.default.object, children: _propTypes2.default.node, allowMouseEvents: _propTypes2.default.bool, onSwipeUp: _propTypes2.default.func, onSwipeDown: _propTypes2.default.func, onSwipeLeft: _propTypes2.default.func, onSwipeRight: _propTypes2.default.func, onSwipeStart: _propTypes2.default.func, onSwipeMove: _propTypes2.default.func, onSwipeEnd: _propTypes2.default.func, innerRef: _propTypes2.default.func, tolerance: _propTypes2.default.number.isRequired }; ReactSwipe.defaultProps = { tagName: 'div', allowMouseEvents: false, onSwipeUp: function onSwipeUp() {}, onSwipeDown: function onSwipeDown() {}, onSwipeLeft: function onSwipeLeft() {}, onSwipeRight: function onSwipeRight() {}, onSwipeStart: function onSwipeStart() {}, onSwipeMove: function onSwipeMove() {}, onSwipeEnd: function onSwipeEnd() {}, innerRef: function innerRef() {}, tolerance: 0 }; exports.default = ReactSwipe; });