User:Brenr/chat.io/chat.js

jQuery(function($){	var socket = io.connect,		$username = $('#username'),		$userlist = $('#userlist'),		$userlistTitle = $('#userlist .title'),		$messages = $('#messages'),		$messageInput = $('#message-input'),		$messageInputCount = $('#message-input-count'),		$jqToggle = true; /************************************************************************************************************************ * General execution																									* ************************************************************************************************************************/ 	$('#message-input').keyup(function {		var inputLength = $messageInput.val.length; showInput = inputLength != 0; $messageInputCount.text(showInput ? inputLength + ' characters' : ''); });

$('#jqToggle').click(function {		$jqToggle = $jqToggle ? false : true;		jqText = $jqToggle ? 'ON' : 'OFF';		$('#jqToggle').text('Join/quit alerts: ' + jqText);	}); $('#clearChat').click(function {		$('.message').remove;	}); /************************************************************************************************************************ * Send: socket.emit																									* ************************************************************************************************************************/	/*	 * Send server the local client is online */	socket.emit('user enter', $username.val); /* Send message out to other clients */ function sendMessage {		if($messageInput != 'undefined') {			socket.emit('send message', $messageInput.val); $messageInput.val(''); }	}	$messageInput.keypress(function(e){ //When the user hits enter		if(e.which == 13)		{			e.preventDefault;			sendMessage;		}	});

/************************************************************************************************************************ * Receive: socket.on																									* ************************************************************************************************************************/

/* Receive an alert when a user enters or leaves the chat */ socket.on('alert user', function(data)	{		status = data.status ? 'joined' : 'left';		if($jqToggle)		{			$messages.append('' + data.user + ' has ' + status + ' the chat. ');		}	});	socket.on('user enter', function	{		$('#chat-pane').show;	}); /* Receive new userlist */ socket.on('update userlist', function(data)	{		var userlistHtml = '';		for(i = 0; i < data.length; i++)		{			userlistHtml += ' ' + data[i] + ' ';		}		$userlist.html(userlistHtml);		$userlistTitle.text('USERS (' + data.length + ')');	}); /* Receive message from other client */ socket.on('new message', function(data)	{		$messages.append(' ' + data.user + ' : ' + data.message + ' ');	}); /* Receive kick alert when a user is kicked */ socket.on('user kicked', function(data) {		$messages.append(' ' + data.user + ' kicked ' + data.kicked + ' from the chat. ');	});	/* Receive ban alert when a user is kicked */ socket.on('user banned', function(data) {		$messages.append(' ' + data.user + ' banned ' + data.banned + ' from the chat. ');	});

/* If user is banned this is what they'll see on their side */ socket.on('you are banned', function(data) {		$('body').html(' You are banned from the chat! ');	}); // EOF });