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"
|
2020-07-07 18:34:44 +00:00
|
|
|
/>
|
2019-10-29 07:20:54 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-07-07 18:34:44 +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';
|
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';
|
2020-10-14 18:41:47 +00:00
|
|
|
import { BUS_EVENTS } from 'shared/constants/busEvents';
|
2021-06-15 14:39:42 +00:00
|
|
|
import { isEmptyObject } from 'widget/helpers/utils';
|
2019-10-29 07:20:54 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'App',
|
2020-07-07 18:34:44 +00:00
|
|
|
components: {
|
|
|
|
Router,
|
|
|
|
},
|
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,
|
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();
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
}
|
2020-05-09 16:32:43 +00:00
|
|
|
this.$store.dispatch('conversationAttributes/get');
|
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: {
|
|
|
|
...mapActions('appConfig', ['setWidgetColor']),
|
2020-07-07 18:34:44 +00:00
|
|
|
...mapActions('conversation', ['fetchOldConversations', 'setUserLastSeen']),
|
2021-06-15 14:39:42 +00:00
|
|
|
...mapActions('campaign', ['initCampaigns', 'executeCampaign']),
|
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'),
|
|
|
|
});
|
|
|
|
},
|
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() {
|
2020-08-28 12:09:46 +00:00
|
|
|
bus.$on('on-agent-message-recieved', () => {
|
|
|
|
if (!this.isIFrame) {
|
|
|
|
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() {
|
|
|
|
bus.$on('on-campaign-view-clicked', campaignId => {
|
|
|
|
const { websiteToken } = window.chatwootWebChannel;
|
|
|
|
this.showCampaignView = false;
|
|
|
|
this.showUnreadView = false;
|
|
|
|
this.unsetUnreadView();
|
|
|
|
this.setUserLastSeen();
|
|
|
|
this.executeCampaign({ campaignId, websiteToken });
|
|
|
|
});
|
|
|
|
},
|
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 =
|
|
|
|
!isEmptyObject(activeCampaign) && !messageCount;
|
|
|
|
if (this.isIFrame && isCampaignReadyToExecute) {
|
|
|
|
this.showCampaignView = true;
|
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'setCampaignMode',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unsetUnreadView() {
|
2020-08-28 12:09:46 +00:00
|
|
|
if (this.isIFrame) {
|
2020-07-07 18:34:44 +00:00
|
|
|
IFrameHelper.sendMessage({ event: 'resetUnreadMode' });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
createWidgetEvents(message) {
|
|
|
|
const { eventName } = message;
|
|
|
|
const isWidgetTriggerEvent = eventName === 'webwidget.triggered';
|
|
|
|
if (isWidgetTriggerEvent && this.showUnreadView) {
|
|
|
|
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-06-15 14:39:42 +00:00
|
|
|
this.initCampaigns({ currentURL: referrerURL, websiteToken });
|
2021-05-17 16:08:35 +00:00
|
|
|
window.referrerURL = referrerURL;
|
|
|
|
bus.$emit(BUS_EVENTS.SET_REFERRER_HOST, 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') {
|
|
|
|
this.$store.dispatch('contacts/setCustomAttributes', {
|
|
|
|
[message.customAttribute]: null,
|
|
|
|
});
|
|
|
|
} 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') {
|
|
|
|
this.showUnreadView = false;
|
2021-06-15 14:39:42 +00:00
|
|
|
this.showCampaignView = false;
|
2020-08-28 12:09:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
sendLoadedEvent() {
|
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'loaded',
|
|
|
|
config: {
|
|
|
|
authToken: window.authToken,
|
|
|
|
channelConfig: window.chatwootWebChannel,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2021-03-15 17:10:26 +00:00
|
|
|
sendRNWebViewLoadedEvent() {
|
|
|
|
RNHelper.sendMessage({
|
|
|
|
event: 'loaded',
|
|
|
|
config: {
|
|
|
|
authToken: window.authToken,
|
|
|
|
channelConfig: window.chatwootWebChannel,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
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>
|