3 var iframe = document.createElement('iframe');
5 iframe['id'] = 'motion-chat-iframe';
6 iframe['src'] = '{{{remote}}}/snippet?location=' + btoa(window.location.host) + '&version=3.0.0&{{{query}}}';
8 var alignment="<%=alignment%>";
9 var verticalAlignment="<%=verticalAlignment%>";
10 var iframe_status = false;
12 document.documentElement.style.setProperty('--xc-ws-div-color', '<%=div_color%>');
13 document.documentElement.style.setProperty('--xc-ws-text-color', '<%=text_color%>');
14 document.documentElement.style.setProperty('--xc-ws-button_color', '<%=button_color%>');
15 document.documentElement.style.setProperty('--xc-ws-background-color', '<%=background_color%>');
17 var link_mobile = document.createElement('link');
18 link_mobile.href = '{{{remote}}}/snippet/styles/mobile.css?version=3.0.0';
19 link_mobile.rel='stylesheet';
20 link_mobile.type = "text/css";
21 document.head.appendChild(link_mobile);
22 <% if (alignment == 'bottom_right') { -%> // Bottom, right
23 iframe.className = 'motion-chat-iframe-bottom_right';
24 verticalAlignment = 0;
25 document.body.appendChild(iframe);
27 <% } else if (alignment == 'right') { -%> // Right
28 iframe.className = 'motion-chat-iframe-right';
29 iframe.style['top'] = verticalAlignment+'%';
30 var iframe_h = topIframe_calc();
31 topIframe_set(iframe_h);
32 var div_tab = document.createElement('div');
33 div_tab['id'] = 'motion-chat-iframe-tab';
34 div_tab.className = 'motion-chat-div-right';
37 <% } else if (alignment == 'left') { -%> // Left
38 iframe.className = 'motion-chat-iframe-left';
39 iframe.style['top'] = verticalAlignment+'%';
40 var iframe_h = topIframe_calc();
41 topIframe_set(iframe_h);
42 var div_tab = document.createElement('div');
43 div_tab.className = 'motion-chat-div-left';
44 div_tab['id'] ='motion-chat-iframe-tab';
49 <% if (alignment == 'right') { -%> // Right
50 topChat_set(iframe_h, verticalAlignment);
51 document.body.appendChild(div_tab);
52 var html = '<div id="chat_tab_arrow"> <strong><</strong> </div> <span id="chat_tab_text" style="writing-mode: tb-rl; transform: rotate(-180deg);" flex><strong><%=labelText%></strong></span>';
53 div_tab.innerHTML = html;
55 div_tab.style.backgroundColor="<%=div_color%>";
56 document.getElementById("chat_tab_text").style.color= "<%=text_color%>";
57 document.getElementById("chat_tab_arrow").style.color="<%=div_color%>";
58 document.getElementById("chat_tab_arrow").style.backgroundColor="<%=text_color%>";
59 document.getElementById("motion-chat-iframe-tab").style.display="flex";
61 function appendLink(){
62 if(iframe_status == false){
63 document.body.appendChild(iframe);
68 function expandChatRight(){
70 div_tab.style.right = '295px';
71 iframe.style.right = '0px';
72 document.getElementById("chat_tab_arrow").innerHTML = "<strong> > </strong>";
73 var iframeEl = document.getElementById('motion-chat-iframe');
74 iframeEl.contentWindow?.postMessage({"evt":"show"}, '*');
77 function collapseChatRight(){
79 div_tab.style.right = '0px';
80 iframe.style.right = '-312px';
81 document.getElementById("chat_tab_arrow").innerHTML = "<strong> < </strong>";
82 var iframeEl = document.getElementById('motion-chat-iframe');
83 iframeEl.contentWindow?.postMessage({"evt":"hide"}, '*');
86 function toggleChatExpandedRight(){
87 if(tab_status % 2 === 0) {
96 document.getElementById("motion-chat-iframe-tab").addEventListener("click", toggleChatExpandedRight);
97 <% } else if (alignment == 'left') { -%>
98 topChat_set(iframe_h, verticalAlignment);
99 document.body.appendChild(div_tab);
100 var html='<div id="chat_tab_arrow"> <strong>></strong> </div><span id="chat_tab_text" style="writing-mode: tb-rl; transform: rotate(-180deg);" flex><strong><%=labelText%></strong></span>';
101 div_tab.innerHTML=html;
103 div_tab.style.backgroundColor="<%=div_color%>";
104 document.getElementById("chat_tab_text").style.color= "<%=text_color%>";
105 document.getElementById("chat_tab_arrow").style.color="<%=div_color%>";
106 document.getElementById("chat_tab_arrow").style.backgroundColor="<%=text_color%>";
107 document.getElementById("motion-chat-iframe-tab").style.display="flex";
109 function appendLink(){
110 if(iframe_status == false){
111 document.body.appendChild(iframe);
116 function expandChatLeft(){
118 div_tab.style.left = '295px';
119 iframe.style.left = '0px';
120 document.getElementById("chat_tab_arrow").innerHTML = "<strong> < </strong>";
121 var iframeEl = document.getElementById('motion-chat-iframe');
122 iframeEl.contentWindow?.postMessage({"evt":"show"}, '*');
125 function collapseChatLeft(){
127 div_tab.style.left = '0px';
128 iframe.style.left = '-312px';
129 document.getElementById("chat_tab_arrow").innerHTML = "<strong> > </strong>";
130 var iframeEl = document.getElementById('motion-chat-iframe');
131 iframeEl.contentWindow?.postMessage({"evt":"hide"}, '*');
134 function toggleChatExpandedLeft(){
135 if(tab_status % 2 === 0) {
144 document.getElementById("motion-chat-iframe-tab").addEventListener("click", toggleChatExpandedLeft);
148 // START Events management
149 window.addEventListener('message', function(evt) {
150 switch (evt.data.cmd) {
152 <% if (alignment == 'bottom_right') { -%> // Bottom, right
153 iframe.style.top = 'auto';
154 iframe.style.bottom = '0px';
155 <% } else if (alignment == 'right') { -%> // Right
156 iframe.style.right = '0px';
157 div_tab.style.right = '295px';
158 <% } else if (alignment == 'left') { -%> // Left
159 iframe.style.left = '0px';
160 div_tab.style.left = '295px';
164 <% if (alignment == 'bottom_right') { -%> // Bottom, right
165 iframe.style.top = 'calc(100% - 50px)';
166 <% } else if (alignment == 'right') { -%> // Right
167 iframe.style.right = '-312px';
168 div_tab.style['right'] = '0px';
169 <% } else if (alignment == 'left') { -%> // Left
170 iframe.style.left = '-312px';
171 div_tab.style.left = '0px';
174 <% if (alignment == 'right' || alignment == 'left') { -%> // Bottom, right
175 div_tab.style.backgroundColor="<%=div_color%>";
176 document.getElementById("chat_tab_text").style.color= "<%=text_color%>";
177 document.getElementById("chat_tab_arrow").style.color="<%=div_color%>";
178 document.getElementById("chat_tab_arrow").style.backgroundColor="<%=text_color%>";
179 document.getElementById("motion-chat-iframe-tab").style.display="flex";
182 <% if (alignment == 'right' || alignment == 'left') { -%> // Bottom, right
183 document.getElementById("motion-chat-iframe-tab").style.display="none";
189 // END Events management
191 // START Proactive action management
192 <% proactiveActions.forEach(function(action, index) { -%> <% if (action.type == 'timeout') { -%>
193 setTimeout(function() {
194 <% if (alignment == 'right') { -%> expandChatRight();
196 <% } else if (alignment == 'left') { -%>
200 iframe.style.bottom = '0px';
201 iframe.contentWindow?.postMessage({
205 }, <%- action.timeout %> * 1000);
206 <% } -%> <% if (action.type == 'mouseOver') { -%> var elements<%- index %> = document.querySelectorAll("<%- action.selector %>");
207 for (var i = 0; i < elements<%- index %>.length; i++) {
208 elements<%- index %>[i].onmouseover = function() {
209 <% if (alignment == 'right') { -%> expandChatRight();
211 <% } else if (alignment == 'left') { -%>
215 iframe.style.bottom = '0px';
216 iframe.contentWindow?.postMessage({
222 <% } -%> <% }); -%> // END Proactive action management
225 if (window.DeviceOrientationEvent) {
226 window.addEventListener('orientationchange', function() {
227 if (alignment == 'right'){
228 document.getElementById("chat_tab_arrow").innerHTML = "<strong> < </strong>";
229 var iframeEl = document.getElementById('motion-chat-iframe');
230 iframeEl.contentWindow?.postMessage({"evt":"hide"}, '*');
232 iframe_h = topIframe_calc();
233 topIframe_set(iframe_h);
234 if(typeof div_tab != "undefined") topChat_set(iframe_h, verticalAlignment);
236 else if (alignment == 'left'){
237 document.getElementById("chat_tab_arrow").innerHTML = "<strong> > </strong>";
238 var iframeEl = document.getElementById('motion-chat-iframe');
239 iframeEl.contentWindow?.postMessage({"evt":"hide"}, '*');
241 iframe_h = topIframe_calc();
242 topIframe_set(iframe_h);
243 if(typeof div_tab != "undefined") topChat_set(iframe_h, verticalAlignment);
247 window.addEventListener("resize", function(event) {
248 var iframe_h = topIframe_calc();
249 topIframe_set(iframe_h);
250 if(typeof div_tab != "undefined") topChat_set(iframe_h, verticalAlignment);
253 function topIframe_calc(){
255 var heightD = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
256 var iframeT = ((heightD/100)*verticalAlignment) + 450;
257 if (heightD < iframeT) {
258 height_iframe = 450 - (iframeT - heightD);
260 else { height_iframe = 450; }
261 return height_iframe;
264 function topIframe_set(height_iframe){
265 if( height_iframe > 340){
266 iframe.style['height'] = height_iframe +'px';
267 if (alignment == "right" || alignment == "left" )
268 { iframe.style['top'] = verticalAlignment + "%"; }
269 else{ iframe.style['bottom'] = "0%"; }
273 iframe.style['height'] = '340px';
274 if (alignment == "right" || alignment == "left" )
275 { iframe.style['top'] = "calc(100% - 340px)"; }
276 else{ iframe.style['bottom'] = "0%"; }
281 function topChat_set(height_iframe, verticalAlignment){
283 if( height_iframe > 340){
284 div_tab.style['top'] = "calc("+ verticalAlignment +"% + " + (height_iframe - 100) + "px)";
287 div_tab.style['top'] = "auto";
288 div_tab.style['bottom'] = "0px";