diff options
Diffstat (limited to 'static/pichat.js')
| -rwxr-xr-x | static/pichat.js | 122 |
1 files changed, 48 insertions, 74 deletions
diff --git a/static/pichat.js b/static/pichat.js index 4c950af..a7c40ca 100755 --- a/static/pichat.js +++ b/static/pichat.js @@ -2,17 +2,6 @@ var Nick = null; -function handleJoinError(resp) { - var respText = resp.responseText ? resp.responseText.trim() : false; - if (respText == 'NICK_TAKEN') { - alert("Nick '" + Nick + "' was taken! Please choose another."); - } else if (respText) { - alert("Cannot join! (" + respText + ")"); - } else { - alert("Cannot join! Please try again later."); - } -} - function handleMsgError(resp) { var respText = resp.responseText ? resp.responseText.trim() : false; if (respText == 'UNKNOWN_USER') { @@ -24,56 +13,22 @@ function handleMsgError(resp) { } } -function join() { - $('#join, #nick').attr('disabled', true); - $('#loginspinner').show(); - Nick = $('#nick').val(); - - onSuccess = function(json) { - generateChatInterface(json.users, json.messages); - }; - - onError = function(resp, textStatus, errorThrown) { - $('#join, #nick').attr('disabled', false); - $('#loginspinner').hide(); - handleJoinError(resp); - }; - - $.ajax({ - type: 'GET', - timeout: 5000, - url: 'join', - data: {'nick': Nick }, - cache: false, - dataType: 'json', - success: onSuccess, - error: onError - }); -} - function escapeHtml(txt) { - return $("<span>").text(txt).html(); + if (!txt) { + return "" + } else { + return $("<span>").text(txt).html(); + } } function buildUserDiv(user) { return '<div>' + escapeHtml(user) + '</div>'; } -// 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 '<a href="' + full_url + '" target="_blank">' + url + '</a>'; - } - ); - } - return text; +// 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 @@ -94,21 +49,6 @@ function buildMessageDiv(msg) { + buildContent(msg.content) + '</div>'; } -function buildChatInterface(users, messages) { - var userList = '<div id="userList">' - + $.map(users, buildUserDiv).join('') + '</div>'; - var messageList = '<div id="messagePane">' - + '<div id="messageList">' - + $.map(messages, buildMessageDiv).join('') - + '</div>' - + '<div id="msgInputDiv">' - + '<input type="input" id="msgInput">' - + '<input type="submit" value="Enter" id="msgSubmit">' - + '</div>' - + '</div>'; - return '<h1>Pichat</h1><div id="chatbox">' + userList + messageList + '</div>'; -} - function submitMessage() { var content = $('#msgInput').val(); var msg = { 'nick': Nick, 'content': content, 'timestamp': new Date() }; @@ -125,6 +65,7 @@ function submitMessage() { var onSuccess = function() {}; var onError = function(resp, textStatus, errorThrown) { + handleMsgError(resp); }; @@ -155,7 +96,7 @@ function scrollToBottom(div) { } function refresh() { - var onSuccess = function(json) { + var onSuccess = function(json) { if (json.messages.length > 0) { var shouldScroll = isScrolledToBottom($('#messageList')[0]); @@ -185,10 +126,43 @@ function refresh() { }); } -function generateChatInterface(users, messages) { - $('#content').html(buildChatInterface(users, messages)); + + +function init() { + var onSuccess = function(json) { + $('#loadingbox').hide(); + Nick = json.nick; + + $('#nickspan').text(Nick); + $('#welcomebar').show(); + + var msgStr = $.map(json.messages, buildMessageDiv).join(''); + $('#messageList').append(msgStr); + $("#userList").html($.map(json.users, buildUserDiv).join('')); + $('#nickInput, #nickSubmit, #msgInput, #msgSubmit').removeAttr('disabled'); + + // Delay scrolling by .5 seconds so images can start loading. + setTimeout(scrollToBottom, 500, $('#messageList')[0]); + setInterval(refresh, 1000); + }; + + var onError = function(resp, textStatus, errorThrown) { + alert("Error connecting to chat server!"); + }; + + $.ajax({ + type: 'GET', + timeout: 5000, + url: 'init', + cache: false, + dataType: 'json', + success: onSuccess, + error: onError + }); + $('#msgInput').keyup(ifEnter(submitMessage)); $('#msgSubmit').click(submitMessage); - setInterval(refresh, 1000); -} + $('#nickInput').keyup(ifEnter(join)); + $('#nickSubmit').click(join); +} |
