react-dynamic-virtual-tree
Version:
Library for visualizing nested lists with dynamically changing sizes.
143 lines (142 loc) • 5.33 kB
JavaScript
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 UPDATE = "update";
var READY = "ready";
var ON_VIRTUAL_TREE = "onVirtualTree";
var sum = function (nums) {
return nums.reduce(function (acc, item) {
return acc + item;
}, 0);
};
var VirtualTree = /** @class */ (function (_super) {
__extends(VirtualTree, _super);
function VirtualTree() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.markup = [];
_this.totalHeight = 0;
return _this;
}
VirtualTree.prototype.flatListItem = function (listItem) {
var result = [];
var func = function (listItem, additionalHeight) {
if (additionalHeight === void 0) { additionalHeight = 0; }
var currentHeight = listItem.currentHeight || 0;
if (listItem.children.length === 0) {
var parents_1 = listItem.getParents();
result.push([
currentHeight + additionalHeight,
listItem,
function (item) {
if (item.id !== listItem.id) {
return !!parents_1.find(function (parentItem) { return parentItem.id === item.id; });
}
return item.id === listItem.id;
},
]);
return;
}
listItem.children.forEach(function (item, index) {
if (index === 0) {
func(item, currentHeight + additionalHeight);
}
else {
func(item);
}
});
};
func(listItem);
return result;
};
VirtualTree.prototype.getSlice = function (start, end) {
if (this.lastSlice && this.lastSlice[0] === start && this.lastSlice[1] === end) {
return this.lastSlice[2];
}
var step = sum(this.markup.slice(0, start).map(function (item) { return item[0]; }));
var slice = this.markup.slice(start, end);
var res = slice.map(function (item) {
var result = { item: item[1], top: step, compare: item[2] };
step += item[0];
return result;
});
this.lastSlice = [start, end, res];
return res;
};
VirtualTree.prototype.update = function (rootListItem, id) {
this.markup = this.flatListItem(rootListItem);
this.totalHeight = this.markup.reduce(function (acc, item) {
return acc + item[0];
}, 0);
this.lastSlice = undefined;
this.dispatch(UPDATE, id);
};
VirtualTree.prototype.getTotalHeight = function () {
return this.totalHeight;
};
VirtualTree.prototype.onVirtualTree = function (callback) {
this.onListen(ON_VIRTUAL_TREE, callback);
};
VirtualTree.prototype.pushNewVirtualTree = function (result) {
this.dispatch(ON_VIRTUAL_TREE, result);
};
VirtualTree.prototype.onUpdate = function (callback) {
return this.onListen(UPDATE, callback);
};
VirtualTree.prototype.onReady = function (callback) {
return this.onListen(READY, callback);
};
VirtualTree.prototype.ready = function () {
this.dispatch(READY, true);
};
VirtualTree.prototype.onOffset = function (callback) {
return this.onListen("offset", callback);
};
VirtualTree.prototype.pushOffset = function (slice) {
this.dispatch("offset", slice);
};
VirtualTree.prototype.getOffset = function (scrollTop, height, overScan) {
if (overScan === void 0) { overScan = 0; }
var startIndex = 0;
var endIndex = 0;
var sum = 0;
for (var i = 0; i < this.markup.length; i++) {
var obj = this.markup[i];
sum += obj[0];
if (sum >= scrollTop) {
startIndex = i;
break;
}
}
sum = 0;
for (var i = startIndex; i < this.markup.length; i++) {
var obj = this.markup[i];
sum += obj[0];
if (sum >= height) {
endIndex = i;
break;
}
}
if (endIndex <= startIndex) {
endIndex = this.markup.length;
}
return [
startIndex - overScan < 0 ? 0 : startIndex - overScan,
endIndex + overScan > this.markup.length ? this.markup.length : endIndex + overScan,
];
};
return VirtualTree;
}(Listener));
export { VirtualTree };