diaspora/app/views/js/_websocket_js.haml
2010-11-15 19:13:45 -08:00

139 lines
4.1 KiB
Text
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-# Copyright (c) 2010, Diaspora Inc. This file is
-# licensed under the Affero General Public License version 3 or later. See
-# the COPYRIGHT file.
= javascript_include_tag 'vendor/FABridge', 'vendor/swfobject', 'vendor/web_socket'
:javascript
WebSocket.__swfLocation = "#{javascript_path 'vendor/WebSocketMain.swf'}";
$(document).ready(function(){
function debug(str){ $("#debug").append("<p>" + str); };
ws = new WebSocket("#{(APP_CONFIG[:socket_secure])?'wss':'ws'}://#{request.host}:#{APP_CONFIG[:socket_port]}/");
//Attach onmessage to websocket
ws.onmessage = function(evt) {
var obj = jQuery.parseJSON(evt.data);
if(obj['notice']){
processNotification(obj['notice']);
}else if (obj['class'] == 'people'){
processPerson(obj['html']);
}else{
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'], {'notification':obj['notification'], 'mine?':obj['mine?'], 'my_post?':obj['my_post?']})
}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 processPerson(html){
$('.people#stream').prepend(html).slideDown('slow', function(){})
}
function processNotification(html){
$('#notification').html(html).fadeIn(200).delay(4000).fadeOut(200, function(){ $(this).html("");});
}
function processRetraction(post_id){
$("*[data-guid='"+post_id+"']").fadeOut(400, function(){$(this).remove()});
if($("#stream")[0].childElementCount == 0){
$("#no_posts").fadeIn(200);
}
}
function processComment(post_id, html, opts){
post = $("*[data-guid='"+post_id+"']'");
$('.comments li:last', post ).before(
$(html).fadeIn("fast", function(){})
);
toggler = $('.show_post_comments', post)
if(toggler.length > 0){
toggler.html(
toggler.html().replace(/\d+/,$('.comments', post)[0].childElementCount -1));
if( !$(".comments", post).is(':visible') ){
toggler.click();
}
}
if( !opts['mine?'] && opts['my_post?']) {
processNotification(opts['notification']);
}
}
function processPost(className, html, aspectIds){
if(onPageForAspects(aspectIds)){
var addPostToStream = function (html){
$("#stream:not('.show')").prepend(
$(html).fadeIn("fast", function(){
$("#stream").find("label").first().inFieldLabels();
})
)
};
if( $("#no_posts").is(":visible") ){
$("#no_posts").fadeOut(400, addPostToStream(html)).hide();
} else {
addPostToStream(html);
}
}
}
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'));
}