$(function () {
"use strict";
var UserCounter = React.createClass({
render: function () {
return (
Online
{this.props.count}
);
}
});
var UserLogin = React.createClass({
authorizeId: function (id) {
id = parseInt(id, 10);
socket.emit("users:authorize", {
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;
},
render: function () {
return (
);
}
})
var UserLine = React.createClass({
render: function () {
return (
{this.props.user.username}
);
}
});
var UserMenu = React.createClass({
getDefaultProps: function () {
return {
count: 0,
users: []
};
},
componentDidMount: function () {
socket.on('userCount', this.updateUsers);
},
updateUsers: function (data) {
this.setProps({
count: data.count,
users: data.users
});
},
render: function () {
var users = this.props.users.map(function (user) {
return (
);
});
return (
);
}
});
var Chatroom = React.createClass({
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 () {
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 (
);
});
return (
);
}
});
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 (
{this.props.username}
{this.state.timeAgo}
{this.props.content}
);
}
});
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 (
);
}
});
var Gather = React.createClass({
getDefaultProps: function () {
return {
gather: {
gatherers: []
}
}
},
joinedGather: function () {
var self = this;
return this.props.gather.gatherers.some(function (gatherer) {
return gatherer.user.id === self.props.user.id;
});
},
componentDidMount: function () {
var self = this;
socket.on("gather:refresh", function (data) {
self.setProps({
gather: data.gather,
user: data.user
});
});
},
joinGather: function (e) {
e.preventDefault();
socket.emit("gather:join", {});
},
leaveGather: function (e) {
e.preventDefault();
socket.emit("gather:leave", {});
},
render: function () {
var joinButton;
if (this.joinedGather()) {
joinButton = ();
} else {
joinButton = ();
}
return (
Current Gather
{this.props.gather.gatherers.length}
{joinButton}
);
}
});
// var GatherState = React.createClass({
// getDefaultProps: function () {
// return {
// "state": "none"
// }
// },
// stateDescription: function () {
// switch(this.props.date) {
// case "gathering":
// return "Waiting on more players to join"
// }
// },
// render: function () {
//
//
{this.displayState}
//
// }
// })
var Gatherers = React.createClass({
render: function () {
var gatherers = this.props.gatherers.map(function (gatherer) {
return (
{gatherer.user.username} |
Division 2 |
Lerk |
);
})
return (
Player |
Ability |
Life Forms |
{gatherers}
);
}
});
var socket;
function initialiseComponents () {
var socketUrl = window.location.protocol + "//" + window.location.host;
socket = io(socketUrl)
.on("connect", function () {
console.log("Connected");
})
.on("reconnect", function () {
console.log("Reconnected");
})
.on("disconnect", function () {
console.log("Disconnected")
});
React.render(, document.getElementById('side-menu'));
React.render(, document.getElementById('chatroom'));
React.render(, document.getElementById('gathers'));
};
initialiseComponents();
});