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

View file

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

View file

@ -1,3 +1,4 @@
export const BUBBLE_DESIGN = ['standard', 'expanded_bubble']; export const BUBBLE_DESIGN = ['standard', 'expanded_bubble'];
export const WIDGET_DESIGN = ['standard', 'flat']; export const WIDGET_DESIGN = ['standard', 'flat'];
export const DARK_MODE = ['light', 'auto']; 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 => export const getBubbleView = type =>
BUBBLE_DESIGN.includes(type) ? type : BUBBLE_DESIGN[0]; BUBBLE_DESIGN.includes(type) ? type : BUBBLE_DESIGN[0];
@ -12,3 +17,6 @@ export const isFlatWidgetStyle = style => style === 'flat';
export const getDarkMode = darkMode => export const getDarkMode = darkMode =>
DARK_MODE.includes(darkMode) ? darkMode : DARK_MODE[0]; 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, @web_widget.hmac_token,
user_id.to_s user_id.to_s
) )
%> %>
<script> <script>
window.chatwootSettings = { window.chatwootSettings = {
hideMessageBubble: false,
position: '<%= @widget_position %>',
locale: 'en',
type: '<%= @widget_type %>',
showPopoutButton: true,
widgetStyle: '<%= @widget_style %>',
darkMode: '<%= @dark_mode %>', darkMode: '<%= @dark_mode %>',
hideMessageBubble: false,
loadingType: '<%= @loading_type %>',
locale: 'en',
position: '<%= @widget_position %>',
showPopoutButton: true,
type: '<%= @widget_type %>',
widgetStyle: '<%= @widget_style %>',
}; };
(function(d,t) { (function(d,t) {