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"
|
|
|
|
: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';
|
|
|
|
import { IFrameHelper } 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';
|
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-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',
|
|
|
|
unreadMessageCount: 'conversation/getUnreadMessageCount',
|
|
|
|
}),
|
|
|
|
isLeftAligned() {
|
|
|
|
const isLeft = this.widgetPosition === 'left';
|
|
|
|
return isLeft;
|
|
|
|
},
|
2020-08-28 12:09:46 +00:00
|
|
|
isIFrame() {
|
|
|
|
return IFrameHelper.isIFrame();
|
|
|
|
},
|
2020-07-07 18:34:44 +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
|
|
|
}
|
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();
|
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)) {
|
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();
|
|
|
|
});
|
|
|
|
},
|
2020-08-28 12:09:46 +00:00
|
|
|
setPopoutDisplay(showPopoutButton) {
|
|
|
|
this.showPopoutButton = showPopoutButton;
|
|
|
|
},
|
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);
|
|
|
|
} else if (message.event === 'widget-visible') {
|
|
|
|
this.scrollConversationToBottom();
|
|
|
|
} else if (message.event === 'set-current-url') {
|
2020-10-14 18:41:47 +00:00
|
|
|
window.referrerURL = message.referrerURL;
|
|
|
|
bus.$emit(BUS_EVENTS.SET_REFERRER_HOST, message.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;
|
|
|
|
} else if (message.event === 'unset-unread-view') {
|
|
|
|
this.showUnreadView = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
sendLoadedEvent() {
|
|
|
|
IFrameHelper.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>
|