UNPKG

react-dynamic-virtual-tree

Version:

Library for visualizing nested lists with dynamically changing sizes.

70 lines (69 loc) 2.1 kB
var Layer = /** @class */ (function () { function Layer(listItem) { var _this = this; this.children = []; this.getRoot = function (layer) { if (layer.parent) { return _this.getRoot(layer.parent); } else { return layer; } }; this.listItem = listItem; } Layer.prototype.addChildren = function (layers) { var _this = this; var clearFunctions = []; layers.forEach(function (layer) { layer.parent = _this; clearFunctions.push(_this.addChild(layer)); }); return function () { clearFunctions.forEach(function (clear) { clear(); }); }; }; Layer.prototype.remove = function () { var _this = this; var parent = this.parent; if (!parent) { throw new Error("You cannot remove root"); } this.listItem.remove(); var index = parent.children.findIndex(function (item) { return item === _this; }); if (index !== -1) { parent.children.splice(index, 1); } }; Layer.prototype.getLayersByLevel = function (level) { var root = this.getRoot(this); var layers = []; var diveIntoLayer = function (layer, level) { if (level === 0) { layers.push(layer); } else { layer.children.forEach(function (item) { diveIntoLayer(item, level - 1); }); } }; diveIntoLayer(root, level); return layers; }; Layer.prototype.addChild = function (layer) { var _this = this; layer.parent = this; this.children.push(layer); return function () { var index = _this.children.findIndex(function (item) { return item === layer; }); if (index !== -1) { _this.children.splice(index, 1); } }; }; return Layer; }()); export { Layer };