@trap_stevo/legendarybuilderproreact-ui
Version:
The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton
103 lines • 4.12 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
var HUDVirtuxifier = /*#__PURE__*/function () {
function HUDVirtuxifier(onPageChangeCallback, onRenderCallback) {
var pageSize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 50;
var viewportHeight = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 800;
var nColumns = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
var dataSource = arguments.length > 5 ? arguments[5] : undefined;
_classCallCheck(this, HUDVirtuxifier);
this.onPageChangeCallback = onPageChangeCallback;
this.onRenderCallback = onRenderCallback;
this.viewportHeight = viewportHeight;
this.dataSource = dataSource || [];
this.nColumns = nColumns;
this.pageSize = pageSize;
this.totalItems = this.dataSource.length;
this.totalRows = Math.ceil(this.totalItems / this.nColumns);
this.totalPages = Math.ceil(this.totalRows / this.pageSize);
this.rowHeight = this.viewportHeight / this.pageSize;
this.lastScrollTop = 0;
this.currentPage = 1;
this.virtuxify(this.currentPage);
this.notifyPageChange();
}
_createClass(HUDVirtuxifier, [{
key: "handleScrollPosition",
value: function handleScrollPosition(scrollPosition) {
var pageHeight = this.pageSize * this.rowHeight;
var currentPage = Math.floor(scrollPosition / pageHeight) + 1;
currentPage = Math.max(1, Math.min(currentPage, this.totalPages));
if (currentPage !== this.currentPage) {
this.lastScrollTop = scrollPosition;
this.currentPage = currentPage;
this.virtuxify(this.currentPage);
this.notifyPageChange();
}
}
}, {
key: "warpToPreviousPage",
value: function warpToPreviousPage() {
if (this.currentPage > 1) {
this.currentPage -= 1;
this.virtuxify(this.currentPage);
this.notifyPageChange();
}
}
}, {
key: "warpToNextPage",
value: function warpToNextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage += 1;
this.virtuxify(this.currentPage);
this.notifyPageChange();
}
}
}, {
key: "warpToPage",
value: function warpToPage(pageNumber) {
if (pageNumber >= 1 && pageNumber <= this.totalPages) {
this.currentPage = pageNumber;
this.virtuxify(this.currentPage);
this.notifyPageChange();
return;
}
console.error("Invalid page number: ".concat(pageNumber, " (1-").concat(this.totalPages, ")."));
return;
}
}, {
key: "virtuxify",
value: function virtuxify(page) {
var startRow = (page - 1) * this.pageSize;
var endRow = Math.min(startRow + this.pageSize, this.totalRows);
var startIndex = startRow * this.nColumns;
var endIndex = Math.min(endRow * this.nColumns, this.totalItems);
var visibleData = this.dataSource.slice(startIndex, endIndex);
var offsetY = startRow * this.rowHeight;
this.onRenderCallback(visibleData, offsetY);
}
}, {
key: "notifyPageChange",
value: function notifyPageChange() {
if (this.onPageChangeCallback) {
this.onPageChangeCallback(this.currentPage, this.totalPages);
}
}
}, {
key: "updateDataSource",
value: function updateDataSource(newDataSource) {
var currentScrollPosition = this.lastScrollTop || (this.currentPage - 1) * this.pageSize * this.rowHeight;
this.totalItems = newDataSource.length;
this.dataSource = newDataSource;
this.totalRows = Math.ceil(this.totalItems / this.nColumns);
this.totalPages = Math.ceil(this.totalRows / this.pageSize);
var pageHeight = this.pageSize * this.rowHeight;
var newCurrentPage = Math.floor(currentScrollPosition / pageHeight) + 1;
this.currentPage = Math.max(1, Math.min(newCurrentPage, this.totalPages));
this.virtuxify(this.currentPage);
this.notifyPageChange();
}
}]);
return HUDVirtuxifier;
}();
export default HUDVirtuxifier;