170 lines
5 KiB
JavaScript
Executable file
170 lines
5 KiB
JavaScript
Executable file
import Cookies from 'js-cookie';
|
|
import { IFrameHelper } from '../sdk/IFrameHelper';
|
|
import {
|
|
getBubbleView,
|
|
getDarkMode,
|
|
getLoadingType,
|
|
getWidgetStyle,
|
|
} from '../sdk/settingsHelper';
|
|
import {
|
|
computeHashForUserData,
|
|
getUserCookieName,
|
|
hasUserKeys,
|
|
} from '../sdk/cookieHelpers';
|
|
import { addClass, removeClass } from '../sdk/DOMHelpers';
|
|
import { SDK_SET_BUBBLE_VISIBILITY } from 'shared/constants/sharedFrameEvents';
|
|
import { LOADING_TYPE } from '../sdk/constants';
|
|
|
|
const isDocumentLoadComplete = () => document.readyState === 'complete';
|
|
|
|
const runSDK = () => {
|
|
const { baseUrl, websiteToken } = window.$chatwoot;
|
|
IFrameHelper.createFrame({ baseUrl, websiteToken });
|
|
};
|
|
|
|
const initializeLoadEventListener = () => {
|
|
document.onreadystatechange = () => {
|
|
if (isDocumentLoadComplete()) {
|
|
runSDK();
|
|
}
|
|
};
|
|
};
|
|
|
|
export const initializeSDK = ({ baseUrl, websiteToken }) => {
|
|
if (window.$chatwoot) {
|
|
return;
|
|
}
|
|
|
|
const chatwootSettings = window.chatwootSettings || {};
|
|
window.$chatwoot = {
|
|
baseUrl,
|
|
darkMode: getDarkMode(chatwootSettings.darkMode),
|
|
hasLoaded: false,
|
|
hideMessageBubble: chatwootSettings.hideMessageBubble || false,
|
|
isOpen: false,
|
|
launcherTitle: chatwootSettings.launcherTitle || '',
|
|
loadingType: getLoadingType(chatwootSettings.loadingType),
|
|
locale: chatwootSettings.locale,
|
|
position: chatwootSettings.position === 'left' ? 'left' : 'right',
|
|
resetTriggered: false,
|
|
showPopoutButton: chatwootSettings.showPopoutButton || false,
|
|
type: getBubbleView(chatwootSettings.type),
|
|
websiteToken,
|
|
widgetStyle: getWidgetStyle(chatwootSettings.widgetStyle) || 'standard',
|
|
|
|
toggle(state) {
|
|
IFrameHelper.events.toggleBubble(state);
|
|
},
|
|
|
|
toggleBubbleVisibility(visibility) {
|
|
let widgetElm = document.querySelector('.woot--bubble-holder');
|
|
let widgetHolder = document.querySelector('.woot-widget-holder');
|
|
if (visibility === 'hide') {
|
|
addClass(widgetHolder, 'woot-widget--without-bubble');
|
|
addClass(widgetElm, 'woot-hidden');
|
|
window.$chatwoot.hideMessageBubble = true;
|
|
} else if (visibility === 'show') {
|
|
removeClass(widgetElm, 'woot-hidden');
|
|
removeClass(widgetHolder, 'woot-widget--without-bubble');
|
|
window.$chatwoot.hideMessageBubble = false;
|
|
}
|
|
IFrameHelper.sendMessage(SDK_SET_BUBBLE_VISIBILITY, {
|
|
hideMessageBubble: window.$chatwoot.hideMessageBubble,
|
|
});
|
|
},
|
|
|
|
popoutChatWindow() {
|
|
IFrameHelper.events.popoutChatWindow({
|
|
baseUrl: window.$chatwoot.baseUrl,
|
|
websiteToken: window.$chatwoot.websiteToken,
|
|
locale: window.$chatwoot.locale,
|
|
});
|
|
},
|
|
|
|
setUser(identifier, user) {
|
|
if (typeof identifier !== 'string' && typeof identifier !== 'number') {
|
|
throw new Error('Identifier should be a string or a number');
|
|
}
|
|
|
|
if (!hasUserKeys(user)) {
|
|
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',
|
|
});
|
|
},
|
|
|
|
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,
|
|
});
|
|
}
|
|
},
|
|
|
|
setLabel(label = '') {
|
|
IFrameHelper.sendMessage('set-label', { label });
|
|
},
|
|
|
|
removeLabel(label = '') {
|
|
IFrameHelper.sendMessage('remove-label', { label });
|
|
},
|
|
|
|
setLocale(localeToBeUsed = 'en') {
|
|
IFrameHelper.sendMessage('set-locale', { locale: localeToBeUsed });
|
|
},
|
|
|
|
reset() {
|
|
if (window.$chatwoot.isOpen) {
|
|
IFrameHelper.events.toggleBubble();
|
|
}
|
|
|
|
Cookies.remove('cw_conversation');
|
|
Cookies.remove(getUserCookieName());
|
|
|
|
const iframe = IFrameHelper.getAppFrame();
|
|
iframe.src = IFrameHelper.getUrl({
|
|
baseUrl: window.$chatwoot.baseUrl,
|
|
websiteToken: window.$chatwoot.websiteToken,
|
|
});
|
|
|
|
window.$chatwoot.resetTriggered = true;
|
|
},
|
|
};
|
|
if (
|
|
window.$chatwoot.loadingType === LOADING_TYPE[0] ||
|
|
isDocumentLoadComplete()
|
|
) {
|
|
runSDK();
|
|
} else {
|
|
initializeLoadEventListener();
|
|
}
|
|
};
|
|
|
|
window.chatwootSDK = {
|
|
run: initializeSDK,
|
|
};
|