2019-10-29 07:20:54 +00:00
|
|
|
<template>
|
2020-07-07 18:34:44 +00:00
|
|
|
<router
|
|
|
|
:show-unread-view="showUnreadView"
|
2021-06-15 14:39:42 +00:00
|
|
|
:show-campaign-view="showCampaignView"
|
2020-07-07 18:34:44 +00:00
|
|
|
:is-mobile="isMobile"
|
|
|
|
:has-fetched="hasFetched"
|
|
|
|
:unread-message-count="unreadMessageCount"
|
|
|
|
:is-left-aligned="isLeftAligned"
|
|
|
|
:hide-message-bubble="hideMessageBubble"
|
2020-08-28 12:09:46 +00:00
|
|
|
:show-popout-button="showPopoutButton"
|
2021-11-11 13:32:16 +00:00
|
|
|
:is-campaign-view-clicked="isCampaignViewClicked"
|
2020-07-07 18:34:44 +00:00
|
|
|
/>
|
2019-10-29 07:20:54 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-09-02 06:43:53 +00:00
|
|
|
import { mapGetters, mapActions, mapMutations } from 'vuex';
|
2020-01-17 08:06:05 +00:00
|
|
|
import { setHeader } from 'widget/helpers/axios';
|
2021-03-15 17:10:26 +00:00
|
|
|
import { IFrameHelper, RNHelper } from 'widget/helpers/utils';
|
2021-11-11 13:32:16 +00:00
|
|
|
import configMixin from './mixins/configMixin';
|
|
|
|
import availabilityMixin from 'widget/mixins/availability';
|
2020-07-07 18:34:44 +00:00
|
|
|
import Router from './views/Router';
|
2020-08-28 12:09:46 +00:00
|
|
|
import { getLocale } from './helpers/urlParamsHelper';
|
2021-06-15 14:39:42 +00:00
|
|
|
import { isEmptyObject } from 'widget/helpers/utils';
|
2021-11-22 09:35:29 +00:00
|
|
|
import {
|
|
|
|
getExtraSpaceToScroll,
|
|
|
|
loadedEventConfig,
|
|
|
|
} from './helpers/IframeEventHelper';
|
2019-10-29 07:20:54 +00:00
|
|
|
export default {
|
|
|
|
name: 'App',
|
2020-07-07 18:34:44 +00:00
|
|
|
components: {
|
|
|
|
Router,
|
|
|
|
},
|
2021-11-11 13:32:16 +00:00
|
|
|
mixins: [availabilityMixin, configMixin],
|
2020-03-07 18:09:41 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2020-07-07 18:34:44 +00:00
|
|
|
showUnreadView: false,
|
2021-06-15 14:39:42 +00:00
|
|
|
showCampaignView: false,
|
2020-03-07 18:09:41 +00:00
|
|
|
isMobile: false,
|
2020-07-07 18:34:44 +00:00
|
|
|
hideMessageBubble: false,
|
|
|
|
widgetPosition: 'right',
|
2020-08-28 12:09:46 +00:00
|
|
|
showPopoutButton: false,
|
2021-06-15 15:53:16 +00:00
|
|
|
isWebWidgetTriggered: false,
|
2021-11-11 13:32:16 +00:00
|
|
|
isCampaignViewClicked: false,
|
2021-10-14 06:21:00 +00:00
|
|
|
isWidgetOpen: false,
|
2020-03-07 18:09:41 +00:00
|
|
|
};
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
|
|
|
hasFetched: 'agent/getHasFetched',
|
2021-06-15 14:39:42 +00:00
|
|
|
messageCount: 'conversation/getMessageCount',
|
2020-07-07 18:34:44 +00:00
|
|
|
unreadMessageCount: 'conversation/getUnreadMessageCount',
|
2021-05-18 06:45:23 +00:00
|
|
|
campaigns: 'campaign/getCampaigns',
|
2021-06-15 14:39:42 +00:00
|
|
|
activeCampaign: 'campaign/getActiveCampaign',
|
2020-07-07 18:34:44 +00:00
|
|
|
}),
|
|
|
|
isLeftAligned() {
|
|
|
|
const isLeft = this.widgetPosition === 'left';
|
|
|
|
return isLeft;
|
|
|
|
},
|
2020-08-28 12:09:46 +00:00
|
|
|
isIFrame() {
|
|
|
|
return IFrameHelper.isIFrame();
|
|
|
|
},
|
2021-03-15 17:10:26 +00:00
|
|
|
isRNWebView() {
|
|
|
|
return RNHelper.isRNWebView();
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
watch: {
|
|
|
|
activeCampaign() {
|
|
|
|
this.setCampaignView();
|
|
|
|
},
|
2021-08-14 03:10:29 +00:00
|
|
|
showUnreadView(newVal) {
|
|
|
|
if (newVal) {
|
|
|
|
this.setIframeHeight(this.isMobile);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
showCampaignView(newVal) {
|
|
|
|
if (newVal) {
|
|
|
|
this.setIframeHeight(this.isMobile);
|
|
|
|
}
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
mounted() {
|
2020-05-06 08:08:36 +00:00
|
|
|
const { websiteToken, locale } = window.chatwootWebChannel;
|
2020-05-17 10:45:53 +00:00
|
|
|
this.setLocale(locale);
|
2020-08-28 12:09:46 +00:00
|
|
|
if (this.isIFrame) {
|
|
|
|
this.registerListeners();
|
|
|
|
this.sendLoadedEvent();
|
|
|
|
setHeader('X-Auth-Token', window.authToken);
|
|
|
|
} else {
|
2019-10-30 05:13:11 +00:00
|
|
|
setHeader('X-Auth-Token', window.authToken);
|
2020-08-28 12:09:46 +00:00
|
|
|
this.fetchOldConversations();
|
|
|
|
this.fetchAvailableAgents(websiteToken);
|
|
|
|
this.setLocale(getLocale(window.location.search));
|
2019-10-30 05:13:11 +00:00
|
|
|
}
|
2021-03-15 17:10:26 +00:00
|
|
|
if (this.isRNWebView) {
|
2021-03-06 05:47:38 +00:00
|
|
|
this.registerListeners();
|
2021-03-15 17:10:26 +00:00
|
|
|
this.sendRNWebViewLoadedEvent();
|
2021-03-06 05:47:38 +00:00
|
|
|
}
|
2021-06-30 15:39:44 +00:00
|
|
|
this.$store.dispatch('conversationAttributes/getAttributes');
|
2020-08-28 12:09:46 +00:00
|
|
|
this.setWidgetColor(window.chatwootWebChannel);
|
2020-07-07 18:34:44 +00:00
|
|
|
this.registerUnreadEvents();
|
2021-06-15 14:39:42 +00:00
|
|
|
this.registerCampaignEvents();
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
2020-01-01 17:00:43 +00:00
|
|
|
methods: {
|
2021-11-22 09:35:29 +00:00
|
|
|
...mapActions('appConfig', ['setWidgetColor', 'setReferrerHost']),
|
2020-07-07 18:34:44 +00:00
|
|
|
...mapActions('conversation', ['fetchOldConversations', 'setUserLastSeen']),
|
2021-11-11 13:32:16 +00:00
|
|
|
...mapActions('campaign', [
|
|
|
|
'initCampaigns',
|
|
|
|
'executeCampaign',
|
|
|
|
'resetCampaign',
|
|
|
|
]),
|
2020-02-05 05:57:22 +00:00
|
|
|
...mapActions('agent', ['fetchAvailableAgents']),
|
2021-09-02 06:43:53 +00:00
|
|
|
...mapMutations('events', ['toggleOpen']),
|
2020-01-01 17:00:43 +00:00
|
|
|
scrollConversationToBottom() {
|
|
|
|
const container = this.$el.querySelector('.conversation-wrap');
|
|
|
|
container.scrollTop = container.scrollHeight;
|
|
|
|
},
|
2020-08-09 10:37:32 +00:00
|
|
|
setBubbleLabel() {
|
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'setBubbleLabel',
|
|
|
|
label: this.$t('BUBBLE.LABEL'),
|
|
|
|
});
|
|
|
|
},
|
2021-08-07 06:26:15 +00:00
|
|
|
setIframeHeight(isFixedHeight) {
|
2021-08-14 03:10:29 +00:00
|
|
|
this.$nextTick(() => {
|
2021-11-22 09:35:29 +00:00
|
|
|
const extraHeight = getExtraSpaceToScroll();
|
2021-08-14 03:10:29 +00:00
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'updateIframeHeight',
|
|
|
|
isFixedHeight,
|
|
|
|
extraHeight,
|
|
|
|
});
|
2021-08-07 06:26:15 +00:00
|
|
|
});
|
|
|
|
},
|
2020-05-17 10:45:53 +00:00
|
|
|
setLocale(locale) {
|
|
|
|
const { enabledLanguages } = window.chatwootWebChannel;
|
|
|
|
if (enabledLanguages.some(lang => lang.iso_639_1_code === locale)) {
|
2020-12-12 06:38:36 +00:00
|
|
|
this.$root.$i18n.locale = locale;
|
2020-05-17 10:45:53 +00:00
|
|
|
}
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
setPosition(position) {
|
|
|
|
const widgetPosition = position || 'right';
|
|
|
|
this.widgetPosition = widgetPosition;
|
|
|
|
},
|
|
|
|
setHideMessageBubble(hideBubble) {
|
|
|
|
this.hideMessageBubble = !!hideBubble;
|
|
|
|
},
|
|
|
|
registerUnreadEvents() {
|
2021-10-14 06:21:00 +00:00
|
|
|
bus.$on('on-agent-message-received', () => {
|
|
|
|
if (!this.isIFrame || this.isWidgetOpen) {
|
2020-08-28 12:09:46 +00:00
|
|
|
this.setUserLastSeen();
|
|
|
|
}
|
|
|
|
this.setUnreadView();
|
|
|
|
});
|
2020-07-07 18:34:44 +00:00
|
|
|
bus.$on('on-unread-view-clicked', () => {
|
|
|
|
this.unsetUnreadView();
|
|
|
|
this.setUserLastSeen();
|
|
|
|
});
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
registerCampaignEvents() {
|
2021-11-11 13:32:16 +00:00
|
|
|
bus.$on('on-campaign-view-clicked', () => {
|
|
|
|
this.isCampaignViewClicked = true;
|
2021-06-15 14:39:42 +00:00
|
|
|
this.showCampaignView = false;
|
|
|
|
this.showUnreadView = false;
|
|
|
|
this.unsetUnreadView();
|
|
|
|
this.setUserLastSeen();
|
2021-11-11 13:32:16 +00:00
|
|
|
// Execute campaign only if pre-chat form (and require email too) is not enabled
|
|
|
|
if (
|
|
|
|
!(this.preChatFormEnabled && this.preChatFormOptions.requireEmail)
|
|
|
|
) {
|
|
|
|
bus.$emit('execute-campaign', this.activeCampaign.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
bus.$on('execute-campaign', campaignId => {
|
|
|
|
const { websiteToken } = window.chatwootWebChannel;
|
2021-06-15 14:39:42 +00:00
|
|
|
this.executeCampaign({ campaignId, websiteToken });
|
|
|
|
});
|
|
|
|
},
|
2021-11-11 13:32:16 +00:00
|
|
|
|
2020-08-28 12:09:46 +00:00
|
|
|
setPopoutDisplay(showPopoutButton) {
|
|
|
|
this.showPopoutButton = showPopoutButton;
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
setCampaignView() {
|
|
|
|
const { messageCount, activeCampaign } = this;
|
|
|
|
const isCampaignReadyToExecute =
|
2021-06-15 15:53:16 +00:00
|
|
|
!isEmptyObject(activeCampaign) &&
|
|
|
|
!messageCount &&
|
|
|
|
!this.isWebWidgetTriggered;
|
2021-06-15 14:39:42 +00:00
|
|
|
if (this.isIFrame && isCampaignReadyToExecute) {
|
|
|
|
this.showCampaignView = true;
|
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'setCampaignMode',
|
|
|
|
});
|
2021-08-07 06:26:15 +00:00
|
|
|
this.setIframeHeight(this.isMobile);
|
2021-06-15 14:39:42 +00:00
|
|
|
}
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
setUnreadView() {
|
|
|
|
const { unreadMessageCount } = this;
|
2020-08-28 12:09:46 +00:00
|
|
|
if (this.isIFrame && unreadMessageCount > 0) {
|
2020-07-07 18:34:44 +00:00
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'setUnreadMode',
|
|
|
|
unreadMessageCount,
|
|
|
|
});
|
2021-08-07 06:26:15 +00:00
|
|
|
this.setIframeHeight(this.isMobile);
|
2021-11-25 14:55:14 +00:00
|
|
|
this.handleUnreadNotificationDot();
|
2020-07-07 18:34:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
unsetUnreadView() {
|
2020-08-28 12:09:46 +00:00
|
|
|
if (this.isIFrame) {
|
2020-07-07 18:34:44 +00:00
|
|
|
IFrameHelper.sendMessage({ event: 'resetUnreadMode' });
|
2021-08-07 06:26:15 +00:00
|
|
|
this.setIframeHeight();
|
2021-11-25 14:55:14 +00:00
|
|
|
this.handleUnreadNotificationDot();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleUnreadNotificationDot() {
|
|
|
|
const { unreadMessageCount } = this;
|
|
|
|
if (this.isIFrame) {
|
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'handleNotificationDot',
|
|
|
|
unreadMessageCount,
|
|
|
|
});
|
2020-07-07 18:34:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
createWidgetEvents(message) {
|
|
|
|
const { eventName } = message;
|
|
|
|
const isWidgetTriggerEvent = eventName === 'webwidget.triggered';
|
2021-06-15 15:53:16 +00:00
|
|
|
this.isWebWidgetTriggered = true;
|
|
|
|
if (
|
|
|
|
isWidgetTriggerEvent &&
|
|
|
|
(this.showUnreadView || this.showCampaignView)
|
|
|
|
) {
|
2020-07-07 18:34:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setUserLastSeen();
|
|
|
|
this.$store.dispatch('events/create', { name: eventName });
|
|
|
|
},
|
2020-08-28 12:09:46 +00:00
|
|
|
registerListeners() {
|
|
|
|
const { websiteToken } = window.chatwootWebChannel;
|
|
|
|
window.addEventListener('message', e => {
|
|
|
|
if (!IFrameHelper.isAValidEvent(e)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const message = IFrameHelper.getMessage(e);
|
|
|
|
if (message.event === 'config-set') {
|
|
|
|
this.setLocale(message.locale);
|
|
|
|
this.setBubbleLabel();
|
|
|
|
this.setPosition(message.position);
|
|
|
|
this.fetchOldConversations().then(() => this.setUnreadView());
|
|
|
|
this.setPopoutDisplay(message.showPopoutButton);
|
|
|
|
this.fetchAvailableAgents(websiteToken);
|
|
|
|
this.setHideMessageBubble(message.hideMessageBubble);
|
2021-03-20 12:14:20 +00:00
|
|
|
this.$store.dispatch('contacts/get');
|
2020-08-28 12:09:46 +00:00
|
|
|
} else if (message.event === 'widget-visible') {
|
|
|
|
this.scrollConversationToBottom();
|
2021-05-17 16:08:35 +00:00
|
|
|
} else if (message.event === 'change-url') {
|
|
|
|
const { referrerURL, referrerHost } = message;
|
2021-10-12 12:28:33 +00:00
|
|
|
this.initCampaigns({
|
|
|
|
currentURL: referrerURL,
|
|
|
|
websiteToken,
|
|
|
|
isInBusinessHours: this.isInBusinessHours,
|
|
|
|
});
|
2021-05-17 16:08:35 +00:00
|
|
|
window.referrerURL = referrerURL;
|
2021-11-22 09:35:29 +00:00
|
|
|
this.setReferrerHost(referrerHost);
|
2020-08-28 12:09:46 +00:00
|
|
|
} else if (message.event === 'toggle-close-button') {
|
|
|
|
this.isMobile = message.showClose;
|
|
|
|
} else if (message.event === 'push-event') {
|
|
|
|
this.createWidgetEvents(message);
|
|
|
|
} else if (message.event === 'set-label') {
|
|
|
|
this.$store.dispatch('conversationLabels/create', message.label);
|
|
|
|
} else if (message.event === 'remove-label') {
|
|
|
|
this.$store.dispatch('conversationLabels/destroy', message.label);
|
|
|
|
} else if (message.event === 'set-user') {
|
|
|
|
this.$store.dispatch('contacts/update', message);
|
|
|
|
} else if (message.event === 'set-custom-attributes') {
|
|
|
|
this.$store.dispatch(
|
|
|
|
'contacts/setCustomAttributes',
|
|
|
|
message.customAttributes
|
|
|
|
);
|
|
|
|
} else if (message.event === 'delete-custom-attribute') {
|
2021-11-15 09:26:35 +00:00
|
|
|
this.$store.dispatch(
|
|
|
|
'contacts/deleteCustomAttribute',
|
|
|
|
message.customAttribute
|
|
|
|
);
|
2020-08-28 12:09:46 +00:00
|
|
|
} else if (message.event === 'set-locale') {
|
|
|
|
this.setLocale(message.locale);
|
|
|
|
this.setBubbleLabel();
|
|
|
|
} else if (message.event === 'set-unread-view') {
|
|
|
|
this.showUnreadView = true;
|
2021-06-15 14:39:42 +00:00
|
|
|
this.showCampaignView = false;
|
2020-08-28 12:09:46 +00:00
|
|
|
} else if (message.event === 'unset-unread-view') {
|
2021-11-11 13:32:16 +00:00
|
|
|
// Reset campaign, If widget opened via clciking on bubble button
|
|
|
|
if (!this.isCampaignViewClicked) {
|
|
|
|
this.resetCampaign();
|
|
|
|
}
|
2020-08-28 12:09:46 +00:00
|
|
|
this.showUnreadView = false;
|
2021-06-15 14:39:42 +00:00
|
|
|
this.showCampaignView = false;
|
2021-11-25 14:55:14 +00:00
|
|
|
this.handleUnreadNotificationDot();
|
2021-09-02 06:43:53 +00:00
|
|
|
} else if (message.event === 'toggle-open') {
|
2021-10-14 06:21:00 +00:00
|
|
|
this.isWidgetOpen = message.isOpen;
|
2021-09-02 06:43:53 +00:00
|
|
|
this.toggleOpen();
|
2020-08-28 12:09:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
sendLoadedEvent() {
|
2021-11-22 09:35:29 +00:00
|
|
|
IFrameHelper.sendMessage(loadedEventConfig());
|
2020-08-28 12:09:46 +00:00
|
|
|
},
|
2021-03-15 17:10:26 +00:00
|
|
|
sendRNWebViewLoadedEvent() {
|
2021-11-22 09:35:29 +00:00
|
|
|
RNHelper.sendMessage(loadedEventConfig());
|
2021-08-14 03:10:29 +00:00
|
|
|
},
|
2020-01-01 17:00:43 +00:00
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
@import '~widget/assets/scss/woot.scss';
|
|
|
|
</style>
|