summaryrefslogtreecommitdiff
path: root/static/pichat.js
diff options
context:
space:
mode:
authorScott Ostler <sostler@deathmachine.local>2010-01-02 20:53:30 -0500
committerScott Ostler <sostler@deathmachine.local>2010-01-02 20:53:30 -0500
commit7fd7757c4db84ec6cf8578ec1f9a778977710bcc (patch)
treee924b98dc8852fb80d06195b47d4dca450a58319 /static/pichat.js
parentfe1b5678c330f0c3ec0e05a2295144338cadd5a6 (diff)
xmas work
Diffstat (limited to 'static/pichat.js')
-rwxr-xr-xstatic/pichat.js93
1 files changed, 54 insertions, 39 deletions
diff --git a/static/pichat.js b/static/pichat.js
index 87bca75..435dc0d 100755
--- a/static/pichat.js
+++ b/static/pichat.js
@@ -1,43 +1,32 @@
-function handleMsgError(resp) {
- var respText = resp.responseText ? resp.responseText.trim() : false;
- if (respText == 'UNKNOWN_USER') {
- alert("Can't send message! Please login.");
- } else if (respText) {
- alert("Cannot send message! (" + respText + ")");
- } else {
- alert("Cannot send message!");
- }
-}
-
function escapeHtml(txt) {
- if (!txt) { return "" }
+ if (!txt) { return ""; }
else { return $("<span>").text(txt).html(); }
}
-function buildUserDiv(user) {
- return '<div class="username"><a href="/u/' + escapeHtml(user) + '"><img src="/static/cat.jpeg" width="50" height="50">' + escapeHtml(user) + '</a></div>';
-}
-
// http://stackoverflow.com/questions/37684/replace-url-with-html-links-javascript
function linkify(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
return text.replace(exp,"<a href='$1'>$1</a>");
}
-
+
// http://snippets.dzone.com/posts/show/6995
-var URLRegex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i;
+var URLRegex = /^((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/i
var PicRegex = /\.(jpg|jpeg|png|gif|bmp)$/i;
+function isImage(content) {
+ var match = URLRegex.exec(content);
+ sansParams = match && match[0].replace(/\?.*$/i, "");
+ return sansParams && PicRegex.test(sansParams);
+}
+
function buildMsgContent(content) {
- content = $.trim(content);
- var match = URLRegex.exec(content)
- if (match && PicRegex.test(match[0])) {
- return '<a href="' + match[0] + '" target="_blank">'
- + '<img src="'+ match[0] + '" /></a>';
+ if (isImage(content)) {
+ return '<a href="' + content + '" target="_blank">'
+ + '<img src="'+ content + '" /></a>';
} else {
return linkify(escapeHtml(content));
}
-}
+}
function buildMessageDiv(msg) {
var nick = escapeHtml(msg.nick);
@@ -45,8 +34,26 @@ function buildMessageDiv(msg) {
+ buildMsgContent(msg.content) + '</div>';
}
+function buildUserDiv(user) {
+ return '<div class="username"><a href="/u/' + escapeHtml(user.nick) + '">' +
+ '<img src="' + user.avatar + '" width="50" height="50">' +
+ escapeHtml(user.nick) + '</a></div>';
+}
+
+function handleMsgError(resp) {
+ var respText = resp.responseText ? resp.responseText.trim() : false;
+ if (respText == 'UNKNOWN_USER') {
+ alert("Can't send message! Please login.");
+ } else if (respText) {
+ alert("Cannot send message! (" + respText + ")");
+ } else {
+ alert("Cannot send message!");
+ }
+}
+
function submitMessage() {
var content = $('#msgInput').val();
+ $('#msgInput').blur();
if (content == '') { return; }
$('#msgInput, #msgSubmit').attr('disabled', 'disabled');
@@ -54,24 +61,24 @@ function submitMessage() {
$('#msgInput, #msgSubmit').removeAttr('disabled');
$('#msgInput').val('');
};
-
+
var onError = function(resp, textStatus, errorThrown) {
$('#msgInput, #msgSubmit').removeAttr('disabled');
handleMsgError(resp);
};
-
- $.ajax({
- type: 'GET',
- timeout: 5000,
- url: 'msg',
- data: { 'content': content },
- cache: false,
- dataType: 'json',
- success: onSuccess,
- error: onError
- });
- updateUI([{ 'nick': Nick || "me", 'content': content}], null);
+ $.ajax({
+ type: 'GET',
+ timeout: 5000,
+ url: 'msg',
+ data: { 'content': content },
+ cache: false,
+ dataType: 'json',
+ success: onSuccess,
+ error: onError
+ });
+
+ updateUI([{ 'nick': Nick, 'content': content}], null);
}
function ifEnter(fn) {
@@ -139,7 +146,7 @@ function initChat() {
});
$('#msgInput').keyup(ifEnter(submitMessage));
- $('msgSubmit').click(submitMessage);
+ $('#msgSubmit').click(submitMessage);
delayedScrollToBottom(500);
setTimeout(refresh, 1000);
@@ -154,7 +161,7 @@ function initProfile() {
var onSubmit = function(original_element, edit, old) {
edit = $.trim(edit);
if (edit == old) { return old };
- // MAJOR TODO: Prevent entering script tags
+ // TODO: Prevent entering script tags
if (original_element == 'avatar' && edit.indexOf("<") != -1) {
return old;
}
@@ -182,3 +189,11 @@ function initProfile() {
$('#contact.editable, #bio.editable').editInPlace(opt);
};
+
+function initLog() {
+ $('.logged-dump .content').each(function() {
+ var t = $(this);
+ t.html(buildMsgContent(t.text()));
+ });
+
+}