PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.8
VikBooking Hotel Booking Engine & PMS v1.8.8
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / modules / mod_vikbooking_chat / assets / script.js

script.js in VikBooking Hotel Booking Engine & PMS 1.8.8, at modules/mod_vikbooking_chat/assets/script.js

209 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function($, w) {
2 'use strict';
3
4 const refreshBadgeCount = (chat, el) => {
5 if (!chat) {
6 // session not started
7 return;
8 }
9
10 // observe only the current chat
11 if (chat.data.environment.context.alias != 'session') {
12 return;
13 }
14
15 $(el).find('.vbo-chat-widget-toggle [data-unread]').attr('data-unread', chat.getUnreadMessagesCount());
16 }
17
18 const startSession = (data) => {
19 return new Promise((resolve, reject) => {
20 VBOChatAjax.do(
21 VBO_CHAT_WIDGET_CONFIG.startSessionUrl,
22 data,
23 (resp) => {
24 resolve(resp.id);
25 },
26 (error) => {
27 reject(error);
28 }
29 );
30 })
31 }
32
33 const renderChat = (contextId) => {
34 return new Promise((resolve, reject) => {
35 VBOChatAjax.do(
36 VBO_CHAT_WIDGET_CONFIG.renderChatUrl,
37 {
38 context: 'session',
39 id_context: contextId,
40 },
41 (resp) => {
42 resolve(resp.html);
43 },
44 (error) => {
45 reject(error);
46 }
47 );
48 });
49 }
50
51 const getWidgetChat = () => {
52 const chat = VBOChat.getInstance();
53 return chat.data ? chat : null;
54 }
55
56 const toggleGreetingsPanel = (el, visible) => {
57 if (visible) {
58 $(el).find('.vbo-chat-session-placeholder').show();
59
60 const chat = getWidgetChat();
61
62 if (chat && (chat.input.getValue() || '').length == 0) {
63 setTimeout(() => {
64 $(el).find('.vbo-chat-placeholder-faqs').addClass('slide-in');
65 }, 400);
66 }
67 } else {
68 $(el).find('.vbo-chat-session-placeholder')
69 .hide()
70 .find('.vbo-chat-placeholder-faqs')
71 .removeClass('slide-in');
72 }
73 }
74
75 $(function() {
76 const modEl = $(VBO_CHAT_WIDGET_CONFIG.selector);
77
78 setTimeout(() => {
79 modEl.find('.vbo-chat-widget-toggle').addClass('bounce-in');
80 }, 500);
81
82 // refresh badge count on load
83 refreshBadgeCount(getWidgetChat(), modEl);
84
85 // open chat when clicking the show button
86 modEl.find('a[data-role="widget.chat.show"]').on('click', () => {
87 modEl.find('.vbo-chat-widget-toggle').hide().removeClass('bounce-in');
88 modEl.find('.vbo-chat-widget-container').addClass('slide-in');
89
90 const chat = getWidgetChat();
91
92 if (chat) {
93 // show greetings panel in case of empty messages
94 toggleGreetingsPanel(modEl, chat.data.environment.messages.length == 0);
95
96 chat.scrollToBottom();
97 chat.input.focus();
98
99 // auto-read the messages when the chat is open
100 chat.data.environment.options.autoread = true;
101 chat.readNotifications();
102 }
103 });
104
105 // hide chat when clicking the dismiss button
106 modEl.find('a[data-role="widget.chat.dismiss"]').on('click', () => {
107 modEl.find('.vbo-chat-widget-container').removeClass('slide-in');
108 modEl.find('.vbo-chat-widget-toggle').show().addClass('bounce-in');
109
110 const chat = getWidgetChat();
111
112 if (chat) {
113 // do not auto-read the messages when the chat is closed
114 chat.data.environment.options.autoread = false;
115 }
116 });
117
118 // when the chat is prepared, choose whether the greetings panel should be displayed or not
119 w.addEventListener('chat.prepare', (event) => {
120 const chat = event.detail.chat;
121
122 // observe only the current chat
123 if (chat.data.environment.context.alias != 'session') {
124 return;
125 }
126
127 toggleGreetingsPanel(modEl, chat.data.environment.messages.length == 0);
128 });
129
130 // refresh badge counter whenever the chat downloads new messages
131 w.addEventListener('chat.sync', (event) => {
132 refreshBadgeCount(event.detail.chat, modEl);
133 });
134
135 // refresh the badge counter whenever the chat reads the unread messages
136 w.addEventListener('chat.read', (event) => {
137 refreshBadgeCount(event.detail.chat, modEl);
138 });
139
140 // hide chat greetings after sending the first message
141 w.addEventListener('chat.send.before', (event) => {
142 if (event.detail.chat.data.environment.context.alias == 'session') {
143 toggleGreetingsPanel(modEl, false);
144 }
145 });
146
147 // start chat session after clicking the button
148 modEl.find('.start-chat-session').on('click', async function() {
149 const user = {};
150 let valid = true;
151
152 const form = modEl.find('.vbo-chat-session-start-form');
153
154 form.find('input').filter('[name]').each(function() {
155 if (!$(this).is(':valid')) {
156 $(this).addClass('invalid');
157 return valid = false;
158 }
159
160 $(this).removeClass('invalid');
161
162 user[$(this).attr('name')] = $(this).val();
163 });
164
165 if (!valid) {
166 return false;
167 }
168
169 // disable button to prevent duplicate requests
170 $(this).prop('disabled', true).append('<span class="loading-spinner"><i class="fas fa-spinner fa-spin"></i></span>');
171 form.find('input').filter('[name]').prop('readonly', true);
172
173 try {
174 // start the session and obtain a unique ID
175 const sessionId = await startSession(user);
176
177 // load the chat contents
178 const chatDOM = await renderChat(sessionId);
179
180 // inject the HTML of the chat within the session target node
181 modEl.find('.vbo-chat-session-start-form').remove();
182 modEl.find('.chat-session-target').html(chatDOM);
183 } catch (error) {
184 // enable button again
185 $(this).prop('disabled', false).find('.loading-spinner').remove();
186 form.find('input').filter('[name]').prop('readonly', false);
187
188 // display error to user
189 VBOToast.dispatch(new VBOToastMessage({
190 body: error.responseText || error.statusText || 'Connection lost.',
191 status: VBOToast.ERROR_STATUS,
192 }));
193 }
194 });
195
196 // auto-populate the chat textarea with the message of the clicked FAQ
197 modEl.find('a[data-question]').on('click', function() {
198 const msg = $(this).data('question');
199
200 // hide hints
201 $(this).closest('.vbo-chat-placeholder-faqs').removeClass('slide-in');
202
203 // set message into the textarea
204 const chat = getWidgetChat();
205 chat.input.setValue(msg);
206 chat.input.focus();
207 });
208 });
209 })(jQuery, window);