diaspora/app/views/js/_websocket_js.haml
2010-09-15 17:56:18 -07:00

109 lines
3.1 KiB
Text

-# Copyright (c) 2010, Diaspora Inc. This file is
-# licensed under the Affero General Public License version 3. See
-# the COPYRIGHT file.
= javascript_include_tag 'FABridge', 'swfobject', 'web_socket'
:javascript
WebSocket.__swfLocation = "/javascripts/WebSocketMain.swf";
$(document).ready(function(){
function debug(str){ $("#debug").append("<p>" + str); };
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);
//console.log(obj);
debug("got a " + obj['class'] + " for aspects " + obj['aspect_ids']);
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{
processPost(obj['class'], obj['html'], obj['aspect_ids'])
}
};
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));
if( !$(".comments", post).is(':visible') ){
toggler.click();
}
}
function processPost(className, html, aspectIds){
if(onPageForAspects(aspectIds)){
$("#stream").prepend(
$(html).fadeIn("fast", function(){
$("#stream label:first").inFieldLabels();
})
);
}
}
function processPhotoInAlbum(photoHash){
if (location.href.indexOf(photoHash['album_id']) == -1){
return ;
}
html = "<div class=\'image_thumb\' id=\'"+photoHash['id']+"\' style=\'padding-right:3px;\'> \
<a href=\"/photos/"+ photoHash['id'] +"\"> \
<img alt=\"New thumbnail\" src=\""+ photoHash['thumb_url'] +"\" /> \
</a> </div>"
$("#thumbnails").append( $(html) )
$("#"+ photoHash['id'] + " img").load( function() {
$(this).fadeIn("slow");
});
}
function onPageForClass(className){
return (location.href.indexOf(className) != -1 );
}
function onPageForAspects(aspectIds){
if(location.pathname == '/' && onPageOne()){
return true
}
var found = false;
$.each(aspectIds, function(index, value) {
if(onPageForAspect(value)){ found = true };
});
return found;
}
function onPageForAspect(aspectId){
return (location.href.indexOf(aspectId) != -1 )
}
function onPageOne() {
var c = document.location.search.charAt(document.location.search.length-1);
return ((c =='') || (c== '1'));
}