2019-10-29 07:20:54 +00:00
|
|
|
<template>
|
2022-01-12 10:55:27 +00:00
|
|
|
<div
|
|
|
|
v-if="!conversationSize && isFetchingList"
|
|
|
|
class="flex flex-1 items-center h-full bg-black-25 justify-center"
|
|
|
|
>
|
|
|
|
<spinner size="" />
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
v-else
|
|
|
|
class="flex flex-col justify-end h-full"
|
|
|
|
:class="{
|
|
|
|
'is-mobile': isMobile,
|
|
|
|
'is-widget-right': isRightAligned,
|
|
|
|
'is-bubble-hidden': hideMessageBubble,
|
2022-02-25 10:48:18 +00:00
|
|
|
'is-flat-design': isWidgetStyleFlat,
|
2022-01-12 10:55:27 +00:00
|
|
|
}"
|
|
|
|
>
|
2022-06-10 13:59:52 +00:00
|
|
|
<router-view />
|
2022-01-12 10:55:27 +00:00
|
|
|
</div>
|
2019-10-29 07:20:54 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-01-12 10:55:27 +00:00
|
|
|
import { mapGetters, mapActions } 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-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';
|
2022-01-12 10:55:27 +00:00
|
|
|
import Spinner from 'shared/components/Spinner.vue';
|
|
|
|
import routerMixin from './mixins/routerMixin';
|
2021-11-22 09:35:29 +00:00
|
|
|
import {
|
|
|
|
getExtraSpaceToScroll,
|
|
|
|
loadedEventConfig,
|
|
|
|
} from './helpers/IframeEventHelper';
|
2022-01-12 10:55:27 +00:00
|
|
|
import {
|
|
|
|
ON_AGENT_MESSAGE_RECEIVED,
|
|
|
|
ON_CAMPAIGN_MESSAGE_CLICK,
|
|
|
|
ON_UNREAD_MESSAGE_CLICK,
|
|
|
|
} from './constants/widgetBusEvents';
|
2022-05-06 14:00:48 +00:00
|
|
|
|
|
|
|
import { SDK_SET_BUBBLE_VISIBILITY } from '../shared/constants/sharedFrameEvents';
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
export default {
|
|
|
|
name: 'App',
|
2020-07-07 18:34:44 +00:00
|
|
|
components: {
|
2022-01-12 10:55:27 +00:00
|
|
|
Spinner,
|
2020-07-07 18:34:44 +00:00
|
|
|
},
|
2022-01-12 10:55:27 +00:00
|
|
|
mixins: [availabilityMixin, configMixin, routerMixin],
|
2020-03-07 18:09:41 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
isMobile: false,
|
|
|
|
};
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
2022-01-12 10:55:27 +00:00
|
|
|
activeCampaign: 'campaign/getActiveCampaign',
|
|
|
|
campaigns: 'campaign/getCampaigns',
|
|
|
|
conversationSize: 'conversation/getConversationSize',
|
|
|
|
currentUser: 'contacts/getCurrentUser',
|
2020-07-07 18:34:44 +00:00
|
|
|
hasFetched: 'agent/getHasFetched',
|
2022-01-12 10:55:27 +00:00
|
|
|
hideMessageBubble: 'appConfig/getHideMessageBubble',
|
|
|
|
isFetchingList: 'conversation/getIsFetchingList',
|
|
|
|
isRightAligned: 'appConfig/isRightAligned',
|
|
|
|
isWidgetOpen: 'appConfig/getIsWidgetOpen',
|
2021-06-15 14:39:42 +00:00
|
|
|
messageCount: 'conversation/getMessageCount',
|
2020-07-07 18:34:44 +00:00
|
|
|
unreadMessageCount: 'conversation/getUnreadMessageCount',
|
2022-02-25 10:48:18 +00:00
|
|
|
isWidgetStyleFlat: 'appConfig/isWidgetStyleFlat',
|
2020-07-07 18:34:44 +00:00
|
|
|
}),
|
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();
|
|
|
|
},
|
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
mounted() {
|
2022-01-12 10:55:27 +00:00
|
|
|
const { websiteToken, locale, widgetColor } = window.chatwootWebChannel;
|
2020-05-17 10:45:53 +00:00
|
|
|
this.setLocale(locale);
|
2022-01-12 10:55:27 +00:00
|
|
|
this.setWidgetColor(widgetColor);
|
2022-06-23 10:18:56 +00:00
|
|
|
setHeader(window.authToken);
|
2020-08-28 12:09:46 +00:00
|
|
|
if (this.isIFrame) {
|
|
|
|
this.registerListeners();
|
|
|
|
this.sendLoadedEvent();
|
|
|
|
} else {
|
|
|
|
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-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: {
|
2022-01-12 10:55:27 +00:00
|
|
|
...mapActions('appConfig', [
|
|
|
|
'setAppConfig',
|
|
|
|
'setReferrerHost',
|
|
|
|
'setWidgetColor',
|
2022-05-06 14:00:48 +00:00
|
|
|
'setBubbleVisibility',
|
2022-01-12 10:55:27 +00:00
|
|
|
]),
|
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']),
|
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
|
|
|
registerUnreadEvents() {
|
2022-03-31 12:05:39 +00:00
|
|
|
bus.$on(ON_AGENT_MESSAGE_RECEIVED, () => {
|
|
|
|
const { name: routeName } = this.$route;
|
|
|
|
if (this.isWidgetOpen && routeName === 'messages') {
|
|
|
|
this.$store.dispatch('conversation/setUserLastSeen');
|
|
|
|
}
|
|
|
|
this.setUnreadView();
|
|
|
|
});
|
2022-01-12 10:55:27 +00:00
|
|
|
bus.$on(ON_UNREAD_MESSAGE_CLICK, () => {
|
|
|
|
this.replaceRoute('messages').then(() => this.unsetUnreadView());
|
2020-07-07 18:34:44 +00:00
|
|
|
});
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
registerCampaignEvents() {
|
2022-01-12 10:55:27 +00:00
|
|
|
bus.$on(ON_CAMPAIGN_MESSAGE_CLICK, () => {
|
2022-04-19 07:17:29 +00:00
|
|
|
if (this.shouldShowPreChatForm) {
|
2022-01-12 10:55:27 +00:00
|
|
|
this.replaceRoute('prechat-form');
|
|
|
|
} else {
|
|
|
|
this.replaceRoute('messages');
|
2022-04-20 05:19:52 +00:00
|
|
|
bus.$emit('execute-campaign', { campaignId: this.activeCampaign.id });
|
2021-11-11 13:32:16 +00:00
|
|
|
}
|
2022-01-12 10:55:27 +00:00
|
|
|
this.unsetUnreadView();
|
2021-11-11 13:32:16 +00:00
|
|
|
});
|
2022-04-20 05:19:52 +00:00
|
|
|
bus.$on('execute-campaign', campaignDetails => {
|
|
|
|
const { customAttributes, campaignId } = campaignDetails;
|
2021-11-11 13:32:16 +00:00
|
|
|
const { websiteToken } = window.chatwootWebChannel;
|
2022-04-20 05:19:52 +00:00
|
|
|
this.executeCampaign({ campaignId, websiteToken, customAttributes });
|
2022-01-12 10:55:27 +00:00
|
|
|
this.replaceRoute('messages');
|
2021-06-15 14:39:42 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
setCampaignView() {
|
|
|
|
const { messageCount, activeCampaign } = this;
|
|
|
|
const isCampaignReadyToExecute =
|
2022-01-12 10:55:27 +00:00
|
|
|
!isEmptyObject(activeCampaign) && !messageCount;
|
2021-06-15 14:39:42 +00:00
|
|
|
if (this.isIFrame && isCampaignReadyToExecute) {
|
2022-01-12 10:55:27 +00:00
|
|
|
this.replaceRoute('campaigns').then(() => {
|
2022-01-12 20:27:16 +00:00
|
|
|
this.setIframeHeight(true);
|
2022-01-12 10:55:27 +00:00
|
|
|
IFrameHelper.sendMessage({ event: 'setUnreadMode' });
|
2021-06-15 14:39:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
setUnreadView() {
|
|
|
|
const { unreadMessageCount } = this;
|
2022-03-31 12:05:39 +00:00
|
|
|
|
2022-01-12 10:55:27 +00:00
|
|
|
if (this.isIFrame && unreadMessageCount > 0 && !this.isWidgetOpen) {
|
|
|
|
this.replaceRoute('unread-messages').then(() => {
|
2022-01-12 20:27:16 +00:00
|
|
|
this.setIframeHeight(true);
|
2022-01-12 10:55:27 +00:00
|
|
|
IFrameHelper.sendMessage({ event: 'setUnreadMode' });
|
2020-07-07 18:34:44 +00:00
|
|
|
});
|
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' });
|
2022-01-12 20:27:16 +00:00
|
|
|
this.setIframeHeight(false);
|
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
|
|
|
if (
|
|
|
|
isWidgetTriggerEvent &&
|
2022-01-12 10:55:27 +00:00
|
|
|
['unread-messages', 'campaigns'].includes(this.$route.name)
|
2021-06-15 15:53:16 +00:00
|
|
|
) {
|
2020-07-07 18:34:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
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.fetchOldConversations().then(() => this.setUnreadView());
|
|
|
|
this.fetchAvailableAgents(websiteToken);
|
2022-01-12 10:55:27 +00:00
|
|
|
this.setAppConfig(message);
|
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') {
|
2022-01-12 10:55:27 +00:00
|
|
|
this.isMobile = message.isMobile;
|
2020-08-28 12:09:46 +00:00
|
|
|
} 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') {
|
2022-06-23 10:18:56 +00:00
|
|
|
this.$store.dispatch('contacts/setUser', message);
|
2020-08-28 12:09:46 +00:00
|
|
|
} 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();
|
2022-01-12 10:55:27 +00:00
|
|
|
} else if (message.event === 'toggle-open') {
|
|
|
|
this.$store.dispatch('appConfig/toggleWidgetOpen', message.isOpen);
|
|
|
|
|
2022-01-12 20:27:16 +00:00
|
|
|
const shouldShowMessageView =
|
2022-01-12 10:55:27 +00:00
|
|
|
['home'].includes(this.$route.name) &&
|
|
|
|
message.isOpen &&
|
2022-01-12 20:27:16 +00:00
|
|
|
this.messageCount;
|
|
|
|
const shouldShowHomeView =
|
|
|
|
!message.isOpen &&
|
|
|
|
['unread-messages', 'campaigns'].includes(this.$route.name);
|
|
|
|
|
|
|
|
if (shouldShowMessageView) {
|
2022-01-12 10:55:27 +00:00
|
|
|
this.replaceRoute('messages');
|
|
|
|
}
|
2022-01-12 20:27:16 +00:00
|
|
|
if (shouldShowHomeView) {
|
2022-01-12 10:55:27 +00:00
|
|
|
this.$store.dispatch('conversation/setUserLastSeen');
|
|
|
|
this.unsetUnreadView();
|
|
|
|
this.replaceRoute('home');
|
|
|
|
}
|
|
|
|
if (!message.isOpen) {
|
2021-11-11 13:32:16 +00:00
|
|
|
this.resetCampaign();
|
|
|
|
}
|
2022-05-06 14:00:48 +00:00
|
|
|
} else if (message.event === SDK_SET_BUBBLE_VISIBILITY) {
|
|
|
|
this.setBubbleVisibility(message.hideMessageBubble);
|
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>
|