1- 'use strict' ;
1+ const io = require ( 'socket.io-client' ) ;
2+ const path = require ( 'path' ) ;
23
3- const configstore = require ( './../configStore.js' ) ;
4- const View = require ( './view.js' ) ;
5- const io = require ( 'socket.io-client' ) ;
4+ const configstore = require ( '../configStore.js' ) ;
5+ const View = require ( './view.js' ) ;
66
7+ /**
8+ * The SocketTester view where we can emit and listen socketss.
9+ */
710class SocketTesterView extends View {
11+ /**
12+ * Initialize datas for the SocketTesterView.
13+ */
814 constructor ( ) {
915 super ( {
1016 events : {
11- 'click #buttonChangeStatus' : 'onClickButtonChangeStatus' ,
12- 'click #buttonSendSocket' : 'onClickButtonSendSocket' ,
13- 'click #buttonSendListen' : 'onClickButtonSendListen' ,
14- 'click #buttonRemoveOutputDiv' : 'onClickButtonRemoveOutputDiv' ,
15- 'click #buttonRemoveOutputDivListen' : 'onButtonRemoveOutputDivListen' ,
16- 'change input[type=radio]' : 'onChangeInput'
17- }
17+ 'click #buttonChangeStatus' : 'onClickButtonChangeStatus' ,
18+ 'click #buttonSendSocket' : 'onClickButtonSendSocket' ,
19+ 'click #buttonSendListen' : 'onClickButtonSendListen' ,
20+ 'click #buttonRemoveOutputDiv' : 'onClickButtonRemoveOutputDiv' ,
21+ 'click #buttonRemoveOutputDivListen' : 'onButtonRemoveOutputDivListen' ,
22+ 'change input[type=radio]' : 'onChangeInput' ,
23+ } ,
1824 } ) ;
1925
20- this . initializeTemplate ( __dirname + '/ ../templates/socket_tester.hbs') ;
26+ this . initializeTemplate ( path . join ( __dirname , ' ../templates/socket_tester.hbs') ) ;
2127
22- this . host = configstore . get ( 'host' ) ;
28+ this . host = configstore . get ( 'host' ) ;
2329 if ( this . host . port . length > 0 ) {
24- this . socket = io ( this . host . host + ':' + this . host . port ) ;
30+ this . socket = io ( ` ${ this . host . host } : ${ this . host . port } ` ) ;
2531 } else {
2632 this . socket = io ( this . host . host ) ;
2733 }
@@ -49,13 +55,22 @@ class SocketTesterView extends View {
4955 } ) ;
5056 }
5157
58+ /**
59+ * Add a div with the result from the socket server.
60+ * @param {String } className - The name the class associated with the div.
61+ * @param {String } eventName - The name of the socket event.
62+ * @param {Object } insertBefore - Insert the div before this element.
63+ * @param {Object } data - The result from the server.
64+ * @param {Function } onRemove - A function called when the element is removed.
65+ * @return {Object } The created div.
66+ */
5267 createOutput ( className , eventName , insertBefore , data , onRemove ) {
5368 const outputDiv = $ ( '#divOutput' ) . clone ( ) ;
5469
5570 outputDiv . attr ( 'id' , '' ) ;
5671 outputDiv . addClass ( className ) ;
5772
58- outputDiv . find ( 'i' ) . click ( function ( ) {
73+ outputDiv . find ( 'i' ) . click ( function ( ) {
5974 $ ( this ) . parent ( ) . remove ( ) ;
6075 onRemove ( ) ;
6176 } ) ;
@@ -71,6 +86,9 @@ class SocketTesterView extends View {
7186 return outputDiv ;
7287 }
7388
89+ /**
90+ * When the user click on the button to reconnect the app to the socket server.
91+ */
7492 onClickButtonChangeStatus ( ) {
7593 if ( this . socket . connected ) {
7694 this . socket . disconnect ( ) ;
@@ -79,6 +97,10 @@ class SocketTesterView extends View {
7997 }
8098 }
8199
100+ /**
101+ * When the user click on the button to emit a message.
102+ * @param {Object } e - The click event.
103+ */
82104 onClickButtonSendSocket ( e ) {
83105 e . preventDefault ( ) ;
84106
@@ -106,14 +128,14 @@ class SocketTesterView extends View {
106128 try {
107129 data = JSON . parse ( data ) ;
108130 } catch ( error ) {
109- $ ( '#sendInputError' ) . show ( ) . find ( 'ul' ) . append ( ' <li>' + error + ' </li>' ) ;
131+ $ ( '#sendInputError' ) . show ( ) . find ( 'ul' ) . append ( ` <li>${ error } </li>` ) ;
110132 }
111133 }
112134 }
113135
114136 if ( ! $ ( '#sendInputError' ) . is ( ':visible' ) ) {
115- this . socket . emit ( eventName , data , ( data ) => {
116- this . createOutput ( 'outputDiv' , eventName , $ ( '#buttonRemoveOutputDiv' ) , data , ( ) => {
137+ this . socket . emit ( eventName , data , ( dataReceived ) => {
138+ this . createOutput ( 'outputDiv' , eventName , $ ( '#buttonRemoveOutputDiv' ) , dataReceived , ( ) => {
117139 if ( $ ( '.outputDiv' ) . length === 0 ) {
118140 $ ( '#buttonRemoveOutputDiv' ) . hide ( ) ;
119141 }
@@ -125,6 +147,10 @@ class SocketTesterView extends View {
125147 }
126148 }
127149
150+ /**
151+ * When the user click on the button to add a socket to listen.
152+ * @param {Object } e - The click event.
153+ */
128154 onClickButtonSendListen ( e ) {
129155 e . preventDefault ( ) ;
130156
@@ -146,24 +172,34 @@ class SocketTesterView extends View {
146172 $ ( '#buttonRemoveOutputDivListen' ) . show ( ) ;
147173 }
148174
149- this . socket . on ( eventName , function ( data ) {
175+ this . socket . on ( eventName , ( data ) => {
150176 outputDiv . find ( 'p' ) . append ( JSON . stringify ( data ) ) ;
151177 outputDiv . find ( 'p' ) . append ( '<br>' ) ;
152178 } ) ;
153179 }
154180 }
155181
182+ /**
183+ * Called when the user click on the cross to remove the emit div output.
184+ */
156185 onClickButtonRemoveOutputDiv ( ) {
157186 $ ( '.outputDiv' ) . remove ( ) ;
158187 $ ( '#buttonRemoveOutputDiv' ) . hide ( ) ;
159188 }
160189
190+ /**
191+ * Called when the user click on the cross to remove the listen div output.
192+ */
161193 onButtonRemoveOutputDivListen ( ) {
162194 $ ( '.outputDivListen' ) . remove ( ) ;
163195 $ ( '#buttonRemoveOutputDivListen' ) . hide ( ) ;
164196 this . socket . removeAllListeners ( ) ;
165197 }
166198
199+ /**
200+ * Method called when the user change the data type.
201+ * @param {Object } e - The change event.
202+ */
167203 onChangeInput ( e ) {
168204 const value = $ ( e . currentTarget ) . val ( ) ;
169205 if ( value === 'text' ) {
@@ -176,6 +212,9 @@ class SocketTesterView extends View {
176212 $ ( '#sendInputError' ) . hide ( ) ;
177213 }
178214
215+ /**
216+ * Render the view.
217+ */
179218 render ( ) {
180219 this . $el . html ( this . template ( this . host ) ) ;
181220 }
0 commit comments