@@ -18,6 +18,8 @@ limitations under the License.
1818
1919var MatrixClientPeg = require ( "../../MatrixClientPeg" ) ;
2020
21+ var PAGINATE_SIZE = 20 ;
22+
2123module . 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