From beb2a433bf18fd1d474314329bd6ee8f40f5bf10 Mon Sep 17 00:00:00 2001 From: timb Date: Mon, 15 Feb 2010 03:35:27 -0800 Subject: attempt to fix scrolling --- static/js/pichat.js | 79 +++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) (limited to 'static/js/pichat.js') diff --git a/static/js/pichat.js b/static/js/pichat.js index d3da949..33788e2 100755 --- a/static/js/pichat.js +++ b/static/js/pichat.js @@ -1,24 +1,44 @@ var cache = {} var pendingMessages = {} +var MaxImagePosts = 40 + function escapeHtml(txt) { - if (!txt) { return ""; } - else { return $("").text(txt).html(); } + if (!txt) { return ""; } + else { return $("").text(txt).html(); } } function linkify(text) { - var URLRegex = /((\b(http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi; - return text.replace(URLRegex, linkReplace); + LastMsgContainsImage = false + var URLRegex = /((\b(http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi; + return text.replace(URLRegex, linkReplace); } +// durty hack to use a global to check this... but otherwise i'd have to rewrite the String.replace function? :/ +var LastMsgContainsImage = false function linkReplace(match){ - var PicRegex = /\.(jpg|jpeg|png|gif|bmp)$/i; - var matchWithoutParams = match.replace(/\?.*$/i, "") - if (PicRegex.test(matchWithoutParams)){ - return "" - } else { - return "" + match + "" - } + var PicRegex = /\.(jpg|jpeg|png|gif|bmp)$/i; + var matchWithoutParams = match.replace(/\?.*$/i, "") + if (PicRegex.test(matchWithoutParams)){ + LastMsgContainsImage = true + return "" + } else { + return "" + match + "" + } +} + +var 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; + } } function buildMsgContent(content) { @@ -26,13 +46,15 @@ function buildMsgContent(content) { } function buildMessageDiv(msg, isLoading) { - var nick = escapeHtml(msg.nick); - var msgId = !isLoading ? 'id="message-' + msg.msg_id + '"' : ''; - var loadingClass = isLoading ? ' loading' : ''; - return '
' - + '' + nick + ': ' - + buildMsgContent(msg.content) - + '
'; + removeOldMessages() + var nick = escapeHtml(msg.nick); + var msgId = !isLoading ? 'id="message-' + msg.msg_id + '"' : ''; + var loadingClass = isLoading ? ' loading' : ''; + var containsImageClass = LastMsgContainsImage ? ' contains-image' : ''; + return '
' + + '' + nick + ': ' + + buildMsgContent(msg.content) + + '
'; } function buildUserDiv(user) { @@ -106,30 +128,12 @@ function ifEnter(fn) { }; } -/* -function isScrolledToBottom(div) { - return Math.abs(div.scrollTop - (div.scrollHeight - div.offsetHeight)) <= 3; -} - -function scrollToBottom(div) { - div.scrollTop = div.scrollHeight; -} - -// Give images time to start loading before scrolling. -// Needed until server knows size of images. -function delayedScrollToBottom(delay) { - setTimeout(scrollToBottom, delay, $('#messageList')[0]); -} -*/ - function addNewMessages(msgs) { - //var wasScrolledToBottom = isScrolledToBottom($('#messageList')[0]); var msgStr = $.map(msgs, buildMessageDiv).join(''); $('#messageList').append(msgStr); } function addNewMessage(msg, isLoading) { - //var wasScrolledToBottom = isScrolledToBottom($('#messageList')[0]); var msgStr = buildMessageDiv(msg, isLoading); var div = $(msgStr).appendTo('#messageList'); return div; @@ -217,7 +221,6 @@ function initChat() { // see /static/webcam/webcam.js if ('webcam' in window) webcam.init() - setTimeout(refresh, 1000); } @@ -297,7 +300,7 @@ function isScrolledToBottom(){ } function scrollIfPossible(){ - if (lastScriptedScrolledPosition == messageList.scrollTop || isScrolledToBottom()) + if (lastScriptedScrolledPosition <= messageList.scrollTop || isScrolledToBottom()) scrollToEnd() } -- cgit v1.2.3-70-g09d2 From 1656fdf68387f7966d9caff9ee1a4f03436b6c51 Mon Sep 17 00:00:00 2001 From: dumpfmprod Date: Sun, 21 Feb 2010 15:25:02 -0500 Subject: Added event tracking, upload filename checking, and fixed linkifying --- static/js/pichat.js | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'static/js/pichat.js') diff --git a/static/js/pichat.js b/static/js/pichat.js index 33788e2..3137ac4 100755 --- a/static/js/pichat.js +++ b/static/js/pichat.js @@ -16,14 +16,17 @@ function linkify(text) { // durty hack to use a global to check this... but otherwise i'd have to rewrite the String.replace function? :/ var LastMsgContainsImage = false -function linkReplace(match){ +function linkReplace(url){ var PicRegex = /\.(jpg|jpeg|png|gif|bmp)$/i; - var matchWithoutParams = match.replace(/\?.*$/i, "") - if (PicRegex.test(matchWithoutParams)){ + var urlWithoutParams = url.replace(/\?.*$/i, ""); + + linkUrl = url.indexOf('http://') == 0 ? url : 'http://' + url; + + if (PicRegex.test(url)){ LastMsgContainsImage = true - return "" + return "" } else { - return "" + match + "" + return "" + url + "" } } @@ -102,6 +105,9 @@ function submitMessage() { var div = addNewMessage(msg, true); var onSuccess = function(json) { + if (typeof pageTracker !== 'undefined') { + pageTracker._trackEvent('Message', 'Submit', typeof Room !== 'undefined' ? Room : 'UnknownRoom'); + } div.attr('id', 'message-' + json) .removeClass('loading').addClass('loaded'); }; @@ -225,6 +231,13 @@ function initChat() { } function initProfile() { + + jQuery(".linkify").each(function() { + var text = jQuery(this).text(); + jQuery(this).html(linkify(text)); + }); + + $('.logged-dump .content').each(function() { var t = $(this); t.html(buildMsgContent(t.text())); @@ -277,11 +290,24 @@ function initLog() { function favoriteImage() {}; function setupUpload(elementId, roomKey) { + var onSubmit = function(file, ext) { + if (!(ext && /^(jpg|png|jpeg|gif)$/i.test(ext))) { + alert('Error: invalid file extension ' + ext); + return false; + } + }; + var onComplete = function(file, response) { + if (typeof pageTracker !== 'undefined') { + pageTracker._trackEvent('Message', 'Upload', typeof Room !== 'undefined' ? Room : 'UnknownRoom'); + } + } new AjaxUpload(elementId, { action: '/upload', autoSubmit: true, name: 'image', - data: { room: roomKey } + data: { room: roomKey }, + onSubmit: onSubmit, + onComplete: onComplete }); } -- cgit v1.2.3-70-g09d2