mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2024-11-27 14:42:13 +00:00
Tentative solution to scrolling problem
This commit is contained in:
parent
94fc499446
commit
b3df96075b
1 changed files with 48 additions and 6 deletions
|
@ -1,14 +1,36 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var Chatroom = React.createClass({
|
var Chatroom = React.createClass({
|
||||||
|
getInitialState() {
|
||||||
|
return {
|
||||||
|
autoScroll: true
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
let self = this;
|
||||||
|
|
||||||
|
this.scrollListener = _.debounce((event) => {
|
||||||
|
self.temporarilyDisableAutoScroll(event);
|
||||||
|
}, 300, {
|
||||||
|
leading: false,
|
||||||
|
trailing: true
|
||||||
|
});
|
||||||
|
|
||||||
|
let node = React.findDOMNode(this.refs.messageContainer);
|
||||||
|
node.addEventListener('scroll', this.scrollListener);
|
||||||
|
|
||||||
this.scrollToBottom();
|
this.scrollToBottom();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
node.removeEventListener('scroll', this.scrollListener);
|
||||||
|
clearTimeout(this.disableScrollTimer);
|
||||||
|
},
|
||||||
|
|
||||||
loadMoreMessages() {
|
loadMoreMessages() {
|
||||||
var earliestMessage = this.props.messages[0];
|
var earliestMessage = this.props.messages[0];
|
||||||
if (earliestMessage === undefined) return;
|
if (earliestMessage === undefined) return;
|
||||||
this.disableScroll = true;
|
|
||||||
socket.emit("message:refresh", {
|
socket.emit("message:refresh", {
|
||||||
before: earliestMessage.createdAt
|
before: earliestMessage.createdAt
|
||||||
});
|
});
|
||||||
|
@ -18,15 +40,35 @@ var Chatroom = React.createClass({
|
||||||
socket.emit("newMessage", {message: message});
|
socket.emit("newMessage", {message: message});
|
||||||
},
|
},
|
||||||
|
|
||||||
componentDidUpdate() {
|
clearAutoScrollTimeout() {
|
||||||
if (this.disableScroll) {
|
if (this.disableScrollTimer) clearTimeout(this.disableScrollTimer);
|
||||||
this.disableScroll = false;
|
},
|
||||||
} else {
|
|
||||||
this.scrollToBottom();
|
temporarilyDisableAutoScroll(event) {
|
||||||
|
let self = this;
|
||||||
|
let node = event.target;
|
||||||
|
if (node) {
|
||||||
|
if (node.scrollHeight - node.scrollTop === node.clientHeight) {
|
||||||
|
this.setState({ autoScroll: true });
|
||||||
|
this.clearAutoScrollTimeout();
|
||||||
|
}
|
||||||
|
if (node.scrollHeight - node.scrollTop - node.clientHeight < 50) return;
|
||||||
}
|
}
|
||||||
|
this.setState({ autoScroll: false });
|
||||||
|
this.clearAutoScrollTimeout();
|
||||||
|
this.disableScrollTimer = setTimeout(() => {
|
||||||
|
self.setState({
|
||||||
|
autoScroll: true
|
||||||
|
})
|
||||||
|
}, 10000);
|
||||||
|
},
|
||||||
|
|
||||||
|
componentDidUpdate() {
|
||||||
|
this.scrollToBottom();
|
||||||
},
|
},
|
||||||
|
|
||||||
scrollToBottom() {
|
scrollToBottom() {
|
||||||
|
if (!this.state.autoScroll) return;
|
||||||
let node = React.findDOMNode(this.refs.messageContainer);
|
let node = React.findDOMNode(this.refs.messageContainer);
|
||||||
node.scrollTop = node.scrollHeight;
|
node.scrollTop = node.scrollHeight;
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue