ensl_gathers/public/js/message.js

148 lines
15 KiB
JavaScript
Raw Normal View History

2015-07-28 15:54:29 +00:00
"use strict";
var Chatroom = React.createClass({displayName: "Chatroom",
getDefaultProps: function () {
return {
history: []
};
},
componentDidMount: function () {
var self = this;
var TIMER_INTERVAL = 60000; // Every minute
socket.on("message:new", function (data) {
var history = self.props.history;
history.push(data);
self.setProps({
history: history
});
self.scrollToBottom();
});
// Message History Retrieved
socket.on("message:refresh", function (data) {
self.setProps({
history: data.chatHistory
});
self.scrollToBottom();
});
socket.emit("message:refresh", {});
self.timer = setInterval(function () {
if (self.refs.messages) self.refs.messages.refreshTime();
}, TIMER_INTERVAL);
},
componentDidUnmount: function () {
clearInterval(this.timer);
},
sendMessage: function (message) {
socket.emit("newMessage", {message: message});
},
scrollToBottom: function () {
var node = React.findDOMNode(this.refs.messageContainer);
node.scrollTop = node.scrollHeight;
},
render: function () {
var messages = this.props.history.map(function (message) {
return (
React.createElement(ChatMessage, {
avatar: message.author.avatar,
username: message.author.username,
content: message.content,
ref: "messages",
createdAt: message.createdAt})
);
});
return (
React.createElement("div", {className: "panel panel-default"},
React.createElement("div", {className: "panel-heading"}, "Gather Chat"),
React.createElement("div", {className: "panel-body"},
React.createElement("ul", {className: "chat", id: "chatmessages", ref: "messageContainer"},
messages
)
),
React.createElement("div", {className: "panel-footer"},
React.createElement(MessageBar, null)
)
)
);
}
});
var ChatMessage = React.createClass({displayName: "ChatMessage",
getInitialState: function () {
return {
timeAgo: $.timeago(this.props.createdAt)
}
},
refreshTime: function () {
var self = this;
self.setState({
timeAgo: $.timeago(self.props.createdAt)
});
},
render: function () {
return (
React.createElement("li", {className: "left clearfix"},
React.createElement("span", {className: "chat-img pull-left"},
React.createElement("img", {
src: this.props.avatar,
alt: "User Avatar",
height: "40",
width: "40",
className: "img-circle"})
),
React.createElement("div", {className: "chat-body clearfix"},
React.createElement("div", {className: "header"},
React.createElement("strong", {className: "primary-font"}, this.props.username),
React.createElement("small", {className: "pull-right text-muted"},
React.createElement("i", {className: "fa fa-clock-o fa-fw"}), " ", this.state.timeAgo
)
),
React.createElement("p", null, this.props.content)
)
)
);
}
});
var MessageBar = React.createClass({displayName: "MessageBar",
sendMessage: function (content) {
socket.emit("message:new", {
content: content
});
},
handleSubmit: function (e) {
e.preventDefault();
var content = React.findDOMNode(this.refs.content).value.trim();
if (!content) return;
React.findDOMNode(this.refs.content).value = '';
this.sendMessage(content);
return;
},
render: function () {
return (
React.createElement("form", {onSubmit: this.handleSubmit},
React.createElement("div", {className: "input-group"},
React.createElement("input", {
id: "btn-input",
type: "text",
className: "form-control",
ref: "content",
placeholder: "Be polite please..."}),
React.createElement("span", {className: "input-group-btn"},
React.createElement("input", {
type: "submit",
className: "btn btn-primary",
id: "btn-chat",
value: "Send"})
)
)
)
);
}
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJhbnNmb3JtZWQuanMiLCJzb3VyY2VzIjpbbnVsbF0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLFlBQVksQ0FBQzs7QUFFYixJQUFJLDhCQUE4Qix3QkFBQTtDQUNqQyxlQUFlLEVBQUUsWUFBWTtFQUM1QixPQUFPO0dBQ04sT0FBTyxFQUFFLEVBQUU7R0FDWCxDQUFDO0VBQ0Y7Q0FDRCxpQkFBaUIsRUFBRSxZQUFZO0VBQzlCLElBQUksSUFBSSxHQUFHLElBQUksQ0FBQztBQUNsQixFQUFFLElBQUksY0FBYyxHQUFHLEtBQUssQ0FBQzs7RUFFM0IsTUFBTSxDQUFDLEVBQUUsQ0FBQyxhQUFhLEVBQUUsVUFBVSxJQUFJLEVBQUU7R0FDeEMsSUFBSSxPQUFPLEdBQUcsSUFBSSxDQUFDLEtBQUssQ0FBQyxPQUFPLENBQUM7R0FDakMsT0FBTyxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQztHQUNuQixJQUFJLENBQUMsUUFBUSxDQUFDO0lBQ2IsT0FBTyxFQ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