forked from rgovindji/react-native-atoz-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FixedHeightWindowedListViewDataSource.js
378 lines (322 loc) · 9.8 KB
/
FixedHeightWindowedListViewDataSource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* @providesModule FixedHeightWindowedListViewDataSource
*/
'use strict';
import _ from 'lodash';
import invariant from 'fbjs/lib/invariant';
/**
* Helper class to perform calcuations required by FixedHeightWindowedListView.
*
* sectionHeader: Different height from cell, groups cells
* cell: Content that is not a section header
* row: A section header or a cell
*
*/
class FixedHeightListViewDataSource {
constructor(params) {
this._dataSource = [];
this._lookup = {};
this._getHeightForSectionHeader = params.getHeightForSectionHeader;
this._getHeightForCell = params.getHeightForCell;
}
computeRowsToRender(options) {
let {
scrollDirection,
firstVisible,
lastVisible,
firstRendered,
lastRendered,
pageSize,
maxNumToRender,
numToRenderAhead,
} = options;
invariant(
numToRenderAhead < maxNumToRender,
`numToRenderAhead must be less than maxNumToRender`,
);
let numRendered = lastRendered - firstRendered + 1;
let lastRow, targetLastRow, firstRow, targetFirstRow;
if (scrollDirection === 'down') {
let lastResult = this.__computeLastRow({numRendered, ...options});
lastRow = lastResult.lastRow;
targetLastRow = lastResult.targetLastRow;
let firstResult = this.__computeFirstRow({lastRow, numRendered, ...options});
firstRow = firstResult.firstRow;
targetFirstRow = firstResult.targetFirstRow;
} else if (scrollDirection === 'up') {
let firstResult = this.__computeFirstRow({numRendered, ...options});
firstRow = firstResult.firstRow;
targetFirstRow = firstResult.targetFirstRow;
let lastResult = this.__computeLastRow({firstRow, numRendered, ...options});
lastRow = lastResult.lastRow;
targetLastRow = lastResult.targetLastRow;
}
return { firstRow, lastRow, targetFirstRow, targetLastRow };
}
__computeFirstRow(options) {
let {
lastRow,
firstVisible,
lastVisible,
maxNumToRender,
numToRenderBehind,
numToRenderAhead,
numRendered,
firstRendered,
scrollDirection,
pageSize,
} = options;
let firstRow, targetFirstRow;
if (scrollDirection === 'down') {
targetFirstRow = firstRow = Math.max(
0,
firstVisible - numToRenderBehind, // Never hide the first visible row
lastRow - maxNumToRender, // Don't exceed max to render
);
} else if (scrollDirection === 'up') {
targetFirstRow = Math.max(
0, // Don't render past the top
firstVisible - numToRenderAhead + numToRenderBehind, // Primary goal -- this is what we need lastVisible for
);
firstRow = Math.max(
targetFirstRow,
firstRendered - pageSize,
);
}
return { firstRow, targetFirstRow };
}
__computeLastRow(options) {
let {
firstVisible,
firstRow,
numRendered,
lastVisible,
totalRows,
numToRenderBehind,
numToRenderAhead,
lastRendered,
pageSize,
maxNumToRender,
scrollDirection,
} = options;
let lastRow, targetLastRow;
if (scrollDirection === 'down') {
targetLastRow = Math.min(
totalRows - 1, // Don't render past the bottom
lastVisible + numToRenderAhead - numToRenderBehind, // Primary goal -- this is what we need lastVisible for
firstVisible + numRendered + numToRenderAhead - numToRenderBehind, // But don't exceed num to render ahead
);
lastRow = Math.min(
targetLastRow,
lastRendered + pageSize,
);
} else if (scrollDirection === 'up') {
targetLastRow = lastRow = lastRendered;
let numToBeRendered = (lastRendered - firstRow);
if (numToBeRendered > maxNumToRender) {
targetLastRow = lastRow = targetLastRow - (numToBeRendered - maxNumToRender);
}
}
return { lastRow, targetLastRow };
}
/**
* Public: Used to set the height of the top spacer
*
* i - the index of a row in _dataSource
*
* Returns the height of spacer before the first rendered row.
*/
getHeightBeforeRow(i) {
let height = 0;
// console.log(this._lookup);
_.forEach(this._lookup, (section, sectionId) => {
if (i > section.range[0] && i <= section.range[1]) {
height += section.sectionHeaderHeight;
height += ((i - 1) - section.range[0]) * section.cellHeight;
} else if (section.range[0] < i) {
height += section.height;
}
});
return height;
}
hasSection(sectionId) {
return !!this._lookup[sectionId];
}
getFirstRowOfSection(sectionId) {
let range = this._lookup[sectionId].range;
let startY = this._lookup[sectionId].startY;
return {
row: range[0],
startY,
};
}
/**
* Public: Find the height between index i and index ii, where i < ii
*/
getHeightBetweenRows(i, ii) {
if (ii < i) {
console.warn('provide the lower index first');
}
return this.getHeightBeforeRow(ii) - this.getHeightBeforeRow(i + 1);
}
/**
* Public: Used to set the height of the bottom spacer
*
* i - the index of a row in _dataSource
*
* Returns the height of spacer after the last rendered row.
*/
getHeightAfterRow(i) {
return (
this.getTotalHeight() -
this.getHeightBeforeRow(i) -
this.getRowHeight(i)
);
}
/**
* Public: Used by computeRowsToRender to determine what the target
* last row is (lastVisible + numToRenderAhead)
*/
computeVisibleRows(scrollY, viewportHeight) {
let firstVisible = this.getRowAtHeight(scrollY);
let lastVisible = this.getRowAtHeight(scrollY + viewportHeight) + 1;
return {
firstVisible,
lastVisible,
};
}
/**
* Public: Gets the number of rows (cells + section headers)
*
* Returns the number of rows.
*/
getRowCount() {
return this._dataSource.length;
}
/**
* Public: Gets the data for a cell or header at the given row index
*
* Returns whatever is stored in datasource for the given index
*/
getRowData(i) {
return this._dataSource[i];
}
/**
* Private: Used internally by computeVisibleRows
*
* scrollY - the Y position at the top of the ScrollView
*
* Returns the index of the row in the _dataSource array that should be
* rendered at the given scrollY.
*/
getRowAtHeight(scrollY) {
if (scrollY < 0) {
return 0;
} else if (scrollY > this.getTotalHeight()) {
return Math.max(this.getRowCount() - 1, 0);
}
let parentSection = _.find(this._lookup, (value) => {
return scrollY >= value.startY && scrollY <= value.endY;
});
let relativeY = scrollY - parentSection.startY;
if (relativeY <= parentSection.sectionHeaderHeight) {
return parentSection.range[0];
} else {
let i = Math.floor(
(relativeY - parentSection.sectionHeaderHeight) /
parentSection.cellHeight
);
return parentSection.range[0] + i;
}
}
getRowHeight(i) {
let row = this._dataSource[i];
if (row && _.isObject(row) && row.sectionId) {
return this.getSectionHeaderHeight(row.sectionId);
} else {
return this.getCellHeight(i);
}
}
getSectionHeaderHeight(sectionId) {
return this._lookup[sectionId].sectionHeaderHeight;
}
getCellHeight(i) {
let parentSection = this.getParentSection(i);
if (parentSection) {
return parentSection.cellHeight;
}
}
getSectionId(i) {
return this.getParentSection(i).sectionId;
}
getParentSection(i) {
return _.find(this._lookup, (section) => {
return i >= section.range[0] && i <= section.range[1];
});
}
getTotalHeight() {
let keys = Object.keys(this._lookup);
let lastSection = this._lookup[keys[keys.length - 1]];
if (lastSection) {
return lastSection.endY;
} else {
return 0;
}
}
cloneWithCellsAndSections(dataBlob, sectionIds = Object.keys(dataBlob)) {
/* Take in { 'A': [{..}, {..}], 'B': [{..}]} and turn it into
* [ { sectionId: 'A' }, {..}, {..}, { sectionId: 'B' }, {..} ]
*
* This is important because we want to treat section headers just as
* other rows.
*/
this._dataSource = [];
let sectionIdsPresent = [];
sectionIds.forEach((sectionId) => {
if (dataBlob[sectionId]) {
this._dataSource.push({ sectionId }, ...dataBlob[sectionId]);
sectionIdsPresent.push(sectionId);
}
});
/* Build a data structure like this so we can easily perform calculations we
* need later:
* { 'A': { rows: 2, range: [0, 2], height: 250, startY: 0, endY: 250, cellHeight: 95, sectionHeaderHeight: 35} }
*/
let lastRow = -1;
let cumulativeHeight = 0;
this._lookup = sectionIdsPresent.reduce((result, sectionId) => {
let sectionHeaderHeight = this._getHeightForSectionHeader(sectionId);
let cellHeight = this._getHeightForCell(sectionId);
let count = dataBlob[sectionId].length;
let sectionHeight = sectionHeaderHeight + cellHeight * count;
result[sectionId] = {
count: count + 1, // Factor in section header
range: [lastRow + 1, lastRow + 1 + count], // Move 1 ahead of previous last row
height: sectionHeight,
startY: cumulativeHeight,
endY: cumulativeHeight + sectionHeight,
cellHeight,
sectionHeaderHeight,
sectionId,
};
cumulativeHeight += sectionHeight;
lastRow = lastRow + 1 + count;
return result;
}, {});
return this;
}
getHeightOfSection(sectionId) {
return this._lookup[sectionId].height;
}
/**
* Returns an array containing the number of rows in each section
*/
getSectionLengths() {
let result = [];
_.forEach(this._lookup, value => {
result.push(value.count);
});
return result;
}
}
module.exports = FixedHeightListViewDataSource;