mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 20:18:08 +00:00
feat: Add the SDK method to programatically toggle live chat bubble (#4223)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -10,7 +10,8 @@ import {
|
|||||||
getUserCookieName,
|
getUserCookieName,
|
||||||
hasUserKeys,
|
hasUserKeys,
|
||||||
} from '../sdk/cookieHelpers';
|
} from '../sdk/cookieHelpers';
|
||||||
|
import { addClass, removeClass } from '../sdk/DOMHelpers';
|
||||||
|
import { SDK_SET_BUBBLE_VISIBILITY } from 'shared/constants/sharedFrameEvents';
|
||||||
const runSDK = ({ baseUrl, websiteToken }) => {
|
const runSDK = ({ baseUrl, websiteToken }) => {
|
||||||
if (window.$chatwoot) {
|
if (window.$chatwoot) {
|
||||||
return;
|
return;
|
||||||
@@ -36,6 +37,23 @@ const runSDK = ({ baseUrl, websiteToken }) => {
|
|||||||
IFrameHelper.events.toggleBubble(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() {
|
popoutChatWindow() {
|
||||||
IFrameHelper.events.popoutChatWindow({
|
IFrameHelper.events.popoutChatWindow({
|
||||||
baseUrl: window.$chatwoot.baseUrl,
|
baseUrl: window.$chatwoot.baseUrl,
|
||||||
|
|||||||
@@ -175,9 +175,6 @@ export const IFrameHelper = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
setBubbleLabel(message) {
|
setBubbleLabel(message) {
|
||||||
if (window.$chatwoot.hideMessageBubble) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setBubbleText(window.$chatwoot.launcherTitle || message.label);
|
setBubbleText(window.$chatwoot.launcherTitle || message.label);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -263,9 +260,9 @@ export const IFrameHelper = {
|
|||||||
if (IFrameHelper.getBubbleHolder().length) {
|
if (IFrameHelper.getBubbleHolder().length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
createBubbleHolder();
|
createBubbleHolder(window.$chatwoot.hideMessageBubble);
|
||||||
onLocationChangeListener();
|
onLocationChangeListener();
|
||||||
if (!window.$chatwoot.hideMessageBubble) {
|
|
||||||
let className = 'woot-widget-bubble';
|
let className = 'woot-widget-bubble';
|
||||||
let closeBtnClassName = `woot-elements--${window.$chatwoot.position} woot-widget-bubble woot--close woot--hide`;
|
let closeBtnClassName = `woot-elements--${window.$chatwoot.position} woot-widget-bubble woot--close woot--hide`;
|
||||||
|
|
||||||
@@ -289,7 +286,6 @@ export const IFrameHelper = {
|
|||||||
bubbleHolder.appendChild(closeBubble);
|
bubbleHolder.appendChild(closeBubble);
|
||||||
bubbleHolder.appendChild(createNotificationBubble());
|
bubbleHolder.appendChild(createNotificationBubble());
|
||||||
onClickChatBubble();
|
onClickChatBubble();
|
||||||
}
|
|
||||||
},
|
},
|
||||||
toggleCloseButton: () => {
|
toggleCloseButton: () => {
|
||||||
let isMobile = false;
|
let isMobile = false;
|
||||||
|
|||||||
@@ -39,7 +39,10 @@ export const createBubbleIcon = ({ className, src, target }) => {
|
|||||||
return target;
|
return target;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createBubbleHolder = () => {
|
export const createBubbleHolder = hideMessageBubble => {
|
||||||
|
if (hideMessageBubble) {
|
||||||
|
addClass(bubbleHolder, 'woot-hidden');
|
||||||
|
}
|
||||||
addClass(bubbleHolder, 'woot--bubble-holder');
|
addClass(bubbleHolder, 'woot--bubble-holder');
|
||||||
body.appendChild(bubbleHolder);
|
body.appendChild(bubbleHolder);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -227,4 +227,8 @@ export const SDK_CSS = `
|
|||||||
width: 400px !important;
|
width: 400px !important;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.woot-hidden {
|
||||||
|
display: none !important;
|
||||||
|
}
|
||||||
`;
|
`;
|
||||||
|
|||||||
1
app/javascript/shared/constants/sharedFrameEvents.js
Normal file
1
app/javascript/shared/constants/sharedFrameEvents.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export const SDK_SET_BUBBLE_VISIBILITY = 'sdk-set-bubble-visibility';
|
||||||
@@ -38,6 +38,9 @@ import {
|
|||||||
ON_CAMPAIGN_MESSAGE_CLICK,
|
ON_CAMPAIGN_MESSAGE_CLICK,
|
||||||
ON_UNREAD_MESSAGE_CLICK,
|
ON_UNREAD_MESSAGE_CLICK,
|
||||||
} from './constants/widgetBusEvents';
|
} from './constants/widgetBusEvents';
|
||||||
|
|
||||||
|
import { SDK_SET_BUBBLE_VISIBILITY } from '../shared/constants/sharedFrameEvents';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'App',
|
name: 'App',
|
||||||
components: {
|
components: {
|
||||||
@@ -103,6 +106,7 @@ export default {
|
|||||||
'setAppConfig',
|
'setAppConfig',
|
||||||
'setReferrerHost',
|
'setReferrerHost',
|
||||||
'setWidgetColor',
|
'setWidgetColor',
|
||||||
|
'setBubbleVisibility',
|
||||||
]),
|
]),
|
||||||
...mapActions('conversation', ['fetchOldConversations', 'setUserLastSeen']),
|
...mapActions('conversation', ['fetchOldConversations', 'setUserLastSeen']),
|
||||||
...mapActions('campaign', [
|
...mapActions('campaign', [
|
||||||
@@ -285,6 +289,8 @@ export default {
|
|||||||
if (!message.isOpen) {
|
if (!message.isOpen) {
|
||||||
this.resetCampaign();
|
this.resetCampaign();
|
||||||
}
|
}
|
||||||
|
} else if (message.event === SDK_SET_BUBBLE_VISIBILITY) {
|
||||||
|
this.setBubbleVisibility(message.hideMessageBubble);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
SET_BUBBLE_VISIBILITY,
|
||||||
SET_REFERRER_HOST,
|
SET_REFERRER_HOST,
|
||||||
SET_WIDGET_APP_CONFIG,
|
SET_WIDGET_APP_CONFIG,
|
||||||
SET_WIDGET_COLOR,
|
SET_WIDGET_COLOR,
|
||||||
@@ -57,6 +58,9 @@ export const actions = {
|
|||||||
setReferrerHost({ commit }, referrerHost) {
|
setReferrerHost({ commit }, referrerHost) {
|
||||||
commit(SET_REFERRER_HOST, referrerHost);
|
commit(SET_REFERRER_HOST, referrerHost);
|
||||||
},
|
},
|
||||||
|
setBubbleVisibility({ commit }, hideMessageBubble) {
|
||||||
|
commit(SET_BUBBLE_VISIBILITY, hideMessageBubble);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const mutations = {
|
export const mutations = {
|
||||||
@@ -76,6 +80,9 @@ export const mutations = {
|
|||||||
[SET_REFERRER_HOST]($state, referrerHost) {
|
[SET_REFERRER_HOST]($state, referrerHost) {
|
||||||
$state.referrerHost = referrerHost;
|
$state.referrerHost = referrerHost;
|
||||||
},
|
},
|
||||||
|
[SET_BUBBLE_VISIBILITY]($state, hideMessageBubble) {
|
||||||
|
$state.hideMessageBubble = hideMessageBubble;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
@@ -11,6 +11,13 @@ describe('#actions', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#setBubbleVisibility', () => {
|
||||||
|
it('creates actions properly', () => {
|
||||||
|
actions.setBubbleVisibility({ commit }, false);
|
||||||
|
expect(commit.mock.calls).toEqual([['SET_BUBBLE_VISIBILITY', false]]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#setWidgetColor', () => {
|
describe('#setWidgetColor', () => {
|
||||||
it('creates actions properly', () => {
|
it('creates actions properly', () => {
|
||||||
actions.setWidgetColor({ commit }, '#eaeaea');
|
actions.setWidgetColor({ commit }, '#eaeaea');
|
||||||
|
|||||||
@@ -9,6 +9,14 @@ describe('#mutations', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#SET_BUBBLE_VISIBILITY', () => {
|
||||||
|
it('sets bubble visibility properly', () => {
|
||||||
|
const state = { hideMessageBubble: false };
|
||||||
|
mutations.SET_BUBBLE_VISIBILITY(state, true);
|
||||||
|
expect(state.hideMessageBubble).toEqual(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#SET_WIDGET_COLOR', () => {
|
describe('#SET_WIDGET_COLOR', () => {
|
||||||
it('sets widget color properly', () => {
|
it('sets widget color properly', () => {
|
||||||
const state = { widgetColor: '' };
|
const state = { widgetColor: '' };
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ export const SET_WIDGET_COLOR = 'SET_WIDGET_COLOR';
|
|||||||
export const UPDATE_CONVERSATION_ATTRIBUTES = 'UPDATE_CONVERSATION_ATTRIBUTES';
|
export const UPDATE_CONVERSATION_ATTRIBUTES = 'UPDATE_CONVERSATION_ATTRIBUTES';
|
||||||
export const TOGGLE_WIDGET_OPEN = 'TOGGLE_WIDGET_OPEN';
|
export const TOGGLE_WIDGET_OPEN = 'TOGGLE_WIDGET_OPEN';
|
||||||
export const SET_REFERRER_HOST = 'SET_REFERRER_HOST';
|
export const SET_REFERRER_HOST = 'SET_REFERRER_HOST';
|
||||||
|
export const SET_BUBBLE_VISIBILITY = 'SET_BUBBLE_VISIBILITY';
|
||||||
|
|||||||
Reference in New Issue
Block a user