summaryrefslogtreecommitdiff
path: root/public/js/chat.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/js/chat.js')
-rw-r--r--public/js/chat.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/public/js/chat.js b/public/js/chat.js
new file mode 100644
index 0000000..76371c7
--- /dev/null
+++ b/public/js/chat.js
@@ -0,0 +1,62 @@
+var Chat = {
+ height: 300,
+
+ init: function(){
+ $("#chat-message").on("keydown", Chat.keys);
+ },
+
+ keys: function (e) {
+ switch (e.keyCode) {
+ case 13: // enter
+ Chat.send ();
+ break
+ case 33: // pageup
+ var x = $("#chat_container").scrollTop() - Chat.height + 20;
+ $("#chat_container").scrollTop( x );
+ break
+ case 34: // pagedown
+ var x = $("#chat_container").scrollTop() + Chat.height + 20;
+ $("#chat_container").scrollTop( x );
+ break
+ }
+ },
+
+ send: function () {
+ var body = $("#chat-message").val();
+ $("#chat-message").val("");
+
+ Chat.add({ nick: Game.nick, body: body, you: true });
+ scrollToBottom("#chat_container")
+
+ Events.send.message({ nick: Game.nick, body: body });
+ },
+
+ parse: function (raw) {
+ if (! raw) return "";
+ if (raw.indexOf("http") !== -1) {
+ var words = sanitize(raw).split(" ")
+ var parsed = []
+ for (var i = 0; i < words.length; i++) {
+ if (words[i].indexOf("http") === 0) {
+ parsed.push ("<a href='"+words[i]+"' target='_blank'>"+words[i]+"</a>")
+ }
+ else {
+ parsed.push (words[i])
+ }
+ }
+ return parsed.join(" ");
+ }
+ return sanitize(raw);
+ },
+
+ add: function (msg) {
+ var klass = msg.you ? "msg you" : "msg";
+ var html = "<p><span class='" + klass + "'>"+msg.nick+"</span><span class='msg'>"+Chat.parse(msg.body)+"</span></p>";
+ $("#chat").append( html );
+ if ($("#chat p").length > 50) {
+ $("#chat").first().remove();
+ }
+ scrollToBottom("#chat_container");
+ }
+}
+