@@ -20,6 +20,9 @@ Class.Define('Chat', {
2020 this . _messages = $elm ( "messages" ) ;
2121 this . _messageForm = $elm ( "message-form" ) ;
2222 this . _recepients = $elm ( "recepients" ) ;
23+ this . _audioElm = $elm ( "msg-sound" ) ;
24+ this . _typingUsersCont = $elm ( "typing-users-cont" ) ;
25+ this . _typingUsers = $elm ( "typing-users" ) ;
2326 } ,
2427 _initEvents : function ( ) {
2528 var scope = this ;
@@ -40,6 +43,16 @@ Class.Define('Chat', {
4043 return scope . _messageFormSubmitHandler ( e || window . event ) ;
4144 }
4245 } ;
46+ this . _messageForm . message . onkeyup = function ( e ) {
47+ e = e || window . event ;
48+ if ( ! ( e . keyCode == 13 && e . ctrlKey ) ) {
49+ var messageText = scope . _messageForm . message . value ;
50+ return scope . _messageFormTypingHandler (
51+ String ( messageText ) . trim ( ) . length > 0 ,
52+ e || window . event
53+ ) ;
54+ }
55+ } ;
4356 window . addEventListener ( "unload" , function ( e ) {
4457 if ( this . _socket )
4558 this . _socket . close ( ) ;
@@ -138,31 +151,45 @@ Class.Define('Chat', {
138151 data . content ,
139152 data . user
140153 ) ;
154+ scope . _audioElm . play ( ) ;
155+ } ) ;
156+ this . _socket . bind ( 'typing' , function ( data ) {
157+ scope . _typingUsersHandler ( data ) ;
141158 } ) ;
142159 } ,
143160 _messageFormSubmitHandler : function ( e ) {
144- var messageText = this . _messageForm . message . value ,
145- recepientRadio = null ,
146- recepient = '' ;
147- for ( var i = 0 , l = this . _messageForm . rcp . length ; i < l ; i += 1 ) {
148- recepientRadio = this . _messageForm . rcp [ i ] ;
149- if ( recepientRadio . checked ) {
150- recepient = recepientRadio . value ;
151- break ;
152- }
153- }
161+ var messageText = this . _messageForm . message . value ;
154162 if ( messageText != '' ) {
155163 this . _socket . send ( 'message' , {
156164 id : this . _id ,
157165 user : this . _user ,
158- recepient : recepient ,
166+ recepient : this . _getRecepient ( ) ,
159167 content : messageText
160168 } ) ;
161169 this . _messageForm . message . value = '' ;
162170 }
163171 e . preventDefault ( ) ;
164172 return false ;
165173 } ,
174+ _messageFormTypingHandler : function ( typing , e ) {
175+ this . _socket . send ( 'typing' , {
176+ id : this . _id ,
177+ user : this . _user ,
178+ recepient : this . _getRecepient ( ) ,
179+ typing : typing
180+ } ) ;
181+ } ,
182+ _getRecepient : function ( ) {
183+ var recepientRadio = null , recepient = '' ;
184+ for ( var i = 0 , l = this . _messageForm . rcp . length ; i < l ; i += 1 ) {
185+ recepientRadio = this . _messageForm . rcp [ i ] ;
186+ if ( recepientRadio . checked ) {
187+ recepient = recepientRadio . value ;
188+ break ;
189+ }
190+ }
191+ return recepient ;
192+ } ,
166193 _anyUserLogInHandler : function ( data ) {
167194 this . _updateOnlineUsersHandler ( data ) ;
168195 this . _addMessage (
@@ -197,6 +224,18 @@ Class.Define('Chat', {
197224 this . _onlineUsers . innerHTML = 'Currently online ('
198225 + data . onlineUsersCount + '): ' + html ;
199226 } ,
227+ _typingUsersHandler : function ( data ) {
228+ var typingUsers = [ ] ;
229+ for ( var userName in data )
230+ if ( userName !== this . _user && data [ userName ] )
231+ typingUsers . push ( userName ) ;
232+ if ( typingUsers . length === 0 ) {
233+ this . _typingUsersCont . style . display = 'none' ;
234+ } else {
235+ this . _typingUsers . innerHTML = typingUsers . join ( ', ' ) ;
236+ this . _typingUsersCont . style . display = 'block' ;
237+ }
238+ } ,
200239 _updateRecepients : function ( onlineUsers ) {
201240 var html = '' ;
202241 console . log ( onlineUsers ) ;
0 commit comments