Compare commits

...

1 commit

Author SHA1 Message Date
Pranav Raj S
e4aa444007 feat: Load Chatwoot SDK after load 2022-06-19 12:49:12 +05:30
5 changed files with 60 additions and 24 deletions

View file

@ -1,9 +1,10 @@
class WidgetTestsController < ActionController::Base
before_action :ensure_dark_mode
before_action :ensure_loading_type
before_action :ensure_web_widget
before_action :ensure_widget_position
before_action :ensure_widget_type
before_action :ensure_widget_style
before_action :ensure_dark_mode
before_action :ensure_widget_type
def index
render
@ -19,6 +20,10 @@ class WidgetTestsController < ActionController::Base
@dark_mode = params[:dark_mode] || 'light'
end
def ensure_loading_type
@loading_type = params[:loading_type] || 'immediate'
end
def ensure_widget_position
@widget_position = params[:position] || 'left'
end

View file

@ -3,6 +3,7 @@ import { IFrameHelper } from '../sdk/IFrameHelper';
import {
getBubbleView,
getDarkMode,
getLoadingType,
getWidgetStyle,
} from '../sdk/settingsHelper';
import {
@ -12,7 +13,24 @@ import {
} from '../sdk/cookieHelpers';
import { addClass, removeClass } from '../sdk/DOMHelpers';
import { SDK_SET_BUBBLE_VISIBILITY } from 'shared/constants/sharedFrameEvents';
const runSDK = ({ baseUrl, websiteToken }) => {
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;
}
@ -20,18 +38,19 @@ const runSDK = ({ baseUrl, websiteToken }) => {
const chatwootSettings = window.chatwootSettings || {};
window.$chatwoot = {
baseUrl,
darkMode: getDarkMode(chatwootSettings.darkMode),
hasLoaded: false,
hideMessageBubble: chatwootSettings.hideMessageBubble || false,
isOpen: false,
position: chatwootSettings.position === 'left' ? 'left' : 'right',
websiteToken,
locale: chatwootSettings.locale,
type: getBubbleView(chatwootSettings.type),
launcherTitle: chatwootSettings.launcherTitle || '',
showPopoutButton: chatwootSettings.showPopoutButton || false,
widgetStyle: getWidgetStyle(chatwootSettings.widgetStyle) || 'standard',
loadingType: getLoadingType(chatwootSettings.loadingType),
locale: chatwootSettings.locale,
position: chatwootSettings.position === 'left' ? 'left' : 'right',
resetTriggered: false,
darkMode: getDarkMode(chatwootSettings.darkMode),
showPopoutButton: chatwootSettings.showPopoutButton || false,
type: getBubbleView(chatwootSettings.type),
websiteToken,
widgetStyle: getWidgetStyle(chatwootSettings.widgetStyle) || 'standard',
toggle(state) {
IFrameHelper.events.toggleBubble(state);
@ -136,13 +155,16 @@ const runSDK = ({ baseUrl, websiteToken }) => {
window.$chatwoot.resetTriggered = true;
},
};
IFrameHelper.createFrame({
baseUrl,
websiteToken,
});
if (
window.$chatwoot.loadingType === LOADING_TYPE[0] ||
isDocumentLoadComplete()
) {
runSDK();
} else {
initializeLoadEventListener();
}
};
window.chatwootSDK = {
run: runSDK,
run: initializeSDK,
};

View file

@ -1,3 +1,4 @@
export const BUBBLE_DESIGN = ['standard', 'expanded_bubble'];
export const WIDGET_DESIGN = ['standard', 'flat'];
export const DARK_MODE = ['light', 'auto'];
export const LOADING_TYPE = ['immediate', 'load_complete'];

View file

@ -1,4 +1,9 @@
import { BUBBLE_DESIGN, DARK_MODE, WIDGET_DESIGN } from './constants';
import {
BUBBLE_DESIGN,
DARK_MODE,
LOADING_TYPE,
WIDGET_DESIGN,
} from './constants';
export const getBubbleView = type =>
BUBBLE_DESIGN.includes(type) ? type : BUBBLE_DESIGN[0];
@ -12,3 +17,6 @@ export const isFlatWidgetStyle = style => style === 'flat';
export const getDarkMode = darkMode =>
DARK_MODE.includes(darkMode) ? darkMode : DARK_MODE[0];
export const getLoadingType = loadingType =>
LOADING_TYPE.includes(loadingType) ? loadingType : LOADING_TYPE[0];

View file

@ -8,18 +8,18 @@
@web_widget.hmac_token,
user_id.to_s
)
%>
<script>
window.chatwootSettings = {
hideMessageBubble: false,
position: '<%= @widget_position %>',
locale: 'en',
type: '<%= @widget_type %>',
showPopoutButton: true,
widgetStyle: '<%= @widget_style %>',
darkMode: '<%= @dark_mode %>',
hideMessageBubble: false,
loadingType: '<%= @loading_type %>',
locale: 'en',
position: '<%= @widget_position %>',
showPopoutButton: true,
type: '<%= @widget_type %>',
widgetStyle: '<%= @widget_style %>',
};
(function(d,t) {