From 662bac182145da031e99011255d60e8b7b6c798c Mon Sep 17 00:00:00 2001 From: sostler Date: Tue, 10 Nov 2009 00:08:47 -0500 Subject: New version --- static/pichat.js | 75 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 26 deletions(-) (limited to 'static/pichat.js') diff --git a/static/pichat.js b/static/pichat.js index 99bf1b5..aeef1bd 100755 --- a/static/pichat.js +++ b/static/pichat.js @@ -51,28 +51,52 @@ function join() { }); } +function escapeHtml(txt) { + return $("").text(txt).html(); +} + function buildUserDiv(user) { - return '
' + user + '
'; + return '
' + escapeHtml(user) + '
'; +} + +// http://rickyrosario.com/blog/converting-a-url-into-a-link-in-javascript-linkify-function +function linkify(text){ + if (text) { + text = text.replace( + /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi, + function(url){ + var full_url = url; + if (!full_url.match('^https?:\/\/')) { + full_url = 'http://' + full_url; + } + return '' + url + ''; + } + ); + } + return text; } // http://snippets.dzone.com/posts/show/6995 var URLRegex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i; +var PicRegex = /\.(jpg|jpeg|png|gif|bmp)$/i; function buildMessageDiv(msg) { - var match = URLRegex.exec(msg.content) - if (match) { - return '
' + msg.nick + ': ' - + '' - + '
'; - } else { - return '
' + msg.nick + ": " - + msg.content + "
"; - } + function buildContent(content) { + var match = URLRegex.exec(content) + if (match && PicRegex.test(match[0])) { + return '' + + ''; + } else { + return linkify(escapeHtml(msg.content)); + } + } + return '
' + escapeHtml(msg.nick) + ': ' + + buildContent(msg.content) + '
'; } function buildChatInterface(users, messages) { - var userList = '
' + var userList = '
' + $.map(users, buildUserDiv).join('') + '
'; var messageList = '
' + '
' @@ -133,21 +157,20 @@ function scrollToBottom(div) { function refresh() { var onSuccess = function(json) { - if (json.messages.length == 0) { - return; - } - - var shouldScroll = isScrolledToBottom($('#messageList')[0]); - - // Ignore our own messages - var filterFunc = function(m) { return m.nick != Nick }; - var msgStr = $.map($.grep(json.messages, filterFunc), - buildMessageDiv).join(''); - $('#messageList').append(msgStr); - - if (shouldScroll) { - scrollToBottom($('#messageList')[0]); + if (json.messages.length > 0) { + var shouldScroll = isScrolledToBottom($('#messageList')[0]); + + // Ignore our own messages + var filterFunc = function(m) { return m.nick != Nick }; + var msgStr = $.map($.grep(json.messages, filterFunc), + buildMessageDiv).join(''); + $('#messageList').append(msgStr); + + if (shouldScroll) { + scrollToBottom($('#messageList')[0]); + } } + $("#userList").html($.map(json.users, buildUserDiv).join('')); }; var onError = function(resp, textStatus, errorThrown) {}; -- cgit v1.2.3-70-g09d2