From 0092069372b616e77c6a20ffb3a3a3da22d30517 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 23 Aug 2010 11:57:29 -0700 Subject: [PATCH] Take signed in check out of websocket js --- app/views/js/_websocket_js.haml | 184 ++++++++++++++++---------------- 1 file changed, 91 insertions(+), 93 deletions(-) diff --git a/app/views/js/_websocket_js.haml b/app/views/js/_websocket_js.haml index 443726e50..282a07624 100644 --- a/app/views/js/_websocket_js.haml +++ b/app/views/js/_websocket_js.haml @@ -1,103 +1,101 @@ -- if user_signed_in? - = javascript_include_tag 'FABridge', 'swfobject', 'web_socket' - :javascript - WebSocket.__swfLocation = "/javascripts/WebSocketMain.swf"; - :javascript - $(document).ready(function(){ - function debug(str){ $("#debug").append("

" + str); }; += javascript_include_tag 'FABridge', 'swfobject', 'web_socket' +:javascript + WebSocket.__swfLocation = "/javascripts/WebSocketMain.swf"; + $(document).ready(function(){ + function debug(str){ $("#debug").append("

" + str); }; - ws = new WebSocket("ws://#{request.host}:8080/#{CGI::escape(current_user.id.to_s)}"); + ws = new WebSocket("ws://#{request.host}:8080/#{CGI::escape(current_user.id.to_s)}"); - //Attach onmessage to websocket - ws.onmessage = function(evt) { - var obj = jQuery.parseJSON(evt.data); - debug("got a " + obj['class'] + " for group " + obj['group_id']); + //Attach onmessage to websocket + ws.onmessage = function(evt) { + var obj = jQuery.parseJSON(evt.data); + debug("got a " + obj['class'] + " for group " + obj['group_id']); - if (obj['class']=="retractions"){ - processRetraction(obj['post_id']); - - }else if (obj['class']=="comments"){ - processComment(obj['post_id'], obj['html']) - - }else if (obj['class']=='photos' && onPageForClass('albums')){ - processPhotoInAlbum(obj['photo_hash']) - }else if (obj['class']=='status_messages'){ - processStatusMessage(obj['class'], obj['html'], obj['status_message_hash'], obj['group_id'], obj['mine?']) - }else{ - processPost(obj['class'], obj['html'], obj['group_id'], obj['mine?']) - } - - - }; - ws.onclose = function() { debug("socket closed"); }; - ws.onopen = function() { - ws.send(location.pathname); - debug("connected..."); - }; - - function processRetraction(post_id){ - $('#' + post_id ).fadeOut(500, function(){ - $(this).remove; - }); - } - - function processComment(post_id, html){ - post = $('#' + post_id)[0] - $(' .comment_set li:last', post ).before( - $(html).fadeIn("fast", function(){}) - ); - toggler = $('.show_post_comments', post) - toggler.html( - toggler.html().replace(/\d/,$('.comment_set', post)[0].childElementCount -1)); - } - - function processPost(className, html, groupId, mineBool){ - if(mineBool || onPageForClass(className) || onPageForGroup(groupId)){ - $("#stream").prepend( - $(html).fadeIn("fast", function(){ - $("#stream label:first").inFieldLabels(); - }) - ); - } - } - - function processStatusMessage(className, html, messageHash, groupId, mineBool){ - processPost(className, html, groupId, mineBool); - console.log(messageHash) - if(messageHash['mine?']){ - updateMyLatestStatus(messageHash); - } - } + if (obj['class']=="retractions"){ + processRetraction(obj['post_id']); - function updateMyLatestStatus(messageHash){ - $("#latest_message").text(messageHash['text']); - $("#latest_message_time").text(' - just now'); + }else if (obj['class']=="comments"){ + processComment(obj['post_id'], obj['html']) + + }else if (obj['class']=='photos' && onPageForClass('albums')){ + processPhotoInAlbum(obj['photo_hash']) + }else if (obj['class']=='status_messages'){ + processStatusMessage(obj['class'], obj['html'], obj['status_message_hash'], obj['group_id'], obj['mine?']) + }else{ + processPost(obj['class'], obj['html'], obj['group_id'], obj['mine?']) } - function processPhotoInAlbum(photoHash){ - if (location.href.indexOf(photoHash['album_id']) == -1){ - return ; - } - html = "

\ - \ - \"New \ -
" - $("#thumbnails").append( $(html) ) - $("#"+ photoHash['id'] + " img").load( function() { - $(this).fadeIn("slow"); - }); - } - function onPageForClass(className){ - return ((location.href.indexOf(className) != -1 ) || (location.pathname == '/')) && onPageOne(); - } + }; + ws.onclose = function() { debug("socket closed"); }; + ws.onopen = function() { + ws.send(location.pathname); + debug("connected..."); + }; - function onPageForGroup(groupId){ - return (location.href.indexOf(groupId) != -1 ) + function processRetraction(post_id){ + $('#' + post_id ).fadeOut(500, function(){ + $(this).remove; + }); + } + + function processComment(post_id, html){ + post = $('#' + post_id)[0] + $(' .comment_set li:last', post ).before( + $(html).fadeIn("fast", function(){}) + ); + toggler = $('.show_post_comments', post) + toggler.html( + toggler.html().replace(/\d/,$('.comment_set', post)[0].childElementCount -1)); + } + + function processPost(className, html, groupId, mineBool){ + if(mineBool || onPageForClass(className) || onPageForGroup(groupId)){ + $("#stream").prepend( + $(html).fadeIn("fast", function(){ + $("#stream label:first").inFieldLabels(); + }) + ); } - - function onPageOne() { - var c = document.location.search.charAt(document.location.search.length-1); - return ((c =='') || (c== '1')); + } + + function processStatusMessage(className, html, messageHash, groupId, mineBool){ + processPost(className, html, groupId, mineBool); + console.log(messageHash) + if(messageHash['mine?']){ + updateMyLatestStatus(messageHash); } - }); + } + + function updateMyLatestStatus(messageHash){ + $("#latest_message").text(messageHash['text']); + $("#latest_message_time").text(' - just now'); + } + + function processPhotoInAlbum(photoHash){ + if (location.href.indexOf(photoHash['album_id']) == -1){ + return ; + } + html = "
\ + \ + \"New \ +
" + $("#thumbnails").append( $(html) ) + $("#"+ photoHash['id'] + " img").load( function() { + $(this).fadeIn("slow"); + }); + } + + function onPageForClass(className){ + return ((location.href.indexOf(className) != -1 ) || (location.pathname == '/')) && onPageOne(); + } + + function onPageForGroup(groupId){ + return (location.href.indexOf(groupId) != -1 ) + } + + function onPageOne() { + var c = document.location.search.charAt(document.location.search.length-1); + return ((c =='') || (c== '1')); + } + });