1+ function init ( ) {
2+
3+ //--------------------------- Important Variables----------------------------
4+ var inactiveMessage = "Server is down, Please contact the developer to activate it"
5+ chatPopup = document . querySelector ( ".chat-popup" )
6+ chatBtn = document . querySelector ( ".chat-btn" )
7+ chatSubmit = document . querySelector ( ".chat-submit" )
8+ chatHeader = document . querySelector ( ".chat-header" )
9+ chatArea = document . querySelector ( ".chat-area" )
10+ chatInput = document . querySelector ( ".chat-input" )
11+ expandWindow = document . querySelector ( ".expand-chat-window" )
12+ root = document . documentElement ;
13+ chatPopup . style . display = "none"
14+ var host = "http://localhost:8080"
15+
16+
17+ //------------------------ ChatBot Toggler -------------------------
18+
19+ chatBtn . addEventListener ( "click" , ( ) => {
20+
21+ mobileDevice = ! detectMob ( )
22+ if ( chatPopup . style . display == "none" && mobileDevice ) {
23+ chatPopup . style . display = "flex"
24+ chatInput . focus ( ) ;
25+ chatBtn . innerHTML = `<i class='material-icons'>close</i>`
26+ } else if ( mobileDevice ) {
27+ chatPopup . style . display = "none"
28+ chatBtn . innerHTML = `<i class='material-icons'>chat</i>`
29+ } else {
30+ mobileView ( )
31+ }
32+ } )
33+
34+ chatSubmit . addEventListener ( "click" , ( ) => {
35+ let userResponse = chatInput . value . trim ( ) ;
36+ if ( userResponse !== "" ) {
37+ setUserResponse ( ) ;
38+ send ( userResponse )
39+ }
40+ } )
41+ }
42+
43+ // end of init function
44+
45+ function userResponseBtn ( e ) {
46+ send ( e . value ) ;
47+ }
48+
49+ // to submit user input when he presses enter
50+ function givenUserInput ( e ) {
51+ if ( e . keyCode == 13 ) {
52+ let userResponse = chatInput . value . trim ( ) ;
53+ if ( userResponse !== "" ) {
54+ setUserResponse ( )
55+ send ( userResponse )
56+ }
57+ }
58+ }
59+
60+ // to display user message on UI
61+ function setUserResponse ( ) {
62+ let userInput = chatInput . value ;
63+ if ( userInput ) {
64+ let temp = `<div class="user-msg"><span class = "msg">${ userInput } </span></div>`
65+ chatArea . innerHTML += temp ;
66+ chatInput . value = ""
67+ } else {
68+ chatInput . disabled = false ;
69+ }
70+ scrollToBottomOfResults ( ) ;
71+ }
72+
73+
74+
75+ function scrollToBottomOfResults ( ) {
76+ chatArea . scrollTop = chatArea . scrollHeight ;
77+ }
78+
79+ /***************************************************************
80+ Frontend Part Completed
81+ ****************************************************************/
82+
83+ function send ( message ) {
84+ chatInput . type = "text"
85+ passwordInput = false ;
86+ chatInput . focus ( ) ;
87+ console . log ( "User Message:" , message )
88+ $ . ajax ( {
89+ url : host ,
90+ method : 'PUT' ,
91+ data : {
92+ message : message
93+ }
94+ } ) ;
95+ chatInput . focus ( ) ;
96+ }
97+
98+ function mobileView ( ) {
99+ $ ( '.chat-popup' ) . width ( $ ( window ) . width ( ) ) ;
100+
101+ if ( chatPopup . style . display == "none" ) {
102+ chatPopup . style . display = "flex"
103+ // chatInput.focus();
104+ chatBtn . style . display = "none"
105+ chatPopup . style . bottom = "0"
106+ chatPopup . style . right = "0"
107+ // chatPopup.style.transition = "none"
108+ expandWindow . innerHTML = `<i class='material-icons'>close</i>`
109+ }
110+ }
111+
112+ function detectMob ( ) {
113+ return ( ( window . innerHeight <= 800 ) && ( window . innerWidth <= 600 ) ) ;
114+ }
115+
116+ function createChatBot ( ) {
117+
118+ host = "http://localhost:8080" ;
119+ init ( )
120+ const msg = document . querySelector ( ".msg" ) ;
121+ msg . innerText = "Welcome to Enarx Chat!" ;
122+
123+ const botTitle = document . querySelector ( ".bot-title" ) ;
124+ botTitle . innerText = "Enarx Chat" ;
125+ }
126+
127+ createChatBot ( ) ;
0 commit comments