2015-12-22 15:18:50 +00:00
|
|
|
/*
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-12-22 15:18:50 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var React = require("react");
|
|
|
|
var ReactDOM = require("react-dom");
|
|
|
|
var GeminiScrollbar = require('react-gemini-scrollbar');
|
2015-12-24 00:10:21 +00:00
|
|
|
var q = require("q");
|
2016-04-05 12:14:11 +00:00
|
|
|
var KeyCode = require('../../KeyCode');
|
2015-12-22 15:18:50 +00:00
|
|
|
|
2016-03-23 14:53:38 +00:00
|
|
|
var DEBUG_SCROLL = false;
|
2016-04-02 17:09:44 +00:00
|
|
|
// var DEBUG_SCROLL = true;
|
2015-12-22 15:18:50 +00:00
|
|
|
|
2016-01-04 16:28:32 +00:00
|
|
|
if (DEBUG_SCROLL) {
|
|
|
|
// using bind means that we get to keep useful line numbers in the console
|
|
|
|
var debuglog = console.log.bind(console);
|
|
|
|
} else {
|
|
|
|
var debuglog = function () {};
|
|
|
|
}
|
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
/* This component implements an intelligent scrolling list.
|
|
|
|
*
|
|
|
|
* It wraps a list of <li> children; when items are added to the start or end
|
|
|
|
* of the list, the scroll position is updated so that the user still sees the
|
|
|
|
* same position in the list.
|
|
|
|
*
|
|
|
|
* It also provides a hook which allows parents to provide more list elements
|
|
|
|
* when we get close to the start or end of the list.
|
|
|
|
*
|
|
|
|
* Each child element should have a 'data-scroll-token'. This token is used to
|
2016-02-03 08:03:10 +00:00
|
|
|
* serialise the scroll state, and returned as the 'trackedScrollToken'
|
2015-12-22 15:18:50 +00:00
|
|
|
* attribute by getScrollState().
|
2016-02-01 16:31:12 +00:00
|
|
|
*
|
|
|
|
* Some notes about the implementation:
|
|
|
|
*
|
|
|
|
* The saved 'scrollState' can exist in one of two states:
|
|
|
|
*
|
2016-02-03 08:03:10 +00:00
|
|
|
* - stuckAtBottom: (the default, and restored by resetScrollState): the
|
|
|
|
* viewport is scrolled down as far as it can be. When the children are
|
|
|
|
* updated, the scroll position will be updated to ensure it is still at
|
|
|
|
* the bottom.
|
2016-02-01 16:31:12 +00:00
|
|
|
*
|
|
|
|
* - fixed, in which the viewport is conceptually tied at a specific scroll
|
|
|
|
* offset. We don't save the absolute scroll offset, because that would be
|
|
|
|
* affected by window width, zoom level, amount of scrollback, etc. Instead
|
|
|
|
* we save an identifier for the last fully-visible message, and the number
|
2016-05-17 19:28:11 +00:00
|
|
|
* of pixels the window was scrolled below it - which is hopefully near
|
2016-02-01 16:31:12 +00:00
|
|
|
* enough.
|
|
|
|
*
|
|
|
|
* The 'stickyBottom' property controls the behaviour when we reach the bottom
|
|
|
|
* of the window (either through a user-initiated scroll, or by calling
|
|
|
|
* scrollToBottom). If stickyBottom is enabled, the scrollState will enter
|
2016-02-03 08:03:10 +00:00
|
|
|
* 'stuckAtBottom' state - ensuring that new additions cause the window to
|
|
|
|
* scroll down further. If stickyBottom is disabled, we just save the scroll
|
|
|
|
* offset as normal.
|
2015-12-22 15:18:50 +00:00
|
|
|
*/
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'ScrollPanel',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
/* stickyBottom: if set to true, then once the user hits the bottom of
|
|
|
|
* the list, any new children added to the list will cause the list to
|
|
|
|
* scroll down to show the new element, rather than preserving the
|
|
|
|
* existing view.
|
|
|
|
*/
|
|
|
|
stickyBottom: React.PropTypes.bool,
|
|
|
|
|
2016-09-16 16:18:58 +00:00
|
|
|
/* startAtBottom: if set to true, the view is assumed to start
|
|
|
|
* scrolled to the bottom.
|
|
|
|
*/
|
|
|
|
startAtBottom: React.PropTypes.bool,
|
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
/* onFillRequest(backwards): a callback which is called on scroll when
|
|
|
|
* the user nears the start (backwards = true) or end (backwards =
|
2015-12-24 00:10:21 +00:00
|
|
|
* false) of the list.
|
|
|
|
*
|
2016-01-04 16:28:32 +00:00
|
|
|
* This should return a promise; no more calls will be made until the
|
|
|
|
* promise completes.
|
2015-12-24 00:10:21 +00:00
|
|
|
*
|
2016-01-04 16:28:32 +00:00
|
|
|
* The promise should resolve to true if there is more data to be
|
|
|
|
* retrieved in this direction (in which case onFillRequest may be
|
|
|
|
* called again immediately), or false if there is no more data in this
|
|
|
|
* directon (at this time) - which will stop the pagination cycle until
|
|
|
|
* the user scrolls again.
|
2015-12-22 15:18:50 +00:00
|
|
|
*/
|
|
|
|
onFillRequest: React.PropTypes.func,
|
|
|
|
|
|
|
|
/* onScroll: a callback which is called whenever any scroll happens.
|
|
|
|
*/
|
|
|
|
onScroll: React.PropTypes.func,
|
|
|
|
|
2016-03-24 00:13:32 +00:00
|
|
|
/* onResize: a callback which is called whenever the Gemini scroll
|
|
|
|
* panel is resized
|
|
|
|
*/
|
|
|
|
onResize: React.PropTypes.func,
|
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
/* className: classnames to add to the top-level div
|
|
|
|
*/
|
|
|
|
className: React.PropTypes.string,
|
|
|
|
|
|
|
|
/* style: styles to add to the top-level div
|
|
|
|
*/
|
|
|
|
style: React.PropTypes.object,
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
stickyBottom: true,
|
2016-09-16 16:18:58 +00:00
|
|
|
startAtBottom: true,
|
2016-01-04 16:28:32 +00:00
|
|
|
onFillRequest: function(backwards) { return q(false); },
|
2015-12-22 15:18:50 +00:00
|
|
|
onScroll: function() {},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
2015-12-24 00:10:21 +00:00
|
|
|
this._pendingFillRequests = {b: null, f: null};
|
2015-12-22 15:18:50 +00:00
|
|
|
this.resetScrollState();
|
|
|
|
},
|
|
|
|
|
2015-12-24 00:10:21 +00:00
|
|
|
componentDidMount: function() {
|
|
|
|
this.checkFillState();
|
|
|
|
},
|
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
componentDidUpdate: function() {
|
|
|
|
// after adding event tiles, we may need to tweak the scroll (either to
|
|
|
|
// keep at the bottom of the timeline, or to maintain the view after
|
|
|
|
// adding events to the top).
|
2016-02-22 17:19:04 +00:00
|
|
|
//
|
|
|
|
// This will also re-check the fill state, in case the paginate was inadequate
|
|
|
|
this.checkScroll();
|
2015-12-22 15:18:50 +00:00
|
|
|
},
|
|
|
|
|
2016-01-11 11:38:04 +00:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
// set a boolean to say we've been unmounted, which any pending
|
|
|
|
// promises can use to throw away their results.
|
|
|
|
//
|
|
|
|
// (We could use isMounted(), but facebook have deprecated that.)
|
|
|
|
this.unmounted = true;
|
|
|
|
},
|
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
onScroll: function(ev) {
|
|
|
|
var sn = this._getScrollNode();
|
2016-04-02 17:09:44 +00:00
|
|
|
debuglog("Scroll event: offset now:", sn.scrollTop,
|
|
|
|
"_lastSetScroll:", this._lastSetScroll);
|
2015-12-22 15:18:50 +00:00
|
|
|
|
|
|
|
// Sometimes we see attempts to write to scrollTop essentially being
|
|
|
|
// ignored. (Or rather, it is successfully written, but on the next
|
|
|
|
// scroll event, it's been reset again).
|
|
|
|
//
|
|
|
|
// This was observed on Chrome 47, when scrolling using the trackpad in OS
|
|
|
|
// X Yosemite. Can't reproduce on El Capitan. Our theory is that this is
|
|
|
|
// due to Chrome not being able to cope with the scroll offset being reset
|
|
|
|
// while a two-finger drag is in progress.
|
|
|
|
//
|
|
|
|
// By way of a workaround, we detect this situation and just keep
|
|
|
|
// resetting scrollTop until we see the scroll node have the right
|
|
|
|
// value.
|
2016-04-02 17:09:44 +00:00
|
|
|
if (this._lastSetScroll !== undefined && sn.scrollTop < this._lastSetScroll-200) {
|
|
|
|
console.log("Working around vector-im/vector-web#528");
|
|
|
|
this._restoreSavedScrollState();
|
|
|
|
return;
|
2015-12-22 15:18:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 16:31:12 +00:00
|
|
|
// If there weren't enough children to fill the viewport, the scroll we
|
|
|
|
// got might be different to the scroll we wanted; we don't want to
|
|
|
|
// forget what we wanted, so don't overwrite the saved state unless
|
|
|
|
// this appears to be a user-initiated scroll.
|
|
|
|
if (sn.scrollTop != this._lastSetScroll) {
|
|
|
|
this._saveScrollState();
|
|
|
|
} else {
|
|
|
|
debuglog("Ignoring scroll echo");
|
2016-03-21 14:48:17 +00:00
|
|
|
|
|
|
|
// only ignore the echo once, otherwise we'll get confused when the
|
|
|
|
// user scrolls away from, and back to, the autoscroll point.
|
|
|
|
this._lastSetScroll = undefined;
|
2016-02-01 16:31:12 +00:00
|
|
|
}
|
2015-12-22 15:18:50 +00:00
|
|
|
|
|
|
|
this.props.onScroll(ev);
|
|
|
|
|
|
|
|
this.checkFillState();
|
|
|
|
},
|
|
|
|
|
2016-03-24 00:13:32 +00:00
|
|
|
onResize: function() {
|
|
|
|
this.props.onResize();
|
|
|
|
this.checkScroll();
|
2016-03-24 01:12:51 +00:00
|
|
|
this.refs.geminiPanel.forceUpdate();
|
2016-03-24 00:13:32 +00:00
|
|
|
},
|
|
|
|
|
2016-02-22 17:19:04 +00:00
|
|
|
// after an update to the contents of the panel, check that the scroll is
|
|
|
|
// where it ought to be, and set off pagination requests if necessary.
|
|
|
|
checkScroll: function() {
|
|
|
|
this._restoreSavedScrollState();
|
|
|
|
this.checkFillState();
|
|
|
|
},
|
|
|
|
|
2016-01-05 10:58:01 +00:00
|
|
|
// return true if the content is fully scrolled down right now; else false.
|
2016-02-03 08:03:10 +00:00
|
|
|
//
|
|
|
|
// note that this is independent of the 'stuckAtBottom' state - it is simply
|
|
|
|
// about whether the the content is scrolled down right now, irrespective of
|
|
|
|
// whether it will stay that way when the children update.
|
2015-12-22 15:18:50 +00:00
|
|
|
isAtBottom: function() {
|
2016-01-05 10:58:01 +00:00
|
|
|
var sn = this._getScrollNode();
|
2016-02-01 16:31:12 +00:00
|
|
|
|
|
|
|
// there seems to be some bug with flexbox/gemini/chrome/richvdh's
|
|
|
|
// understanding of the box model, wherein the scrollNode ends up 2
|
|
|
|
// pixels higher than the available space, even when there are less
|
|
|
|
// than a screenful of messages. + 3 is a fudge factor to pretend
|
|
|
|
// that we're at the bottom when we're still a few pixels off.
|
|
|
|
|
|
|
|
return sn.scrollHeight - Math.ceil(sn.scrollTop) <= sn.clientHeight + 3;
|
2015-12-22 15:18:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// check the scroll state and send out backfill requests if necessary.
|
|
|
|
checkFillState: function() {
|
2016-01-11 11:38:04 +00:00
|
|
|
if (this.unmounted) {
|
2016-01-08 12:03:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
var sn = this._getScrollNode();
|
|
|
|
|
2016-01-04 16:28:32 +00:00
|
|
|
// if there is less than a screenful of messages above or below the
|
|
|
|
// viewport, try to get some more messages.
|
|
|
|
//
|
|
|
|
// scrollTop is the number of pixels between the top of the content and
|
|
|
|
// the top of the viewport.
|
|
|
|
//
|
|
|
|
// scrollHeight is the total height of the content.
|
|
|
|
//
|
|
|
|
// clientHeight is the height of the viewport (excluding borders,
|
|
|
|
// margins, and scrollbars).
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// .---------. - -
|
|
|
|
// | | | scrollTop |
|
|
|
|
// .-+---------+-. - - |
|
|
|
|
// | | | | | |
|
|
|
|
// | | | | | clientHeight | scrollHeight
|
|
|
|
// | | | | | |
|
|
|
|
// `-+---------+-' - |
|
|
|
|
// | | |
|
|
|
|
// | | |
|
|
|
|
// `---------' -
|
|
|
|
//
|
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
if (sn.scrollTop < sn.clientHeight) {
|
2016-01-04 16:28:32 +00:00
|
|
|
// need to back-fill
|
2015-12-24 00:10:21 +00:00
|
|
|
this._maybeFill(true);
|
|
|
|
}
|
2015-12-24 00:08:17 +00:00
|
|
|
if (sn.scrollTop > sn.scrollHeight - sn.clientHeight * 2) {
|
2016-01-04 16:28:32 +00:00
|
|
|
// need to forward-fill
|
2015-12-24 00:08:17 +00:00
|
|
|
this._maybeFill(false);
|
2015-12-22 15:18:50 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-24 00:10:21 +00:00
|
|
|
// check if there is already a pending fill request. If not, set one off.
|
|
|
|
_maybeFill: function(backwards) {
|
|
|
|
var dir = backwards ? 'b' : 'f';
|
|
|
|
if (this._pendingFillRequests[dir]) {
|
2016-01-04 16:28:32 +00:00
|
|
|
debuglog("ScrollPanel: Already a "+dir+" fill in progress - not starting another");
|
2015-12-24 00:10:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-04 16:28:32 +00:00
|
|
|
debuglog("ScrollPanel: starting "+dir+" fill");
|
2015-12-24 00:10:21 +00:00
|
|
|
|
|
|
|
// onFillRequest can end up calling us recursively (via onScroll
|
|
|
|
// events) so make sure we set this before firing off the call. That
|
|
|
|
// does present the risk that we might not ever actually fire off the
|
|
|
|
// fill request, so wrap it in a try/catch.
|
|
|
|
this._pendingFillRequests[dir] = true;
|
2016-01-04 16:28:32 +00:00
|
|
|
var fillPromise;
|
2015-12-24 00:10:21 +00:00
|
|
|
try {
|
2016-01-04 16:28:32 +00:00
|
|
|
fillPromise = this.props.onFillRequest(backwards);
|
2015-12-24 00:10:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
this._pendingFillRequests[dir] = false;
|
|
|
|
throw e;
|
2015-12-22 15:18:50 +00:00
|
|
|
}
|
2015-12-24 00:10:21 +00:00
|
|
|
|
2016-01-04 16:28:32 +00:00
|
|
|
q.finally(fillPromise, () => {
|
2015-12-24 00:10:21 +00:00
|
|
|
this._pendingFillRequests[dir] = false;
|
2016-01-04 16:28:32 +00:00
|
|
|
}).then((hasMoreResults) => {
|
2016-02-01 16:31:12 +00:00
|
|
|
debuglog("ScrollPanel: "+dir+" fill complete; hasMoreResults:"+hasMoreResults);
|
2016-01-04 16:28:32 +00:00
|
|
|
if (hasMoreResults) {
|
2015-12-24 00:10:21 +00:00
|
|
|
// further pagination requests have been disabled until now, so
|
|
|
|
// it's time to check the fill state again in case the pagination
|
|
|
|
// was insufficient.
|
|
|
|
this.checkFillState();
|
|
|
|
}
|
|
|
|
}).done();
|
2015-12-22 15:18:50 +00:00
|
|
|
},
|
|
|
|
|
2016-02-03 08:03:10 +00:00
|
|
|
/* get the current scroll state. This returns an object with the following
|
|
|
|
* properties:
|
|
|
|
*
|
|
|
|
* boolean stuckAtBottom: true if we are tracking the bottom of the
|
|
|
|
* scroll. false if we are tracking a particular child.
|
|
|
|
*
|
|
|
|
* string trackedScrollToken: undefined if stuckAtBottom is true; if it is
|
|
|
|
* false, the data-scroll-token of the child which we are tracking.
|
|
|
|
*
|
|
|
|
* number pixelOffset: undefined if stuckAtBottom is true; if it is false,
|
|
|
|
* the number of pixels the bottom of the tracked child is above the
|
|
|
|
* bottom of the scroll panel.
|
|
|
|
*/
|
2015-12-22 15:18:50 +00:00
|
|
|
getScrollState: function() {
|
|
|
|
return this.scrollState;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* reset the saved scroll state.
|
|
|
|
*
|
|
|
|
* This is useful if the list is being replaced, and you don't want to
|
|
|
|
* preserve scroll even if new children happen to have the same scroll
|
|
|
|
* tokens as old ones.
|
2016-02-01 16:31:12 +00:00
|
|
|
*
|
|
|
|
* This will cause the viewport to be scrolled down to the bottom on the
|
|
|
|
* next update of the child list. This is different to scrollToBottom(),
|
|
|
|
* which would save the current bottom-most child as the active one (so is
|
|
|
|
* no use if no children exist yet, or if you are about to replace the
|
|
|
|
* child list.)
|
2015-12-22 15:18:50 +00:00
|
|
|
*/
|
|
|
|
resetScrollState: function() {
|
2016-09-16 16:18:58 +00:00
|
|
|
this.scrollState = {stuckAtBottom: this.props.startAtBottom};
|
2015-12-22 15:18:50 +00:00
|
|
|
},
|
|
|
|
|
2016-04-05 12:14:11 +00:00
|
|
|
/**
|
|
|
|
* jump to the top of the content.
|
|
|
|
*/
|
|
|
|
scrollToTop: function() {
|
|
|
|
this._setScrollTop(0);
|
|
|
|
this._saveScrollState();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jump to the bottom of the content.
|
|
|
|
*/
|
2015-12-22 15:18:50 +00:00
|
|
|
scrollToBottom: function() {
|
2016-02-01 16:31:12 +00:00
|
|
|
// the easiest way to make sure that the scroll state is correctly
|
|
|
|
// saved is to do the scroll, then save the updated state. (Calculating
|
|
|
|
// it ourselves is hard, and we can't rely on an onScroll callback
|
|
|
|
// happening, since there may be no user-visible change here).
|
2016-03-22 19:33:02 +00:00
|
|
|
this._setScrollTop(Number.MAX_VALUE);
|
2016-02-01 16:31:12 +00:00
|
|
|
this._saveScrollState();
|
2015-12-22 15:18:50 +00:00
|
|
|
},
|
|
|
|
|
2016-04-05 12:14:11 +00:00
|
|
|
/**
|
|
|
|
* Page up/down.
|
|
|
|
*
|
|
|
|
* mult: -1 to page up, +1 to page down
|
|
|
|
*/
|
|
|
|
scrollRelative: function(mult) {
|
|
|
|
var scrollNode = this._getScrollNode();
|
|
|
|
var delta = mult * scrollNode.clientHeight * 0.5;
|
|
|
|
this._setScrollTop(scrollNode.scrollTop + delta);
|
|
|
|
this._saveScrollState();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scroll up/down in response to a scroll key
|
|
|
|
*/
|
|
|
|
handleScrollKey: function(ev) {
|
|
|
|
switch (ev.keyCode) {
|
|
|
|
case KeyCode.PAGE_UP:
|
|
|
|
this.scrollRelative(-1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyCode.PAGE_DOWN:
|
|
|
|
this.scrollRelative(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyCode.HOME:
|
|
|
|
if (ev.ctrlKey) {
|
|
|
|
this.scrollToTop();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyCode.END:
|
|
|
|
if (ev.ctrlKey) {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-10 16:44:50 +00:00
|
|
|
/* Scroll the panel to bring the DOM node with the scroll token
|
|
|
|
* `scrollToken` into view.
|
|
|
|
*
|
|
|
|
* offsetBase gives the reference point for the pixelOffset. 0 means the
|
|
|
|
* top of the container, 1 means the bottom, and fractional values mean
|
|
|
|
* somewhere in the middle. If omitted, it defaults to 0.
|
|
|
|
*
|
|
|
|
* pixelOffset gives the number of pixels *above* the offsetBase that the
|
|
|
|
* node (specifically, the bottom of it) will be positioned. If omitted, it
|
|
|
|
* defaults to 0.
|
|
|
|
*/
|
|
|
|
scrollToToken: function(scrollToken, pixelOffset, offsetBase) {
|
|
|
|
pixelOffset = pixelOffset || 0;
|
|
|
|
offsetBase = offsetBase || 0;
|
2016-02-01 16:31:12 +00:00
|
|
|
|
2016-03-10 16:44:50 +00:00
|
|
|
// convert pixelOffset so that it is based on the bottom of the
|
|
|
|
// container.
|
|
|
|
pixelOffset += this._getScrollNode().clientHeight * (1-offsetBase);
|
2016-02-01 16:31:12 +00:00
|
|
|
|
|
|
|
// save the desired scroll state. It's important we do this here rather
|
|
|
|
// than as a result of the scroll event, because (a) we might not *get*
|
|
|
|
// a scroll event, and (b) it might not currently be possible to set
|
|
|
|
// the requested scroll state (eg, because we hit the end of the
|
|
|
|
// timeline and need to do more pagination); we want to save the
|
|
|
|
// *desired* scroll state rather than what we end up achieving.
|
|
|
|
this.scrollState = {
|
2016-02-03 08:03:10 +00:00
|
|
|
stuckAtBottom: false,
|
|
|
|
trackedScrollToken: scrollToken,
|
2016-02-01 16:31:12 +00:00
|
|
|
pixelOffset: pixelOffset
|
|
|
|
};
|
|
|
|
|
|
|
|
// ... then make it so.
|
|
|
|
this._restoreSavedScrollState();
|
|
|
|
},
|
|
|
|
|
|
|
|
// set the scrollTop attribute appropriately to position the given child at the
|
|
|
|
// given offset in the window. A helper for _restoreSavedScrollState.
|
|
|
|
_scrollToToken: function(scrollToken, pixelOffset) {
|
2015-12-22 15:18:50 +00:00
|
|
|
/* find the dom node with the right scrolltoken */
|
|
|
|
var node;
|
|
|
|
var messages = this.refs.itemlist.children;
|
|
|
|
for (var i = messages.length-1; i >= 0; --i) {
|
|
|
|
var m = messages[i];
|
|
|
|
if (!m.dataset.scrollToken) continue;
|
|
|
|
if (m.dataset.scrollToken == scrollToken) {
|
|
|
|
node = m;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node) {
|
2016-02-01 16:31:12 +00:00
|
|
|
debuglog("ScrollPanel: No node with scrollToken '"+scrollToken+"'");
|
2015-12-22 15:18:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var scrollNode = this._getScrollNode();
|
|
|
|
var wrapperRect = ReactDOM.findDOMNode(this).getBoundingClientRect();
|
|
|
|
var boundingRect = node.getBoundingClientRect();
|
|
|
|
var scrollDelta = boundingRect.bottom + pixelOffset - wrapperRect.bottom;
|
2016-04-02 17:09:44 +00:00
|
|
|
|
|
|
|
debuglog("Scrolling to token '" + node.dataset.scrollToken + "'+" +
|
|
|
|
pixelOffset + " (delta: "+scrollDelta+")");
|
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
if(scrollDelta != 0) {
|
2016-03-22 19:33:02 +00:00
|
|
|
this._setScrollTop(scrollNode.scrollTop + scrollDelta);
|
2015-12-22 15:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-02-01 16:31:12 +00:00
|
|
|
_saveScrollState: function() {
|
|
|
|
if (this.props.stickyBottom && this.isAtBottom()) {
|
2016-02-03 08:03:10 +00:00
|
|
|
this.scrollState = { stuckAtBottom: true };
|
2016-02-01 16:31:12 +00:00
|
|
|
debuglog("Saved scroll state", this.scrollState);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-22 15:18:50 +00:00
|
|
|
|
|
|
|
var itemlist = this.refs.itemlist;
|
|
|
|
var wrapperRect = ReactDOM.findDOMNode(this).getBoundingClientRect();
|
|
|
|
var messages = itemlist.children;
|
|
|
|
|
|
|
|
for (var i = messages.length-1; i >= 0; --i) {
|
|
|
|
var node = messages[i];
|
|
|
|
if (!node.dataset.scrollToken) continue;
|
|
|
|
|
|
|
|
var boundingRect = node.getBoundingClientRect();
|
|
|
|
if (boundingRect.bottom < wrapperRect.bottom) {
|
2016-02-01 16:31:12 +00:00
|
|
|
this.scrollState = {
|
2016-02-03 08:03:10 +00:00
|
|
|
stuckAtBottom: false,
|
|
|
|
trackedScrollToken: node.dataset.scrollToken,
|
2015-12-22 15:18:50 +00:00
|
|
|
pixelOffset: wrapperRect.bottom - boundingRect.bottom,
|
|
|
|
}
|
2016-02-01 16:31:12 +00:00
|
|
|
debuglog("Saved scroll state", this.scrollState);
|
|
|
|
return;
|
2015-12-22 15:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 16:31:12 +00:00
|
|
|
debuglog("Unable to save scroll state: found no children in the viewport");
|
2015-12-22 15:18:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_restoreSavedScrollState: function() {
|
|
|
|
var scrollState = this.scrollState;
|
2016-02-01 16:31:12 +00:00
|
|
|
var scrollNode = this._getScrollNode();
|
|
|
|
|
2016-02-03 08:03:10 +00:00
|
|
|
if (scrollState.stuckAtBottom) {
|
2016-03-22 19:33:02 +00:00
|
|
|
this._setScrollTop(Number.MAX_VALUE);
|
2016-02-03 08:03:10 +00:00
|
|
|
} else if (scrollState.trackedScrollToken) {
|
|
|
|
this._scrollToToken(scrollState.trackedScrollToken,
|
2015-12-22 15:18:50 +00:00
|
|
|
scrollState.pixelOffset);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-22 19:33:02 +00:00
|
|
|
_setScrollTop: function(scrollTop) {
|
|
|
|
var scrollNode = this._getScrollNode();
|
|
|
|
|
|
|
|
var prevScroll = scrollNode.scrollTop;
|
|
|
|
|
2016-03-23 10:51:07 +00:00
|
|
|
// FF ignores attempts to set scrollTop to very large numbers
|
|
|
|
scrollNode.scrollTop = Math.min(scrollTop, scrollNode.scrollHeight);
|
|
|
|
|
|
|
|
// If this change generates a scroll event, we should not update the
|
|
|
|
// saved scroll state on it. See the comments in onScroll.
|
2016-03-22 19:33:02 +00:00
|
|
|
//
|
|
|
|
// If we *don't* expect a scroll event, we need to leave _lastSetScroll
|
|
|
|
// alone, otherwise we'll end up ignoring a future scroll event which is
|
|
|
|
// nothing to do with this change.
|
|
|
|
|
|
|
|
if (scrollNode.scrollTop != prevScroll) {
|
|
|
|
this._lastSetScroll = scrollNode.scrollTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
debuglog("Set scrollTop:", scrollNode.scrollTop,
|
|
|
|
"requested:", scrollTop,
|
|
|
|
"_lastSetScroll:", this._lastSetScroll);
|
|
|
|
},
|
2016-02-01 16:31:12 +00:00
|
|
|
|
2015-12-22 15:18:50 +00:00
|
|
|
/* get the DOM node which has the scrollTop property we care about for our
|
|
|
|
* message panel.
|
|
|
|
*/
|
|
|
|
_getScrollNode: function() {
|
2016-01-11 11:38:04 +00:00
|
|
|
if (this.unmounted) {
|
2016-01-08 12:03:45 +00:00
|
|
|
// this shouldn't happen, but when it does, turn the NPE into
|
|
|
|
// something more meaningful.
|
|
|
|
throw new Error("ScrollPanel._getScrollNode called when unmounted");
|
|
|
|
}
|
|
|
|
|
2016-03-22 19:33:02 +00:00
|
|
|
return this.refs.geminiPanel.scrollbar.getViewElement();
|
2015-12-22 15:18:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
// TODO: the classnames on the div and ol could do with being updated to
|
|
|
|
// reflect the fact that we don't necessarily contain a list of messages.
|
|
|
|
// it's not obvious why we have a separate div and ol anyway.
|
2016-03-14 15:29:29 +00:00
|
|
|
return (<GeminiScrollbar autoshow={true} ref="geminiPanel"
|
2016-03-24 00:13:32 +00:00
|
|
|
onScroll={this.onScroll} onResize={this.onResize}
|
2015-12-22 15:18:50 +00:00
|
|
|
className={this.props.className} style={this.props.style}>
|
|
|
|
<div className="mx_RoomView_messageListWrapper">
|
|
|
|
<ol ref="itemlist" className="mx_RoomView_MessageList" aria-live="polite">
|
|
|
|
{this.props.children}
|
|
|
|
</ol>
|
|
|
|
</div>
|
|
|
|
</GeminiScrollbar>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|