diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-08-06 13:04:24 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-08-06 13:04:24 -0400 |
| commit | 277e73e9d7e118b5cc1bd5888eb502b3d7380ec8 (patch) | |
| tree | f1b2f03ad2728cfa2a8b10fcbf10f5f3030dc216 /public/js/lib/views/room/chat.js | |
| parent | fd3e6661d8911fc0ede063ade5c094c7188443e5 (diff) | |
| parent | 18ebdc7abb9982ceeba1aac651b1c874fad2b6c0 (diff) | |
Merge branch 'master' of ghghgh.us:asdf-yt
Diffstat (limited to 'public/js/lib/views/room/chat.js')
| -rw-r--r-- | public/js/lib/views/room/chat.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/public/js/lib/views/room/chat.js b/public/js/lib/views/room/chat.js new file mode 100644 index 0000000..0932e47 --- /dev/null +++ b/public/js/lib/views/room/chat.js @@ -0,0 +1,62 @@ +var ChatView = View.extend({ + + el: "#chat", + + template: $("#message_template").html(), + + events: { + "submit form": "send", + "scroll #messages": "scroll", + }, + + initialize: function(socket){ + var $msg = this.$msg = this.$("#message") + this.$messages = this.$("#messages") + this.messages = this.$messages.get(0) + this.$msg.focus() + $(window).focus(function(){ $msg.focus() }) + }, + + seen: {}, + add: function(msg){ + var key = msg.date + "_" + msg.nick + if (key in this.seen) return + this.seen[key] = true + + var $el = $( this.template ) + $el.find(".nick").html(sanitize(msg.nick)) + $el.find(".msg").html(sanitizeHTML(msg.msg)) + this.$messages.append($el) + if (! this.scrolled) { + this.scrollToBottom() + } + }, + + send: function(e){ + e && e.preventDefault() + var val = this.$msg.sanitize() + this.$msg.focus() + if (! val) return + this.$msg.val("") + var msg = {} + msg.room = room.name + msg.msg = val + msg.nick = user.username + msg.date = +new Date + this.add(msg) + app.socket.emit("message", msg) + }, + + empty: function(){ + this.$messages.empty() + }, + + scrolled: false, + scroll: function(){ + this.scrolled = (this.messages.scrollTop > this.messages.scrollHeight - this.$el.height() - 100) + }, + scrollToBottom: function(){ + this.messages.scrollTop = document.body.scrollHeight + } + +}) |
