Permalink
Browse files

Separated some chat functionality into separate class.

  • Loading branch information...
1 parent fd152a3 commit 81a06566360375d1bdcbe6c03e6a4688de4d17e9 @mcantelon committed Jul 23, 2012
Showing with 87 additions and 87 deletions.
  1. +0 −87 public/javascripts/chat.js
  2. +86 −0 public/javascripts/chat_ui.js
  3. +1 −0 views/index.jade
View
@@ -1,5 +1,3 @@
-var socket = io.connect();
-
var Chat = function(socket) {
this.socket = socket;
};
@@ -44,88 +42,3 @@ Chat.prototype.processCommand = function(currentRoom, command) {
return message;
};
-
-function divEscapedContentElement(message) {
- return $('<div></div>').text(message);
-}
-
-function divSystemContentElement(message) {
- return $('<div></div>').html(message);
-}
-
-function processUserInput(chatApp, socket) {
- var message = $('#send-message').val()
- , systemMessage;
-
- if (message[0] == '/') {
- systemMessage = chatApp.processCommand($('#room').text(), message);
- $('#messages').append(divSystemContentElement(systemMessage));
- } else {
- chatApp.sendMessage($('#room').text(), message);
- $('#messages').append(divEscapedContentElement(message));
- $('#messages').scrollTop($('#messages').prop('scrollHeight'));
- }
-
- $('#send-message').val('');
-}
-
-$(document).ready(function() {
- var name;
-
- $('#room').text('Lobby');
-
- var chatApp = new Chat(socket);
-
- socket.on('nameResult', function(result) {
- var message;
- if (result.success) {
- message = 'You are now known as ' + result.name + '.';
- name = result.name;
- } else {
- message = result.message;
- }
- message = '<i>' + message + '</i>';
- $('#messages').append(divSystemContentElement(message));
- });
-
- socket.on('joinResult', function(result) {
- $('#room').text(result.room);
- $('#messages').append(divSystemContentElement('<i>Room changed.</i>'));
- });
-
- socket.on('message', function (message) {
- var newElement = $('<div></div>').text(message.text);
- $('#messages').append(newElement);
- });
-
- socket.on('rooms', function(rooms) {
- $('#room-list').empty();
-
- for(var room in rooms) {
- room = room.substring(1, room.length);
- if (room != '') {
- $('#room-list').append(divEscapedContentElement(room));
- }
- }
-
- $('#room-list div').click(function() {
- chatApp.processCommand(
- $('#room').text(),
- '/join ' + $(this).text()
- )
- });
- });
-
- setInterval(function() {
- socket.emit('rooms');
- }, 1000);
-
- $('#send-form').submit(function() {
- processUserInput(chatApp, socket);
- return false;
- });
-
- $('#send-button').click(function() {
- processUserInput(chatApp, socket);
- });
-});
@@ -0,0 +1,86 @@
+var socket = io.connect();
+
+function divEscapedContentElement(message) {
+ return $('<div></div>').text(message);
+}
+
+function divSystemContentElement(message) {
+ return $('<div></div>').html(message);
+}
+
+function processUserInput(chatApp, socket) {
+ var message = $('#send-message').val()
+ , systemMessage;
+
+ if (message[0] == '/') {
+ systemMessage = chatApp.processCommand($('#room').text(), message);
+ $('#messages').append(divSystemContentElement(systemMessage));
+ } else {
+ chatApp.sendMessage($('#room').text(), message);
+ $('#messages').append(divEscapedContentElement(message));
+ $('#messages').scrollTop($('#messages').prop('scrollHeight'));
+ }
+
+ $('#send-message').val('');
+}
+
+$(document).ready(function() {
+ var name;
+
+ $('#room').text('Lobby');
+
+ var chatApp = new Chat(socket);
+
+ socket.on('nameResult', function(result) {
+ var message;
+ if (result.success) {
+ message = 'You are now known as ' + result.name + '.';
+ name = result.name;
+ } else {
+ message = result.message;
+ }
+ message = '<i>' + message + '</i>';
+ $('#messages').append(divSystemContentElement(message));
+ });
+
+ socket.on('joinResult', function(result) {
+ $('#room').text(result.room);
+ $('#messages').append(divSystemContentElement('<i>Room changed.</i>'));
+ });
+
+ socket.on('message', function (message) {
+ var newElement = $('<div></div>').text(message.text);
+ $('#messages').append(newElement);
+ });
+
+ socket.on('rooms', function(rooms) {
+ $('#room-list').empty();
+
+ for(var room in rooms) {
+ room = room.substring(1, room.length);
+ if (room != '') {
+ $('#room-list').append(divEscapedContentElement(room));
+ }
+ }
+
+ $('#room-list div').click(function() {
+ chatApp.processCommand(
+ $('#room').text(),
+ '/join ' + $(this).text()
+ )
+ });
+ });
+
+ setInterval(function() {
+ socket.emit('rooms');
+ }, 1000);
+
+ $('#send-form').submit(function() {
+ processUserInput(chatApp, socket);
+ return false;
+ });
+
+ $('#send-button').click(function() {
+ processUserInput(chatApp, socket);
+ });
+});
View
@@ -19,3 +19,4 @@ block content
script(src='/socket.io/socket.io.js')
script(src='/javascripts/jquery-1.7.2.min.js')
script(src='/javascripts/chat.js')
+ script(src='/javascripts/chat_ui.js')

0 comments on commit 81a0656

Please sign in to comment.