2019-10-30 05:13:11 +00:00
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
|
|
|
|
import { SDK_CSS } from '../widget/assets/scss/sdk';
|
2019-10-29 07:20:54 +00:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
const bubbleImg =
|
|
|
|
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAAUVBMVEUAAAD///////////////////////////////////////////////////////////////////////////////////////////////////////8IN+deAAAAGnRSTlMAAwgJEBk0TVheY2R5eo+ut8jb5OXs8fX2+cjRDTIAAADsSURBVHgBldZbkoMgFIThRgQv8SKKgGf/C51UnJqaRI30/9zfe+NQUQ3TvG7bOk9DVeCmshmj/CuOTYnrdBfkUOg0zlOtl9OWVuEk4+QyZ3DIevmSt/ioTvK1VH/s5bY3YdM9SBZ/mUUyWgx+U06ycgp7D8msxSvtc4HXL9BLdj2elSEfhBJAI0QNgJEBI1BEBsQClVBVGDgwYOLAhJkDM1YOrNg4sLFAsLJgZsHEgoEFFQt0JAFGFjQsKAMJ0LFAexKgZYFyJIDxJIBNJEDNAtSJBLCeBDCOBFAPzwFA94ED+zmhwDO9358r8ANtIsMXi7qVAwAAAABJRU5ErkJggg==';
|
|
|
|
|
|
|
|
const body = document.getElementsByTagName('body')[0];
|
|
|
|
const holder = document.createElement('div');
|
|
|
|
|
|
|
|
const bubbleHolder = document.createElement('div');
|
|
|
|
const chatBubble = document.createElement('div');
|
|
|
|
const closeBubble = document.createElement('div');
|
|
|
|
|
|
|
|
const notification_bubble = document.createElement('span');
|
|
|
|
const bodyOverFlowStyle = document.body.style.overflow;
|
|
|
|
|
|
|
|
function loadCSS() {
|
|
|
|
const css = document.createElement('style');
|
|
|
|
css.type = 'text/css';
|
2019-10-30 05:13:11 +00:00
|
|
|
css.innerHTML = `${SDK_CSS}`;
|
2019-10-29 07:20:54 +00:00
|
|
|
document.body.appendChild(css);
|
|
|
|
}
|
|
|
|
|
|
|
|
function wootOn(elm, event, fn) {
|
|
|
|
if (document.addEventListener) {
|
|
|
|
elm.addEventListener(event, fn, false);
|
|
|
|
} else if (document.attachEvent) {
|
|
|
|
// <= IE 8 loses scope so need to apply, we add this to object so we
|
|
|
|
// can detach later (can't detach anonymous functions)
|
|
|
|
// eslint-disable-next-line
|
|
|
|
elm[event + fn] = function() {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
return fn.apply(elm, arguments);
|
|
|
|
};
|
|
|
|
elm.attachEvent(`on${event}`, elm[event + fn]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function classHelper(classes, action, elm) {
|
|
|
|
let search;
|
|
|
|
let replace;
|
|
|
|
let i;
|
|
|
|
let has = false;
|
|
|
|
if (classes) {
|
|
|
|
// Trim any whitespace
|
2019-10-30 05:13:11 +00:00
|
|
|
const classarray = classes.split(/\s+/);
|
2019-10-29 07:20:54 +00:00
|
|
|
for (i = 0; i < classarray.length; i += 1) {
|
|
|
|
search = new RegExp(`\\b${classarray[i]}\\b`, 'g');
|
|
|
|
replace = new RegExp(` *${classarray[i]}\\b`, 'g');
|
|
|
|
if (action === 'remove') {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
elm.className = elm.className.replace(replace, '');
|
|
|
|
} else if (action === 'toggle') {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
elm.className = elm.className.match(search)
|
|
|
|
? elm.className.replace(replace, '')
|
|
|
|
: `${elm.className} ${classarray[i]}`;
|
|
|
|
} else if (action === 'has') {
|
|
|
|
if (elm.className.match(search)) {
|
|
|
|
has = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return has;
|
|
|
|
}
|
|
|
|
|
2019-10-30 05:13:11 +00:00
|
|
|
function addClass(elm, classes) {
|
|
|
|
if (classes) {
|
|
|
|
elm.className += ` ${classes}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
// Toggle class
|
|
|
|
function toggleClass(elm, classes) {
|
|
|
|
classHelper(classes, 'toggle', elm);
|
|
|
|
}
|
|
|
|
|
|
|
|
const createBubbleIcon = ({ className, src, target }) => {
|
|
|
|
target.className = className;
|
|
|
|
const bubbleIcon = document.createElement('img');
|
|
|
|
bubbleIcon.src = src;
|
|
|
|
target.appendChild(bubbleIcon);
|
|
|
|
return target;
|
|
|
|
};
|
|
|
|
|
|
|
|
function createBubbleHolder() {
|
|
|
|
addClass(bubbleHolder, 'woot--bubble-holder');
|
|
|
|
body.appendChild(bubbleHolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNotificationBubble() {
|
|
|
|
addClass(notification_bubble, 'woot--notification');
|
|
|
|
return notification_bubble;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bubbleClickCallback() {
|
|
|
|
toggleClass(chatBubble, 'woot--hide');
|
|
|
|
toggleClass(closeBubble, 'woot--hide');
|
|
|
|
toggleClass(holder, 'woot--hide');
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClickChatBubble() {
|
2019-11-09 11:42:31 +00:00
|
|
|
wootOn(bubbleHolder, 'click', bubbleClickCallback);
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function disableScroll() {
|
|
|
|
document.body.style.overflow = 'hidden';
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableScroll() {
|
|
|
|
document.body.style.overflow = bodyOverFlowStyle;
|
|
|
|
}
|
|
|
|
|
2019-10-30 05:13:11 +00:00
|
|
|
const IFrameHelper = {
|
|
|
|
createFrame: ({ baseUrl, websiteToken }) => {
|
|
|
|
const iframe = document.createElement('iframe');
|
|
|
|
const cwCookie = Cookies.get('cw_conversation');
|
2020-01-09 07:36:40 +00:00
|
|
|
let widgetUrl = `${baseUrl}/widget?website_token=${websiteToken}`;
|
2019-10-30 05:13:11 +00:00
|
|
|
if (cwCookie) {
|
|
|
|
widgetUrl = `${widgetUrl}&cw_conversation=${cwCookie}`;
|
|
|
|
}
|
|
|
|
iframe.src = widgetUrl;
|
|
|
|
|
2020-01-01 17:00:43 +00:00
|
|
|
iframe.id = 'chatwoot_live_chat_widget';
|
2019-10-30 05:13:11 +00:00
|
|
|
iframe.style.visibility = 'hidden';
|
|
|
|
holder.className = 'woot-widget-holder woot--hide';
|
|
|
|
holder.appendChild(iframe);
|
|
|
|
body.appendChild(holder);
|
|
|
|
IFrameHelper.initPostMessageCommunication();
|
2020-01-01 17:00:43 +00:00
|
|
|
IFrameHelper.initLocationListener();
|
2019-10-30 05:13:11 +00:00
|
|
|
},
|
2020-01-01 17:00:43 +00:00
|
|
|
getAppFrame: () => document.getElementById('chatwoot_live_chat_widget'),
|
2019-10-30 05:13:11 +00:00
|
|
|
sendMessage: (key, value) => {
|
|
|
|
const element = IFrameHelper.getAppFrame();
|
|
|
|
element.contentWindow.postMessage(
|
|
|
|
`chatwoot-widget:${JSON.stringify({ event: key, ...value })}`,
|
|
|
|
'*'
|
|
|
|
);
|
|
|
|
},
|
2020-01-17 08:06:05 +00:00
|
|
|
events: {
|
|
|
|
loaded: message => {
|
|
|
|
Cookies.set('cw_conversation', message.config.authToken);
|
|
|
|
IFrameHelper.sendMessage('config-set', {});
|
|
|
|
IFrameHelper.onLoad(message.config.channelConfig);
|
|
|
|
IFrameHelper.setCurrentUrl();
|
|
|
|
},
|
|
|
|
set_auth_token: message => {
|
|
|
|
Cookies.set('cw_conversation', message.authToken);
|
|
|
|
},
|
|
|
|
toggleBubble: () => {
|
|
|
|
bubbleClickCallback();
|
|
|
|
},
|
|
|
|
},
|
2019-10-30 05:13:11 +00:00
|
|
|
initPostMessageCommunication: () => {
|
|
|
|
window.onmessage = e => {
|
|
|
|
if (
|
|
|
|
typeof e.data !== 'string' ||
|
|
|
|
e.data.indexOf('chatwoot-widget:') !== 0
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const message = JSON.parse(e.data.replace('chatwoot-widget:', ''));
|
2020-01-17 08:06:05 +00:00
|
|
|
if (typeof IFrameHelper.events[message.event] === 'function') {
|
|
|
|
IFrameHelper.events[message.event](message);
|
2019-10-30 05:13:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2020-01-01 17:00:43 +00:00
|
|
|
initLocationListener: () => {
|
|
|
|
window.onhashchange = () => {
|
|
|
|
IFrameHelper.setCurrentUrl();
|
|
|
|
};
|
|
|
|
},
|
2019-11-25 18:40:27 +00:00
|
|
|
onLoad: ({ widget_color: widgetColor }) => {
|
2019-10-30 05:13:11 +00:00
|
|
|
const iframe = IFrameHelper.getAppFrame();
|
|
|
|
iframe.style.visibility = '';
|
|
|
|
iframe.setAttribute('id', `chatwoot_live_chat_widget`);
|
|
|
|
iframe.onmouseenter = disableScroll;
|
|
|
|
iframe.onmouseleave = enableScroll;
|
|
|
|
|
|
|
|
loadCSS();
|
|
|
|
createBubbleHolder();
|
|
|
|
|
2019-11-25 18:40:27 +00:00
|
|
|
const chatIcon = createBubbleIcon({
|
|
|
|
className: 'woot-widget-bubble',
|
|
|
|
src: bubbleImg,
|
|
|
|
target: chatBubble,
|
|
|
|
});
|
|
|
|
|
2020-01-13 06:40:40 +00:00
|
|
|
const closeIcon = closeBubble;
|
|
|
|
closeIcon.className = 'woot-widget-bubble woot--close woot--hide';
|
2019-11-25 18:40:27 +00:00
|
|
|
|
|
|
|
chatIcon.style.background = widgetColor;
|
|
|
|
closeIcon.style.background = widgetColor;
|
|
|
|
|
|
|
|
bubbleHolder.appendChild(chatIcon);
|
|
|
|
bubbleHolder.appendChild(closeIcon);
|
2019-10-30 05:13:11 +00:00
|
|
|
bubbleHolder.appendChild(createNotificationBubble());
|
|
|
|
onClickChatBubble();
|
|
|
|
},
|
2020-01-01 17:00:43 +00:00
|
|
|
setCurrentUrl: () => {
|
|
|
|
IFrameHelper.sendMessage('set-current-url', {
|
|
|
|
refererURL: window.location.href,
|
|
|
|
});
|
|
|
|
},
|
2019-10-30 05:13:11 +00:00
|
|
|
};
|
2019-10-29 07:20:54 +00:00
|
|
|
|
2019-10-30 05:13:11 +00:00
|
|
|
function loadIframe({ baseUrl, websiteToken }) {
|
|
|
|
IFrameHelper.createFrame({
|
|
|
|
baseUrl,
|
|
|
|
websiteToken,
|
|
|
|
});
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.chatwootSDK = {
|
|
|
|
run: loadIframe,
|
|
|
|
};
|