ensl_gathers/public/js/app.js

266 lines
26 KiB
JavaScript
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({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("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 (
React.createElement("form", {onSubmit: this.handleSubmit},
2015-07-22 15:57:15 +00:00
React.createElement("div", {className: "input-group signin"},
2015-07-22 00:00:05 +00:00
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:57:15 +00:00
),
React.createElement("div", {className: "signin"},
React.createElement("p", {className: "text-center"}, React.createElement("small", null, "Just a temporary measure until genuine authentication is implemented"))
2015-07-22 00:00:05 +00:00
)
)
);
}
})
2015-07-22 15:57:15 +00:00
var UserLine = React.createClass({displayName: "UserLine",
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:57:15 +00:00
React.createElement(UserLine, {user: user})
2015-07-21 14:10:24 +00:00
);
});
return (
React.createElement("ul", {className: "nav", id: "side-menu"},
2015-07-22 15:49:20 +00:00
React.createElement(UserCounter, React.__spread({}, this.props)),
2015-07-22 15:57:15 +00:00
users,
React.createElement("li", null, React.createElement("br", null), React.createElement(UserLogin, null), React.createElement("br", null))
2015-07-21 14:10:24 +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
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJhbnNmb3JtZWQuanMiLCJzb3VyY2VzIjpbbnVsbF0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLENBQUMsQ0FBQyxZQUFZOztBQUVkLFlBQVksQ0FBQzs7QUFFYixJQUFJLGlDQUFpQywyQkFBQTtDQUNwQyxNQUFNLEVBQUUsWUFBWTtFQUNuQjtHQUNDLG9CQUFBLElBQUcsRUFBQSxJQUFDLEVBQUE7SUFDSCxvQkFBQSxHQUFFLEVBQUEsQ0FBQSxDQUFDLElBQUEsRUFBSSxDQUFDLEdBQUksQ0FBQSxFQUFBO0tBQ1gsb0JBQUEsR0FBRSxFQUFBLENBQUEsQ0FBQyxTQUFBLEVBQVMsQ0FBQyxtQkFBb0IsQ0FBSSxDQUFBLEVBQUEsU0FBQSxFQUFBLENBQUE7QUFBQSxLQUNyQyxvQkFBQSxNQUFLLEVBQUEsQ0FBQSxDQUFDLFNBQUEsRUFBUyxDQUFDLGdCQUFpQixDQUFBLEVBQUEsR0FBQSxFQUFFLElBQUksQ0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