2020-04-03 07:34:58 +00:00
|
|
|
import Cookies from 'js-cookie';
|
2020-08-09 10:37:32 +00:00
|
|
|
import { wootOn, addClass, loadCSS, removeClass } from './DOMHelpers';
|
2020-04-03 07:34:58 +00:00
|
|
|
import {
|
|
|
|
body,
|
|
|
|
widgetHolder,
|
|
|
|
createBubbleHolder,
|
|
|
|
createBubbleIcon,
|
|
|
|
bubbleImg,
|
|
|
|
chatBubble,
|
|
|
|
closeBubble,
|
|
|
|
bubbleHolder,
|
|
|
|
createNotificationBubble,
|
|
|
|
onClickChatBubble,
|
|
|
|
onBubbleClick,
|
2020-08-09 10:37:32 +00:00
|
|
|
setBubbleText,
|
2020-04-03 07:34:58 +00:00
|
|
|
} from './bubbleHelpers';
|
2020-07-25 16:24:58 +00:00
|
|
|
import { dispatchWindowEvent } from 'shared/helpers/CustomEventHelper';
|
|
|
|
|
|
|
|
const EVENT_NAME = 'chatwoot:ready';
|
2020-04-03 07:34:58 +00:00
|
|
|
|
|
|
|
export const IFrameHelper = {
|
|
|
|
getUrl({ baseUrl, websiteToken }) {
|
|
|
|
return `${baseUrl}/widget?website_token=${websiteToken}`;
|
|
|
|
},
|
|
|
|
createFrame: ({ baseUrl, websiteToken }) => {
|
|
|
|
const iframe = document.createElement('iframe');
|
|
|
|
const cwCookie = Cookies.get('cw_conversation');
|
|
|
|
let widgetUrl = IFrameHelper.getUrl({ baseUrl, websiteToken });
|
|
|
|
if (cwCookie) {
|
|
|
|
widgetUrl = `${widgetUrl}&cw_conversation=${cwCookie}`;
|
|
|
|
}
|
|
|
|
iframe.src = widgetUrl;
|
|
|
|
|
|
|
|
iframe.id = 'chatwoot_live_chat_widget';
|
|
|
|
iframe.style.visibility = 'hidden';
|
2020-08-09 10:37:32 +00:00
|
|
|
|
|
|
|
const holderClassName = `woot-widget-holder woot--hide woot-elements--${window.$chatwoot.position}`;
|
|
|
|
addClass(widgetHolder, holderClassName);
|
2020-04-03 07:34:58 +00:00
|
|
|
widgetHolder.appendChild(iframe);
|
|
|
|
body.appendChild(widgetHolder);
|
|
|
|
IFrameHelper.initPostMessageCommunication();
|
|
|
|
IFrameHelper.initLocationListener();
|
|
|
|
IFrameHelper.initWindowSizeListener();
|
2020-06-13 15:03:23 +00:00
|
|
|
IFrameHelper.preventDefaultScroll();
|
2020-04-03 07:34:58 +00:00
|
|
|
},
|
|
|
|
getAppFrame: () => document.getElementById('chatwoot_live_chat_widget'),
|
|
|
|
sendMessage: (key, value) => {
|
|
|
|
const element = IFrameHelper.getAppFrame();
|
|
|
|
element.contentWindow.postMessage(
|
|
|
|
`chatwoot-widget:${JSON.stringify({ event: key, ...value })}`,
|
|
|
|
'*'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
initLocationListener: () => {
|
|
|
|
window.onhashchange = () => {
|
|
|
|
IFrameHelper.setCurrentUrl();
|
|
|
|
};
|
|
|
|
},
|
|
|
|
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:', ''));
|
|
|
|
if (typeof IFrameHelper.events[message.event] === 'function') {
|
|
|
|
IFrameHelper.events[message.event](message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
initWindowSizeListener: () => {
|
2020-08-09 10:37:32 +00:00
|
|
|
wootOn(window, 'resize', () => IFrameHelper.toggleCloseButton());
|
2020-04-03 07:34:58 +00:00
|
|
|
},
|
2020-06-13 15:03:23 +00:00
|
|
|
preventDefaultScroll: () => {
|
|
|
|
widgetHolder.addEventListener('wheel', event => {
|
|
|
|
const deltaY = event.deltaY;
|
|
|
|
const contentHeight = widgetHolder.scrollHeight;
|
|
|
|
const visibleHeight = widgetHolder.offsetHeight;
|
|
|
|
const scrollTop = widgetHolder.scrollTop;
|
|
|
|
|
|
|
|
if (
|
|
|
|
(scrollTop === 0 && deltaY < 0) ||
|
|
|
|
(visibleHeight + scrollTop === contentHeight && deltaY > 0)
|
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2020-04-03 07:34:58 +00:00
|
|
|
events: {
|
|
|
|
loaded: message => {
|
|
|
|
Cookies.set('cw_conversation', message.config.authToken, {
|
|
|
|
expires: 365,
|
2020-07-25 16:24:58 +00:00
|
|
|
sameSite: 'Lax',
|
2020-04-03 07:34:58 +00:00
|
|
|
});
|
|
|
|
window.$chatwoot.hasLoaded = true;
|
2020-05-17 10:45:53 +00:00
|
|
|
IFrameHelper.sendMessage('config-set', {
|
|
|
|
locale: window.$chatwoot.locale,
|
2020-07-07 18:34:44 +00:00
|
|
|
position: window.$chatwoot.position,
|
|
|
|
hideMessageBubble: window.$chatwoot.hideMessageBubble,
|
2020-05-17 10:45:53 +00:00
|
|
|
});
|
2020-08-09 10:37:32 +00:00
|
|
|
IFrameHelper.onLoad({
|
|
|
|
widgetColor: message.config.channelConfig.widgetColor,
|
|
|
|
});
|
2020-04-03 07:34:58 +00:00
|
|
|
IFrameHelper.setCurrentUrl();
|
|
|
|
IFrameHelper.toggleCloseButton();
|
|
|
|
|
|
|
|
if (window.$chatwoot.user) {
|
|
|
|
IFrameHelper.sendMessage('set-user', window.$chatwoot.user);
|
|
|
|
}
|
2020-07-25 16:24:58 +00:00
|
|
|
dispatchWindowEvent(EVENT_NAME);
|
2020-04-03 07:34:58 +00:00
|
|
|
},
|
|
|
|
|
2020-08-09 10:37:32 +00:00
|
|
|
setBubbleLabel(message) {
|
|
|
|
setBubbleText(message.label);
|
|
|
|
},
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
toggleBubble: () => {
|
|
|
|
onBubbleClick();
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
|
|
|
|
onBubbleToggle: isOpen => {
|
|
|
|
if (!isOpen) {
|
|
|
|
IFrameHelper.events.resetUnreadMode();
|
|
|
|
} else {
|
|
|
|
IFrameHelper.pushEvent('webwidget.triggered');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setUnreadMode: message => {
|
|
|
|
const { unreadMessageCount } = message;
|
|
|
|
const { isOpen } = window.$chatwoot;
|
|
|
|
const toggleValue = true;
|
|
|
|
|
|
|
|
if (!isOpen && unreadMessageCount > 0) {
|
|
|
|
IFrameHelper.sendMessage('set-unread-view');
|
|
|
|
onBubbleClick({ toggleValue });
|
|
|
|
const holderEl = document.querySelector('.woot-widget-holder');
|
|
|
|
addClass(holderEl, 'has-unread-view');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
resetUnreadMode: () => {
|
|
|
|
IFrameHelper.sendMessage('unset-unread-view');
|
|
|
|
IFrameHelper.events.removeUnreadClass();
|
|
|
|
},
|
|
|
|
|
|
|
|
removeUnreadClass: () => {
|
|
|
|
const holderEl = document.querySelector('.woot-widget-holder');
|
|
|
|
removeClass(holderEl, 'has-unread-view');
|
|
|
|
},
|
2020-04-03 07:34:58 +00:00
|
|
|
},
|
2020-04-22 07:44:09 +00:00
|
|
|
pushEvent: eventName => {
|
|
|
|
IFrameHelper.sendMessage('push-event', { eventName });
|
|
|
|
},
|
2020-04-19 18:10:28 +00:00
|
|
|
onLoad: ({ widgetColor }) => {
|
2020-04-03 07:34:58 +00:00
|
|
|
const iframe = IFrameHelper.getAppFrame();
|
|
|
|
iframe.style.visibility = '';
|
|
|
|
iframe.setAttribute('id', `chatwoot_live_chat_widget`);
|
|
|
|
|
|
|
|
loadCSS();
|
|
|
|
createBubbleHolder();
|
|
|
|
|
|
|
|
if (!window.$chatwoot.hideMessageBubble) {
|
|
|
|
const chatIcon = createBubbleIcon({
|
|
|
|
className: 'woot-widget-bubble',
|
|
|
|
src: bubbleImg,
|
|
|
|
target: chatBubble,
|
|
|
|
});
|
|
|
|
|
|
|
|
const closeIcon = closeBubble;
|
2020-07-07 18:34:44 +00:00
|
|
|
const closeIconclassName = `woot-elements--${window.$chatwoot.position} woot-widget-bubble woot--close woot--hide`;
|
|
|
|
addClass(closeIcon, closeIconclassName);
|
2020-04-03 07:34:58 +00:00
|
|
|
|
|
|
|
chatIcon.style.background = widgetColor;
|
|
|
|
closeIcon.style.background = widgetColor;
|
|
|
|
|
|
|
|
bubbleHolder.appendChild(chatIcon);
|
|
|
|
bubbleHolder.appendChild(closeIcon);
|
|
|
|
bubbleHolder.appendChild(createNotificationBubble());
|
|
|
|
onClickChatBubble();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setCurrentUrl: () => {
|
|
|
|
IFrameHelper.sendMessage('set-current-url', {
|
|
|
|
refererURL: window.location.href,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
toggleCloseButton: () => {
|
|
|
|
if (window.matchMedia('(max-width: 668px)').matches) {
|
2020-07-07 18:34:44 +00:00
|
|
|
IFrameHelper.sendMessage('toggle-close-button', {
|
|
|
|
showClose: true,
|
|
|
|
});
|
2020-04-03 07:34:58 +00:00
|
|
|
} else {
|
2020-07-07 18:34:44 +00:00
|
|
|
IFrameHelper.sendMessage('toggle-close-button', {
|
|
|
|
showClose: false,
|
|
|
|
});
|
2020-04-03 07:34:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|