-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathendless.js
408 lines (345 loc) · 12.3 KB
/
endless.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Endless: A bidirectional, React infinite scroll component.
Based on ReactList (https://github.com/orgsync/react-list)
*/
/* jshint browser: true */
/* global define, module */
/* jshint -W116 */ // Don't warn about single-line if's.
const ReactDOM = require('react-dom');
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['react'], factory);
} else if (typeof exports !== 'undefined') {
module.exports = factory(require('react'));
} else {
root.Endless = factory(root.React);
}
})(this, function(React) {
'use strict';
var buildReactElement = function(jsonml) {
if (!Array.isArray(jsonml)) {
return jsonml;
}
if (jsonml.length > 1 &&
(typeof jsonml[1] !== 'object' || Array.isArray(jsonml[1]))
) {
jsonml.splice(1, 0, null);
}
return React.createElement.apply(React, jsonml.map(buildReactElement));
};
var requestAnimationFrame =
(typeof window !== 'undefined' && window.requestAnimationFrame) ||
function(cb) {
return setTimeout(cb, 16);
};
var cancelAnimationFrame =
(typeof window !== 'undefined' && window.cancelAnimationFrame) ||
clearTimeout;
var isEqualSubset = function(a, b) {
for (var key in a)
if (b[key] !== a[key]) return false;
return true;
};
var isEqual = function(a, b) {
return isEqualSubset(a, b) && isEqualSubset(b, a);
};
var getComputedValue = function (el, propName) {
var value = parseFloat(window.getComputedStyle(el)[propName]);
if(isNaN(value)) value = 0;
return value;
};
return React.createClass({
getDefaultProps: function() {
return {
items: [],
margin: 2000,
atTop: false,
atBottom: false,
onScroll: function( /* key, above, below */ ) {},
onMount: function( /* event */ ) {},
onUnmount: function() {}
};
},
getInitialState: function() {
this.lastState = this.lastState || { jumpRequired: true, offset: 0 };
if (this.props.items.length) {
if (this.props.atTop) {
this.lastState.jumpToIndex = 0;
this.lastState.position = 'top';
} else if (this.props.atBottom) {
this.lastState.jumpToIndex = this.props.items.length - 1;
this.lastState.position = 'bottom';
} else {
this.lastState.jumpToIndex = 0;
this.lastState.position = buildReactElement(this.props.items[0]).key;
}
}
if (this.props.position) {
this.lastState.position = this.props.position;
}
return {
itemHeight: 25,
columns: 1,
topReached: this.props.atTop,
bottomReached: this.props.atBottom,
topRemoved: 0,
bottomRemoved: 0
};
},
update: function() {
var items = this.props.items,
itemsEl = ReactDOM.findDOMNode(this.refs.items),
itemEls = itemsEl.children,
jumped = false;
if (!itemEls.length) {
this.stid = setTimeout(this.update, 200);
return;
}
if (this.lastState.jumpRequired && this.lastState.position) {
if (this.lastState.position == 'top') {
this.setScroll(-9E99);
// this.scrollTo(0, 0);
jumped = true;
} else if (this.lastState.position == 'bottom') {
this.setScroll(9E99);
// this.scrollTo(items.length - 1, this.getBottom(itemEls[itemEls.length-1]) - this.getTop(itemEls[itemEls.length-1]));
jumped = true;
} else {
for (i = 0; i < items.length; i++) {
if (items[i].key == this.lastState.position) {
this.scrollTo(i, this.lastState.offset);
jumped = true;
break;
}
}
}
if(jumped) {
// this.lastState.offset = 0;
this.lastState.jumpRequired = false;
} else {
console.error("Endless Error: Jump was required but it did not happen. "+
"This usually happens when scrolling down very fast.");
this.lastState.jumpRequired = false;
}
this.afid = requestAnimationFrame(this.update);
return;
}
var viewTop = this.getScroll(),
viewHeight = this.getViewportHeight(),
viewBottom = viewTop + viewHeight,
elBottom = ReactDOM.findDOMNode(this).scrollHeight,
last = this.lastState,
position, i, offset, above, below, itemHeight, top, columns;
top = this.getTop(itemEls[0]);
for(columns=1; columns<itemEls.length && this.getTop(itemEls[columns]) == top; columns++);
itemHeight = (this.getBottom(itemEls[itemEls.length - 1]) - top) / itemEls.length;
if(itemHeight <= 0) itemHeight = 20; // Ugly hack for handling display:none items.
itemHeight *= columns;
if (this.state.bottomReached && !this.state.bottomRemoved && viewBottom >= elBottom-4) {
position = 'bottom';
offset = 0;
above = columns * Math.ceil(viewHeight / itemHeight);
below = 0;
} else if (this.state.topReached && !this.state.topRemoved && viewTop <= 4) {
position = 'top';
offset = 0;
above = 0;
below = columns * Math.ceil(viewHeight / itemHeight);
} else {
for (i = 0; i < itemEls.length; i++) {
offset = itemEls[i].offsetTop + itemEls[i].scrollHeight - viewTop;
if (offset > 0) {
offset = viewTop - itemEls[i].offsetTop;
break;
}
}
if (i == itemEls.length) i--; // All the items are above the viewport; pick last one.
position = buildReactElement(items[i]).key; // building in case it is JSONML
if (viewTop < itemEls[0].offsetTop) {
offset = viewTop - itemEls[0].offsetTop;
above = columns * Math.ceil(-offset / itemHeight);
} else {
above = 0;
}
below = Math.max(0, columns * Math.ceil(Math.max(
viewHeight, (viewBottom - itemEls[itemEls.length-1].offsetTop)
) / itemHeight) - above);
}
above = Math.round(above);
below = Math.round(below);
if (last.position !== position || last.above < above || last.below < below) {
// console.debug("Position changed to", position, above, below);
last.position = position;
last.offset = offset;
last.above = above;
last.below = below;
this.afid = requestAnimationFrame(this.update);
this.props.onScroll(position, above, below);
} else if (last.offset !== offset) {
last.offset = offset;
this.afid = requestAnimationFrame(this.update);
} else {
this.stid = setTimeout(this.update, 200);
}
},
windowResized: function() {
this.lastState.jumpRequired = true;
},
componentDidMount: function() {
this.update();
window.addEventListener('resize', this.windowResized);
this.props.onMount();
},
componentWillUnmount: function() {
cancelAnimationFrame(this.afid);
window.removeEventListener('resize', this.windowResized);
clearTimeout(this.stid);
this.props.onUnmount();
},
shouldComponentUpdate: function(props, state) {
if (isEqual(this.props, props) && isEqual(this.state, state)) return false;
return true;
},
componentWillReceiveProps: function(nextProps) {
if (nextProps.position && nextProps.position !== this.lastState.position) {
this.lastState.position = nextProps.position;
this.lastState.offset = 0;
this.lastState.jumpRequiredAfterUpdate = true;
// console.debug('Received a position property that will cause a jump');
}
this.setState({
topReached: this.state.topReached || nextProps.atTop,
bottomReached: this.state.bottomReached || nextProps.atBottom,
topRemoved: nextProps.atTop? 0: Math.max(10, this.state.topRemoved),
bottomRemoved: nextProps.atBottom? 0: Math.max(10, this.state.bottomRemoved),
});
},
componentDidUpdate: function(prevProps) {
// if (this.lastState.jumpToIndex !== null) return;
var prevItems = prevProps.items.map(buildReactElement),
items = this.props.items.map(buildReactElement),
metrics, prevMetrics = this.metrics, i,
topAdded, topRemoved, bottomAdded, bottomRemoved;
if (!items.length || !prevItems.length) return;
// Calculate the new metrics (tops and bottoms of each element)
metrics = [].slice.call(ReactDOM.findDOMNode(this.refs.items).children).map(function(itemEl) {
return {
top: this.getTop(itemEl),
bottom: this.getBottom(itemEl)
};
}.bind(this));
if (prevMetrics && prevMetrics.length !== prevItems.length) {
console.error("Endless Error: prevMetrics.length ≠ prevItems.length. "+
"Did you modify the items array after calling setProps?", prevMetrics.length, prevItems.length);
}
if (metrics && prevMetrics && items.length && prevItems.length) {
for (i = 0; i < items.length && items[i].key != prevItems[0].key; i++);
topAdded = (i == items.length) ? 0 : metrics[i].top - metrics[0].top;
for (i = 0; i < prevItems.length && prevItems[i].key != items[0].key; i++);
topRemoved = (i == prevItems.length) ? 0 : prevMetrics[i].top - prevMetrics[0].top;
for (i = items.length - 1; i >= 0 && items[i].key != prevItems[prevItems.length - 1].key; i--);
bottomAdded = (i < 0) ? 0 : metrics[metrics.length - 1].bottom - metrics[i].bottom;
for (i = prevItems.length - 1; i >= 0 && prevItems[i].key != items[items.length - 1].key; i--);
bottomRemoved = (i < 0) ? 0 : prevMetrics[prevMetrics.length - 1].bottom - prevMetrics[i].bottom;
this.setState({
topRemoved: Math.max(0, this.state.topRemoved + topRemoved - topAdded),
bottomRemoved: Math.max(0, this.state.bottomRemoved + bottomRemoved - bottomAdded)
});
}
// console.log('Rendered', items[0].key, 'through', items[items.length-1].key,
// 'Removed space ', this.state.topRemoved, this.state.bottomRemoved);
this.metrics = metrics;
if(
items[0].key != prevItems[0].key ||
!prevProps.topReached && this.props.atTop ||
this.lastState.position === 'bottom' ||
this.lastState.jumpRequiredAfterUpdate
) {
// console.debug("Scheduled a jump to", this.lastState.position,
// items[0].key != prevItems[0].key? 'topItemChanged': '',
// !prevProps.topReached && this.props.atTop? 'justReachedTop': '',
// this.lastState.jumpRequiredAfterUpdate? 'positionInProp': ''
// );
this.lastState.jumpRequired = true;
delete this.lastState.jumpRequiredAfterUpdate;
}
},
getTop: function(el) {
return el.getBoundingClientRect().top - getComputedValue(el, 'marginTop');
},
getBottom: function(el) {
return el.getBoundingClientRect().bottom + getComputedValue(el, 'marginBottom');
},
getScrollParent: function() {
for (var el = ReactDOM.findDOMNode(this); el; el = el.parentElement) {
var overflowY = window.getComputedStyle(el).overflowY;
if (overflowY === 'auto' || overflowY === 'scroll') {
return el;
}
}
return window;
},
// Get scroll position relative to the top of the list.
getScroll: function() {
var scrollParent = this.getScrollParent(),
el = ReactDOM.findDOMNode(this);
if (scrollParent === el) {
return el.scrollTop;
} else if (scrollParent === window) {
return -el.getBoundingClientRect().top;
} else {
return scrollParent.getBoundingClientRect().top -
el.getBoundingClientRect().top + getComputedValue(scrollParent, 'borderTop');
}
},
setScroll: function(y) {
var scrollParent = this.getScrollParent(),
el = ReactDOM.findDOMNode(this);
// console.debug('setScroll called with ', y);
if (scrollParent != el) {
y += (scrollParent.scrollTop -
(scrollParent.getBoundingClientRect().top -
el.getBoundingClientRect().top +
getComputedValue(scrollParent, 'borderTop')));
}
y = Math.min(scrollParent.scrollHeight, Math.max(0, y));
if (scrollParent === window) return window.scrollTo(0, y);
// console.debug('About to scroll', scrollParent, y);
scrollParent.scrollTop = y;
// console.debug('After scroll', scrollParent.scrollHeight, scrollParent.scrollTop, scrollParent.clientHeight);
},
getViewportHeight: function() {
var scrollParent = this.getScrollParent();
return scrollParent === window ? scrollParent.innerHeight : scrollParent.clientHeight;
},
scrollTo: function(index, offset) {
var y = ReactDOM.findDOMNode(this.refs.items).children[index].offsetTop + offset;
// console.log(
// "scrolling from", this.getScroll(), "to",
// ReactDOM.findDOMNode(this).children[index].offsetTop, offset
// );
this.setScroll(y);
},
render: function() {
return buildReactElement(['div', {
style: {
position: 'relative'
}
},
['div', {
style: {
height: this.state.topRemoved + (this.state.topReached ? 0 : this.props.margin)
}
}],
['div', {
ref: 'items'
}].concat(this.props.items), ['div', {
style: {
clear: 'both',
height: this.state.bottomRemoved + (this.state.bottomReached ? 0 : this.props.margin)
}
}]
]);
}
});
});