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';
|
2022-04-01 15:29:03 +00:00
|
|
|
import {
|
|
|
|
getBubbleView,
|
|
|
|
getDarkMode,
|
|
|
|
getWidgetStyle,
|
|
|
|
} from '../sdk/settingsHelper';
|
2022-02-25 10:48:18 +00:00
|
|
|
import {
|
|
|
|
computeHashForUserData,
|
|
|
|
getUserCookieName,
|
|
|
|
hasUserKeys,
|
|
|
|
} from '../sdk/cookieHelpers';
|
2022-10-12 21:42:06 +00:00
|
|
|
import { addClasses, removeClasses } from '../sdk/DOMHelpers';
|
2022-05-06 14:00:48 +00:00
|
|
|
import { SDK_SET_BUBBLE_VISIBILITY } from 'shared/constants/sharedFrameEvents';
|
2020-04-03 07:34:58 +00:00
|
|
|
const runSDK = ({ baseUrl, websiteToken }) => {
|
2022-01-26 00:39:19 +00:00
|
|
|
if (window.$chatwoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
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-24 17:14:26 +00:00
|
|
|
locale: chatwootSettings.locale,
|
2020-08-09 10:37:32 +00:00
|
|
|
type: getBubbleView(chatwootSettings.type),
|
2020-08-23 12:40:36 +00:00
|
|
|
launcherTitle: chatwootSettings.launcherTitle || '',
|
2020-08-28 12:09:46 +00:00
|
|
|
showPopoutButton: chatwootSettings.showPopoutButton || false,
|
2022-04-01 15:29:03 +00:00
|
|
|
widgetStyle: getWidgetStyle(chatwootSettings.widgetStyle) || 'standard',
|
2022-02-28 09:23:11 +00:00
|
|
|
resetTriggered: false,
|
2022-04-01 15:29:03 +00:00
|
|
|
darkMode: getDarkMode(chatwootSettings.darkMode),
|
2019-10-29 07:20:54 +00:00
|
|
|
|
2021-09-28 13:55:44 +00:00
|
|
|
toggle(state) {
|
|
|
|
IFrameHelper.events.toggleBubble(state);
|
2020-04-03 07:34:58 +00:00
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
|
2022-05-06 14:00:48 +00:00
|
|
|
toggleBubbleVisibility(visibility) {
|
|
|
|
let widgetElm = document.querySelector('.woot--bubble-holder');
|
|
|
|
let widgetHolder = document.querySelector('.woot-widget-holder');
|
|
|
|
if (visibility === 'hide') {
|
2022-10-12 21:42:06 +00:00
|
|
|
addClasses(widgetHolder, 'woot-widget--without-bubble');
|
|
|
|
addClasses(widgetElm, 'woot-hidden');
|
2022-05-06 14:00:48 +00:00
|
|
|
window.$chatwoot.hideMessageBubble = true;
|
|
|
|
} else if (visibility === 'show') {
|
2022-10-12 21:42:06 +00:00
|
|
|
removeClasses(widgetElm, 'woot-hidden');
|
|
|
|
removeClasses(widgetHolder, 'woot-widget--without-bubble');
|
2022-05-06 14:00:48 +00:00
|
|
|
window.$chatwoot.hideMessageBubble = false;
|
|
|
|
}
|
|
|
|
IFrameHelper.sendMessage(SDK_SET_BUBBLE_VISIBILITY, {
|
|
|
|
hideMessageBubble: window.$chatwoot.hideMessageBubble,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-03-28 08:01:51 +00:00
|
|
|
popoutChatWindow() {
|
|
|
|
IFrameHelper.events.popoutChatWindow({
|
|
|
|
baseUrl: window.$chatwoot.baseUrl,
|
|
|
|
websiteToken: window.$chatwoot.websiteToken,
|
|
|
|
locale: window.$chatwoot.locale,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
setUser(identifier, user) {
|
2020-09-26 17:03:20 +00:00
|
|
|
if (typeof identifier !== 'string' && typeof identifier !== 'number') {
|
2020-04-03 07:34:58 +00:00
|
|
|
throw new Error('Identifier should be a string or a number');
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
2020-09-26 17:03:20 +00:00
|
|
|
|
2020-10-02 04:47:12 +00:00
|
|
|
if (!hasUserKeys(user)) {
|
2020-09-26 17:03:20 +00:00
|
|
|
throw new Error(
|
|
|
|
'User object should have one of the keys [avatar_url, email, name]'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const userCookieName = getUserCookieName();
|
|
|
|
const existingCookieValue = Cookies.get(userCookieName);
|
|
|
|
const hashToBeStored = computeHashForUserData({ identifier, user });
|
|
|
|
if (hashToBeStored === existingCookieValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.$chatwoot.identifier = identifier;
|
|
|
|
window.$chatwoot.user = user;
|
|
|
|
IFrameHelper.sendMessage('set-user', { identifier, user });
|
|
|
|
Cookies.set(userCookieName, hashToBeStored, {
|
|
|
|
expires: 365,
|
|
|
|
sameSite: 'Lax',
|
|
|
|
});
|
2020-01-17 08:06:05 +00:00
|
|
|
},
|
2019-10-30 05:13:11 +00:00
|
|
|
|
2020-08-21 14:00:27 +00:00
|
|
|
setCustomAttributes(customAttributes = {}) {
|
|
|
|
if (!customAttributes || !Object.keys(customAttributes).length) {
|
|
|
|
throw new Error('Custom attributes should have atleast one key');
|
|
|
|
} else {
|
|
|
|
IFrameHelper.sendMessage('set-custom-attributes', { customAttributes });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteCustomAttribute(customAttribute = '') {
|
|
|
|
if (!customAttribute) {
|
|
|
|
throw new Error('Custom attribute is required');
|
|
|
|
} else {
|
|
|
|
IFrameHelper.sendMessage('delete-custom-attribute', {
|
|
|
|
customAttribute,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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');
|
2020-09-26 17:03:20 +00:00
|
|
|
Cookies.remove(getUserCookieName());
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
const iframe = IFrameHelper.getAppFrame();
|
|
|
|
iframe.src = IFrameHelper.getUrl({
|
|
|
|
baseUrl: window.$chatwoot.baseUrl,
|
|
|
|
websiteToken: window.$chatwoot.websiteToken,
|
|
|
|
});
|
2022-02-28 09:23:11 +00:00
|
|
|
|
|
|
|
window.$chatwoot.resetTriggered = true;
|
2020-04-03 07:34:58 +00:00
|
|
|
},
|
|
|
|
};
|
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
|
|
|
};
|