summaryrefslogtreecommitdiff
path: root/static/js/src/chat.js
blob: f24917c7588a152c40ee07429ae3a5fc3e286e7a (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Called on loading a chatroom

function initChat() {
  Search.initInpage()

  $('#textbutton input').attr('checked', TextEnabled).change(setTextEnable);
  $('#imgbutton input').attr('checked', ImgsEnabled).change(setImgsEnable);
/*  $('#clearbutton input').click(function() {
    track('UI', 'ClearScreen');
    $('.dump').remove();
    $(this).removeAttr('checked');
    return false;
  });
*/
  
    $('.oldmsg').each(function() {
        var dump = $(this);
        var content = dump.find(".content")
        MessageContentCache[dump.attr("id").substr(8)] = content.text()
        content.html(buildMsgContent(content.text(), Recips));

        if ((ImgsEnabled && dump.hasClass('contains-image')) || (TextEnabled && !dump.hasClass('contains-image')))
            dump.show();            
        else
            dump.hide();
    });
    Drag.bindImages();

    $('#msgInput').keyup(ifEnter(submitMessage));
    $('#msgSubmit').click(sendClicked);
    $('#palette-button').click(paletteClicked);

    messageList = $("#messageList")[0]

    initChatThumb();

    scrollToEnd()
    scrollWatcher()
    
    // see /static/webcam/webcam.js
    if ('webcam' in window) webcam.init()
    
    startChatUpdater();
}


window.imgZoomThreshhold = [125, 125];

function initChatMsgs() {
    $('.msgDiv .content').live('mouseenter', function(e) {
	$(this).addClass('msg-hover');
    });

    $('.msgDiv .content').live('mouseleave', function(e) {
	$(this).removeClass('msg-hover');
    });

    $('.msgDiv .content .img-wrapper').live('mouseenter', function(e) {
	var img = $(this).find('img');

	if (img.width() < imgZoomThreshhold[0] || img.height() < imgZoomThreshhold[1])
	    return;	
	
        var zoomlink = $('<a>')
	    .attr({'href': img.attr('src') })
	    .addClass('msg-image-zoom')
	    .append($('<img>').attr('src', 'http://dump.fm/static/img/zoom.gif')
		    .addClass('zoom-icon'))
	    .click(function() { window.open(img.attr('src')); return false; });
        $(this).append(zoomlink);
    });

    $('.msgDiv .content .img-wrapper').live('mouseleave', function(e) {
	$(this).find('.msg-image-zoom').remove();
    });
    

    $('.content').live('click', function(e) {
	var tagName = e.target.tagName;
	if (tagName == 'A' || tagName == 'EMBED' || $(e.target).hasClass('youtube-thumb')) {
            return true;
	}
	var msg = $(this).parent('.msgDiv');
	var wasFavorited = msg.hasClass("favorite");
	var button = msg.find('.chat-thumb');
	if (wasFavorited) {
            $(button).attr("src", Imgs.chatThumbOff);
	} else {
            $(button).attr("src", Imgs.chatThumbBig);
            $(button).stop().animate(Anim.chatThumbBig, 'fast').animate(Anim.chatThumb, 'fast', 'swing');
	}
	Tag.favorite(button);
	return false;
    });
}
function startChatUpdater() {
    setTimeout(refresh, 1000);
}

function makePlainText() {
    var j = $(this);
    j.text(j.text());
}



function initChatThumb(){
  $(".chat-thumb").live('mouseover mouseout',
    function(e) {
      var favorited = $(this).parents(".dump").hasClass("favorite") ? true : false;
      if (e.type == "mouseover") {
        if (favorited) {
          $(this).attr("src", Imgs.chatThumbOff);
        } else {
          $(this).attr("src", Imgs.chatThumbBig);
          $(this).stop().animate(Anim.chatThumbBig, 'fast')
        }
      } else { // mouseout
        if (favorited) {
          $(this).attr("src", Imgs.chatThumb);
          $(this).stop().animate(Anim.chatThumb, 'fast');
        } else {
          $(this).delay(600).stop().animate(Anim.chatThumbTiny, 'fast', 'swing',
            function(){
              $(this).attr("src", Imgs.chatThumbDot)
              $(this).animate(Anim.chatThumb, 0)
            })
        }
      }
  })
}





// grab message id etc from some element e that's inside a dump
// (messages have something like id="message-0001" class="dump" )
function getMessageInfo(e){
    var message = $(e).parents(".dump")
    var id = message.attr("id").substr(8) // cut "message-001" to "001"
    var nick = message.attr("nick")
    var link = "http://dump.fm/p/" + nick + "/" + id
    var content = message.find(".linkify")
    if (!content.length) content = message.find(".content")
    var rawContent = content.html()
    var img = content.find("img").attr("src")
    var via = "via " + nick + " on dump.fm"
    return {"nick": nick, "id": id, "link": encodeURIComponent(link), 
            "content": rawContent, "img": encodeURIComponent(img), 
            "via": encodeURIComponent(via)}
}
// Message Handling

window.ImageMsgCount = 0
function removeOldMessages(){
    // don't count posts that are all text
    if (LastMsgContainsImage) ImageMsgCount += 1;
    while (ImageMsgCount > MaxImagePosts) {
        var imgMsg = $(".contains-image:first")
        if (imgMsg.length) {
            imgMsg.prevAll().remove() // remove all text messages before the image message
            imgMsg.remove()
        } else break;
        ImageMsgCount -= 1;
    }
}

window.TextEnabled = Preferences.getProperty("chat.textEnabled", "true") == "true";
window.ImgsEnabled = Preferences.getProperty("chat.imgsEnabled", "true") == "true";

function muteSelector() {
    var muted = [];
    for (nick in MUTES) {
        muted.push(".nick_" + nick);
    }
    return muted.join(",");
}

function setTextEnable() {
    var muted = muteSelector();
    if ($(this).attr('checked')) {
        TextEnabled = true;
        Preferences.setProperty("chat.textEnabled", "false");
        track('UI', 'TextEnabled');
        $('.dump').not('.contains-image,'+muted).show();
    } else {
        TextEnabled = false;
        Preferences.setProperty("chat.textEnabled", "false");
        track('UI', 'TextDisabled');
        $('.dump').not('.contains-image').hide()
    }
};

function setImgsEnable() {
    var muted = muteSelector();
    if ($(this).attr('checked')) {
        ImgsEnabled = true;
        Preferences.setProperty("chat.imgsEnabled", "true");
        track('UI', 'ImgsEnabled');
        $('.contains-image').not(muted).show();
    } else {
        ImgsEnabled = false;
        Preferences.setProperty("chat.imgsEnabled", "false");
        track('UI', 'ImgsDisabled');
        $('.contains-image').hide();
    }
};


// scrolling stuff
// this code keeps the div scrolled to the bottom, but will also let the user scroll up, without jumping down

function isScrolledToBottom(){
    var threshold = 15;
    
    var containerHeight = messageList.style.pixelHeight || messageList.offsetHeight
    var currentHeight = (messageList.scrollHeight > 0) ? messageList.scrollHeight : 0

    var result = (currentHeight - messageList.scrollTop - containerHeight < threshold);

    return result;  
}

function scrollIfPossible(){
    if (lastScriptedScrolledPosition <= messageList.scrollTop || isScrolledToBottom())
        scrollToEnd()
}

window.lastScriptedScrolledPosition = 0
function scrollToEnd(){
    messageList.scrollTop = messageList.scrollHeight
    lastScriptedScrolledPosition = messageList.scrollTop
}

function scrollWatcher(){
    scrollIfPossible()
    setTimeout(scrollWatcher, 500)
}