UNPKG

2.31 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _calculateAverageHeight = require('./calculate-average-height');
8
9var _calculateAverageHeight2 = _interopRequireDefault(_calculateAverageHeight);
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13var calculateRows = function calculateRows(_ref) {
14 var measuredRows = _ref.measuredRows,
15 height = _ref.height,
16 rowKey = _ref.rowKey,
17 rows = _ref.rows,
18 _ref$scrollTop = _ref.scrollTop,
19 scrollTop = _ref$scrollTop === undefined ? 0 : _ref$scrollTop;
20
21 // Calculate amount of rows to render based on average height and take the
22 // amount of actual rows into account.
23 var averageHeight = (0, _calculateAverageHeight2.default)({ measuredRows: measuredRows, rows: rows, rowKey: rowKey });
24 var amountOfRowsToRender = Math.ceil(height / averageHeight) + 2;
25
26 var startIndex = Math.floor(scrollTop / averageHeight);
27 var rowsToRender = rows.slice(startIndex, startIndex + amountOfRowsToRender);
28
29 if (process.env.NODE_ENV !== 'production' && window.LOG_VIRTUALIZED) {
30 console.log( // eslint-disable-line no-console
31 'update rows to render', 'scroll top', scrollTop, 'measured rows', measuredRows, 'amount of rows to render', amountOfRowsToRender, 'rows to render', rowsToRender, 'start index', startIndex);
32 }
33
34 // Escape if there are no rows to render for some reason
35 if (!rowsToRender.length) {
36 return null;
37 }
38
39 var startHeight = startIndex * averageHeight;
40
41 // Calculate the padding of the last row so we can match whole height. This
42 // won't be totally accurate if row heights differ but should get close
43 // enough in most cases.
44 var endHeight = Math.max((rows.length - amountOfRowsToRender) * averageHeight - startHeight, 0);
45
46 if (process.env.NODE_ENV !== 'production' && window.LOG_VIRTUALIZED) {
47 console.log( // eslint-disable-line no-console
48 'average height', averageHeight, 'body height', height, 'scroll top', scrollTop, 'start height', startHeight, 'end height', endHeight);
49 }
50
51 return {
52 amountOfRowsToRender: amountOfRowsToRender,
53 startIndex: startIndex,
54 showExtraRow: !(startIndex % 2),
55 startHeight: startHeight,
56 endHeight: endHeight
57 };
58};
59
60exports.default = calculateRows;
\No newline at end of file