2019-10-30 05:13:11 +00:00
|
|
|
import Cookies from 'js-cookie';
|
2020-04-03 07:34:58 +00:00
|
|
|
import { IFrameHelper } from '../sdk/IFrameHelper';
|
2019-10-30 05:13:11 +00:00
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
const runSDK = ({ baseUrl, websiteToken }) => {
|
|
|
|
const chatwootSettings = window.chatwootSettings || {};
|
|
|
|
window.$chatwoot = {
|
|
|
|
baseUrl,
|
|
|
|
hasLoaded: false,
|
|
|
|
hideMessageBubble: chatwootSettings.hideMessageBubble || false,
|
|
|
|
isOpen: false,
|
2020-04-25 17:48:00 +00:00
|
|
|
position: chatwootSettings.position === 'left' ? 'left' : 'right',
|
2020-04-03 07:34:58 +00:00
|
|
|
websiteToken,
|
2020-05-17 10:45:53 +00:00
|
|
|
locale: chatwootSettings.locale || 'en',
|
2019-10-29 07:20:54 +00:00
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
toggle() {
|
2020-04-10 11:12:37 +00:00
|
|
|
IFrameHelper.events.toggleBubble();
|
2020-04-03 07:34:58 +00:00
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
setUser(identifier, user) {
|
|
|
|
if (typeof identifier === 'string' || typeof identifier === 'number') {
|
|
|
|
window.$chatwoot.identifier = identifier;
|
|
|
|
window.$chatwoot.user = user || {};
|
|
|
|
IFrameHelper.sendMessage('set-user', {
|
|
|
|
identifier,
|
|
|
|
user: window.$chatwoot.user,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error('Identifier should be a string or a number');
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
2020-01-17 08:06:05 +00:00
|
|
|
},
|
2019-10-30 05:13:11 +00:00
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
setLabel(label = '') {
|
|
|
|
IFrameHelper.sendMessage('set-label', { label });
|
|
|
|
},
|
2019-11-25 18:40:27 +00:00
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
removeLabel(label = '') {
|
|
|
|
IFrameHelper.sendMessage('remove-label', { label });
|
|
|
|
},
|
2019-11-25 18:40:27 +00:00
|
|
|
|
2020-05-17 10:45:53 +00:00
|
|
|
setLocale(localeToBeUsed = 'en') {
|
|
|
|
IFrameHelper.sendMessage('set-locale', { locale: localeToBeUsed });
|
|
|
|
},
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
reset() {
|
|
|
|
if (window.$chatwoot.isOpen) {
|
2020-04-10 11:12:37 +00:00
|
|
|
IFrameHelper.events.toggleBubble();
|
2020-04-03 07:34:58 +00:00
|
|
|
}
|
2019-11-25 18:40:27 +00:00
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
Cookies.remove('cw_conversation');
|
|
|
|
const iframe = IFrameHelper.getAppFrame();
|
|
|
|
iframe.src = IFrameHelper.getUrl({
|
|
|
|
baseUrl: window.$chatwoot.baseUrl,
|
|
|
|
websiteToken: window.$chatwoot.websiteToken,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2019-10-29 07:20:54 +00:00
|
|
|
|
2019-10-30 05:13:11 +00:00
|
|
|
IFrameHelper.createFrame({
|
|
|
|
baseUrl,
|
|
|
|
websiteToken,
|
|
|
|
});
|
2020-04-03 07:34:58 +00:00
|
|
|
};
|
2019-10-29 07:20:54 +00:00
|
|
|
|
|
|
|
window.chatwootSDK = {
|
2020-04-03 07:34:58 +00:00
|
|
|
run: runSDK,
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|