Skip to content

Commit 8f1258b

Browse files
committed
Infinite scrolling
1 parent aaa38d9 commit 8f1258b

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎skins/base/views/organisms/RoomView.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var React = require('react');
2121
var MatrixClientPeg = require("../../../../src/MatrixClientPeg");
2222

2323
var ComponentBroker = require('../../../../src/ComponentBroker');
24+
var classNames = require("classnames");
2425

2526
var MessageTile = ComponentBroker.get('molecules/MessageTile');
2627
var RoomHeader = ComponentBroker.get('molecules/RoomHeader');
@@ -68,11 +69,17 @@ module.exports = React.createClass({
6869
);
6970
}
7071
} else {
72+
var scrollheader_classes = classNames({
73+
mx_RoomView_scrollheader: true,
74+
loading: this.state.paginating
75+
});
7176
return (
7277
<div className="mx_RoomView">
7378
<RoomHeader room={this.state.room} />
7479
<div className="mx_RoomView_HSplit">
75-
<ul className="mx_RoomView_MessageList" ref="messageList">
80+
<ul className="mx_RoomView_MessageList" ref="messageList" onScroll={this.onMessageListScroll}>
81+
<li className={scrollheader_classes}>
82+
</li>
7683
{this.getMessageTiles()}
7784
</ul>
7885
<MemberList roomId={this.props.roomId} key={this.props.roomId} />

‎src/controllers/organisms/RoomView.js‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ limitations under the License.
1818

1919
var MatrixClientPeg = require("../../MatrixClientPeg");
2020

21+
var PAGINATE_SIZE = 20;
22+
2123
module.exports = {
2224
getInitialState: function() {
2325
return {
@@ -43,6 +45,12 @@ module.exports = {
4345
},*/
4446

4547
onRoomTimeline: function(ev, room, toStartOfTimeline) {
48+
// ignore anything that comes in whilst pagingating: we get one
49+
// event for each new matrix event so this would cause a huge
50+
// number of UI updates. Just update the UI when the paginate
51+
// call returns.
52+
if (this.state.paginating) return;
53+
4654
// no point handling anything while we're waiting for the join to finish:
4755
// we'll only be showing a spinner.
4856
if (this.state.joining) return;
@@ -55,22 +63,59 @@ module.exports = {
5563
this.setState({
5664
room: MatrixClientPeg.get().getRoom(this.props.roomId)
5765
});
66+
67+
if (toStartOfTimeline && !this.state.paginating) {
68+
this.fillSpace();
69+
}
5870
},
5971

6072
componentDidMount: function() {
6173
if (this.refs.messageList) {
6274
var messageUl = this.refs.messageList.getDOMNode();
6375
messageUl.scrollTop = messageUl.scrollHeight;
76+
77+
this.fillSpace();
6478
}
6579
},
6680

6781
componentDidUpdate: function() {
68-
if (this.refs.messageList && this.atBottom) {
69-
var messageUl = this.refs.messageList.getDOMNode();
82+
if (!this.refs.messageList) return;
83+
84+
var messageUl = this.refs.messageList.getDOMNode();
85+
86+
if (this.state.paginating && !this.waiting_for_paginate) {
87+
var heightGained = messageUl.scrollHeight - this.oldScrollHeight;
88+
messageUl.scrollTop += heightGained;
89+
this.oldScrollHeight = undefined;
90+
if (!this.fillSpace()) {
91+
this.setState({paginating: false});
92+
}
93+
} else if (this.atBottom) {
7094
messageUl.scrollTop = messageUl.scrollHeight;
7195
}
7296
},
7397

98+
fillSpace: function() {
99+
var messageUl = this.refs.messageList.getDOMNode();
100+
if (messageUl.scrollTop < messageUl.clientHeight) {
101+
this.setState({paginating: true});
102+
this.waiting_for_paginate = true;
103+
104+
this.oldScrollHeight = messageUl.scrollHeight;
105+
106+
var that = this;
107+
MatrixClientPeg.get().scrollback(this.state.room, PAGINATE_SIZE).finally(function() {
108+
that.waiting_for_paginate = false;
109+
that.setState({
110+
room: MatrixClientPeg.get().getRoom(that.props.roomId)
111+
});
112+
// wait and set paginating to false when the component updates
113+
});
114+
return true;
115+
}
116+
return false;
117+
},
118+
74119
onJoinButtonClicked: function(ev) {
75120
var that = this;
76121
MatrixClientPeg.get().joinRoom(this.props.roomId).then(function() {
@@ -87,6 +132,14 @@ module.exports = {
87132
this.setState({
88133
joining: true
89134
});
135+
},
136+
137+
onMessageListScroll: function(ev) {
138+
if (this.refs.messageList) {
139+
var messageUl = this.refs.messageList.getDOMNode();
140+
this.atBottom = messageUl.scrollHeight - messageUl.scrollTop <= messageUl.clientHeight;
141+
}
142+
if (!this.state.paginating) this.fillSpace();
90143
}
91144
};
92145

0 commit comments

Comments
 (0)