2019-10-29 07:20:54 +00:00
|
|
|
<template>
|
2020-07-07 18:34:44 +00:00
|
|
|
<router
|
|
|
|
:show-unread-view="showUnreadView"
|
|
|
|
:is-mobile="isMobile"
|
|
|
|
:grouped-messages="groupedMessages"
|
|
|
|
:unread-messages="unreadMessages"
|
|
|
|
:conversation-size="conversationSize"
|
|
|
|
:available-agents="availableAgents"
|
|
|
|
:has-fetched="hasFetched"
|
|
|
|
:conversation-attributes="conversationAttributes"
|
|
|
|
:unread-message-count="unreadMessageCount"
|
|
|
|
:is-left-aligned="isLeftAligned"
|
|
|
|
:hide-message-bubble="hideMessageBubble"
|
|
|
|
/>
|
2019-10-29 07:20:54 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-07-07 18:34:44 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import { mapGetters, mapActions } from 'vuex';
|
2020-01-17 08:06:05 +00:00
|
|
|
import { setHeader } from 'widget/helpers/axios';
|
|
|
|
import { IFrameHelper } from 'widget/helpers/utils';
|
2020-07-07 18:34:44 +00:00
|
|
|
|
|
|
|
import Router from './views/Router';
|
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,
|
2020-03-07 18:09:41 +00:00
|
|
|
isMobile: false,
|
2020-07-07 18:34:44 +00:00
|
|
|
hideMessageBubble: false,
|
|
|
|
widgetPosition: 'right',
|
2020-03-07 18:09:41 +00:00
|
|
|
};
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
|
|
|
groupedMessages: 'conversation/getGroupedConversation',
|
|
|
|
unreadMessages: 'conversation/getUnreadTextMessages',
|
|
|
|
conversationSize: 'conversation/getConversationSize',
|
|
|
|
availableAgents: 'agent/availableAgents',
|
|
|
|
hasFetched: 'agent/getHasFetched',
|
|
|
|
conversationAttributes: 'conversationAttributes/getConversationParams',
|
|
|
|
unreadMessageCount: 'conversation/getUnreadMessageCount',
|
|
|
|
}),
|
|
|
|
isLeftAligned() {
|
|
|
|
const isLeft = this.widgetPosition === 'left';
|
|
|
|
return isLeft;
|
|
|
|
},
|
|
|
|
},
|
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-05-06 08:08:36 +00:00
|
|
|
|
2019-10-30 05:13:11 +00:00
|
|
|
if (IFrameHelper.isIFrame()) {
|
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'loaded',
|
|
|
|
config: {
|
|
|
|
authToken: window.authToken,
|
2019-11-25 18:40:27 +00:00
|
|
|
channelConfig: window.chatwootWebChannel,
|
2019-10-30 05:13:11 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
setHeader('X-Auth-Token', window.authToken);
|
|
|
|
}
|
2019-11-25 18:40:27 +00:00
|
|
|
this.setWidgetColor(window.chatwootWebChannel);
|
2019-10-30 05:13:11 +00:00
|
|
|
|
|
|
|
window.addEventListener('message', e => {
|
2020-02-05 05:57:22 +00:00
|
|
|
const wootPrefix = 'chatwoot-widget:';
|
|
|
|
const isDataNotString = typeof e.data !== 'string';
|
|
|
|
const isNotFromWoot = isDataNotString || e.data.indexOf(wootPrefix) !== 0;
|
|
|
|
|
|
|
|
if (isNotFromWoot) return;
|
|
|
|
|
|
|
|
const message = JSON.parse(e.data.replace(wootPrefix, ''));
|
2019-10-30 05:13:11 +00:00
|
|
|
if (message.event === 'config-set') {
|
2020-08-09 10:37:32 +00:00
|
|
|
this.setLocale(message.locale);
|
|
|
|
this.setBubbleLabel();
|
|
|
|
this.setPosition(message.position);
|
2020-07-07 18:34:44 +00:00
|
|
|
this.fetchOldConversations().then(() => {
|
|
|
|
this.setUnreadView();
|
|
|
|
});
|
2020-02-05 05:57:22 +00:00
|
|
|
this.fetchAvailableAgents(websiteToken);
|
2020-07-07 18:34:44 +00:00
|
|
|
this.setHideMessageBubble(message.hideMessageBubble);
|
2019-11-10 17:28:55 +00:00
|
|
|
} else if (message.event === 'widget-visible') {
|
|
|
|
this.scrollConversationToBottom();
|
2020-01-01 17:00:43 +00:00
|
|
|
} else if (message.event === 'set-current-url') {
|
|
|
|
window.refererURL = message.refererURL;
|
2020-03-07 18:09:41 +00:00
|
|
|
} else if (message.event === 'toggle-close-button') {
|
|
|
|
this.isMobile = message.showClose;
|
2020-04-10 11:12:37 +00:00
|
|
|
} else if (message.event === 'push-event') {
|
2020-07-07 18:34:44 +00:00
|
|
|
this.createWidgetEvents(message);
|
2020-04-03 07:34:58 +00:00
|
|
|
} 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);
|
2020-08-21 14:00:27 +00:00
|
|
|
} 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,
|
|
|
|
});
|
2020-05-17 10:45:53 +00:00
|
|
|
} else if (message.event === 'set-locale') {
|
|
|
|
this.setLocale(message.locale);
|
2020-08-09 10:37:32 +00:00
|
|
|
this.setBubbleLabel();
|
2020-07-07 18:34:44 +00:00
|
|
|
} else if (message.event === 'set-unread-view') {
|
|
|
|
this.showUnreadView = true;
|
|
|
|
} else if (message.event === 'unset-unread-view') {
|
|
|
|
this.showUnreadView = false;
|
2019-10-30 05:13:11 +00:00
|
|
|
}
|
|
|
|
});
|
2020-05-09 16:32:43 +00:00
|
|
|
|
|
|
|
this.$store.dispatch('conversationAttributes/get');
|
2020-07-07 18:34:44 +00:00
|
|
|
this.registerUnreadEvents();
|
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']),
|
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)) {
|
|
|
|
Vue.config.lang = locale;
|
|
|
|
}
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
setPosition(position) {
|
|
|
|
const widgetPosition = position || 'right';
|
|
|
|
this.widgetPosition = widgetPosition;
|
|
|
|
},
|
|
|
|
setHideMessageBubble(hideBubble) {
|
|
|
|
this.hideMessageBubble = !!hideBubble;
|
|
|
|
},
|
|
|
|
registerUnreadEvents() {
|
|
|
|
bus.$on('on-agent-message-recieved', () => this.setUnreadView());
|
|
|
|
bus.$on('on-unread-view-clicked', () => {
|
|
|
|
this.unsetUnreadView();
|
|
|
|
this.setUserLastSeen();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setUnreadView() {
|
|
|
|
const { unreadMessageCount } = this;
|
|
|
|
if (IFrameHelper.isIFrame() && unreadMessageCount > 0) {
|
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'setUnreadMode',
|
|
|
|
unreadMessageCount,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unsetUnreadView() {
|
|
|
|
if (IFrameHelper.isIFrame()) {
|
|
|
|
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-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>
|