2015-07-28 15:54:29 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Chatroom = React.createClass({
|
2015-08-11 00:31:03 +00:00
|
|
|
componentDidMount() {
|
2015-10-01 10:22:23 +00:00
|
|
|
this.scrollToBottom();
|
2015-07-28 15:54:29 +00:00
|
|
|
},
|
|
|
|
|
2015-09-20 16:42:34 +00:00
|
|
|
loadMoreMessages() {
|
2015-10-01 10:22:23 +00:00
|
|
|
var earliestMessage = this.props.messages[0];
|
2015-09-20 17:05:22 +00:00
|
|
|
if (earliestMessage === undefined) return;
|
2015-10-01 13:50:31 +00:00
|
|
|
this.disableScroll = true;
|
2015-09-20 16:42:34 +00:00
|
|
|
socket.emit("message:refresh", {
|
2015-09-20 17:05:22 +00:00
|
|
|
before: earliestMessage.createdAt
|
2015-09-20 16:42:34 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:31:03 +00:00
|
|
|
sendMessage(message) {
|
2015-07-28 15:54:29 +00:00
|
|
|
socket.emit("newMessage", {message: message});
|
2015-08-11 00:37:43 +00:00
|
|
|
},
|
2015-08-11 00:31:03 +00:00
|
|
|
|
2015-10-01 13:50:31 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
if (this.disableScroll) {
|
|
|
|
this.disableScroll = false;
|
|
|
|
} else {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:31:03 +00:00
|
|
|
scrollToBottom() {
|
2015-08-11 00:37:43 +00:00
|
|
|
let node = React.findDOMNode(this.refs.messageContainer);
|
2015-07-28 15:54:29 +00:00
|
|
|
node.scrollTop = node.scrollHeight;
|
|
|
|
},
|
2015-08-11 00:31:03 +00:00
|
|
|
|
|
|
|
render() {
|
2015-10-01 13:28:58 +00:00
|
|
|
let messages = this.props.messages.map(message => {
|
|
|
|
if (message) {
|
|
|
|
return <ChatMessage message={message}
|
2015-10-01 14:03:48 +00:00
|
|
|
id={message._id}
|
|
|
|
user={this.props.user} />
|
2015-10-01 13:28:58 +00:00
|
|
|
}
|
|
|
|
});
|
2015-07-28 15:54:29 +00:00
|
|
|
return (
|
2015-09-21 20:15:27 +00:00
|
|
|
<div className="panel panel-primary chatbox">
|
2015-07-28 15:54:29 +00:00
|
|
|
<div className="panel-heading">Gather Chat</div>
|
|
|
|
<div className="panel-body">
|
|
|
|
<ul className="chat" id="chatmessages" ref="messageContainer">
|
2015-09-20 16:42:34 +00:00
|
|
|
<li className="text-center">
|
|
|
|
<a href="#"
|
2015-09-20 17:05:22 +00:00
|
|
|
onClick={this.loadMoreMessages}
|
2015-09-20 16:42:34 +00:00
|
|
|
className="btn btn-primary btn-xs">
|
|
|
|
Load more messages
|
|
|
|
</a>
|
|
|
|
</li>
|
2015-07-28 15:54:29 +00:00
|
|
|
{messages}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div className="panel-footer">
|
|
|
|
<MessageBar />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var ChatMessage = React.createClass({
|
2015-09-20 16:20:20 +00:00
|
|
|
mixins: [
|
2015-09-23 16:00:15 +00:00
|
|
|
ReactAutolink,
|
|
|
|
ReactEmoji
|
2015-09-20 16:20:20 +00:00
|
|
|
],
|
|
|
|
|
2015-09-20 16:33:43 +00:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
createdAt: ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateCreatedAt() {
|
|
|
|
let self = this;
|
|
|
|
if (this.props.message.createdAt) {
|
|
|
|
self.setState({
|
|
|
|
createdAt: $.timeago(self.props.message.createdAt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-09-22 21:56:31 +00:00
|
|
|
componentWillMount() {
|
2015-09-20 16:33:43 +00:00
|
|
|
this.updateCreatedAt();
|
2015-09-22 21:56:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
2015-09-20 16:35:56 +00:00
|
|
|
this.interval = setInterval(this.updateCreatedAt, 60000);
|
2015-09-17 20:57:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function () {
|
|
|
|
clearInterval(this.interval);
|
2015-07-28 15:54:29 +00:00
|
|
|
},
|
2015-08-11 00:37:43 +00:00
|
|
|
|
2015-09-20 16:20:20 +00:00
|
|
|
messageContent: function () {
|
2015-09-23 16:00:15 +00:00
|
|
|
let self = this;
|
|
|
|
return self.autolink(self.props.message.content, {
|
2015-09-20 16:20:20 +00:00
|
|
|
target: "_blank",
|
|
|
|
rel: "nofollow"
|
2015-09-23 16:00:15 +00:00
|
|
|
}).map((elem) => {
|
|
|
|
if (_.isString(elem)) {
|
|
|
|
return self.emojify(elem);
|
|
|
|
} else {
|
|
|
|
return elem;
|
|
|
|
}
|
2015-09-20 16:20:20 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:37:43 +00:00
|
|
|
render() {
|
2015-09-14 21:03:39 +00:00
|
|
|
let deleteButton;
|
2015-10-01 10:22:23 +00:00
|
|
|
let user = this.props.user;
|
|
|
|
if (user && user.admin) {
|
2015-09-14 21:03:39 +00:00
|
|
|
deleteButton = <DeleteMessageButton messageId={this.props.message._id} />;
|
|
|
|
}
|
2015-07-28 15:54:29 +00:00
|
|
|
return (
|
|
|
|
<li className="left clearfix">
|
|
|
|
<span className="chat-img pull-left">
|
|
|
|
<img
|
2015-07-31 11:21:07 +00:00
|
|
|
src={this.props.message.author.avatar}
|
2015-07-28 15:54:29 +00:00
|
|
|
alt="User Avatar"
|
|
|
|
height="40"
|
|
|
|
width="40"
|
|
|
|
className="img-circle" />
|
|
|
|
</span>
|
|
|
|
<div className="chat-body clearfix">
|
|
|
|
<div className="header">
|
2015-09-17 20:40:54 +00:00
|
|
|
<strong className="primary-font">
|
|
|
|
{this.props.message.author.username}
|
|
|
|
</strong>
|
2015-07-28 15:54:29 +00:00
|
|
|
<small className="pull-right text-muted">
|
2015-09-14 21:03:39 +00:00
|
|
|
{deleteButton}
|
2015-10-03 15:23:32 +00:00
|
|
|
<span className="hidden-xs">
|
|
|
|
<i className="fa fa-clock-o fa-fw"></i>
|
|
|
|
{this.state.createdAt}
|
|
|
|
</span>
|
2015-07-28 15:54:29 +00:00
|
|
|
</small>
|
|
|
|
</div>
|
2015-09-20 16:42:34 +00:00
|
|
|
<p className="wordwrap">{this.messageContent()}</p>
|
2015-07-28 15:54:29 +00:00
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-14 21:03:39 +00:00
|
|
|
var DeleteMessageButton = React.createClass({
|
|
|
|
handleClick (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
socket.emit("message:delete", {
|
|
|
|
id: this.props.messageId
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<a href="#" onClick={this.handleClick}>
|
|
|
|
<i className="fa fa-trash-o"></i>
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-07-28 15:54:29 +00:00
|
|
|
var MessageBar = React.createClass({
|
2015-08-11 00:37:43 +00:00
|
|
|
sendMessage(content) {
|
2015-07-28 15:54:29 +00:00
|
|
|
socket.emit("message:new", {
|
|
|
|
content: content
|
|
|
|
});
|
|
|
|
},
|
2015-08-11 00:37:43 +00:00
|
|
|
|
|
|
|
handleSubmit(e) {
|
2015-07-28 15:54:29 +00:00
|
|
|
e.preventDefault();
|
2015-08-11 00:37:43 +00:00
|
|
|
let content = React.findDOMNode(this.refs.content).value.trim();
|
2015-07-28 15:54:29 +00:00
|
|
|
if (!content) return;
|
|
|
|
React.findDOMNode(this.refs.content).value = '';
|
|
|
|
this.sendMessage(content);
|
|
|
|
return;
|
|
|
|
},
|
2015-08-11 00:37:43 +00:00
|
|
|
|
|
|
|
render() {
|
2015-07-28 15:54:29 +00:00
|
|
|
return (
|
|
|
|
<form onSubmit={this.handleSubmit} >
|
|
|
|
<div className="input-group">
|
|
|
|
<input
|
|
|
|
id="btn-input"
|
|
|
|
type="text"
|
|
|
|
className="form-control"
|
|
|
|
ref="content"
|
|
|
|
placeholder="Be polite please..." />
|
|
|
|
<span className="input-group-btn">
|
|
|
|
<input
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-primary"
|
|
|
|
id="btn-chat"
|
|
|
|
value="Send" />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|