UNPKG

wix-style-react

Version:
385 lines (300 loc) • 12.5 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.swapItems = swapItems; exports.getSiblingsByNodePosition = getSiblingsByNodePosition; exports.getNodeAtPosition = getNodeAtPosition; exports.getNodePosition = getNodePosition; exports.recursiveMap = recursiveMap; exports.isLastItem = isLastItem; exports.isFistItem = isFistItem; exports.isRootItem = isRootItem; exports.hoverAboveItself = hoverAboveItself; exports.isItemHasChildren = isItemHasChildren; exports.getDropParent = getDropParent; exports.moveItem = moveItem; exports.moveItemToTheChildOfPrevSibling = moveItemToTheChildOfPrevSibling; exports.moveItemVertically = moveItemVertically; exports.moveItemOutsideOfTheParent = moveItemOutsideOfTheParent; exports.setCollapse = setCollapse; exports.addToTree = exports.removeFromTree = exports.getValuesByKey = exports.getDepth = exports.generateUniqueGroupId = exports.VerticalMovementDirection = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } var VerticalMovementDirection = { top: -1, bottom: 1 }; exports.VerticalMovementDirection = VerticalMovementDirection; var generateUniqueGroupId = function generateUniqueGroupId() { if (process.env.NODE_ENV === 'test') { return 'test_id'; } return Symbol('nestable-list-id'); }; exports.generateUniqueGroupId = generateUniqueGroupId; var getDepth = function getDepth(item, childrenProperty) { // returns depth of item and children var depth = 0; if (item[childrenProperty]) { item[childrenProperty].forEach(function (d) { var tmpDepth = getDepth(d, childrenProperty); if (tmpDepth > depth) { depth = tmpDepth; } }); } return depth + 1; }; exports.getDepth = getDepth; var getValuesByKey = function getValuesByKey(data, key, childrenProp) { var values = [data[key]]; if (data[childrenProp]) { data[childrenProp].forEach(function (item) { values.push.apply(values, (0, _toConsumableArray2["default"])(getValuesByKey(item, key, childrenProp))); }); } return values; }; // this method mutates an array exports.getValuesByKey = getValuesByKey; var removeFromTree = function removeFromTree(items, position) { var childrenProperty = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var lastIndex = position.length - 1; var itemsRemoveCandidate = items; position.forEach(function (pos, index) { if (index === lastIndex) { itemsRemoveCandidate.splice(pos, 1)[0]; // eslint-disable-line no-unused-expressions } else { itemsRemoveCandidate = itemsRemoveCandidate[pos][childrenProperty]; } }); return items; }; // this methods mutates an array exports.removeFromTree = removeFromTree; var addToTree = function addToTree(items, item, position) { var childrenProperty = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'children'; var lastIndex = position.length - 1; var itemsAddCandidate = items; position.forEach(function (pos, index) { if (index === lastIndex) { itemsAddCandidate.splice(pos, 0, item); // eslint-disable-line no-unused-expressions } else { if (itemsAddCandidate[pos] && !itemsAddCandidate[pos][childrenProperty]) { itemsAddCandidate[pos][childrenProperty] = []; } itemsAddCandidate = itemsAddCandidate[pos][childrenProperty]; } }); return items; }; exports.addToTree = addToTree; function swapItems(items, firstItem, secondItem) { var childProp = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'children'; return recursiveMap(items, function (item) { if (item.id === firstItem.id) { return secondItem; } if (item.id === secondItem.id) { return firstItem; } return item; }, childProp); } function getSiblingsByNodePosition(items) { var position = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var childProp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; return position.reduce(function (siblings, pos, i) { if (siblings && siblings[pos]) { if (position.length - 1 === i) { return siblings; } return siblings[pos][childProp]; } return null; }, items); } function getNodeAtPosition(items) { var position = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var childProp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; return position.reduce(function (siblings, pos, i) { if (siblings[pos]) { if (position.length - 1 === i) { return siblings[pos]; } return siblings[pos][childProp]; } return null; }, items); } function getNodePosition(items, item) { var childProp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var position = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : []; if (!items) { return; } for (var i = 0; i < items.length; i++) { var currentPosition = [].concat((0, _toConsumableArray2["default"])(position), [i]); if (item.id === items[i].id) { return currentPosition; } var nodePosition = getNodePosition(items[i][childProp], item, childProp, currentPosition); if (nodePosition) { return nodePosition; } } } function recursiveMap(items, predicateFn) { var childProp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; return items.map(function (currentItem) { var item = predicateFn(currentItem); if (item !== currentItem) { return item; } if (currentItem[childProp]) { return _objectSpread(_objectSpread({}, currentItem), {}, { children: recursiveMap(currentItem[childProp], predicateFn) }); } return currentItem; }); } function isLastItem(siblings, item) { return siblings && siblings[siblings.length - 1] === item; } function isFistItem(siblings, item) { return siblings && siblings[0] === item; } function isRootItem(depth) { return depth === 1; } function hoverAboveItself(prevPosition, nextPosition) { return prevPosition.every(function (position, index) { return nextPosition[index] === position; }); } function isItemHasChildren(item, childrenProperty) { return Boolean(item[childrenProperty] && item[childrenProperty].length); } function getDropParent(items, nextPosition, childrenProperty) { return nextPosition.slice(1, nextPosition.length - 1).reduce(function (item, childIndex) { if (!item) { return null; } return item[childrenProperty][childIndex]; }, items[nextPosition[0]]); } function moveItem(items, item, currentPosition, newPosition) { var childrenProp = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 'children'; var newItems = (0, _toConsumableArray2["default"])(items); removeFromTree(newItems, currentPosition, childrenProp); addToTree(newItems, item, newPosition, childrenProp); return newItems; } function moveItemToTheChildOfPrevSibling(items, item) { var childrenProp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var currentPosition = getNodePosition(items, item, childrenProp); // if there is not prev sibling we cannot move if (currentPosition[currentPosition.length - 1] === 0) { return items; } var newPosition = currentPosition.reduce(function (acc, pos, index, arr) { if (index === arr.length - 1) { acc.push(pos - 1); acc.push(0); } else { acc.push(pos); } return acc; }, []); return moveItem(items, item, currentPosition, newPosition); } function getVerticalMovedPosition(items, position, step) { var childProp = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'children'; var suitableNextParent = (0, _toConsumableArray2["default"])(position).reverse().reduce(function (acc, pos, index) { if (acc) { return acc; } var prefix = position.slice(0, position.length - 1 - index); var siblings = getSiblingsByNodePosition(items, position.slice(0, position.length - index), childProp); for (var i = pos + step; i >= 0 && i < siblings.length; i += step) { var candidate = [].concat((0, _toConsumableArray2["default"])(prefix), [i]); var candidateSiblings = getSiblingsByNodePosition(items, candidate, childProp); if (!candidateSiblings) { return null; } var totalDepth = getDepth(getNodeAtPosition(items, candidate, childProp), childProp) + position.length - index; if (totalDepth >= position.length) { return candidate; } } return acc; }, null); if (!suitableNextParent) { return null; } if (position.length === suitableNextParent.length) { return suitableNextParent; } return position.reduce(function (acc, _, index) { var lastItem = index === position.length - 1; if (suitableNextParent[index] !== undefined) { acc.push(suitableNextParent[index]); } else { var siblings = getSiblingsByNodePosition(items, [].concat((0, _toConsumableArray2["default"])(acc), [0]), childProp); if (!siblings) { acc.push(0); return acc; } var maxIndex = lastItem ? siblings.length : siblings.length - 1; var candidateIndex = step < 0 ? maxIndex : 0; while (candidateIndex >= 0 && candidateIndex <= maxIndex) { var candidate = [].concat((0, _toConsumableArray2["default"])(acc), [candidateIndex]); if (lastItem) { return candidate; } var candidateDepth = getDepth(getNodeAtPosition(items, candidate, childProp), childProp) + index; if (candidateDepth < position.length - 1) { candidateIndex += step; continue; } return candidate; } } return acc; }, []); } function getParentPosition(position) { return position.slice(0, position.length - 1); } function moveItemVertically(items, item) { var step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : VerticalMovementDirection.bottom; var childProp = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'children'; var currentPosition = getNodePosition(items, item, childProp); var newPosition = getVerticalMovedPosition(items, currentPosition, step, childProp); if (newPosition) { return moveItem(items, item, currentPosition, newPosition, childProp); } return items; } function moveItemOutsideOfTheParent(items, item) { var childProp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var currentPosition = getNodePosition(items, item, childProp); // if currentPosition is root of the tree we cannot move item to the left if (currentPosition.length < 2) { return items; } var newPosition = getParentPosition(currentPosition); newPosition[newPosition.length - 1] = newPosition[newPosition.length - 1] + 1; return moveItem(items, item, currentPosition, newPosition); } function setCollapse(items, itemId, isCollapsed) { return recursiveMap(items, function (item) { if (itemId === item.id) { return _objectSpread(_objectSpread({}, item), {}, { isCollapsed: isCollapsed }); } return item; }); }