vanilla-recycler-view
Version:
high performance UI rendering library for web browser
96 lines • 3.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VirtualElement = void 0;
var _1 = require("./");
var VirtualElement = /** @class */ (function () {
function VirtualElement(parent, data) {
this.index = 0;
this.start = 0;
this.size = 0;
this.wrapperElement = null;
this.renderer = null;
this.parent = parent;
this.data = data;
}
VirtualElement.prototype.setPosition = function (start, size) {
this.start = start;
this.size = size;
};
VirtualElement.prototype.setIndex = function (index) {
this.index = index;
};
VirtualElement.prototype.updatePosition = function () {
if (this.wrapperElement) {
switch (this.parent._direction) {
case _1.DIRECTION.VERTICAL:
this.wrapperElement.style.top = _1.toPx(this.start);
this.wrapperElement.style.height = _1.toPx(this.size);
break;
case _1.DIRECTION.HORIZONTAL:
this.wrapperElement.style.left = _1.toPx(this.start);
this.wrapperElement.style.width = _1.toPx(this.size);
break;
}
}
else {
throw new Error('element not mounted');
}
};
VirtualElement.prototype.isMounted = function () {
return !!this.renderer && !!this.wrapperElement;
};
VirtualElement.prototype.mountRenderer = function (reusable) {
if (!this.wrapperElement && !this.renderer) {
switch (this.parent._direction) {
case _1.DIRECTION.VERTICAL:
reusable.wrapperElement.style.top = _1.toPx(this.start);
reusable.wrapperElement.style.height = _1.toPx(this.size);
break;
case _1.DIRECTION.HORIZONTAL:
reusable.wrapperElement.style.left = _1.toPx(this.start);
reusable.wrapperElement.style.width = _1.toPx(this.size);
break;
}
this.wrapperElement = reusable.wrapperElement;
this.wrapperElement.classList.remove('hidden');
this.renderer = reusable.renderer;
if (!this.wrapperElement.parentElement) {
this.parent.container.appendChild(this.wrapperElement);
}
}
else {
throw new Error('reusable already mounted');
}
};
VirtualElement.prototype.unmountRenderer = function () {
var renderer = this.renderer;
var wrapperElement = this.wrapperElement;
if (renderer && wrapperElement) {
this.renderer = null;
this.wrapperElement = null;
if (renderer.onUnmount) {
renderer.onUnmount({
api: this.parent,
data: this.data,
index: this.index,
});
}
wrapperElement === null || wrapperElement === void 0 ? void 0 : wrapperElement.classList.add('hidden');
return {
renderer: renderer,
wrapperElement: wrapperElement,
};
}
else {
throw new Error('renderer not mounted');
}
};
VirtualElement.prototype.destroyRenderer = function () {
var _a;
var reusable = this.unmountRenderer();
(_a = reusable.wrapperElement.parentElement) === null || _a === void 0 ? void 0 : _a.removeChild(reusable.wrapperElement);
};
return VirtualElement;
}());
exports.VirtualElement = VirtualElement;
//# sourceMappingURL=virtual-element.js.map