wix-style-react
Version:
wix-style-react
121 lines (104 loc) • 5.3 kB
JavaScript
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;
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; }
import React from 'react';
import WixComponent from '../../../BaseComponents/WixComponent';
import PropTypes from 'prop-types';
import { DropTarget } from 'react-dnd';
import { ItemTypes } from '../types';
/* eslint-disable new-cap */
var target = {
drop: function drop(props, monitor) {
/** if drop was already done(on child), we skip this drop call */
if (monitor.getDropResult()) {
return;
}
/**
after drop released we send containerId and index of dropped item to dropResult,
so endDrag inside of drag source can use this data
*/
return {
containerId: props.containerId,
index: props.index,
groupName: props.groupName
};
},
hover: function hover(props, monitor, component) {
var monitorItem = monitor.getItem();
var dragIndex = monitorItem.index; // position of item that we drag
var hoverIndex = props.index; // position of item that we hover(want to put draggable item on it)
var isSameGroup = props.groupName && monitorItem.groupName && props.groupName === monitorItem.groupName; // check that items from same group
var isSameContainer = props.containerId === monitorItem.realTime.containerId; // check that items from same container
/** in case that item not in same group and not from same container - do nothing */
if (!isSameContainer && !isSameGroup) {
return;
}
/** in case that we hover over itself - do nothing */
if (!component || hoverIndex === dragIndex && isSameContainer) {
return;
}
/**
if item is from same group but different container, thats mean that we move item
from one container to another, and we need to move out item from previous container
*/
if (isSameGroup && !isSameContainer) {
monitorItem.realTime.onMoveOut(monitorItem.id);
}
/**
as react-dnd store same snapshot in monitor(so containerId of item will be same, even if we moved it with hover to another container)
after any hovers, we need to save also real position of monitor, with real links to current container
*/
monitorItem.realTime.onMoveOut = props.onMoveOut;
monitorItem.realTime.containerId = props.containerId;
/**
call callback, to ask parent to do some action, for example swap items or add new one,
we send original position of item and new one, also id of item and original item state(
it required for case, when we moving item from 1 container to another
)
*/
props.onHover(dragIndex, hoverIndex, {
id: monitorItem.id,
item: monitorItem.originalItem
});
/** set new index for item */
monitorItem.index = hoverIndex;
}
};
var collect = function collect(connect) {
return {
connectDropTarget: connect.dropTarget()
};
};
var DraggableTarget = (_dec = DropTarget(ItemTypes.DRAGGABLE, target, collect), _dec(_class = function (_WixComponent) {
_inherits(DraggableTarget, _WixComponent);
function DraggableTarget() {
_classCallCheck(this, DraggableTarget);
return _possibleConstructorReturn(this, (DraggableTarget.__proto__ || Object.getPrototypeOf(DraggableTarget)).apply(this, arguments));
}
_createClass(DraggableTarget, [{
key: 'render',
value: function render() {
if (!this.props.connectDropTarget) {
return null;
}
return this.props.connectDropTarget(React.createElement(
'div',
null,
this.props.children
));
}
}]);
return DraggableTarget;
}(WixComponent)) || _class);
DraggableTarget.propTypes = {
children: PropTypes.any,
connectDropTarget: PropTypes.func, // from react-dnd
containerId: PropTypes.string,
groupName: PropTypes.string,
index: PropTypes.number,
onMoveOut: PropTypes.func,
onHover: PropTypes.func
};
export default DraggableTarget;