diff options
Diffstat (limited to 'static')
| -rw-r--r-- | static/js/pichat.js | 158 |
1 files changed, 90 insertions, 68 deletions
diff --git a/static/js/pichat.js b/static/js/pichat.js index 6a6d962..0a849eb 100644 --- a/static/js/pichat.js +++ b/static/js/pichat.js @@ -119,6 +119,8 @@ Log.initialize(); URLRegex = /((\b(http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi; PicRegex = /\.(jpg|jpeg|png|gif|bmp|svg|fid)$/i; +RecipRegex = /(^|\s)@\w+/g; + function getImagesAsArray(text) { var imgs = [] @@ -133,10 +135,24 @@ function getImagesAsArray(text) { return imgs } -function linkify(text) { - LastMsgContainsImage = false - text = text.replace(URLRegex, linkReplace); - return text +function linkify(text, recips) { + LastMsgContainsImage = false; + var recipWrapper = function(text) { return recipientReplace(text, recips); }; + return text.replace(URLRegex, linkReplace).replace(RecipRegex, recipWrapper); +} + +function recipientReplace(atText, recips) { + if (atText[0] == ' ') { + atText = atText.slice(1); + var space = ' '; + } else { + var space = ''; + } + var nick = atText.slice(1); + if (!recips || recips.indexOf(nick.toLowerCase()) == -1) { + return space + atText; + } else + return space + '<a target="_blank" href="/' + nick + '">' + atText + '</a>'; } // use this in escapeHtml to turn everyone's text lIkE tHiS @@ -312,22 +328,23 @@ function setImgsEnable() { } }; -function buildMsgContent(content) { +function buildMsgContent(content, recips) { if (content.substr(0,6) == "<safe>") return content.substr(6,content.length - 13) - else return linkify(escapeHtml(content)); + else return linkify(escapeHtml(content), recips); } // todo: // isLoading doesn't get passed the right thing by $.map in addMessages -function buildMessageDiv(msg, isLoading) { +function buildMessageDiv(msg, opts) { + var opts = opts || {}; var nick = escapeHtml(msg.nick); removeOldMessages(); - var builtContent = buildMsgContent(msg.content); + var builtContent = buildMsgContent(msg.content, opts.recips); var msgId = ('msg_id' in msg) ? 'id="message-' + msg.msg_id + '"' : ''; - var loadingClass = isLoading ? ' loading' : ''; + var loadingClass = opts.isLoading ? ' loading' : ''; var containsImageClass = LastMsgContainsImage ? ' contains-image' : ''; var displayStyle = ((ImgsEnabled && LastMsgContainsImage) || (TextEnabled && !LastMsgContainsImage)) ? '' : ' style="display: none"'; @@ -436,55 +453,56 @@ function clearMessages(){ } function submitMessage() { - var content = $.trim($('#msgInput').val()); - - if (content == "/clear") { - clearMessages() + var content = $.trim($('#msgInput').val()); + + if (content == "/clear") { + clearMessages() + $('#msgInput').val(''); + return; + } + + var invalidDomain = invalidImageDomain(content); + if (invalidDomain) { + $('#msgInput').blur(); // Remove focus to prevent FF alert loop + alert("Sorry, cannot accept images from " + invalidDomain + ". Maybe host the image elsewhere?"); + return; + } + $('#msgInput').val(''); - return; - } - - var invalidDomain = invalidImageDomain(content); - if (invalidDomain) { - $('#msgInput').blur(); // Remove focus to prevent FF alert loop - alert("Sorry, cannot accept images from " + invalidDomain + ". Maybe host the image elsewhere?"); - return; - } - - $('#msgInput').val(''); - if (content == '') { return; } - if (content.length > 2468) { - alert("POST TOO LONG DUDE!"); - return; - } // this shouldn't just be client side :V - PendingMessages[content] = true; - - var msg = { 'nick': Nick, 'content': content }; - var div = addNewMessage(msg, true); - - var onSuccess = function(json) { - if (typeof pageTracker !== 'undefined') { - pageTracker._trackEvent('Message', 'Submit', - typeof Room !== 'undefined' ? Room : 'UnknownRoom'); + if (content == '') { return; } + if (content.length > 2468) { + alert("POST TOO LONG DUDE!"); + return; } - div.attr('id', 'message-' + json) - .removeClass('loading').addClass('loaded'); - }; - var onError = function(resp, textStatus, errorThrown) { - div.remove(); - handleMsgError(resp); - }; - - $.ajax({ - type: 'POST', - timeout: 15000, - url: '/msg', - data: { 'room': Room, 'content': content }, - cache: false, - dataType: 'json', - success: onSuccess, - error: onError - }); + PendingMessages[content] = true; + + var msg = { 'nick': Nick, 'content': content }; + 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.msgid) + .removeClass('loading').addClass('loaded'); + div.find('.content').html(buildMsgContent(content, json.recips)); + }; + var onError = function(resp, textStatus, errorThrown) { + div.remove(); + handleMsgError(resp); + }; + + $.ajax({ + type: 'POST', + timeout: 15000, + url: '/msg', + data: { 'room': Room, 'content': content }, + cache: false, + dataType: 'json', + success: onSuccess, + error: onError + }); } function ifEnter(fn) { @@ -493,13 +511,14 @@ function ifEnter(fn) { }; } -function addNewMessages(msgs) { - var msgStr = $.map(msgs, buildMessageDiv).join(''); +function addNewMessages(msgs, recips) { + var msgOpts = { recips: recips }; + var msgStr = $.map(msgs, function(msg) { buildMessageDiv(msg, msgOpts).join(''); }); $('#messageList').append(msgStr); } function addNewMessage(msg, isLoading) { - var msgStr = buildMessageDiv(msg, isLoading); + var msgStr = buildMessageDiv(msg, { isLoading: true }); var div = $(msgStr).appendTo('#messageList'); return div; } @@ -516,11 +535,11 @@ function flattenUserJson(users) { return s; } -function updateUI(msgs, users, favs) { +function updateUI(msgs, users, favs, recips) { if (window['growlize'] && msgs && msgs.length > 0) { $.map(msgs, buildGrowlDataAndPopDatShit) } else if (msgs && msgs.length > 0) { - addNewMessages(msgs); + addNewMessages(msgs, recips); } if (users !== null) { var flattened = flattenUserJson(users); @@ -552,14 +571,13 @@ function isDuplicateMessage(m) { function refresh() { var onSuccess = function(json) { try { - Timestamp = json.timestamp; - + Timestamp = json.timestamp; $.map(json.messages, function(msg){ MessageContentCache[msg.msg_id.toString()] = msg.content }) var messages = $.grep( json.messages, function(m) { return !isDuplicateMessage(m) }); - updateUI(messages, json.users, json.favs); + updateUI(messages, json.users, json.favs, json.recips); if (!Away.HasFocus) Away.UnseenMsgCounter += messages.length; } catch(e) { @@ -619,7 +637,7 @@ function initChat() { var dump = $(this); var content = dump.find(".content") MessageContentCache[dump.attr("id").substr(8)] = content.text() - content.html(buildMsgContent(content.text())); + content.html(buildMsgContent(content.text(), Recips)); if ((ImgsEnabled && dump.hasClass('contains-image')) || (TextEnabled && !dump.hasClass('contains-image'))) dump.show(); @@ -698,11 +716,15 @@ function enableProfileEdit() { } function initProfile() { - Search.initInpage() - $(".linkify").each(function() { + Search.initInpage(); + $(".linkify-text").each(function() { var text = jQuery(this).text(); jQuery(this).html(linkifyWithoutImage(text)); - }); + }); + + $(".linkify-full").each(function() { + $(this).html(buildMsgContent($(this).text(), Recips)); + }); $('#edit-toggle').click(enableProfileEdit); activateProfileEditable(); |
