1- import * as signalR from "@aspnet/signalr" ;
2- import mitt from 'mitt' ;
31import deleteImg from '../../images/delete.png' ;
2+ import { AppLogic } from './logic/app.logic' ;
3+ import { ContentType , AppEvents } from './logic/lib/app.common'
44
5- const ContentType = {
6- TEXT : "Text" ,
7- NUMBER : "Number" ,
8- JSON : "JSON"
9- }
10-
11- const eventEmitter = mitt ( ) ;
12- var connection = null ;
13- var storeFunc = null ;
5+ //var storeFunc = null;
146var isConnected = false ;
15- var isAdvanceView = false ;
16- var isTokenRequired = false ;
7+ // var isAdvanceView = false;
8+ //ar appLogic = null ;
179
1810export function Init ( ) {
11+
12+ window . appLogic = new AppLogic ( ) ;
13+
1914 //Connect Button Events
20- var connectBtnClass = document . getElementsByClassName ( 'connectbtn' ) ;
21- for ( var i = 0 ; i < connectBtnClass . length ; i ++ ) {
22- connectBtnClass [ i ] . addEventListener ( 'click' ,
23- function ( ) {
24- OnConnect ( ) ;
25- } ,
26- false ) ;
27- }
15+ var connectBtn = document
16+ . getElementById ( 'btn-connect' )
17+ . addEventListener ( 'click' ,
18+ function ( ) {
19+ OnConnect ( ) ;
20+ } ,
21+ false ) ;
2822
2923 //Disconnect Button Events
30- var disconnectBtnClass = document . getElementsByClassName ( 'disconnectbtn' ) ;
31- for ( var i = 0 ; i < disconnectBtnClass . length ; i ++ ) {
32- disconnectBtnClass [ i ] . addEventListener ( 'click' ,
33- function ( ) {
34- OnDisConnect ( ) ;
35- } ,
36- false ) ;
37- }
24+ var disconnectBtn = document
25+ . getElementById ( 'btn-disconnectbtn' )
26+ . addEventListener ( 'click' ,
27+ function ( ) {
28+ OnDisConnect ( ) ;
29+ } ,
30+ false ) ;
3831
3932 //Send Payload Button Events
40- var sendBtnClass = document . getElementsByClassName ( 'btn-send-payload' ) ;
41- for ( var i = 0 ; i < disconnectBtnClass . length ; i ++ ) {
42- sendBtnClass [ i ] . addEventListener ( 'click' ,
43- function ( ) {
44- SendPayload ( ) ;
45- } ,
46- false ) ;
47- }
33+ var sendBtn = document
34+ . getElementById ( 'btn-send-payload' )
35+ . addEventListener ( 'click' ,
36+ function ( ) {
37+ SendPayload ( ) ;
38+ } ,
39+ false ) ;
4840
4941 NotConnected ( ) ;
5042 RigisterNavigationTabEvent ( ) ;
@@ -53,29 +45,34 @@ export function Init() {
5345 . addEventListener ( 'change' , ( event ) => {
5446 if ( event . target . checked ) {
5547 document . getElementById ( 'authHeader' ) . disabled = false ;
56- isTokenRequired = true ;
48+ window . appLogic . EnableAuth ( ) ;
5749 } else {
58- isTokenRequired = false ;
50+ window . appLogic . DisableAuth ( ) ;
5951 document . getElementById ( 'authHeader' ) . disabled = true ;
6052 }
6153 } ) ;
6254
6355}
6456
6557//#region ConnectedEvent
66-
67- eventEmitter . on ( 'OnConnected' , ( ) => {
68- if ( isAdvanceView === true ) {
69- document . getElementById ( 'chk-ws' ) . disabled = true ;
70- document . getElementById ( 'chk-sse' ) . disabled = true ;
71- }
58+ // eventEmitter.on('OnConnected', () => {
59+ // if(window.appLogic.GetCurrentView() !== true) {
60+ // document.getElementById('chk-ws').disabled= true;
61+ // document.getElementById('chk-sse').disabled= true;
62+ // }
63+ // } );
64+
65+ AppEvents . on ( 'Init' , ( ) => {
66+ debugger ;
67+ console . log ( 'Init Event Emitter' ) ;
7268} ) ;
7369
7470//#endregion
7571
7672//#region OnDisconnected
77- eventEmitter . on ( 'OnDisconnected' , ( ) => {
78- if ( isAdvanceView === true ) {
73+ AppEvents . on ( 'OnDisconnected' , ( ) => {
74+ debugger ;
75+ if ( window . appLogic . GetCurrentView ( ) !== true ) {
7976 document . getElementById ( 'chk-ws' ) . disabled = false ;
8077 document . getElementById ( 'chk-sse' ) . disabled = false ;
8178 }
@@ -97,31 +94,32 @@ export function RigisterNavigationTabEvent() {
9794
9895export function OnTabChange ( tabName ) {
9996 if ( tabName == 'basic' ) {
100- isAdvanceView = false ;
101- AdvanceViewElements ( isAdvanceView ) ;
97+ window . appLogic . SetCurrentViewAsBasic ( ) ;
98+ AdvanceViewElements ( false ) ;
10299 }
103100 else {
104- isAdvanceView = true ;
105- AdvanceViewElements ( isAdvanceView ) ;
101+ window . appLogic . SetCurrentViewAsAdvance ( ) ;
102+ AdvanceViewElements ( true ) ;
106103 }
107104}
108105
109-
110106export function AdvanceViewElements ( enable ) {
107+
111108 if ( enable === true ) {
112109 document . getElementById ( 'protocol-support' ) . style = 'display:block' ;
113110 document . getElementById ( 'auth-container' ) . style = 'display:block' ;
114111 if ( isConnected === true ) {
115112 document . getElementById ( 'chk-req-token' ) . disabled = true ;
116113 document . getElementById ( 'authHeader' ) . disabled = true ;
117- } else {
114+ DisableElementByClassName ( 'protocol-support' ) ;
115+ }
116+ else {
118117 document . getElementById ( 'chk-req-token' ) . disabled = false ;
119- if ( isTokenRequired === true ) {
118+ if ( window . appLogic . IsAuthEnabled ( ) === true ) {
120119 document . getElementById ( 'authHeader' ) . disabled = false ;
121- }
122-
120+ }
123121 }
124- }
122+ }
125123 else {
126124 document . getElementById ( 'protocol-support' ) . style = 'display:none' ;
127125 document . getElementById ( 'auth-container' ) . style = 'display:none' ;
@@ -143,9 +141,7 @@ export function AddArgumentsCallBack() {
143141 for ( var i = 0 ; i < parentDiv . length ; i ++ ) {
144142
145143 var divElement = document . createElement ( 'div' ) ;
146- //divElement.setAttribute('class', 'form-group form-inline args-container');
147144 divElement . setAttribute ( 'class' , 'container args-container' ) ;
148- //divElement.setAttribute('style', 'border:1px solid #cecece');
149145
150146 var hr = document . createElement ( 'hr' ) ;
151147 hr . setAttribute ( 'class' , 'style13' ) ;
@@ -163,7 +159,6 @@ export function GetSelectElement() {
163159 var div = document . createElement ( 'div' ) ;
164160 div . setAttribute ( 'class' , 'form-group col-sm-5' ) ;
165161
166-
167162 var selectElement = document . createElement ( 'select' ) ;
168163 selectElement . setAttribute ( 'class' , 'req-content-type form-control' ) ;
169164
@@ -209,7 +204,6 @@ export function GetImageElement() {
209204
210205 var imgElement = document . createElement ( 'img' ) ;
211206 imgElement . src = deleteImg ;
212- // inputTxtElement.src = require('../../images/delete.png');
213207 imgElement . addEventListener ( 'click' , function ( ) {
214208 console . log ( 'Delete Button' ) ;
215209 this . parentElement . parentElement . remove ( ) ;
@@ -267,43 +261,12 @@ export function NotConnected() {
267261}
268262
269263export function buildConnection ( url ) {
270- // var option = {
271- // accessTokenFactory: () => 'YOUR ACCESS TOKEN TOKEN HERE'
272- // };
273-
274- // const options = {
275- // accessTokenFactory: getToken
276- // };
277-
278- var option = { } ;
279-
280- if ( isAdvanceView ) {
281- if ( isTokenRequired === true ) {
282- option . accessTokenFactory = ( ) => document . getElementById ( 'authHeader' ) . value ;
283- }
284- }
285-
286- connection = new signalR . HubConnectionBuilder ( )
287- . withUrl ( url , option )
288- // .withUrl(url,
289- // {
290- // accessTokenFactory: () => "MyTokenGoesHere" // Return access token
291- // })
292- . configureLogging ( signalR . LogLevel . Information )
293- //.withAutomaticReconnect([0, 2000, 10000, 30000])
294- . build ( ) ;
264+ var option = { url : url , getToken : ( ) => document . getElementById ( 'authHeader' ) . value } ;
265+ window . appLogic . Init ( option ) ;
295266}
296267
297268export function start ( ) {
298-
299- connection
300- . start ( )
301- . then ( function ( ) {
302- //console.log('Connected');
303- } )
304- . catch ( function ( err ) {
305- return console . error ( err . toString ( ) ) ;
306- } ) ;
269+ window . appLogic . OnConnect ( ) ;
307270}
308271
309272export function connectToServer ( url ) {
@@ -313,6 +276,7 @@ export function connectToServer(url) {
313276
314277export function OnConnect ( ) {
315278
279+ var isAdvanceView = ! window . appLogic . GetCurrentView ( ) ;
316280 if ( isAdvanceView ) {
317281 SetConnectionProtocol ( ) ;
318282 }
@@ -332,16 +296,15 @@ export function OnConnect() {
332296
333297 //Receive Data
334298 //Reading the raw response
335- storeFunc = connection . processIncomingData ;
336- connection . processIncomingData = function ( data ) {
337- console . log ( 'processIncomingData' + data ) ;
338- storeFunc . call ( connection , data ) ;
339- }
340- connection . on ( "ReceiveData" , function ( data ) {
341- document . querySelector ( "#inputResponseData" ) . value += JSON . stringify ( data ) + '\n' ;
342- } ) ;
299+ // storeFunc = connection.processIncomingData;
300+ // connection.processIncomingData = function (data) {
301+ // console.log('processIncomingData'+ data);
302+ // storeFunc.call(connection, data);
303+ // }
343304
344- eventEmitter . emit ( 'OnConnected' ) ;
305+ window . appLogic . OnReceive ( ( data ) => { document . querySelector ( "#inputResponseData" ) . value += JSON . stringify ( data ) + '\n' } ) ;
306+
307+ AppEvents . emit ( 'OnConnected' ) ;
345308 AddArguments ( ) ;
346309
347310 //Disable Url
@@ -366,7 +329,6 @@ export function SetConnectionProtocol() {
366329 else if ( elements [ i ] . value === "lp" && elements [ i ] . checked !== true )
367330 {
368331 //console.log("Server Sent Event disabled");
369- //EventSource = undefined;
370332 }
371333 }
372334}
@@ -400,15 +362,14 @@ export function OnDisConnect() {
400362 Reset ( ) ;
401363 EnableElementByClassName ( 'connectbtn' ) ;
402364 NotConnected ( ) ;
403- AdvanceViewElements ( isAdvanceView ) ;
365+ AdvanceViewElements ( ! window . appLogic . GetCurrentView ( ) ) ;
404366 //Enable URL textBix
405367 document . getElementById ( "inputUrl" ) . disabled = false ;
406368}
407369
408370export function Reset ( ) {
409371 //Clear Server Method Text
410372 document . getElementById ( 'inputServerMethod' ) . value = "" ;
411-
412373
413374 var addArgBtnClass = document . getElementsByClassName ( 'btn-add-argument' ) ;
414375 for ( var i = 0 ; i < addArgBtnClass . length ; i ++ ) {
@@ -419,25 +380,14 @@ export function Reset() {
419380}
420381
421382export function Disconnect ( ) {
422- connection . stop ( )
423- . then ( function ( ) {
424- console . log ( 'Disconnected' ) ;
425- eventEmitter . emit ( 'OnDisconnected' ) ;
426- } )
427- . catch ( function ( err ) {
428- return console . error ( err . toString ( ) ) ;
429- } ) ;
383+ window . appLogic . OnDisConnect ( ) ;
430384}
431385
432386export function SendPayload ( ) {
433387
434388 var methodName = document . getElementById ( "inputServerMethod" ) . value ;
435389 var methodArguments = new Array ( ) ;
436-
390+
437391 methodArguments = ReadAndFormatArguments ( ) ;
438-
439- connection . invoke ( methodName , ...methodArguments )
440- . catch ( function ( err ) {
441- return console . log ( err ) ;
442- } ) ;
392+ window . appLogic . OnSend ( { methodName : methodName , methodArguments : methodArguments } ) ;
443393}
0 commit comments