forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridUtils.js
More file actions
295 lines (257 loc) · 8.93 KB
/
GridUtils.js
File metadata and controls
295 lines (257 loc) · 8.93 KB
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
/**
* Helper method that determines when to recalculate row or column metadata.
*
* @param cellsCount Number of rows or columns in the current axis
* @param cellsSize Width or height of cells for the current axis
* @param computeMetadataCallback Method to invoke if cell metadata should be recalculated
* @param computeMetadataCallbackProps Parameters to pass to :computeMetadataCallback
* @param computeMetadataOnNextUpdate Flag specifying that metadata should be recalculated
* @param nextCellsCount Newly updated number of rows or columns in the current axis
* @param nextCellsSize Newly updated width or height of cells for the current axis
* @param nextScrollToIndex Newly updated scroll-to-index
* @param scrollToIndex Scroll-to-index
* @param updateScrollOffsetForScrollToIndex Callback to invoke if the scroll position should be recalculated
*/
export function computeCellMetadataAndUpdateScrollOffsetHelper ({
cellsCount,
cellSize,
computeMetadataCallback,
computeMetadataCallbackProps,
computeMetadataOnNextUpdate,
nextCellsCount,
nextCellSize,
nextScrollToIndex,
scrollToIndex,
updateScrollOffsetForScrollToIndex
}) {
// Don't compare cell sizes if they are functions because inline functions would cause infinite loops.
// In that event users should use the manual recompute methods to inform of changes.
if (
computeMetadataOnNextUpdate ||
cellsCount !== nextCellsCount ||
(
(
typeof cellSize === 'number' ||
typeof nextCellSize === 'number'
) &&
cellSize !== nextCellSize
)
) {
computeMetadataCallback(computeMetadataCallbackProps)
// Updated cell metadata may have hidden the previous scrolled-to item.
// In this case we should also update the scrollTop to ensure it stays visible.
if (scrollToIndex >= 0 && scrollToIndex === nextScrollToIndex) {
updateScrollOffsetForScrollToIndex()
}
}
}
/**
* Helper utility that updates the specified callback whenever any of the specified indices have changed.
*/
export function createCallbackMemoizer (requireAllKeys = true) {
let cachedIndices = {}
return ({
callback,
indices
}) => {
const keys = Object.keys(indices)
const allInitialized = !requireAllKeys || keys.every(key => indices[key] >= 0)
const indexChanged = keys.some(key => cachedIndices[key] !== indices[key])
cachedIndices = indices
if (allInitialized && indexChanged) {
callback(indices)
}
}
}
/**
* Binary search function inspired by react-infinite.
*/
export function findNearestCell ({
cellMetadata,
mode,
offset
}) {
let high = cellMetadata.length - 1
let low = 0
let middle
let currentOffset
// TODO Add better guards here against NaN offset
while (low <= high) {
middle = low + Math.floor((high - low) / 2)
currentOffset = cellMetadata[middle].offset
if (currentOffset === offset) {
return middle
} else if (currentOffset < offset) {
low = middle + 1
} else if (currentOffset > offset) {
high = middle - 1
}
}
if (mode === findNearestCell.EQUAL_OR_LOWER && low > 0) {
return low - 1
} else if (mode === findNearestCell.EQUAL_OR_HIGHER && high < cellMetadata.length - 1) {
return high + 1
}
}
findNearestCell.EQUAL_OR_LOWER = 1
findNearestCell.EQUAL_OR_HIGHER = 2
export function getOverscanIndices ({ cellsCount, overscanCellsCount, startIndex, stopIndex }) {
return {
overscanStartIndex: Math.max(0, startIndex - overscanCellsCount),
overscanStopIndex: Math.min(cellsCount - 1, stopIndex + overscanCellsCount)
}
}
/**
* Determines a new offset that ensures a certain cell is visible, given the current offset.
* If the cell is already visible then the current offset will be returned.
* If the current offset is too great or small, it will be adjusted just enough to ensure the specified index is visible.
*
* @param cellMetadata Metadata initially computed by initCellMetadata()
* @param containerSize Total size (width or height) of the container
* @param currentOffset Container's current (x or y) offset
* @param targetIndex Index of target cell
* @return Offset to use to ensure the specified cell is visible
*/
export function getUpdatedOffsetForIndex ({
cellMetadata,
containerSize,
currentOffset,
targetIndex
}) {
if (cellMetadata.length === 0) {
return 0
}
targetIndex = Math.max(0, Math.min(cellMetadata.length - 1, targetIndex))
const datum = cellMetadata[targetIndex]
const maxOffset = datum.offset
const minOffset = maxOffset - containerSize + datum.size
const newOffset = Math.max(minOffset, Math.min(maxOffset, currentOffset))
return newOffset
}
/**
* Determines the range of cells to display for a given offset in order to fill the specified container.
*
* @param cellsCount Total number of cells.
* @param cellMetadata Metadata initially computed by initCellMetadata()
* @param containerSize Total size (width or height) of the container
* @param currentOffset Container's current (x or y) offset
* @return An object containing :start and :stop attributes, each specifying a cell index
*/
export function getVisibleCellIndices ({
cellsCount,
cellMetadata,
containerSize,
currentOffset
}) {
if (cellsCount === 0) {
return {}
}
// TODO Add better guards here against NaN offset
const lastDatum = cellMetadata[cellMetadata.length - 1]
const totalCellSize = lastDatum.offset + lastDatum.size
// Ensure offset is within reasonable bounds
currentOffset = Math.max(0, Math.min(totalCellSize - containerSize, currentOffset))
const maxOffset = Math.min(totalCellSize, currentOffset + containerSize)
let start = findNearestCell({
cellMetadata,
mode: findNearestCell.EQUAL_OR_LOWER,
offset: currentOffset
})
let datum = cellMetadata[start]
currentOffset = datum.offset + datum.size
let stop = start
while (currentOffset < maxOffset && stop < cellsCount - 1) {
stop++
currentOffset += cellMetadata[stop].size
}
return {
start,
stop
}
}
/**
* Initializes metadata for an axis and its cells.
* This data is used to determine which cells are visible given a container size and scroll position.
*
* @param cellsCount Total number of cells.
* @param size Either a fixed size or a function that returns the size for a given given an index.
* @return Object mapping cell index to cell metadata (size, offset)
*/
export function initCellMetadata ({
cellsCount,
size
}) {
const sizeGetter = size instanceof Function
? size
: index => size
const cellMetadata = []
let offset = 0
for (var i = 0; i < cellsCount; i++) {
let size = sizeGetter(i)
if (size == null || isNaN(size)) {
throw Error(`Invalid size returned for cell ${i} of value ${size}`)
}
cellMetadata[i] = {
size,
offset
}
offset += size
}
return cellMetadata
}
/**
* Helper function that determines when to update scroll offsets to ensure that a scroll-to-index remains visible.
*
* @param cellMetadata Metadata initially computed by initCellMetadata()
* @param cellsCount Number of rows or columns in the current axis
* @param cellsSize Width or height of cells for the current axis
* @param previousCellsCount Previous number of rows or columns
* @param previousCellsSize Previous width or height of cells
* @param previousScrollToIndex Previous scroll-to-index
* @param previousSize Previous width or height of the virtualized container
* @param scrollOffset Current scrollLeft or scrollTop
* @param scrollToIndex Scroll-to-index
* @param size Width or height of the virtualized container
* @param updateScrollIndexCallback Callback to invoke with an optional scroll-to-index override
*/
export function updateScrollIndexHelper ({
cellMetadata,
cellsCount,
cellSize,
previousCellsCount,
previousCellSize,
previousScrollToIndex,
previousSize,
scrollOffset,
scrollToIndex,
size,
updateScrollIndexCallback
}) {
const hasScrollToIndex = scrollToIndex >= 0 && scrollToIndex < cellsCount
const sizeHasChanged = (
size !== previousSize ||
!previousCellSize ||
(
typeof cellSize === 'number' &&
cellSize !== previousCellSize
)
)
// If we have a new scroll target OR if height/row-height has changed,
// We should ensure that the scroll target is visible.
if (hasScrollToIndex && (sizeHasChanged || scrollToIndex !== previousScrollToIndex)) {
updateScrollIndexCallback()
// If we don't have a selected item but list size or number of children have decreased,
// Make sure we aren't scrolled too far past the current content.
} else if (!hasScrollToIndex && (size < previousSize || cellsCount < previousCellsCount)) {
const calculatedScrollOffset = getUpdatedOffsetForIndex({
cellMetadata,
containerSize: size,
currentOffset: scrollOffset,
targetIndex: cellsCount - 1
})
// Only adjust the scroll position if we've scrolled below the last set of rows.
if (calculatedScrollOffset < scrollOffset) {
updateScrollIndexCallback(cellsCount - 1)
}
}
}