UNPKG

react-aim

Version:

Determine the cursor aim for triggering mouse events.

260 lines (220 loc) 10.2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; 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; }; }(); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.default = function (target) { var spec = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; if (spec === null && (typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object') { spec = target; target = null; } return function (WrappedComponent) { var _class, _temp; return _temp = _class = function (_Component) { _inherits(_class, _Component); _createClass(_class, [{ key: 'getChildContext', value: function getChildContext() { return { source: this }; } }]); function _class() { _classCallCheck(this, _class); var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this)); _this._isOver = false; _this._isMounted = false; _this.childrenTargets = []; _this.buffer = function (e, cb) { var timeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; setTimeout(function () { return cb(e); }, timeout); }; _this.bufferHandleMouseMove = function (e) { return _this.buffer(e, _this.handleMouseMove); }; _this.bufferHandleMouseOut = function (e) { return _this.buffer(e, _this.handleMouseOut); }; _this.handleMouseOut = function (e) { if (!_this._isMounted) return _this.unbindEvents(); if (e.toElement == null && e.relatedTarget == null) { _this.handleMouseLeave(e); } else { _this.handleMouseMove(e); } }; _this.handleMouseMove = function (e) { if (!_this._isMounted) return _this.unbindEvents(); if (_monitor2.default.mouseOver(e, _this)) _this.handleMouseEnter(e);else _this.handleMouseLeave(e); }; _this.handleMouseEnter = function () { if (!_this._isOver) { _monitor2.default.requestMouseEnter(_this).then(function () { _this.forceMouseEnter(); }).catch(function () { return null; }); } }; _this.forceMouseEnter = function () { _this._isOver = true; _this.triggerMouseEnter(); _this.trackMouseLeave(); }; _this.handleMouseLeave = function () { if (_this._isOver) { _monitor2.default.requestMouseLeave(_this).then(function () { return _this.forceMouseLeave(); }).catch(function () { return null; }); } }; _this.forceMouseLeave = function () { if (_this._isOver) { _this._isOver = false; _this.triggerMouseLeave(); _this.untrackMouseLeave(); } }; _this._target = target; _this.spec = spec; return _this; } _createClass(_class, [{ key: 'isOver', value: function isOver() { return this._isOver; } }, { key: 'hasChildrenOver', value: function hasChildrenOver() { var target = this.target; if (target && (target.isOver() || target.hasChildrenOver())) return true; for (var i = 0, len = this.childrenTargets.length; i < len; ++i) { if (this.childrenTargets[i].isOver() || this.childrenTargets[i].hasChildrenOver()) return true; } return false; } }, { key: 'hasChildrenAimed', value: function hasChildrenAimed() { var target = this.target; if (target && (target.isAimed() || target.hasChildrenAimed())) return true; for (var i = 0, len = this.childrenTargets.length; i < len; ++i) { if (this.childrenTargets[i].isAimed() || this.childrenTargets[i].hasChildrenAimed()) return true; } return false; } }, { key: 'addChildrenTarget', value: function addChildrenTarget(target) { this.childrenTargets.push(target); } }, { key: 'removeChildrenTarget', value: function removeChildrenTarget(target) { this.childrenTargets = this.childrenTargets.filter(function (item) { return item !== target; }); } }, { key: 'componentDidMount', value: function componentDidMount() { if (this.context.target) { this.context.target.addChildrenSource(this); } this._isMounted = true; var element = _reactDom2.default.findDOMNode(this); element.addEventListener('mousemove', this.bufferHandleMouseMove); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.context.target) { this.context.target.removeChildrenSource(this); } this.unbindEvents(); this._isMounted = false; } }, { key: 'unbindEvents', value: function unbindEvents() { if (this._isMounted) { var element = _reactDom2.default.findDOMNode(this); element.removeEventListener('mousemove', this.bufferHandleMouseMove); } document.removeEventListener('mousemove', this.bufferHandleMouseMove); document.removeEventListener('mouseout', this.bufferHandleMouseOut); } }, { key: 'trackMouseLeave', value: function trackMouseLeave() { var element = _reactDom2.default.findDOMNode(this); document.addEventListener('mousemove', this.bufferHandleMouseMove); document.addEventListener('mouseout', this.bufferHandleMouseOut); element.removeEventListener('mousemove', this.bufferHandleMouseMove); } }, { key: 'untrackMouseLeave', value: function untrackMouseLeave() { var element = _reactDom2.default.findDOMNode(this); document.removeEventListener('mousemove', this.bufferHandleMouseMove); document.removeEventListener('mouseout', this.bufferHandleMouseOut); element.addEventListener('mousemove', this.bufferHandleMouseMove); } }, { key: 'triggerMouseEnter', value: function triggerMouseEnter() { if (_typeof(this.spec) === 'object' && this.spec && typeof this.spec.mouseEnter === 'function') { this.spec.mouseEnter(this.wrappedComponent.props, this.wrappedComponent); } } }, { key: 'triggerMouseLeave', value: function triggerMouseLeave() { if (_typeof(this.spec) === 'object' && this.spec && typeof this.spec.mouseLeave === 'function') { this.spec.mouseLeave(this.wrappedComponent.props, this.wrappedComponent); } } }, { key: 'render', value: function render() { var _this2 = this; return _react2.default.createElement(WrappedComponent, _extends({ ref: function ref(_ref) { return _this2.wrappedComponent = _ref; } }, this.props)); } }, { key: 'target', get: function get() { if (typeof this._target === 'function' && this.wrappedComponent) return this._target(this.wrappedComponent.props, this.wrappedComponent); return null; } }]); return _class; }(_react.Component), _class.childContextTypes = { source: _propTypes2.default.object }, _class.contextTypes = { target: _propTypes2.default.object }, _temp; }; }; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _monitor = require('./monitor'); var _monitor2 = _interopRequireDefault(_monitor); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; }