wix-style-react
Version:
344 lines (287 loc) • 11.8 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.changeItemDepth = exports.dragAndDropDriverFactory = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _reactDomInstance = require("react-dom-instance");
var _dragAndDropTestUtils = require("./dragAndDropTestUtils");
var _constants = require("../../DragAndDrop/Draggable/constants");
var _dragAndDropPuppeteerDriverFactory = require("./dragAndDropPuppeteerDriverFactory");
var getManager = function getManager(instance) {
return (0, _dragAndDropTestUtils.getInstanceOfDraggableProvider)(instance).getManager();
};
var getDraggableId = function getDraggableId(element, dataHook, id) {
return dataHook ? element.querySelector("[data-hook=\"".concat(dataHook, "\"]")).getAttribute(_constants.dataAttributes.id) : id;
};
var simulateMouseMove = function simulateMouseMove(dndInstance, draggableId) {
getManager(dndInstance).getBackend().simulateHover([(0, _dragAndDropTestUtils.getInstanceOfDraggableTarget)(dndInstance, draggableId).getHandlerId()]);
};
var dragAndDropDriverFactory = function dragAndDropDriverFactory(_ref) {
var base = _ref.base,
element = _ref.element;
var isPuppeteer = (base === null || base === void 0 ? void 0 : base.type) === 'puppeteer';
var dragAndDropPuppeteerDriver = (0, _dragAndDropPuppeteerDriverFactory.dragAndDropPuppeteerDriverFactory)({
base: base,
element: element
});
return {
beginDrag: function () {
var _beginDrag = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_ref2) {
var dataHook, id, dndInstance, draggableId, handleId;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
dataHook = _ref2.dataHook, id = _ref2.id;
if (!isPuppeteer) {
_context.next = 3;
break;
}
return _context.abrupt("return", dragAndDropPuppeteerDriver.beginDrag({
dataHook: dataHook,
id: id
}));
case 3:
dndInstance = (0, _reactDomInstance.findInstance)(element);
draggableId = getDraggableId(element, dataHook, id);
handleId = (0, _dragAndDropTestUtils.getInstanceOfDraggableSource)(dndInstance, draggableId).getHandlerId();
getManager(dndInstance).getBackend().simulateBeginDrag([handleId]);
case 7:
case "end":
return _context.stop();
}
}
}, _callee);
}));
function beginDrag(_x) {
return _beginDrag.apply(this, arguments);
}
return beginDrag;
}(),
dragOver: function () {
var _dragOver = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(_ref3) {
var dataHook, id, offset, dndInstance, draggableId;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
dataHook = _ref3.dataHook, id = _ref3.id, offset = _ref3.offset;
if (!isPuppeteer) {
_context2.next = 3;
break;
}
return _context2.abrupt("return", dragAndDropPuppeteerDriver.dragOver({
dataHook: dataHook,
id: id,
offset: offset
}));
case 3:
dndInstance = (0, _reactDomInstance.findInstance)(element);
draggableId = getDraggableId(element, dataHook, id);
if (offset) {
getManager(dndInstance).getMonitor().getClientOffset = jest.fn(function () {
return offset;
}); // on horizontal item move we have to simulate one more mouse move event
// in order to get correct drop target
simulateMouseMove(dndInstance, draggableId);
}
simulateMouseMove(dndInstance, draggableId);
case 7:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
function dragOver(_x2) {
return _dragOver.apply(this, arguments);
}
return dragOver;
}(),
endDrag: function () {
var _endDrag = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(item) {
var backend;
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (!isPuppeteer) {
_context3.next = 2;
break;
}
return _context3.abrupt("return", dragAndDropPuppeteerDriver.endDrag({
dataHook: item.dataHook
}));
case 2:
backend = getManager((0, _reactDomInstance.findInstance)(element)).getBackend();
backend.simulateDrop();
backend.simulateEndDrag();
case 5:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
function endDrag(_x3) {
return _endDrag.apply(this, arguments);
}
return endDrag;
}(),
getItemPosition: function () {
var _getItemPosition = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(_ref4) {
var dataHook, draggableItems;
return _regenerator["default"].wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
dataHook = _ref4.dataHook;
_context5.next = 3;
return base.$$("[".concat(_constants.dataAttributes.draggableTarget, "]")).map( /*#__PURE__*/function () {
var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(item) {
return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return item.$$("[".concat(_constants.dataAttributes.draggableSource, "]")).get(0);
case 2:
_context4.next = 4;
return _context4.sent.attr('data-hook');
case 4:
_context4.t0 = _context4.sent;
return _context4.abrupt("return", {
dataHook: _context4.t0
});
case 6:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
return function (_x5) {
return _ref5.apply(this, arguments);
};
}());
case 3:
draggableItems = _context5.sent;
return _context5.abrupt("return", draggableItems.findIndex(function (item) {
return item.dataHook === dataHook;
}));
case 5:
case "end":
return _context5.stop();
}
}
}, _callee5);
}));
function getItemPosition(_x4) {
return _getItemPosition.apply(this, arguments);
}
return getItemPosition;
}(),
getItemDepth: function () {
var _getItemDepth = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(_ref6) {
var dataHook, depth;
return _regenerator["default"].wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
dataHook = _ref6.dataHook;
_context6.next = 3;
return base.$("[data-hook=\"".concat(dataHook, "\"]")).attr(_constants.dataAttributes.depth);
case 3:
depth = _context6.sent;
return _context6.abrupt("return", parseInt(depth, 10));
case 5:
case "end":
return _context6.stop();
}
}
}, _callee6);
}));
function getItemDepth(_x6) {
return _getItemDepth.apply(this, arguments);
}
return getItemDepth;
}()
};
};
exports.dragAndDropDriverFactory = dragAndDropDriverFactory;
var changeItemDepthByOneLevel = /*#__PURE__*/function () {
var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(_ref7) {
var dragAndDropDriver, dataHook, offset;
return _regenerator["default"].wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
dragAndDropDriver = _ref7.dragAndDropDriver, dataHook = _ref7.dataHook, offset = _ref7.offset;
_context7.next = 3;
return dragAndDropDriver.beginDrag({
dataHook: dataHook
});
case 3:
_context7.next = 5;
return dragAndDropDriver.dragOver({
dataHook: dataHook,
offset: {
x: offset,
y: 0
}
});
case 5:
_context7.next = 7;
return dragAndDropDriver.endDrag({
dataHook: dataHook
});
case 7:
case "end":
return _context7.stop();
}
}
}, _callee7);
}));
return function changeItemDepthByOneLevel(_x7) {
return _ref8.apply(this, arguments);
};
}();
var changeItemDepth = /*#__PURE__*/function () {
var _ref10 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(_ref9) {
var dragAndDropDriver, depthLevel, threshold, dataHook, i;
return _regenerator["default"].wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
dragAndDropDriver = _ref9.dragAndDropDriver, depthLevel = _ref9.depthLevel, threshold = _ref9.threshold, dataHook = _ref9.dataHook;
i = 0;
case 2:
if (!(i < Math.abs(depthLevel))) {
_context8.next = 8;
break;
}
_context8.next = 5;
return changeItemDepthByOneLevel({
dragAndDropDriver: dragAndDropDriver,
dataHook: dataHook,
offset: depthLevel > 0 ? threshold : threshold * -1
});
case 5:
i++;
_context8.next = 2;
break;
case 8:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
return function changeItemDepth(_x8) {
return _ref10.apply(this, arguments);
};
}();
exports.changeItemDepth = changeItemDepth;