2015-07-22 00:00:05 +00:00
|
|
|
$(function () {
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2015-07-22 15:49:20 +00:00
|
|
|
var UserCounter = React.createClass({displayName: "UserCounter",
|
2015-07-21 14:10:24 +00:00
|
|
|
render: function () {
|
|
|
|
return (
|
|
|
|
React.createElement("li", null,
|
|
|
|
React.createElement("a", {href: "#"},
|
2015-07-22 15:49:20 +00:00
|
|
|
React.createElement("i", {className: "fa fa-users fa-fw"}), " Online",
|
2015-07-21 14:10:24 +00:00
|
|
|
React.createElement("span", {className: "badge add-left"}, " ", this.props.count, " ")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-07-21 00:24:14 +00:00
|
|
|
});
|
|
|
|
|
2015-07-22 00:00:05 +00:00
|
|
|
var UserLogin = React.createClass({displayName: "UserLogin",
|
2015-07-22 15:35:40 +00:00
|
|
|
authorizeId: function (id) {
|
|
|
|
id = parseInt(id, 10);
|
|
|
|
socket.emit("authorize:id", {
|
|
|
|
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 (
|
|
|
|
React.createElement("form", {onSubmit: this.handleSubmit},
|
|
|
|
React.createElement("div", {className: "input-group"},
|
|
|
|
React.createElement("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..."}),
|
|
|
|
React.createElement("span", {className: "input-group-btn"},
|
|
|
|
React.createElement("input", {
|
|
|
|
type: "submit",
|
|
|
|
className: "btn btn-primary",
|
|
|
|
id: "btn-chat",
|
|
|
|
value: "Login"})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-07-22 15:49:20 +00:00
|
|
|
var User = React.createClass({displayName: "User",
|
2015-07-21 14:10:24 +00:00
|
|
|
render: function () {
|
|
|
|
return (
|
|
|
|
React.createElement("li", null,
|
2015-07-22 15:49:20 +00:00
|
|
|
React.createElement("a", {href: "#"}, this.props.user.username)
|
2015-07-21 14:10:24 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-07-21 00:24:14 +00:00
|
|
|
});
|
|
|
|
|
2015-07-22 15:49:20 +00:00
|
|
|
var UserMenu = React.createClass({displayName: "UserMenu",
|
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:49:20 +00:00
|
|
|
React.createElement(User, {user: user})
|
2015-07-21 14:10:24 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
React.createElement("ul", {className: "nav", id: "side-menu"},
|
2015-07-22 00:00:05 +00:00
|
|
|
React.createElement("li", null,
|
|
|
|
React.createElement(UserLogin, null)
|
|
|
|
),
|
2015-07-22 15:49:20 +00:00
|
|
|
React.createElement(UserCounter, React.__spread({}, this.props)),
|
|
|
|
users
|
2015-07-21 14:10:24 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-07-20 22:47:18 +00:00
|
|
|
});
|
|
|
|
|
2015-07-21 14:10:24 +00:00
|
|
|
var Chatroom = React.createClass({displayName: "Chatroom",
|
|
|
|
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);
|
|
|
|
console.log(node)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2015-07-22 15:35:40 +00:00
|
|
|
imageUrl: function () {
|
|
|
|
var BASE_URL = "http://www.ensl.org/"
|
|
|
|
return BASE_URL + this.props.avatar;
|
|
|
|
},
|
2015-07-21 14:10:24 +00:00
|
|
|
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", {
|
2015-07-22 15:35:40 +00:00
|
|
|
src: this.imageUrl(),
|
2015-07-21 14:10:24 +00:00
|
|
|
alt: "User Avatar",
|
|
|
|
height: "40",
|
2015-07-22 15:20:28 +00:00
|
|
|
width: "40",
|
2015-07-21 14:10:24 +00:00
|
|
|
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"})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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-22 15:49:20 +00:00
|
|
|
React.render(React.createElement(UserMenu, {count: 0, users: []}), document.getElementById('side-menu'));
|
2015-07-22 00:00:05 +00:00
|
|
|
React.render(React.createElement(Chatroom, {history: []}), document.getElementById('chatroom'));
|
|
|
|
};
|
|
|
|
|
|
|
|
initialiseComponents();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-07-22 14:13:08 +00:00
|
|
|
|
2015-07-22 15:49:20 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJhbnNmb3JtZWQuanMiLCJzb3VyY2VzIjpbbnVsbF0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLENBQUMsQ0FBQyxZQUFZOztBQUVkLFlBQVksQ0FBQzs7QUFFYixJQUFJLGlDQUFpQywyQkFBQTtDQUNwQyxNQUFNLEVBQUUsWUFBWTtFQUNuQjtHQUNDLG9CQUFBLElBQUcsRUFBQSxJQUFDLEVBQUE7SUFDSCxvQkFBQSxHQUFFLEVBQUEsQ0FBQSxDQUFDLElBQUEsRUFBSSxDQUFDLEdBQUksQ0FBQSxFQUFBO0tBQ1gsb0JBQUEsR0FBRSxFQUFBLENBQUEsQ0FBQyxTQUFBLEVBQVMsQ0FBQyxtQkFBb0IsQ0FBSSxDQUFBLEVBQUEsU0FBQSxFQUFBLENBQUE7QUFBQSxLQUNyQyxvQkFBQSxNQUFLLEVBQUEsQ0FBQSxDQUFDLFNBQUEsRUFBUyxDQUFDLGdCQUFpQixDQUFBLEVBQUEsR0FBQSxFQUFFLElBQUksQ0FBQyxLQUFLLENBQUMsS0FBSyxFQUFDLEdBQVEsQ0FBQTtJQUN6RCxDQUFBO0dBQ0EsQ0FBQTtJQUNKO0VBQ0Y7QUFDRixDQUFDLENBQUMsQ0FBQzs7QUFFSCxJ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