wix-style-react
Version:
326 lines (279 loc) • 11 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { findInstance } from 'react-dom-instance';
import { getInstanceOfDraggableProvider, getInstanceOfDraggableSource, getInstanceOfDraggableTarget } from './dragAndDropTestUtils';
import { dataAttributes } from '../../DragAndDrop/Draggable/constants';
import { dragAndDropPuppeteerDriverFactory } from './dragAndDropPuppeteerDriverFactory';
var getManager = function getManager(instance) {
return getInstanceOfDraggableProvider(instance).getManager();
};
var getDraggableId = function getDraggableId(element, dataHook, id) {
return dataHook ? element.querySelector("[data-hook=\"".concat(dataHook, "\"]")).getAttribute(dataAttributes.id) : id;
};
var simulateMouseMove = function simulateMouseMove(dndInstance, draggableId) {
getManager(dndInstance).getBackend().simulateHover([getInstanceOfDraggableTarget(dndInstance, draggableId).getHandlerId()]);
};
export 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 = dragAndDropPuppeteerDriverFactory({
base: base,
element: element
});
return {
beginDrag: function () {
var _beginDrag = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref2) {
var dataHook, id, dndInstance, draggableId, handleId;
return _regeneratorRuntime.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 = findInstance(element);
draggableId = getDraggableId(element, dataHook, id);
handleId = 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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(_ref3) {
var dataHook, id, offset, dndInstance, draggableId;
return _regeneratorRuntime.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 = 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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(item) {
var backend;
return _regeneratorRuntime.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(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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(_ref4) {
var dataHook, draggableItems;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
dataHook = _ref4.dataHook;
_context5.next = 3;
return base.$$("[".concat(dataAttributes.draggableTarget, "]")).map( /*#__PURE__*/function () {
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(item) {
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return item.$$("[".concat(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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(_ref6) {
var dataHook, depth;
return _regeneratorRuntime.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(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;
}()
};
};
var changeItemDepthByOneLevel = /*#__PURE__*/function () {
var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(_ref7) {
var dragAndDropDriver, dataHook, offset;
return _regeneratorRuntime.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);
};
}();
export var changeItemDepth = /*#__PURE__*/function () {
var _ref10 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(_ref9) {
var dragAndDropDriver, depthLevel, threshold, dataHook, i;
return _regeneratorRuntime.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);
};
}();