react-layouts-builder
Version:
Lightweight and powerfull react layouts drag and drop
52 lines (41 loc) • 1.72 kB
JavaScript
import { __spreadArray, __assign } from '../../node_modules/tslib/tslib.es6.js';
function insertLayout(options) {
var layouts = options.layouts,
targetId = options.targetId,
moved = options.moved,
position = options.position;
function findParentAndInsert(layout) {
var _a, _b, _c;
var targetLayout = (_a = layout === null || layout === void 0 ? void 0 : layout.childrens) === null || _a === void 0 ? void 0 : _a.find(function (child) {
var id = child['@id'] || child.id;
return id === targetId;
});
if (targetLayout && ((_b = layout.childrens) === null || _b === void 0 ? void 0 : _b.length)) {
var childrens = layout.childrens.reduce(function (childs, nextChild) {
var id = nextChild['@id'] || nextChild.id;
if (id === targetId) {
if (position === 'top') {
return __spreadArray(__spreadArray([], childs, true), [moved, nextChild], false);
}
return __spreadArray(__spreadArray([], childs, true), [nextChild, moved], false);
}
return childs.concat(nextChild);
}, []);
return __assign(__assign({}, layout), {
childrens: childrens
});
} else if ((layout === null || layout === void 0 ? void 0 : layout.childrens) && ((_c = layout.childrens) === null || _c === void 0 ? void 0 : _c.length)) {
var newLayout = layout.childrens.map(function (child) {
return findParentAndInsert(child);
}).filter(function (child) {
return child !== null;
});
return __assign(__assign({}, layout), {
childrens: newLayout
});
}
return layout;
}
return findParentAndInsert(layouts);
}
export { insertLayout };