"use strict"; var Chatroom = React.createClass({ getDefaultProps() { return { history: [] }; }, componentDidMount() { let self = this; socket.on("message:append", data => { let history = self.props.history; let historicalUpdate = !!data.messages; if (historicalUpdate) { history = history.concat(data.messages); } else { history.push(data); } self.setProps({ history: history.sort((a, b) => { return new Date(a.createdAt) - new Date(b.createdAt); }) }); if (!historicalUpdate) { self.scrollToBottom(); } }); // Message History Retrieved socket.on("message:refresh", data => { self.setProps({ history: data.chatHistory }); self.scrollToBottom(); }); socket.on("users:update", data => { self.setProps({ currentUser: data.currentUser }); }); socket.emit("message:refresh", {}); }, loadMoreMessages() { var earliestMessage = this.props.history[0]; if (earliestMessage === undefined) return; socket.emit("message:refresh", { before: earliestMessage.createdAt }); }, sendMessage(message) { socket.emit("newMessage", {message: message}); }, scrollToBottom() { let node = React.findDOMNode(this.refs.messageContainer); node.scrollTop = node.scrollHeight; }, render() { let messages = this.props.history.map(message => ); return (
Gather Chat
); } }); var ChatMessage = React.createClass({ mixins: [ ReactAutolink ], getInitialState() { return { createdAt: "" } }, updateCreatedAt() { let self = this; if (this.props.message.createdAt) { self.setState({ createdAt: $.timeago(self.props.message.createdAt) }) } }, componentDidMount() { this.updateCreatedAt(); this.interval = setInterval(this.updateCreatedAt, 60000); }, componentWillUnmount: function () { clearInterval(this.interval); }, messageContent: function () { return this.autolink(this.props.message.content, { target: "_blank", rel: "nofollow" }); }, render() { let deleteButton; let currentUser = this.props.currentUser; if (currentUser && currentUser.admin) { deleteButton = ; } return (
  • User Avatar
    {this.props.message.author.username} {deleteButton} {this.state.createdAt}

    {this.messageContent()}

  • ); } }); var DeleteMessageButton = React.createClass({ handleClick (e) { e.preventDefault(); socket.emit("message:delete", { id: this.props.messageId }); }, render() { return ( ); } }) var MessageBar = React.createClass({ sendMessage(content) { socket.emit("message:new", { content: content }); }, handleSubmit(e) { e.preventDefault(); let content = React.findDOMNode(this.refs.content).value.trim(); if (!content) return; React.findDOMNode(this.refs.content).value = ''; this.sendMessage(content); return; }, render() { return (
    ); } });