summaryrefslogtreecommitdiff
path: root/public/js/lib
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2015-08-04 23:54:12 -0400
committerJules Laplace <jules@okfoc.us>2015-08-04 23:54:12 -0400
commit18648921f75a7c458cf0c951c249b28a48f08a6b (patch)
tree4842285de287da85b820ed878b363175293e34f7 /public/js/lib
parent82f90cffd91b0a68e9e60a41192d7e775f523843 (diff)
ws shit
Diffstat (limited to 'public/js/lib')
-rw-r--r--public/js/lib/chat.js20
-rw-r--r--public/js/lib/socket.js59
-rw-r--r--public/js/lib/ws.js43
3 files changed, 61 insertions, 61 deletions
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
}
})
diff --git a/public/js/lib/socket.js b/public/js/lib/socket.js
deleted file mode 100644
index 4fd11a5..0000000
--- a/public/js/lib/socket.js
+++ /dev/null
@@ -1,59 +0,0 @@
-var socket = function(){
- var socket = {}
- socket.connect: function () {
- if (this.socket) return;
- var socketPath = window.location.origin + '/' + posthang.room.subdomain
-
- this.socket = io(socketPath);
- // this.socket.on('connect', function(){ console.log(new Date(), "connected")});
- this.socket.on('ready', $.proxy(this.socketReady, this));
- this.socket.on('error', $.proxy(this.socketError, this));
- this.socket.on('connect', $.proxy(this.socketConnected, this));
- this.socket.on('reconnect', $.proxy(this.socketReconnected, this));
- this.socket.on('disconnect', $.proxy(this.disconnected, this))
-
- this.socket.on('users', $.proxy(this.updateUsers, this));
- this.socket.on('message', $.proxy(this.chatView.append, this.chatView));
- this.socket.on('info', $.proxy(this.chatView.appendInfo, this.chatView));
- this.socket.on('mediaId', $.proxy(this.mediaView.updateMediaId, this.mediaView));
- this.socket.on('messageId', $.proxy(this.chatView.updateMessageId, this.chatView));
- this.socket.on('roomDeleted', $.proxy(this.roomDeleted, this));
- this.socket.on('userBanned', $.proxy(this.userBanned, this));
- this.socket.on('fave', $.proxy(this.faveView.faved, this.faveView))
-
- $(window).on("scroll", $.proxy(this.didScroll, this))
- $(window).on("focus", $.proxy(this.focus, this))
- $(window).on("blur", $.proxy(this.blur, this))
-
- this.chatView.fetchMessages();
- this.mediaView.fetchMedia();
- },
-
- socketReady: function (obj) {
- console.log(new Date(), "ready")
-
- // presumably we might have reconnected?
- if (this.socketIsReady) {
- this.chatView.fetchAndDedupe()
- }
- else {
- this.socketIsReady = true
- this.checkIfLoaded()
- }
- },
-
- socketError: function (a,b,c){
- console.log(new Date(), "error", a, b, c)
- },
- socketConnected: function (){
- console.log(new Date(), "connected")
- },
- socketReconnected: function (){
- console.log(new Date(), "reconnected")
- },
-
- socketDisconnected: function (){
- this.chatView.appendInfo({ content: "Disconnected." })
- }
- }
-} \ No newline at end of file
diff --git a/public/js/lib/ws.js b/public/js/lib/ws.js
new file mode 100644
index 0000000..37a8248
--- /dev/null
+++ b/public/js/lib/ws.js
@@ -0,0 +1,43 @@
+var ws = function(){
+ var ws = {}
+ var socket, socketIsReady
+ ws.connect = function () {
+ if (this.socket) return;
+ var socketPath = window.location.origin + '/' + posthang.room.subdomain
+
+ ws.socket = socket = io(socketPath)
+ // this.socket.on('connect', function(){ console.log(new Date(), "connected")})
+ socket.on('ready', ws.ready)
+ socket.on('error', ws.error)
+ socket.on('connect', ws.connected)
+ socket.on('reconnect', ws.reconnected)
+ socket.on('disconnect', ws.disconnected)
+ }
+
+ ws.ready = function (obj) {
+ console.log(new Date(), "ready")
+
+ // presumably we might have reconnected?
+ if (socketIsReady) {
+ this.chatView.fetchAndDedupe()
+ }
+ else {
+ socketIsReady = true
+ this.checkIfLoaded()
+ }
+ }
+
+ ws.error = function (a,b,c){
+ console.log(new Date(), "error", a, b, c)
+ }
+ ws.connected = function (){
+ console.log(new Date(), "connected")
+ }
+ ws.reconnected = function (){
+ console.log(new Date(), "reconnected")
+ }
+ ws.disconnected = function (){
+ console.log(new Date(), "disconnected")
+ // this.chatView.appendInfo({ content: "Disconnected." })
+ }
+} \ No newline at end of file