ensl_gathers/lib/react/app.jsx

328 lines
6.9 KiB
React
Raw Normal View History

2015-07-22 00:00:05 +00:00
$(function () {
"use strict";
2015-07-22 15:49:20 +00:00
var UserCounter = React.createClass({
2015-07-21 14:10:24 +00:00
render: function () {
return (
<li>
<a href="#">
2015-07-22 15:49:20 +00:00
<i className="fa fa-users fa-fw"></i> Online
2015-07-21 14:10:24 +00:00
<span className="badge add-left"> {this.props.count} </span>
</a>
</li>
);
}
});
2015-07-22 00:00:05 +00:00
var UserLogin = React.createClass({
2015-07-22 15:35:40 +00:00
authorizeId: function (id) {
id = parseInt(id, 10);
socket.emit("users:authorize", {
2015-07-22 15:35:40 +00:00
id: id
});
},
handleSubmit: function (e) {
e.preventDefault();
var id = React.findDOMNode(this.refs.authorize_id).value.trim();
if (!id) return;
React.findDOMNode(this.refs.authorize_id).value = '';
this.authorizeId(id);
return;
2015-07-22 00:00:05 +00:00
},
render: function () {
return (
<form onSubmit={this.handleSubmit} >
2015-07-22 15:57:15 +00:00
<div className="input-group signin">
2015-07-22 00:00:05 +00:00
<input
id="btn-input"
type="text"
className="form-control"
2015-07-22 15:35:40 +00:00
ref="authorize_id"
2015-07-22 00:00:05 +00:00
placeholder="Choose an ID..." />
<span className="input-group-btn">
<input
type="submit"
className="btn btn-primary"
id="btn-chat"
value="Login" />
</span>
</div>
2015-07-22 15:57:15 +00:00
<div className="signin">
<p className="text-center"><small>Just a temporary measure until genuine authentication is implemented</small></p>
</div>
2015-07-22 00:00:05 +00:00
</form>
);
}
})
2015-07-22 15:57:15 +00:00
var UserLine = React.createClass({
2015-07-21 14:10:24 +00:00
render: function () {
return (
<li>
2015-07-22 15:49:20 +00:00
<a href="#">{this.props.user.username}</a>
2015-07-21 14:10:24 +00:00
</li>
);
}
2015-07-21 00:24:14 +00:00
});
2015-07-22 15:49:20 +00:00
var UserMenu = React.createClass({
2015-07-24 15:01:56 +00:00
getDefaultProps: function () {
return {
count: 0,
users: []
};
},
2015-07-21 14:10:24 +00:00
componentDidMount: function () {
2015-07-22 15:49:20 +00:00
socket.on('userCount', this.updateUsers);
2015-07-21 14:10:24 +00:00
},
2015-07-22 15:49:20 +00:00
updateUsers: function (data) {
2015-07-21 14:10:24 +00:00
this.setProps({
count: data.count,
2015-07-22 15:49:20 +00:00
users: data.users
2015-07-21 14:10:24 +00:00
});
},
render: function () {
2015-07-22 15:49:20 +00:00
var users = this.props.users.map(function (user) {
2015-07-21 14:10:24 +00:00
return (
2015-07-22 15:57:15 +00:00
<UserLine user={user} />
2015-07-21 14:10:24 +00:00
);
});
return (
<ul className="nav" id="side-menu">
2015-07-22 15:49:20 +00:00
<UserCounter {...this.props} />
{users}
2015-07-22 15:57:15 +00:00
<li><br /><UserLogin /><br /></li>
2015-07-21 14:10:24 +00:00
</ul>
);
}
2015-07-21 00:24:14 +00:00
});
2015-07-21 14:10:24 +00:00
var Chatroom = React.createClass({
2015-07-24 15:01:56 +00:00
getDefaultProps: function () {
return {
history: []
};
},
2015-07-21 14:10:24 +00:00
componentDidMount: function () {
var self = this;
var TIMER_INTERVAL = 60000; // Every minute
2015-07-21 00:24:14 +00:00
2015-07-21 14:10:24 +00:00
socket.on("message:new", function (data) {
var history = self.props.history;
history.push(data);
self.setProps({
history: history
});
self.scrollToBottom();
});
2015-07-21 00:24:14 +00:00
2015-07-21 14:10:24 +00:00
// Message History Retrieved
socket.on("message:refresh", function (data) {
self.setProps({
history: data.chatHistory
});
self.scrollToBottom();
});
socket.emit("message:refresh", {});
self.timer = setInterval(function () {
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 (
<ChatMessage
avatar={message.author.avatar}
username={message.author.username}
content={message.content}
ref="messages"
createdAt={message.createdAt} />
);
});
return (
<div className="panel panel-default">
<div className="panel-heading">Gather Chat</div>
<div className="panel-body">
<ul className="chat" id="chatmessages" ref="messageContainer">
{messages}
</ul>
</div>
<div className="panel-footer">
<MessageBar />
</div>
</div>
);
}
});
var ChatMessage = React.createClass({
getInitialState: function () {
return {
timeAgo: $.timeago(this.props.createdAt)
}
},
refreshTime: function () {
var self = this;
self.setState({
timeAgo: $.timeago(self.props.createdAt)
});
},
render: function () {
return (
<li className="left clearfix">
<span className="chat-img pull-left">
<img
2015-07-22 23:30:14 +00:00
src={this.props.avatar}
2015-07-21 14:10:24 +00:00
alt="User Avatar"
height="40"
2015-07-22 15:35:40 +00:00
width="40"
2015-07-21 14:10:24 +00:00
className="img-circle" />
</span>
<div className="chat-body clearfix">
<div className="header">
<strong className="primary-font">{this.props.username}</strong>
<small className="pull-right text-muted">
<i className="fa fa-clock-o fa-fw"></i> {this.state.timeAgo}
</small>
</div>
<p>{this.props.content}</p>
</div>
</li>
);
}
});
var MessageBar = React.createClass({
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 (
<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>
);
}
});
2015-07-22 22:34:57 +00:00
var Gather = React.createClass({
2015-07-24 15:01:56 +00:00
getDefaultProps: function () {
return {
gather: {
gatherers: []
}
}
},
componentDidMount: function () {
var self = this;
socket.on("gather:refresh", function (data) {
self.setProps({
gather: data.gather
});
});
},
2015-07-22 22:34:57 +00:00
joinGather: function (e) {
e.preventDefault();
2015-07-24 15:01:56 +00:00
socket.emit("gather:join", {});
2015-07-22 22:34:57 +00:00
},
render: function () {
2015-07-24 15:01:56 +00:00
var gatherers = this.props.gather.gatherers.map(function (gatherer) {
console.log(gatherer)
return (<Gatherer gatherer={gatherer} />);
})
2015-07-22 22:34:57 +00:00
return (
<div className="panel panel-default">
<div className="panel-heading">
Current Gather
</div>
2015-07-24 15:01:56 +00:00
<table className="table">
<tbody>
{gatherers}
</tbody>
</table>
2015-07-22 22:34:57 +00:00
<div className="panel-body">
</div>
<div className="panel-footer">
<button
onClick={this.joinGather}
className="btn btn-primary"
ref="joinbutton">Join Gather</button>
</div>
</div>
);
}
});
2015-07-24 15:01:56 +00:00
var Gatherer = React.createClass({
render: function () {
return (
<tr><td>{this.props.gatherer.user.username}</td></tr>
);
}
});
2015-07-22 00:00:05 +00:00
var socket;
function initialiseComponents () {
2015-07-22 14:13:08 +00:00
var socketUrl = window.location.protocol + "//" + window.location.host;
socket = io(socketUrl)
2015-07-22 00:00:05 +00:00
.on("connect", function () {
console.log("Connected");
})
.on("reconnect", function () {
console.log("Reconnected");
})
.on("disconnect", function () {
console.log("Disconnected")
});
2015-07-24 15:01:56 +00:00
React.render(<UserMenu />, document.getElementById('side-menu'));
React.render(<Chatroom />, document.getElementById('chatroom'));
React.render(<Gather />, document.getElementById('gathers'));
2015-07-22 00:00:05 +00:00
};
initialiseComponents();
});