3 var iframe = document.createElement('iframe');
5 iframe['id'] = 'motion-chat-iframe';
6 iframe['src'] = '{{{remote}}}/snippet?location=' + btoa(window.location.host) + '&version=1.0.16&{{{query}}}';
8 var alignment="<%=alignment%>";
9 var verticalAlignment="<%=verticalAlignment%>";
11 var iframe_status = false;
13 var link_mobile = document.createElement('link');
14 link_mobile.href = '{{{remote}}}/snippet/styles/mobile.css?version=1.0.16';
15 link_mobile.rel='stylesheet';
16 link_mobile.type = "text/css";
17 <% if (alignment == 'bottom_right') { -%> // Bottom, right
18 iframe.className = 'motion-chat-iframe-bottom_right';
19 verticalAlignment = 0;
20 iframe.appendChild(link_mobile);
21 document.body.appendChild(iframe);
23 <% } else if (alignment == 'right') { -%> // Right
24 iframe.className = 'motion-chat-iframe-right';
25 iframe.style['top'] = verticalAlignment+'%';
26 var iframe_h = topIframe_calc();
27 topIframe_set(iframe_h);
28 var div_tab = document.createElement('div');
29 div_tab['id'] = 'motion-chat-iframe-tab';
30 div_tab.className = 'motion-chat-div-right';
33 <% } else if (alignment == 'left') { -%> // Left
34 iframe.className = 'motion-chat-iframe-left';
35 iframe.style['top'] = verticalAlignment+'%';
36 var iframe_h = topIframe_calc();
37 topIframe_set(iframe_h);
38 var div_tab = document.createElement('div');
39 div_tab.className = 'motion-chat-div-left';
40 div_tab['id'] ='motion-chat-iframe-tab';
45 <% if (alignment == 'right') { -%> // Bottom, right
46 topChat_set(iframe_h, verticalAlignment);
47 document.body.appendChild(div_tab);
48 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> CHAT </strong></span>';
49 div_tab.innerHTML = html;
50 div_tab.appendChild(link_mobile);
51 chat_tab_text.appendChild(link_mobile);
52 chat_tab_arrow.appendChild(link_mobile);
53 <% if (alignment == 'right' || alignment == 'left') { -%> // Bottom, right
54 div_tab.style.backgroundColor="<%=div_color%>";
55 document.getElementById("chat_tab_text").style.color= "<%=text_color%>";
56 document.getElementById("chat_tab_arrow").style.color="<%=div_color%>";
57 document.getElementById("chat_tab_arrow").style.backgroundColor="<%=text_color%>";
58 document.getElementById("motion-chat-iframe-tab").style.display="block";
61 document.getElementById("motion-chat-iframe-tab").addEventListener("click", function(){
62 if(iframe_status == false){
64 iframe.appendChild(link_mobile);
65 document.body.appendChild(iframe);
69 div_tab.style.right = '295px';
70 iframe.style.right = '0px';
71 document.getElementById("chat_tab_arrow").innerHTML = "<strong> > </strong>";
72 var iframeEl = document.getElementById('motion-chat-iframe');
73 iframeEl.contentWindow.postMessage({"evt":"show"}, '*');
76 div_tab.style.right = '0px';
77 iframe.style.right = '-312px';
78 document.getElementById("chat_tab_arrow").innerHTML = "<strong> < </strong>";
79 var iframeEl = document.getElementById('motion-chat-iframe');
80 iframeEl.contentWindow.postMessage({"evt":"hide"}, '*');
84 <% } else if (alignment == 'left') { -%>
85 topChat_set(iframe_h, verticalAlignment);
86 document.body.appendChild(div_tab);
87 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> CHAT </strong></span>';
88 div_tab.innerHTML=html;
89 div_tab.appendChild(link_mobile);
90 chat_tab_text.appendChild(link_mobile);
91 chat_tab_arrow.appendChild(link_mobile);
92 <% if (alignment == 'right' || alignment == 'left') { -%> // Bottom, right
93 div_tab.style.backgroundColor="<%=div_color%>";
94 document.getElementById("chat_tab_text").style.color= "<%=text_color%>";
95 document.getElementById("chat_tab_arrow").style.color="<%=div_color%>";
96 document.getElementById("chat_tab_arrow").style.backgroundColor="<%=text_color%>";
97 document.getElementById("motion-chat-iframe-tab").style.display="block";
100 document.getElementById("motion-chat-iframe-tab").addEventListener("click", function(){
102 if(iframe_status == false){
104 iframe.appendChild(link_mobile);
105 document.body.appendChild(iframe);
109 if(tab_status%2==0) {
110 div_tab.style.left = '295px';
111 iframe.style.left = '0px';
112 document.getElementById("chat_tab_arrow").innerHTML = "<strong> < </strong>";
113 var iframeEl = document.getElementById('motion-chat-iframe');
114 iframeEl.contentWindow.postMessage({"evt":"show"}, '*');
118 div_tab.style.left = '0px';
119 iframe.style.left = '-312px';
120 document.getElementById("chat_tab_arrow").innerHTML = "<strong> > </strong>";
121 var iframeEl = document.getElementById('motion-chat-iframe');
122 iframeEl.contentWindow.postMessage({"evt":"hide"}, '*');
129 // START Events management
130 window.addEventListener('message', function(evt) {
131 switch (evt.data.cmd) {
133 <% if (alignment == 'bottom_right') { -%> // Bottom, right
134 iframe.style.top = 'auto';
135 iframe.style.bottom = '0px';
136 <% } else if (alignment == 'right') { -%> // Right
137 iframe.style.right = '0px';
138 div_tab.style.right = '295px';
139 <% } else if (alignment == 'left') { -%> // Left
140 iframe.style.left = '0px';
141 div_tab.style.left = '295px';
145 <% if (alignment == 'bottom_right') { -%> // Bottom, right
146 iframe.style.top = 'calc(100% - 50px)';
147 <% } else if (alignment == 'right') { -%> // Right
148 iframe.style.right = '-312px';
149 div_tab.style['right'] = '0px';
150 <% } else if (alignment == 'left') { -%> // Left
151 iframe.style.left = '-312px';
152 div_tab.style.left = '0px';
155 <% if (alignment == 'right' || alignment == 'left') { -%> // Bottom, right
156 div_tab.style.backgroundColor="<%=div_color%>";
157 document.getElementById("chat_tab_text").style.color= "<%=text_color%>";
158 document.getElementById("chat_tab_arrow").style.color="<%=div_color%>";
159 document.getElementById("chat_tab_arrow").style.backgroundColor="<%=text_color%>";
160 document.getElementById("motion-chat-iframe-tab").style.display="block";
163 <% if (alignment == 'right' || alignment == 'left') { -%> // Bottom, right
164 document.getElementById("motion-chat-iframe-tab").style.display="none";
170 // END Events management
172 // START Proactive action management
173 <% proactiveActions.forEach(function(action) { -%> <% if (action.type == 'timeout') { -%> setTimeout(function() {
174 iframe.style.bottom = '0px';
175 iframe.contentWindow.postMessage({
178 }, <%- action.timeout %> * 1000);
179 <% } -%> <% if (action.type == 'mouseOver') { -%> var elements = document.querySelectorAll("<%- action.selector %>");
180 for (var i = 0; i < elements.length; i++) {
181 elements[i].onmouseover = function() {
182 iframe.style.bottom = '0px';
183 iframe.contentWindow.postMessage({
188 <% } -%> <% }); -%> // END Proactive action management
191 if (window.DeviceOrientationEvent) {
192 window.addEventListener('orientationchange', function() {
193 if (alignment == 'right'){
194 document.getElementById("chat_tab_arrow").innerHTML = "<strong> < </strong>";
195 var iframeEl = document.getElementById('motion-chat-iframe');
196 iframeEl.contentWindow.postMessage({"evt":"hide"}, '*');
198 iframe_h = topIframe_calc();
199 topIframe_set(iframe_h);
200 if(typeof div_tab != "undefined") topChat_set(iframe_h, verticalAlignment);
202 else if (alignment == 'left'){
203 document.getElementById("chat_tab_arrow").innerHTML = "<strong> > </strong>";
204 var iframeEl = document.getElementById('motion-chat-iframe');
205 iframeEl.contentWindow.postMessage({"evt":"hide"}, '*');
207 iframe_h = topIframe_calc();
208 topIframe_set(iframe_h);
209 if(typeof div_tab != "undefined") topChat_set(iframe_h, verticalAlignment);
213 window.addEventListener("resize", function(event) {
214 var iframe_h = topIframe_calc();
215 topIframe_set(iframe_h);
216 if(typeof div_tab != "undefined") topChat_set(iframe_h, verticalAlignment);
219 function topIframe_calc(){
220 var heightD = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
221 var iframeT = ((heightD/100)*verticalAlignment) + 450;
222 if (heightD < iframeT) {
223 var height_iframe = 450 - (iframeT - heightD);
226 else { height_iframe = 450; }
227 return height_iframe;
230 function topIframe_set(height_iframe){
231 if( height_iframe > 340){
232 iframe.style['height'] = height_iframe +'px';
233 if (alignment == "right" || alignment == "left" )
234 { iframe.style['top'] = verticalAlignment + "%"; }
235 else{ iframe.style['bottom'] = "0%"; }
239 iframe.style['height'] = '340px';
240 if (alignment == "right" || alignment == "left" )
241 { iframe.style['top'] = "calc(100% - 340px)"; }
242 else{ iframe.style['bottom'] = "0%"; }
247 function topChat_set(height_iframe, verticalAlignment){
249 if( height_iframe > 340){
250 div_tab.style['top'] = "calc("+ verticalAlignment +"% + " + (height_iframe - 100) + "px)";
253 div_tab.style['top'] = "auto";
254 div_tab.style['bottom'] = "0px";