summaryrefslogtreecommitdiff
path: root/static/js/chat.js
blob: 3cd7e01b205fdbea4adbebb98a4a24c34bdb6aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var chat = (function()
{
  var self = {
    show: function () 
    {      
      $("#chaticon").hide("slide", { direction: "down" }, 500, function()
      {
        $("#chatbox").show("slide", { direction: "down" }, 750, self.scrollDown);
 	$("#chatbox").resizable({ handles: 'nw', start: function(event,ui){
	  $("#editorcontainer").hide();
        }, stop: function(event,ui){
          $("#editorcontainer").show();
 	  self.scrollDown();
        }});

      });
    },
    hide: function () 
    {
      $("#chatcounter").text("0");
      $("#chatbox").hide("slide", { direction: "down" }, 750, function()
      {
        $("#chaticon").show("slide", { direction: "down" }, 500);
      });
    },
    scrollDown: function()
    {
      //console.log($('#chatbox').css("display"));
    
      if($('#chatbox').css("display") != "none")
        $('#chattext').animate({scrollTop: $('#chattext')[0].scrollHeight}, "slow");
    }, 
    send: function()
    {
      var text = $("#chatinput").val();
      pad.collabClient.sendMessage({"type": "CHAT_MESSAGE", "text": text});
      $("#chatinput").val("");
    },
    addMessage: function(msg, increment)
    {    
      //correct the time
      msg.time += pad.clientTimeOffset; 
      
      //create the time string
      var minutes = "" + new Date(msg.time).getMinutes();
      var hours = "" + new Date(msg.time).getHours();
      if(minutes.length == 1)
        minutes = "0" + minutes ;
      if(hours.length == 1)
        hours = "0" + hours ;
      var timeStr = hours + ":" + minutes;
        
      //create the authorclass
      var authorClass = "author-" + msg.userId.replace(/[^a-y0-9]/g, function(c)
      {
        if (c == ".") return "-";
        return 'z' + c.charCodeAt(0) + 'z';
      });

      var text = padutils.escapeHtmlWithClickableLinks(padutils.escapeHtml(msg.text), "_blank");
      var authorName = msg.userName == null ? "unnamed" : padutils.escapeHtml(msg.userName); 
      
      var html = "<p class='" + authorClass + "'><b>" + authorName + ":</b><span class='time'>" + timeStr + "</span> " + text + "</p>";
      $("#chattext").append(html);
      
      //should we increment the counter??
      if(increment)
      {
        var count = Number($("#chatcounter").text());
        count++;
        $("#chatcounter").text(count);
        // chat throb stuff -- Just make it throb in for ~2 secs then fadeotu
        $('#chatthrob').html("<b>"+authorName+"</b>" + ": " + text);
        $('#chatthrob').effect("pulsate", {times:1,mode:"hide"},2000);
      }
      
      self.scrollDown();

    },
    init: function()
    {
      $("#chatinput").keypress(function(evt)
      {
        //if the user typed enter, fire the send
        if(evt.which == 13)
        {
          evt.preventDefault();
          self.send();
        }
      });
      
      for(var i in clientVars.chatHistory)
      {
        this.addMessage(clientVars.chatHistory[i], false);
      }
      $("#chatcounter").text(clientVars.chatHistory.length);
    }
  }

  return self;
}());