Skip to content
Extraits de code Groupes Projets
_websocket_js.haml 3,29 ko
Newer Older
  • Learn to ignore specific revisions
  • -  if user_signed_in?
      - unless request.user_agent.include? "Safari" ||"Chrome"
        = javascript_include_tag 'FABridge', 'swfobject', 'web_socket'
        :javascript
            WebSocket.__swfLocation = "/javascripts/WebSocketMain.swf";
      :javascript
        $(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
    
            var obj = jQuery.parseJSON(evt.data);
            debug("got a " + obj['class']);
    
            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'])
    
            }else{
              processPost(obj['class'], obj['html'])
    
          }; 
          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(){})
            );
    
    Raphael's avatar
    Raphael a validé
            toggler = $('.show_post_comments', post)
            toggler.html(
              toggler.html().replace(/\d/,$('.comment_set', post)[0].childElementCount -1));
    
          function processPost(className, html){
            if(onPageForClass(className)){
    
                $(html).fadeIn("fast", function(){
    
                  $("#stream label:first").inFieldLabels();
    
          function processStatusMessage(className, html, messageHash){
            processPost(className, html);
    
            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 ;
            }
    
    Raphael's avatar
    Raphael a validé
            html =  "<div class=\'image_thumb\' id=\'"+photoHash['id']+"\'> \
              <a href=\"/photos/"+ photoHash['id'] +"\"> \
              <img alt=\"New thumbnail\" src=\""+ photoHash['thumb_url'] +"\" /> \
              </a> </div>"  
            $("#thumbnails").append( $(html) )
    
            $("#"+ photoHash['id'] + "  img").load( function() {
    
    Raphael's avatar
    Raphael a validé
              $(this).fadeIn("slow");
            });
    
          
          function onPageForClass(className){
            return ((location.href.indexOf(className) != -1 ) || (location.pathname == '/')) && onPageOne();
          }
          
          function onPageOne() {
              var c = document.location.search.charAt(document.location.search.length-1);
              return ((c =='') || (c== '1'));
          }
        });