11$ ( function ( ) {
2- var FADE_TIME = 150 ; // ms
3- var TYPING_TIMER_LENGTH = 400 ; // ms
4- var COLORS = [
2+ const FADE_TIME = 150 ; // ms
3+ const TYPING_TIMER_LENGTH = 400 ; // ms
4+ const COLORS = [
55 '#e21400' , '#91580f' , '#f8a700' , '#f78b00' ,
66 '#58dc00' , '#287b00' , '#a8f07a' , '#4ae8c4' ,
77 '#3b88eb' , '#3824aa' , '#a700ff' , '#d300e7'
88 ] ;
99
1010 // Initialize variables
11- var $window = $ ( window ) ;
12- var $usernameInput = $ ( '.usernameInput' ) ; // Input for username
13- var $messages = $ ( '.messages' ) ; // Messages area
14- var $inputMessage = $ ( '.inputMessage' ) ; // Input message input box
11+ const $window = $ ( window ) ;
12+ const $usernameInput = $ ( '.usernameInput' ) ; // Input for username
13+ const $messages = $ ( '.messages' ) ; // Messages area
14+ const $inputMessage = $ ( '.inputMessage' ) ; // Input message input box
1515
16- var $loginPage = $ ( '.login.page' ) ; // The login page
17- var $chatPage = $ ( '.chat.page' ) ; // The chatroom page
16+ const $loginPage = $ ( '.login.page' ) ; // The login page
17+ const $chatPage = $ ( '.chat.page' ) ; // The chatroom page
1818
19- // Prompt for setting a username
20- var username ;
21- var connected = false ;
22- var typing = false ;
23- var lastTypingTime ;
24- var $currentInput = $usernameInput . focus ( ) ;
19+ const socket = io ( ) ;
2520
26- var socket = io ( ) ;
21+ // Prompt for setting a username
22+ let username ;
23+ let connected = false ;
24+ let typing = false ;
25+ let lastTypingTime ;
26+ let $currentInput = $usernameInput . focus ( ) ;
2727
2828 const addParticipantsMessage = ( data ) => {
29- var message = '' ;
29+ let message = '' ;
3030 if ( data . numUsers === 1 ) {
31- message += " there's 1 participant" ;
31+ message += ` there's 1 participant` ;
3232 } else {
33- message += " there are " + data . numUsers + " participants" ;
33+ message += ` there are ${ data . numUsers } participants` ;
3434 }
3535 log ( message ) ;
3636 }
@@ -53,45 +53,41 @@ $(function() {
5353
5454 // Sends a chat message
5555 const sendMessage = ( ) => {
56- var message = $inputMessage . val ( ) ;
56+ let message = $inputMessage . val ( ) ;
5757 // Prevent markup from being injected into the message
5858 message = cleanInput ( message ) ;
5959 // if there is a non-empty message and a socket connection
6060 if ( message && connected ) {
6161 $inputMessage . val ( '' ) ;
62- addChatMessage ( {
63- username : username ,
64- message : message
65- } ) ;
62+ addChatMessage ( { username, message } ) ;
6663 // tell server to execute 'new message' and send along one parameter
6764 socket . emit ( 'new message' , message ) ;
6865 }
6966 }
7067
7168 // Log a message
72- const log = ( message , options ) => {
73- var $el = $ ( '<li>' ) . addClass ( 'log' ) . text ( message ) ;
69+ const log = ( message , options ) => {
70+ const $el = $ ( '<li>' ) . addClass ( 'log' ) . text ( message ) ;
7471 addMessageElement ( $el , options ) ;
7572 }
7673
7774 // Adds the visual chat message to the message list
7875 const addChatMessage = ( data , options ) => {
7976 // Don't fade the message in if there is an 'X was typing'
80- var $typingMessages = getTypingMessages ( data ) ;
81- options = options || { } ;
77+ const $typingMessages = getTypingMessages ( data ) ;
8278 if ( $typingMessages . length !== 0 ) {
8379 options . fade = false ;
8480 $typingMessages . remove ( ) ;
8581 }
8682
87- var $usernameDiv = $ ( '<span class="username"/>' )
83+ const $usernameDiv = $ ( '<span class="username"/>' )
8884 . text ( data . username )
8985 . css ( 'color' , getUsernameColor ( data . username ) ) ;
90- var $messageBodyDiv = $ ( '<span class="messageBody">' )
86+ const $messageBodyDiv = $ ( '<span class="messageBody">' )
9187 . text ( data . message ) ;
9288
93- var typingClass = data . typing ? 'typing' : '' ;
94- var $messageDiv = $ ( '<li class="message"/>' )
89+ const typingClass = data . typing ? 'typing' : '' ;
90+ const $messageDiv = $ ( '<li class="message"/>' )
9591 . data ( 'username' , data . username )
9692 . addClass ( typingClass )
9793 . append ( $usernameDiv , $messageBodyDiv ) ;
@@ -119,8 +115,7 @@ $(function() {
119115 // options.prepend - If the element should prepend
120116 // all other messages (default = false)
121117 const addMessageElement = ( el , options ) => {
122- var $el = $ ( el ) ;
123-
118+ const $el = $ ( el ) ;
124119 // Setup default options
125120 if ( ! options ) {
126121 options = { } ;
@@ -141,6 +136,7 @@ $(function() {
141136 } else {
142137 $messages . append ( $el ) ;
143138 }
139+
144140 $messages [ 0 ] . scrollTop = $messages [ 0 ] . scrollHeight ;
145141 }
146142
@@ -159,8 +155,8 @@ $(function() {
159155 lastTypingTime = ( new Date ( ) ) . getTime ( ) ;
160156
161157 setTimeout ( ( ) => {
162- var typingTimer = ( new Date ( ) ) . getTime ( ) ;
163- var timeDiff = typingTimer - lastTypingTime ;
158+ const typingTimer = ( new Date ( ) ) . getTime ( ) ;
159+ const timeDiff = typingTimer - lastTypingTime ;
164160 if ( timeDiff >= TYPING_TIMER_LENGTH && typing ) {
165161 socket . emit ( 'stop typing' ) ;
166162 typing = false ;
@@ -179,12 +175,12 @@ $(function() {
179175 // Gets the color of a username through our hash function
180176 const getUsernameColor = ( username ) => {
181177 // Compute hash code
182- var hash = 7 ;
183- for ( var i = 0 ; i < username . length ; i ++ ) {
184- hash = username . charCodeAt ( i ) + ( hash << 5 ) - hash ;
178+ let hash = 7 ;
179+ for ( let i = 0 ; i < username . length ; i ++ ) {
180+ hash = username . charCodeAt ( i ) + ( hash << 5 ) - hash ;
185181 }
186182 // Calculate color
187- var index = Math . abs ( hash % COLORS . length ) ;
183+ const index = Math . abs ( hash % COLORS . length ) ;
188184 return COLORS [ index ] ;
189185 }
190186
@@ -229,7 +225,7 @@ $(function() {
229225 socket . on ( 'login' , ( data ) => {
230226 connected = true ;
231227 // Display the welcome message
232- var message = " Welcome to Socket.IO Chat – " ;
228+ const message = ' Welcome to Socket.IO Chat – ' ;
233229 log ( message , {
234230 prepend : true
235231 } ) ;
@@ -243,13 +239,13 @@ $(function() {
243239
244240 // Whenever the server emits 'user joined', log it in the chat body
245241 socket . on ( 'user joined' , ( data ) => {
246- log ( data . username + ' joined' ) ;
242+ log ( ` ${ data . username } joined` ) ;
247243 addParticipantsMessage ( data ) ;
248244 } ) ;
249245
250246 // Whenever the server emits 'user left', log it in the chat body
251247 socket . on ( 'user left' , ( data ) => {
252- log ( data . username + ' left' ) ;
248+ log ( ` ${ data . username } left` ) ;
253249 addParticipantsMessage ( data ) ;
254250 removeChatTyping ( data ) ;
255251 } ) ;
0 commit comments