diff options
| author | Jules Laplace <jules@okfoc.us> | 2013-02-22 14:14:21 -0800 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2013-02-22 14:14:21 -0800 |
| commit | 66c9c742c4efad7d72e72d13ef8b9c6040504a12 (patch) | |
| tree | 9a53ad9bda6f9584a6861f25f99b363688bd9fa0 /public/js/chat.js | |
| parent | e632451fec8be5d5ec6a39d7789f91e0c7feaeb3 (diff) | |
Yeah
Diffstat (limited to 'public/js/chat.js')
| -rw-r--r-- | public/js/chat.js | 62 |
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"); + } +} + |
