"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _reactDom = _interopRequireDefault(require("react-dom")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _Hide = require("../Hide"); var _Tooltip = require("../Tooltip"); var _utils = require("../utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _templateObject() { var data = _taggedTemplateLiteral(["\n @media screen and (max-width: 768px) {\n display: none;\n }\n\n * {\n box-sizing: border-box;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB',\n 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif, 'Apple Color Emoji',\n 'Segoe UI Emoji', 'Segoe UI Symbol';\n }\n\n &:hover {\n ", "\n ", " {\n opacity: 1;\n transform: scale(1);\n }\n ", " {\n opacity: ", ";\n visibility: visible;\n transform: translate(-50%, 0);\n }\n }\n"]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } var initRight = 60; var initBottom = 30; var Container = _styledComponents.default.div(_templateObject(), function (_ref) { var hide = _ref.hide, dragged = _ref.dragged; return hide && !dragged ? 'right: 0!important;' : ''; }, _Hide.HideWrapper, _Tooltip.TooltipWrapper, function (_ref2) { var open = _ref2.open, hide = _ref2.hide; return open || hide ? 0 : 1; }); var Draggable = /*#__PURE__*/function (_React$Component) { _inherits(Draggable, _React$Component); var _super = _createSuper(Draggable); function Draggable(props) { var _this; _classCallCheck(this, Draggable); _this = _super.call(this, props); _this.isFixedElem = function (element) { var position = element && window.getComputedStyle(element) && window.getComputedStyle(element).position ? window.getComputedStyle(element).position : 'static'; return position === 'fixed'; }; _this.overlapDetection = function () { var node = _this.nodeDom; var _node$getBoundingClie = node.getBoundingClientRect(), left = _node$getBoundingClie.left, top = _node$getBoundingClie.top, right = _node$getBoundingClie.right, bottom = _node$getBoundingClie.bottom, width = _node$getBoundingClie.width, height = _node$getBoundingClie.height; var aroundElems = [document.elementFromPoint(left, top), document.elementFromPoint(right, bottom), document.elementFromPoint(left + width / 2, top + height / 2)]; // eslint-disable-next-line no-restricted-syntax for (var _i = 0, _aroundElems = aroundElems; _i < _aroundElems.length; _i++) { var elem = _aroundElems[_i]; if (elem !== node && _this.isFixedElem(elem)) { // other element // const { width: aroundW, height: aroundH } = elem.getBoundingClientRect(); // const adjustRight = width / 2 + aroundW / 2 + initRight; // const adjustBottom = height / 2 + aroundH / 2 + initBottom; // console.log('offsetRight', adjustRight, 'offsetBottom', adjustBottom); // node.style.right = `${adjustRight}px`; // node.style.bottom = `${adjustBottom}px`; if (_this.props.onOverlap) { _this.props.onOverlap(); } break; } } }; _this.handleResize = function () { // update defaultScrollBarSize _this.defaultScrollBarSize = (0, _utils.getScrollBarSize)(); var node = _this.nodeDom; var clientRect = node.getBoundingClientRect(); var width = clientRect.width, height = clientRect.height; var _window$getComputedSt = window.getComputedStyle(node), styleRight = _window$getComputedSt.right, styleBottom = _window$getComputedSt.bottom; var right = Number(styleRight.replace('px', '')); var bottom = Number(styleBottom.replace('px', '')); var clientWidth = (0, _utils.getClientWidth)(); var clientHeight = (0, _utils.getClientHeight)(); var deltaX = clientWidth - right - width; var deltaY = clientHeight - bottom - height; // console.log('resize obj', { // deltaX, // deltaY, // right, // bottom, // clientRect, // }); // drag boundary detection if (deltaX < 0) { right += deltaX; } if (deltaY < 0) { bottom += deltaY; } node.style.bottom = "".concat(bottom, "px"); node.style.right = "".concat(right, "px"); }; _this.handleMouseDown = function (e) { _this.intervalStart = new Date().getTime(); var scroll = (0, _utils.getScrollOffsets)(); var node = _this.nodeDom; var startX = e.clientX + scroll.x; var startY = e.clientY + scroll.y; var origX = node.offsetLeft; var origY = node.offsetTop; _this.deltaX = startX - origX; _this.deltaY = startY - origY; _this.addMouseEventListener(); if (_this.props.onDragStart) { _this.props.onDragStart(); } _this.setState({ dragged: true }); // We've handled this event. Don't let anybody else see it. if (e.stopPropagation) e.stopPropagation(); // Standard model else e.cancelBubble = true; // IE // Now prevent any default action. if (e.preventDefault) e.preventDefault(); // Standard model else e.returnValue = false; // IE }; _this.handleContextMenu = function () { _this.removeMouseEventListener(); }; _this.addMouseEventListener = function () { window.addEventListener('contextmenu', _this.handleContextMenu, false); window.addEventListener('mousemove', _this.handleMouseMove, false); window.addEventListener('mouseup', _this.handleMouseUp, false); }; _this.removeMouseEventListener = function () { window.removeEventListener('contextmenu', _this.handleContextMenu, false); window.removeEventListener('mousemove', _this.handleMouseMove, false); window.removeEventListener('mouseup', _this.handleMouseUp, false); }; _this.handleMouseMove = function (e) { var clientX = e.clientX, clientY = e.clientY; if (e.button !== 0) { // not left mouse down _this.removeMouseEventListener(); return; } var dragged = _this.state.dragged; var _assertThisInitialize = _assertThisInitialized(_this), deltaX = _assertThisInitialize.deltaX, deltaY = _assertThisInitialize.deltaY; var node = _this.nodeDom; var hide = _this.props.hide; if (!dragged || !!hide) { return; } var scroll = (0, _utils.getScrollOffsets)(); var clientWidth = (0, _utils.getClientWidth)(); var clientHeight = (0, _utils.getClientHeight)(); var _node$getBoundingClie2 = node.getBoundingClientRect(), width = _node$getBoundingClie2.width, height = _node$getBoundingClie2.height; var left = clientX + scroll.x - deltaX; var top = clientY + scroll.y - deltaY; var right = clientWidth - left - width; var bottom = clientHeight - top - height; // boundary detection // left boundary if (right > clientWidth - width) { right = clientWidth - width; } // top boundary if (bottom > clientHeight - height) { bottom = clientHeight - height; } // right boundary if (right < 0) { right = 0; } // bottom boundary if (bottom < 0) { bottom = 0; } // for better performance node.style.right = "".concat(right - _this.defaultScrollBarSize, "px"); node.style.bottom = "".concat(bottom, "px"); _this.setState({ dragged: true // right, // bottom, }, function () { if (_this.props.onDrag) { _this.props.onDrag({ right: right, bottom: bottom }); } }); }; _this.handleMouseUp = function (e) { _this.removeMouseEventListener(); var interval = new Date().getTime() - _this.intervalStart; if (interval < 150 && e.target.id !== 'umi-ui-mini-hide') { _this.props.onClick(e); _this.setState({ dragged: false }); return; } _this.setState({ dragged: false }, function () { if (_this.props.onDragEnd) { _this.props.onDragEnd(); } }); }; _this.saveRef = function (node) { if (node) { _this.node = node; } }; _this.node = null; _this.intervalStart = 0; _this.deltaX = 0; _this.deltaY = 0; _this.defaultScrollBarSize = (0, _utils.getScrollBarSize)(); _this.state = { dragged: false, width: 0 }; return _this; } _createClass(Draggable, [{ key: "componentDidMount", value: function componentDidMount() { var hide = this.props.hide; window.addEventListener('resize', this.handleResize, false); // eslint-disable-next-line this.nodeDom = _reactDom.default.findDOMNode(this); var _this$nodeDom$getBoun = this.nodeDom.getBoundingClientRect(), width = _this$nodeDom$getBoun.width; window.addEventListener('load', this.overlapDetection, false); this.setState({ width: width }); } }, { key: "componentDidUpdate", value: function componentDidUpdate(preProps) { var preOpen = preProps.open; var open = this.props.open; var bodyIsOverflowing = document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight) && window.innerWidth > document.body.offsetWidth; // 关闭到打开 增加 right if (!preOpen && open) { var scrollBarSize = (0, _utils.getScrollBarSize)(); var dom = this.nodeDom; var right = dom.style.right || '60px'; if (dom && bodyIsOverflowing) { requestAnimationFrame(function () { dom.style.right = "".concat(parseInt(right.replace('px', ''), 0) + scrollBarSize, "px"); }); } } // 打开到关闭,减少right if (preOpen && !open) { var _scrollBarSize = (0, _utils.getScrollBarSize)(); var _dom = this.nodeDom; var _right = _dom.style.right || '60px'; if (_dom && bodyIsOverflowing) { requestAnimationFrame(function () { _dom.style.right = "".concat(parseInt(_right.replace('px', ''), 0) - _scrollBarSize, "px"); }); } } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener('load', this.overlapDetection, false); this.removeMouseEventListener(); window.removeEventListener('resize', this.handleResize, false); } }, { key: "render", value: function render() { var _this$props = this.props, children = _this$props.children, hide = _this$props.hide, open = _this$props.open; var _this$state = this.state, dragged = _this$state.dragged, width = _this$state.width; var style = { cursor: dragged ? 'grab' : 'pointer', position: 'fixed', zIndex: 999, right: hide ? -width / 1.5 : initRight, bottom: initBottom, fontSize: 14, transition: hide ? 'all .3s' : '' }; return /*#__PURE__*/_react.default.createElement(Container, { ref: this.saveRef, onMouseDown: this.handleMouseDown, dragged: dragged, style: style, open: open, hide: hide }, children); } }]); return Draggable; }(_react.default.Component); exports.default = Draggable;