From 18648921f75a7c458cf0c951c249b28a48f08a6b Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 4 Aug 2015 23:54:12 -0400 Subject: ws shit --- public/js/lib/chat.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'public/js/lib/chat.js') diff --git a/public/js/lib/chat.js b/public/js/lib/chat.js index 05592c4..0968418 100644 --- a/public/js/lib/chat.js +++ b/public/js/lib/chat.js @@ -3,30 +3,46 @@ var ChatView = View.extend({ template: $("#collaborator-template").html(), events: { - "submit form": "send" + "submit form": "send", + "scroll #messages": "scroll", }, initialize: function(){ this.$msg = this.$("#message") this.$messages = this.$("#messages") + this.messages = this.$messages.get(0) }, add: function(msg){ var $el = $( this.template ) $el.find(".nick").html(msg.nick) $el.find(".msg").html(msg.msg) + this.$messages.append($el) + if (! this.scrolled) { + this.scrollToBottom() + } }, send: function(){ + var val = this.$msg.sanitize() + if (! val) return var msg = {} msg.room = room.name() - msg.msg = this.$msg.val() + msg.msg = val msg.nick = user.username // app.socket.send("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 } }) -- cgit v1.2.3-70-g09d2