/static/js/jchat.js

http://django-jchat.googlecode.com/ · JavaScript · 183 lines · 116 code · 21 blank · 46 comment · 14 complexity · 20ad5faf3081ec93b885c79fc643ecd0 MD5 · raw file

  1. var chat_room_id = undefined;
  2. var last_received = 0;
  3. /**
  4. * Initialize chat:
  5. * - Set the room id
  6. * - Generate the html elements (chat box, forms & inputs, etc)
  7. * - Sync with server
  8. * @param chat_room_id the id of the chatroom
  9. * @param html_el_id the id of the html element where the chat html should be placed
  10. * @return
  11. */
  12. function init_chat(chat_id, html_el_id) {
  13. chat_room_id = chat_id;
  14. layout_and_bind(html_el_id);
  15. sync_messages();
  16. }
  17. var img_dir = "/static/img/";
  18. /**
  19. * Asks the server which was the last message sent to the room, and stores it's id.
  20. * This is used so that when joining the user does not request the full list of
  21. * messages, just the ones sent after he logged in.
  22. * @return
  23. */
  24. function sync_messages() {
  25. $.ajax({
  26. type: 'POST',
  27. data: {id:window.chat_room_id},
  28. url:'/chat/sync/',
  29. dataType: 'json',
  30. success: function (json) {
  31. last_received = json.last_message_id;
  32. }
  33. });
  34. setTimeout("get_messages()", 2000);
  35. }
  36. /**
  37. * Generate the Chat box's HTML and bind the ajax events
  38. * @param target_div_id the id of the html element where the chat will be placed
  39. */
  40. function layout_and_bind(html_el_id) {
  41. // layout stuff
  42. var html = '<div id="chat-messages-container">'+
  43. '<div id="chat-messages"> </div>'+
  44. '<div id="chat-last"> </div>'+
  45. '</div>'+
  46. '<form id="chat-form">'+
  47. '<input name="message" type="text" class="message" />'+
  48. '<input type="submit" value="Say!!!"/>'+
  49. '</form>';
  50. $("#"+html_el_id).append(html);
  51. // event stuff
  52. $("#chat-form").submit( function () {
  53. var $inputs = $(this).children('input');
  54. var values = {};
  55. $inputs.each(function(i,el) {
  56. values[el.name] = $(el).val();
  57. });
  58. values['chat_room_id'] = window.chat_room_id;
  59. $.ajax({
  60. data: values,
  61. dataType: 'json',
  62. type: 'post',
  63. url: '/chat/send/'
  64. });
  65. $('#chat-form .message').val('');
  66. return false;
  67. });
  68. };
  69. /**
  70. * Gets the list of messages from the server and appends the messages to the chatbox
  71. */
  72. function get_messages() {
  73. $.ajax({
  74. type: 'POST',
  75. data: {id:window.chat_room_id, offset: window.last_received},
  76. url:'/chat/receive/',
  77. dataType: 'json',
  78. success: function (json) {
  79. var scroll = false;
  80. // first check if we are at the bottom of the div, if we are, we shall scroll once the content is added
  81. var $containter = $("#chat-messages-container");
  82. if ($containter.scrollTop() == $containter.attr("scrollHeight") - $containter.height())
  83. scroll = true;
  84. // add messages
  85. $.each(json, function(i,m){
  86. // filter message for bad bad stuff (XSS injections for instance)
  87. m.message = clean_message(m.message);
  88. // depending on the message type, present it in a different manner
  89. if (m.type == 's')
  90. $('#chat-messages').append('<div class="system">' + replace_emoticons(m.message) + '</div>');
  91. else if (m.type == 'm')
  92. $('#chat-messages').append('<div class="message"><div class="author">'+m.author+'</div>'+replace_emoticons(m.message) + '</div>');
  93. else if (m.type == 'j')
  94. $('#chat-messages').append('<div class="join">'+m.author+' has joined</div>');
  95. else if (m.type == 'l')
  96. $('#chat-messages').append('<div class="leave">'+m.author+' has left</div>');
  97. last_received = m.id;
  98. })
  99. // scroll to bottom
  100. if (scroll)
  101. $("#chat-messages-container").animate({ scrollTop: $("#chat-messages-container").attr("scrollHeight") }, 500);
  102. }
  103. });
  104. // wait for next
  105. setTimeout("get_messages()", 2000);
  106. }
  107. /**
  108. * Tells the chat app that we are joining
  109. */
  110. function chat_join() {
  111. $.ajax({
  112. async: false,
  113. type: 'POST',
  114. data: {chat_room_id:window.chat_room_id},
  115. url:'/chat/join/',
  116. });
  117. }
  118. /**
  119. * Tells the chat app that we are leaving
  120. */
  121. function chat_leave() {
  122. $.ajax({
  123. async: false,
  124. type: 'POST',
  125. data: {chat_room_id:window.chat_room_id},
  126. url:'/chat/leave/',
  127. });
  128. }
  129. // attach join and leave events
  130. $(window).load(function(){chat_join()});
  131. $(window).unload(function(){chat_leave()});
  132. // emoticons
  133. var emoticons = {
  134. '>:D' : 'emoticon_evilgrin.png',
  135. ':D' : 'emoticon_grin.png',
  136. '=D' : 'emoticon_happy.png',
  137. ':\\)' : 'emoticon_smile.png',
  138. ':O' : 'emoticon_surprised.png',
  139. ':P' : 'emoticon_tongue.png',
  140. ':\\(' : 'emoticon_unhappy.png',
  141. ':3' : 'emoticon_waii.png',
  142. ';\\)' : 'emoticon_wink.png',
  143. '\\(ball\\)' : 'sport_soccer.png'
  144. }
  145. /**
  146. * Regular expression maddness!!!
  147. * Replace the above strings for their img counterpart
  148. */
  149. function replace_emoticons(text) {
  150. $.each(emoticons, function(char, img) {
  151. re = new RegExp(char,'g');
  152. // replace the following at will
  153. text = text.replace(re, '<img src="'+img_dir+img+'" />');
  154. });
  155. return text;
  156. }
  157. /**
  158. * Clean unwanted characters from message string to prevent code injection
  159. */
  160. function clean_message(text) {
  161. return text.replace(/</g,"&lt;").replace(/>/g,"&gt;")
  162. }