-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
ScalingCellSizeAndPositionManager.js
203 lines (173 loc) · 5.35 KB
/
ScalingCellSizeAndPositionManager.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
/** @flow */
import type {Alignment, CellSizeGetter, VisibleCellRange} from '../types';
import CellSizeAndPositionManager from './CellSizeAndPositionManager';
import {getMaxElementSize} from './maxElementSize.js';
type ContainerSizeAndOffset = {
containerSize: number,
offset: number,
};
/**
* Browsers have scroll offset limitations (eg Chrome stops scrolling at ~33.5M pixels where as Edge tops out at ~1.5M pixels).
* After a certain position, the browser won't allow the user to scroll further (even via JavaScript scroll offset adjustments).
* This util picks a lower ceiling for max size and artificially adjusts positions within to make it transparent for users.
*/
type Params = {
maxScrollSize?: number,
cellCount: number,
cellSizeGetter: CellSizeGetter,
estimatedCellSize: number,
};
/**
* Extends CellSizeAndPositionManager and adds scaling behavior for lists that are too large to fit within a browser's native limits.
*/
export default class ScalingCellSizeAndPositionManager {
_cellSizeAndPositionManager: CellSizeAndPositionManager;
_maxScrollSize: number;
constructor({maxScrollSize = getMaxElementSize(), ...params}: Params) {
// Favor composition over inheritance to simplify IE10 support
this._cellSizeAndPositionManager = new CellSizeAndPositionManager(params);
this._maxScrollSize = maxScrollSize;
}
areOffsetsAdjusted(): boolean {
return (
this._cellSizeAndPositionManager.getTotalSize() > this._maxScrollSize
);
}
configure(params: {
cellCount: number,
estimatedCellSize: number,
cellSizeGetter: CellSizeGetter,
}) {
this._cellSizeAndPositionManager.configure(params);
}
getCellCount(): number {
return this._cellSizeAndPositionManager.getCellCount();
}
getEstimatedCellSize(): number {
return this._cellSizeAndPositionManager.getEstimatedCellSize();
}
getLastMeasuredIndex(): number {
return this._cellSizeAndPositionManager.getLastMeasuredIndex();
}
/**
* Number of pixels a cell at the given position (offset) should be shifted in order to fit within the scaled container.
* The offset passed to this function is scaled (safe) as well.
*/
getOffsetAdjustment({
containerSize,
offset, // safe
}: ContainerSizeAndOffset): number {
const totalSize = this._cellSizeAndPositionManager.getTotalSize();
const safeTotalSize = this.getTotalSize();
const offsetPercentage = this._getOffsetPercentage({
containerSize,
offset,
totalSize: safeTotalSize,
});
return Math.round(offsetPercentage * (safeTotalSize - totalSize));
}
getSizeAndPositionOfCell(index: number) {
return this._cellSizeAndPositionManager.getSizeAndPositionOfCell(index);
}
getSizeAndPositionOfLastMeasuredCell() {
return this._cellSizeAndPositionManager.getSizeAndPositionOfLastMeasuredCell();
}
/** See CellSizeAndPositionManager#getTotalSize */
getTotalSize(): number {
return Math.min(
this._maxScrollSize,
this._cellSizeAndPositionManager.getTotalSize(),
);
}
/** See CellSizeAndPositionManager#getUpdatedOffsetForIndex */
getUpdatedOffsetForIndex({
align = 'auto',
containerSize,
currentOffset, // safe
targetIndex,
}: {
align: Alignment,
containerSize: number,
currentOffset: number,
targetIndex: number,
}) {
currentOffset = this._safeOffsetToOffset({
containerSize,
offset: currentOffset,
});
const offset = this._cellSizeAndPositionManager.getUpdatedOffsetForIndex({
align,
containerSize,
currentOffset,
targetIndex,
});
return this._offsetToSafeOffset({
containerSize,
offset,
});
}
/** See CellSizeAndPositionManager#getVisibleCellRange */
getVisibleCellRange({
containerSize,
offset, // safe
}: ContainerSizeAndOffset): VisibleCellRange {
offset = this._safeOffsetToOffset({
containerSize,
offset,
});
return this._cellSizeAndPositionManager.getVisibleCellRange({
containerSize,
offset,
});
}
resetCell(index: number): void {
this._cellSizeAndPositionManager.resetCell(index);
}
_getOffsetPercentage({
containerSize,
offset, // safe
totalSize,
}: {
containerSize: number,
offset: number,
totalSize: number,
}) {
return totalSize <= containerSize
? 0
: offset / (totalSize - containerSize);
}
_offsetToSafeOffset({
containerSize,
offset, // unsafe
}: ContainerSizeAndOffset): number {
const totalSize = this._cellSizeAndPositionManager.getTotalSize();
const safeTotalSize = this.getTotalSize();
if (totalSize === safeTotalSize) {
return offset;
} else {
const offsetPercentage = this._getOffsetPercentage({
containerSize,
offset,
totalSize,
});
return Math.round(offsetPercentage * (safeTotalSize - containerSize));
}
}
_safeOffsetToOffset({
containerSize,
offset, // safe
}: ContainerSizeAndOffset): number {
const totalSize = this._cellSizeAndPositionManager.getTotalSize();
const safeTotalSize = this.getTotalSize();
if (totalSize === safeTotalSize) {
return offset;
} else {
const offsetPercentage = this._getOffsetPercentage({
containerSize,
offset,
totalSize: safeTotalSize,
});
return Math.round(offsetPercentage * (totalSize - containerSize));
}
}
}