UNPKG

react-dynamic-virtual-tree

Version:

Library for visualizing nested lists with dynamically changing sizes.

213 lines (212 loc) 7.96 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { Listener } from "./listener"; var LIST_ITEM_HEIGHT_EVENT = "listItemHeightEvent"; var CHILDREN_READY = "childrenReady"; var ListItem = /** @class */ (function (_super) { __extends(ListItem, _super); function ListItem(id, initialHeight) { var _this = _super.call(this) || this; _this.defaultHeight = 0; _this.childCounter = 0; _this.additionalHeights = {}; _this.children = []; if (initialHeight) { _this.currentHeight = initialHeight; } _this.id = id; return _this; } ListItem.prototype.memorizeState = function (state) { this.memorizedState = state; }; ListItem.prototype.getMemorizedState = function () { return this.memorizedState; }; ListItem.prototype.onUpdateHeight = function (callback) { return this.onListen(LIST_ITEM_HEIGHT_EVENT, callback); }; ListItem.prototype.onChildrenReady = function (callback) { return this.onListen(CHILDREN_READY, callback); }; ListItem.prototype.addChildren = function (children, ready) { var _this = this; var clearFunctions = []; var promises = []; var _loop_1 = function (i) { var child = children[i]; child.parent = this_1; var clearFunction = function () { }; var promise = new Promise(function (resolve) { clearFunction = _this.addChild(child, resolve); }); promises.push(promise); clearFunctions.push(clearFunction); }; var this_1 = this; for (var i = 0; i < children.length; i++) { _loop_1(i); } Promise.all(promises).finally(function () { _this.dispatch(CHILDREN_READY, undefined); ready && ready(); }); return function () { clearFunctions.forEach(function (callback) { return callback(); }); }; }; ListItem.prototype.addChild = function (child, ready) { var _this = this; var clearFunctions = []; var childId = "child-".concat(this.childCounter); this.childCounter += 1; child.parent = this; var initClearListener = child.onUpdateHeight(function (childHeight) { _this.additionalHeights[childId] = childHeight; ready && ready(); initClearListener(); var clearListener = child.onUpdateHeight(function (childHeight) { _this.additionalHeights[childId] = childHeight; _this.dispatch(LIST_ITEM_HEIGHT_EVENT, _this.height); }); _this.dispatch(LIST_ITEM_HEIGHT_EVENT, _this.height); clearFunctions.push(clearListener); }); this.children.push(child); return function () { var index = _this.children.findIndex(function (item) { return item === child; }); clearFunctions.forEach(function (callback) { return callback(); }); if (index !== -1) { _this.children.splice(index, 1); delete _this.additionalHeights[childId]; } }; }; ListItem.prototype.remove = function () { var _this = this; var parent = this.parent; if (!parent) { return; } var index = parent.children.findIndex(function (item) { return item === _this; }); this.removeAllListeners(); if (index !== -1) { parent.children.splice(index, 1); } }; ListItem.prototype.getAbsoluteTop = function () { var _this = this; var parent = this.parent; if (!parent) { return 0; } var index = parent.children.findIndex(function (item) { return item === _this; }); if (index === -1) { return 0; } var top = 0; for (var i = 0; i < index; i++) { top += parent.children[i].height; } return top; }; ListItem.prototype.genListItemHeightTree = function () { var root = { id: this.id, children: {}, height: this.currentHeight || this.defaultHeight }; var saveHeight = function (listItem, dct) { var currentItem = { id: listItem.id, children: {}, height: listItem.currentHeight !== undefined ? listItem.currentHeight : listItem.defaultHeight, }; dct.children[listItem.id] = currentItem; listItem.children.forEach(function (child) { saveHeight(child, currentItem); }); }; this.children.forEach(function (child) { saveHeight(child, root); }); return root; }; ListItem.prototype.getChild = function (id) { return this.children.find(function (listItem) { return listItem.id === id; }); }; ListItem.prototype.setListItemHeightTree = function (rootTree) { var _this = this; var setHeight = function (tree, listItem) { listItem.editHeight(tree.height); Object.values(tree.children).forEach(function (childItem) { var item = listItem.getChild(childItem.id); if (!item) { return; } setHeight(childItem, item); }); }; Object.values(rootTree.children).forEach(function (childItem) { var item = _this.getChild(childItem.id); if (!item) { return; } setHeight(childItem, item); }); }; ListItem.prototype.setChildrenHeight = function (height, depth) { var editHeight = function (listItem, currentDepth) { if (depth === 0) { return; } listItem.children.forEach(function (child) { child.editHeight(height); editHeight(child, currentDepth === undefined ? currentDepth : currentDepth - 1); }); }; editHeight(this, depth); }; Object.defineProperty(ListItem.prototype, "height", { get: function () { var currentHeight = this.currentHeight || 0; return (currentHeight + Object.values(this.additionalHeights).reduce(function (acc, item) { return acc + item; }, 0)); }, set: function (value) { this.defaultHeight = value; this.currentHeight = value; this.dispatch(LIST_ITEM_HEIGHT_EVENT, this.height); }, enumerable: false, configurable: true }); ListItem.prototype.editHeight = function (value) { this.currentHeight = value; this.dispatch(LIST_ITEM_HEIGHT_EVENT, this.height); }; ListItem.prototype.getParents = function () { var parents = []; var func = function (listItem) { if (listItem.parent) { parents.push(listItem.parent); func(listItem.parent); } }; func(this); return parents; }; return ListItem; }(Listener)); export { ListItem };