UNPKG

es-grid-template

Version:

es-grid-template

144 lines (141 loc) 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFullRenderRange = getFullRenderRange; exports.makeRowHeightManager = makeRowHeightManager; var _utils = require("../utils"); function getFullRenderRange(rowCount) { return { topIndex: 0, topBlank: 0, bottomIndex: rowCount, bottomBlank: 0 }; } function makeRowHeightManager(initRowCount, estimatedRowHeight) { const cache = new Array(initRowCount).fill(estimatedRowHeight); function getRenderRange(offset, maxRenderHeight, rowCount) { if (cache.length !== rowCount) { setRowCount(rowCount); } if (maxRenderHeight <= 0) { // maxRenderHeight <= 0 means the table is outside the viewport if (offset <= 0) { // Table is below the viewport return getRenderRangeWhenBelowView(); } else { // Table is above the viewport return getRenderRangeWhenAboveView(); } } else { // Table is within the viewport return getRenderRangeWhenInView(); } function getRenderRangeWhenBelowView() { const start = { topIndex: 0, topBlank: 0 }; const end = getEnd(0, start); return { ...start, ...end }; } function getRenderRangeWhenAboveView() { const totalSize = getEstimatedTotalSize(rowCount); const start = getStart(totalSize); const end = getEnd(totalSize, start); return { ...start, ...end }; } function getRenderRangeWhenInView() { const start = getStart(offset); const end = getEnd(offset + maxRenderHeight, start); return { ...start, ...end }; } /** Get virtual scroll info at the start offset */ function getStart(offset1) { if (cache.length === 0) { return { topIndex: 0, topBlank: 0 }; } let topIndex = 0; let topBlank = 0; while (topIndex < cache.length) { const h = cache[topIndex]; if (topBlank + h >= offset1) { break; } topBlank += h; topIndex += 1; } return overscanUpwards(topIndex, topBlank); } function overscanUpwards(topIndex, topBlank) { let overscanSize = 0; let overscanCount = 0; while (overscanCount < topIndex && overscanSize < _utils.OVERSCAN_SIZE) { overscanCount += 1; overscanSize += cache[topIndex - overscanCount]; } return { topIndex: topIndex - overscanCount, topBlank: topBlank - overscanSize }; } /** Get virtual scroll info at the end offset */ function getEnd(endOffset, startInfo) { let bottomIndex = startInfo.topIndex; let offset1 = startInfo.topBlank; while (bottomIndex < rowCount && offset1 < endOffset) { offset1 += cache[bottomIndex]; bottomIndex += 1; } const bottomBlank = getEstimatedTotalSize(rowCount) - offset1; return overscanDownwards(bottomIndex, bottomBlank); } function overscanDownwards(bottomIndex, bottomBlank) { let overscanSize = 0; let overscanCount = 0; while (overscanCount < rowCount - bottomIndex && overscanSize < _utils.OVERSCAN_SIZE) { overscanSize += cache[bottomIndex + overscanCount]; overscanCount += 1; } return { bottomIndex: bottomIndex + overscanCount, bottomBlank: bottomBlank - overscanSize }; } function getEstimatedTotalSize(rowCount1) { return (0, _utils.sum)(cache) + (rowCount1 - cache.length) * estimatedRowHeight; } function setRowCount(count) { // Set the cache length to count if (count < cache.length) { cache.length = count; } else { const prevSize = cache.length; cache.length = count; cache.fill(estimatedRowHeight, prevSize); } } } function updateRow(index, offset, size) { cache[index] = size; } return { getRenderRange, updateRow, // Export cache for debugging only; not intended for consumer usage cache }; }