UNPKG

wix-style-react

Version:
291 lines (245 loc) • 10.2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; 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 _dec, _class; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactDnd = require('react-dnd'); var _reactDndHtml5Backend = require('react-dnd-html5-backend'); var _noop = require('lodash/noop'); var _noop2 = _interopRequireDefault(_noop); var _DragLayer = require('./DragLayer'); var _DragLayer2 = _interopRequireDefault(_DragLayer); var _types = require('../types'); 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; } /* eslint-disable new-cap */ var source = { beginDrag: function beginDrag(_ref) { var id = _ref.id, index = _ref.index, containerId = _ref.containerId, groupName = _ref.groupName, item = _ref.item, onMoveOut = _ref.onMoveOut, onDragStart = _ref.onDragStart; if (onDragStart) { onDragStart({ id: id, index: index, containerId: containerId, groupName: groupName, item: item }); } /** we setup monitor.getItem() snapshot, so we will be always able to get info about item that we drag */ return { id: id, index: index, containerId: containerId, groupName: groupName, originalIndex: index, // as index is mutable during d&d, we need another storage for original index originalItem: item, onMoveOut: onMoveOut, realTime: { onMoveOut: onMoveOut, containerId: containerId } }; }, endDrag: function endDrag(_ref2, monitor) { var id = _ref2.id, index = _ref2.index, containerId = _ref2.containerId, groupName = _ref2.groupName, item = _ref2.item, onDrop = _ref2.onDrop, onDragEnd = _ref2.onDragEnd; /** if drop was called, on drop target and drag is end, then we notify parent about this */ if (onDragEnd) { onDragEnd({ id: id, index: index, containerId: containerId, groupName: groupName, item: item }); } if (monitor.getDropResult()) { var isSameGroup = groupName && monitor.getItem().groupName && groupName === monitor.getDropResult().groupName; var isSameContainer = containerId === monitor.getDropResult().containerId; if (isSameGroup || isSameContainer) { onDrop({ payload: monitor.getItem().originalItem, // original item removedIndex: monitor.getItem().originalIndex, // original item index addedIndex: monitor.getItem().index, // new item index addedToContainerId: monitor.getDropResult().containerId, // new container for item removedFromContainerId: containerId // original item container }); } } }, isDragging: function isDragging(_ref3, monitor) { var id = _ref3.id, containerId = _ref3.containerId, groupName = _ref3.groupName; var item = monitor.getItem(); var isSameGroup = groupName && item.groupName && groupName === item.groupName; var isSameContainer = containerId === item.containerId; return (isSameGroup || isSameContainer) && item.id === id; } }; var collect = function collect(connect, monitor) { return { connectDragSource: connect.dragSource(), connectDragPreview: connect.dragPreview(), isDragging: monitor.isDragging() }; }; var DraggableSource = (_dec = (0, _reactDnd.DragSource)(_types.ItemTypes.DRAGGABLE, source, collect), _dec(_class = function (_React$Component) { _inherits(DraggableSource, _React$Component); function DraggableSource() { var _ref4; var _temp, _this, _ret; _classCallCheck(this, DraggableSource); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref4 = DraggableSource.__proto__ || Object.getPrototypeOf(DraggableSource)).call.apply(_ref4, [this].concat(args))), _this), _this.state = { offsetOfHandle: { x: 0, y: 0 } }, _this._renderPreview = function (_ref5) { var previewStyles = _ref5.previewStyles; var _this$props = _this.props, renderItem = _this$props.renderItem, id = _this$props.id, item = _this$props.item; return renderItem({ id: id, item: item, previewStyles: previewStyles, isPreview: true, connectHandle: _noop2.default }); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(DraggableSource, [{ key: 'componentDidMount', value: function componentDidMount() { if (this.props.connectDragPreview) { this.props.connectDragPreview((0, _reactDndHtml5Backend.getEmptyImage)(), { captureDraggingState: true }); } this.updateDiff(); } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps) { if (prevProps.id !== this.props.id || prevProps.containerId !== this.props.containerId) { this.updateDiff(); } } }, { key: 'updateDiff', value: function updateDiff() { /* in case if we have handle, the drag will start in wrong position and we need to fix this */ if (this.props.withHandle && this.handleNode) { this.setState({ offsetOfHandle: { x: this.handleNode.getBoundingClientRect().x - this.rootNode.getBoundingClientRect().x, y: this.handleNode.getBoundingClientRect().y - this.rootNode.getBoundingClientRect().y } }); } } }, { key: '_renderDraggableItem', value: function _renderDraggableItem() { var _this2 = this; var _props = this.props, isDragging = _props.isDragging, connectDragSource = _props.connectDragSource, withHandle = _props.withHandle, renderItem = _props.renderItem, id = _props.id, item = _props.item; if (withHandle) { return renderItem({ id: id, item: item, isPlaceholder: isDragging, connectHandle: function connectHandle(handle) { var handleWithRef = _react2.default.cloneElement(handle, { ref: function ref(node) { return _this2.handleNode = _reactDom2.default.findDOMNode(node); } }); return connectDragSource(handleWithRef); } }); } return connectDragSource(renderItem({ id: id, item: item, isPlaceholder: isDragging, connectHandle: _noop2.default })); } }, { key: '_renderPreviewItem', value: function _renderPreviewItem() { var _props2 = this.props, id = _props2.id, usePortal = _props2.usePortal; return _react2.default.createElement(_DragLayer2.default, { offsetOfHandle: this.state.offsetOfHandle, usePortal: usePortal, renderPreview: this._renderPreview, id: id, draggedType: _types.ItemTypes.DRAGGABLE }); } }, { key: 'render', value: function render() { var _this3 = this; var connectDragSource = this.props.connectDragSource; return connectDragSource ? _react2.default.createElement( 'div', { ref: function ref(node) { return _this3.rootNode = node; } }, this._renderDraggableItem(), this._renderPreviewItem() ) : null; } }]); return DraggableSource; }(_react2.default.Component)) || _class); exports.default = DraggableSource; DraggableSource.propTypes = { isDragging: _propTypes2.default.bool, // from react-dnd connectDragSource: _propTypes2.default.func, // from react-dnd connectDragPreview: _propTypes2.default.func, // from react-dnd usePortal: _propTypes2.default.bool, groupName: _propTypes2.default.string, containerId: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]), renderItem: _propTypes2.default.func, index: _propTypes2.default.number, id: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]), item: _propTypes2.default.object, withHandle: _propTypes2.default.bool, onDrop: _propTypes2.default.func, onMoveOut: _propTypes2.default.func, onDragStart: _propTypes2.default.func, onDragEnd: _propTypes2.default.func };