chat.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. (function(){
  2. // The only room. Must match the server's $MAIN_ROOM and the id of the pane
  3. // in index.html -- messages are routed into '#<room> #room_messages', so
  4. // all three have to agree.
  5. var ROOM = 'Lobby';
  6. // ***************************************************************************
  7. // Socket.io events
  8. // ***************************************************************************
  9. var socket = window.RADIO.socket;
  10. // Connection established
  11. socket.on('connected', function (data) {
  12. console.log(data);
  13. // Get users connected to mainroom
  14. socket.emit('getUsersInRoom', {'room':ROOM});
  15. });
  16. // Disconnected from server
  17. socket.on('disconnect', function (data) {
  18. var info = {'room':ROOM, 'username':'Daveo-bot', 'msg':'---- Lost connection ----'};
  19. addBotMessage(info);
  20. });
  21. // Reconnected to server
  22. socket.on('reconnect', function (data) {
  23. var info = {'room':ROOM, 'username':'Daveo-bot', 'msg':'---- Reconnected ----'};
  24. addBotMessage(info);
  25. });
  26. // subscriptionConfirmed / unsubscriptionConfirmed are gone with rooms. The
  27. // single pane is static markup, so there is no room UI left to build or
  28. // tear down at runtime.
  29. // User joins room
  30. socket.on('userJoinsRoom', function(data) {
  31. console.log("userJoinsRoom: %s", JSON.stringify(data));
  32. // Log join in conversation
  33. addMessage(data);
  34. // Add user to connected users list
  35. addUser(data);
  36. });
  37. // User leaves room
  38. socket.on('userLeavesRoom', function(data) {
  39. console.log("userLeavesRoom: %s", JSON.stringify(data));
  40. // Log leave in conversation
  41. addMessage(data);
  42. // Remove user from connected users list
  43. removeUser(data);
  44. });
  45. // Message received
  46. socket.on('newMessage', function (data) {
  47. console.log("newMessage: %s", JSON.stringify(data));
  48. addMessage(data);
  49. // Scroll down room messages
  50. var room_messages = '#'+data.room+' #room_messages';
  51. $(room_messages).animate({
  52. scrollTop: $(room_messages).height()
  53. }, 300);
  54. });
  55. // Users in room received
  56. socket.on('usersInRoom', function(data) {
  57. console.log('usersInRoom: %s', JSON.stringify(data));
  58. _.each(data.users, function(user) {
  59. addUser(user);
  60. });
  61. });
  62. // User nickname updated
  63. socket.on('userNicknameUpdated', function(data) {
  64. console.log("userNicknameUpdated: %s", JSON.stringify(data));
  65. updateNickname(data);
  66. msg = '----- ' + data.oldUsername + ' is now ' + data.newUsername + ' -----';
  67. var info = {'room':data.room, 'username':'Daveo-bot', 'msg':msg};
  68. addBotMessage(info);
  69. });
  70. // ***************************************************************************
  71. // Templates and helpers
  72. // ***************************************************************************
  73. var templates = {};
  74. var getTemplate = function(path, callback) {
  75. var source;
  76. var template;
  77. // Check first if we've the template cached
  78. if (_.has(templates, path)) {
  79. if (callback) callback(templates[path]);
  80. // If not we get and compile it
  81. } else {
  82. $.ajax({
  83. url: window.RADIO.url(path),
  84. success: function(data) {
  85. source = data;
  86. template = Handlebars.compile(source);
  87. // Store compiled template in cache
  88. templates[path] = template;
  89. if (callback) callback(template);
  90. }
  91. });
  92. }
  93. }
  94. // Add message to room
  95. var addMessage = function(msg) {
  96. getTemplate('js/templates/message.handlebars', function(template) {
  97. var room_messages = '#'+msg.room+' #room_messages';
  98. $(room_messages).append(template(msg));
  99. });
  100. };
  101. // Robot Add message to room
  102. var addBotMessage = function(msg) {
  103. getTemplate('js/templates/message-bot.handlebars', function(template) {
  104. var room_messages = '#'+msg.room+' #room_messages';
  105. $(room_messages).append(template(msg));
  106. });
  107. };
  108. // Add user to connected users list
  109. var addUser = function(user) {
  110. getTemplate('js/templates/user.handlebars', function(template) {
  111. var room_users = '#'+user.room+' #room_users';
  112. // Add only if it doesn't exist in the room
  113. var user_badge = '#'+user.room+' #'+user.id;
  114. if (!($(user_badge).length)) {
  115. $(room_users).append(template(user));
  116. }
  117. });
  118. }
  119. // Remove user from connected users list
  120. var removeUser = function(user) {
  121. var user_badge = '#'+user.room+' #'+user.id;
  122. $(user_badge).remove();
  123. };
  124. // Get current room.
  125. //
  126. // This used to read the active tab's text. With the tab bar gone that
  127. // returned '', which the server rejects as an invalid room name -- posting
  128. // would have failed silently. There's one room, so name it directly.
  129. var getCurrentRoom = function() {
  130. return ROOM;
  131. };
  132. // Get message text from input field
  133. var getMessageText = function() {
  134. var text = $('#message_text').val();
  135. $('#message_text').val("");
  136. return text;
  137. };
  138. // Get nickname from input field
  139. var getNickname = function() {
  140. var nickname = $('#nickname').val();
  141. $('#nickname').val("");
  142. return nickname;
  143. };
  144. // Update nickname in badges
  145. var updateNickname = function(data) {
  146. var badges = '#'+data.room+' #'+data.id;
  147. $(badges).text(data.newUsername);
  148. };
  149. // ***************************************************************************
  150. // Events
  151. // ***************************************************************************
  152. // Send new message
  153. $('#b_send_message').click(function(eventObject) {
  154. eventObject.preventDefault();
  155. if ($('#message_text').val() != "") {
  156. socket.emit('newMessage', {'room':getCurrentRoom(), 'msg':getMessageText()});
  157. }
  158. });
  159. // Joining and leaving rooms is gone -- there is one room now, and the
  160. // server no longer accepts subscribe/unsubscribe, so it isn't just the UI
  161. // that's hidden.
  162. })();