if (!Object.assign) { Object.defineProperty(Object, 'assign', { enumerable: false, configurable: true, writable: true, value: function (target) { 'use strict'; if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object'); } var to = Object(target); for (var i = 1; i < arguments.length; i++) { var nextSource = arguments[i]; if (nextSource === undefined || nextSource === null) { continue; } nextSource = Object(nextSource); var keysArray = Object.keys(Object(nextSource)); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { var nextKey = keysArray[nextIndex]; var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); if (desc !== undefined && desc.enumerable) { to[nextKey] = nextSource[nextKey]; } } } return to; } }); } ; (function () { function getConfig(embedId, callback, callbackError) { var API = chat.helpers.getDomain() + '/1.0/api/embed/' + embedId; var xhr = new XMLHttpRequest(); xhr.open('GET', API); xhr.send(null); xhr.onreadystatechange = function () { var DONE = 4; var OK = 200; if (xhr.readyState === DONE) { if (xhr.status === OK) { var response = JSON.parse(xhr.responseText); callback(response); } } }; } var chat = { init: function (params) { this.insert.widget(this.template(), params); this.communication.messageReceiver(); this.handlers.widget(); }, actions: { applyParams: function (params) { var widgetButton = document.querySelector('#hi-chat-widget'); var chatContainer = document.querySelector('#hi-chat-container'); var notificationIcon = document.querySelector('#hi-chat-notification'); chat.container.classList.remove('TopLeft', 'TopCenter', 'TopRight', 'MiddleLeft', 'MiddleRight', 'BottomLeft', 'BottomCenter', 'BottomRight'); chat.container.classList.remove('None', 'Balloon', 'Circle', 'Box', 'Oval'); chat.container.classList.add(params.widgetConfiguration.position); chatContainer.classList.add(params.widgetConfiguration.minimizedConfiguration.shape); widgetButton.style.backgroundColor = params.widgetConfiguration.minimizedConfiguration.primaryColor; widgetButton.style.color = params.widgetConfiguration.minimizedConfiguration.primaryColor; notificationIcon.style.backgroundColor = params.widgetConfiguration.minimizedConfiguration.secondaryColor; notificationIcon.style.color = params.widgetConfiguration.minimizedConfiguration.textColor; if (params.widgetConfiguration.minimizedConfiguration.imageUrl && params.widgetConfiguration.minimizedConfiguration.shape !== 'None') { widgetButton.style.backgroundImage = 'url(' + params.widgetConfiguration.minimizedConfiguration.imageUrl + ')'; } if (params.widgetConfiguration.minimizedConfiguration.shape === 'None') { var imgInPage = document.querySelector('#img-widget') if (!imgInPage) { var img = document.createElement('img') img.style.display = 'none'; img.id = 'img-widget' widgetButton.style.backgroundImage = 'none' img.src = params.widgetConfiguration.minimizedConfiguration.imageUrl setTimeout(function () { widgetButton.insertAdjacentElement('afterbegin', img); img.style.display = 'inline'; }, 500); } } else if (params.widgetConfiguration.minimizedConfiguration.shape) { var img = document.querySelector('#img-widget') if (img) widgetButton.removeChild(img) } }, closeChat: function () { chat.control.chatIsClosed = true; }, elementMouseStart: function (px, py) { var dragabble = chat.control.dragabble; dragabble.mouse.x = px; dragabble.mouse.y = py; dragabble.posCachedValues.x = Number(chat.iFrame.offsetLeft); dragabble.posCachedValues.y = Number(chat.iFrame.offsetTop); }, elementMouseMove: function (px, py) { var dragabble = chat.control.dragabble; if (!chat.iFrame.classList.contains('isDragging')) { chat.iFrame.classList.add('isDragging') } dragabble.posCachedValues.x = dragabble.posCachedValues.x + px - dragabble.mouse.x; dragabble.posCachedValues.y = dragabble.posCachedValues.y + py - dragabble.mouse.y; if (dragabble.posCachedValues.x > 0) { chat.iFrame.style.left = dragabble.posCachedValues.x + 'px'; } if (dragabble.posCachedValues.y > 0) { chat.iFrame.style.top = dragabble.posCachedValues.y + 'px'; } }, elementMouseStop: function () { var dragabble = chat.control.dragabble; var limitX = window.innerHeight - chat.iFrame.offsetHeight - 10; var limitY = window.innerWidth - chat.iFrame.offsetWidth - 10; if (chat.iFrame.classList.contains('isDragging')) { chat.iFrame.classList.remove('isDragging') } if (dragabble.posCachedValues.x > limitX) { dragabble.posCachedValues.x = Number(chat.iFrame.offsetLeft); } if (dragabble.posCachedValues.y > limitY) { dragabble.posCachedValues.y = Number(chat.iFrame.offsetTop); } }, fillInitialSurvey: function (data) { if (!chat.control.chatIsMinimized) { var params = { departmentId: chat.control.departmentId, tenantId: chat.control.tenantId }; chat.control.initialSurveyData = data; /* chat.iFrame.src = chat.helpers.getUrlWithParams(params) */ } else { chat.control.initialSurveyData = data; } }, getDialogStatusLocalStorage: function () { var siteUrl = window.location.hostname; return window.localStorage.getItem('CHAT_WIDGET_DIALOG_' + siteUrl); }, getLocalStorageInfos: function () { var siteUrl = window.location.host; return { tenantId: window.localStorage.getItem('CHAT_WIDGET_TENANTID_' + siteUrl), departmentId: window.localStorage.getItem('CHAT_WIDGET_DEPARTMENTID_' + siteUrl), templateId: window.localStorage.getItem('CHAT_WIDGET_TEMPLATEID_' + siteUrl) } }, handleUnreadMessages: function (unreadMessages) { chat.control.unreadMessages = unreadMessages; chat.actions.verifyNotifications(); }, haveLocalStorageInfos: function () { var siteUrl = window.location.host; return window.localStorage.getItem('CHAT_WIDGET_TENANTID_' + siteUrl) !== null && window.localStorage.getItem('CHAT_WIDGET_DEPARTMENTID_' + siteUrl) !== null; }, insertParamsOnUrl: function (params) { return chat.helpers.getStaticDomain() + '?tenantId=' + params.tenantId + '&departmentId=' + params.departmentId; }, maximizeChat: function () { if (!chat.helpers.chatIsPreview()) { var iframe = document.querySelector('#hi-chat-conversation'); var assets = iframe.src.indexOf('loadingAssets') !== -1; if (assets) { chat.actions.reloadIframeWithParameters() } document.querySelector('#hi-chat-container').classList.remove('chatMinimized'); chat.iFrame.style.display = 'block' chat.control.chatIsMinimized = false; } }, minimizeChat: function () { document.querySelector('#hi-chat-container').classList.add('chatMinimized'); chat.control.chatIsMinimized = true; chat.control.unreadMessages = chat.helpers.chatIsPreview() ? 1 : 0; chat.actions.showWidgetButton(true); chat.actions.verifyNotifications(); }, mergeParams: function (params) { chat.control = Object.assign({}, chat.control, params); }, notOnDialog: function () { chat.control.chatIsOnDialog = false; setTimeout(function () { chat.actions.setDialogOffLocalStorage(); chat.actions.removeLocalStorageInfos(); }, 2000); }, onDialog: function () { window.Hi.Chat.isOnDialog = true chat.actions.setDialogOnLocalStorage(); chat.actions.setLocalStorageInfos( chat.control.tenantId, chat.control.departmentId, chat.control.templateId); }, openChat: function () { var timeout = 0; var iframe = document.querySelector('#hi-chat-conversation'); iframe.style.display = 'block' if (chat.control.chatNotStarted || chat.control.chatIsClosed) { chat.actions.reloadIframeWithParameters(); timeout = 1000; } setTimeout(function () { if (chat.control.chatIsClosed) { chat.actions.uncloseChat(); } chat.messages.maximizeChat(); chat.actions.maximizeChat(); }, timeout); }, removeLocalStorageInfos: function () { var siteUrl = window.location.host; window.localStorage.removeItem('CHAT_WIDGET_TENANTID_' + siteUrl); window.localStorage.removeItem('CHAT_WIDGET_DEPARTMENTID_' + siteUrl); window.localStorage.removeItem('CHAT_WIDGET_TEMPLATEID_' + siteUrl); }, receiveCommunicationApi: function () { var callbackFromUser = window[chat.helpers.getParams().callback]; // Using this to not override any method from other products if (window.Hi === undefined) { window.Hi = {}; } window.Hi.Chat = {}; window.Hi.Chat.quitDialog = chat.messages.quitDialog; window.Hi.Chat.isOnDialog = chat.control.chatIsOnDialog; window.Hi.Chat.fillSurvey = chat.actions.fillInitialSurvey; window.Hi.Chat.start = chat.actions.start; window.Hi.Chat.preview = chat.actions.previewWidget; window.Hi.Chat.quitQueue = chat.messages.quitQueue; window.Hi.Chat.status = chat.messages.chatStatus; window.Hi.Chat.hide = function () { chat.actions.showWidgetButton(false); }; window.Hi.Chat.show = function () { chat.actions.showWidgetButton(true); }; window.Hi.Chat.maximize = chat.actions.maximizeChat; window.Hi.Chat.minimize = chat.actions.minimizeChat; window.Hi.Chat.send = function () { chat.communication.messageSender('IS_CHAT_WIDGET'); } if (typeof callbackFromUser === 'function') { callbackFromUser(); } }, reloadIframeWithParameters: function () { chat.waiters.waitForParams(10000, function (params) { var iframe = document.querySelector('#hi-chat-conversation'); iframe.src = chat.helpers.getUrlWithParams(params); if (iframe) { iframe.contentWindow.location.replace(chat.helpers.getUrlWithParams(params)); } chat.control.chatNotStarted = false; }); }, setDialogOffLocalStorage: function () { var siteUrl = window.location.hostname; window.localStorage.setItem('CHAT_WIDGET_DIALOG_' + siteUrl, 'off'); }, setDialogOnLocalStorage: function () { var siteUrl = window.location.hostname; window.localStorage.setItem('CHAT_WIDGET_DIALOG_' + siteUrl, 'on'); }, setLocalStorageInfos: function (tenantId, departmentId, templateId) { var siteUrl = window.location.host; window.localStorage.setItem('CHAT_WIDGET_TENANTID_' + siteUrl, tenantId); window.localStorage.setItem('CHAT_WIDGET_DEPARTMENTID_' + siteUrl, departmentId); window.localStorage.setItem('CHAT_WIDGET_TEMPLATEID_' + siteUrl, templateId); }, start: function (data) { if (data) { chat.actions.fillInitialSurvey(data) } else { chat.actions.openChat(); } }, showWidgetButton: function (shouldShowButton) { if (shouldShowButton) { document.querySelector('#hi-chat-widget').classList.remove('hidden'); } else { document.querySelector('#hi-chat-widget').classList.add('hidden'); } }, verifyNotifications: function () { var notification = document.querySelector('#hi-chat-notification'); if (chat.control.unreadMessages) { notification.textContent = chat.control.unreadMessages; notification.style.display = 'block'; } else { notification.textContent = chat.control.unreadMessages; notification.style.display = 'none'; } }, uncloseChat: function () { chat.control.chatIsClosed = false; }, previewWidget: function (params) { chat.actions.applyParams(params); } }, communication: { messageReceiver: function () { window.addEventListener('message', function (event) { var type = event.data.type; var message = event.data.message; var payload = event.data.payload; if (type === 'HI_CHAT_WIDGET') { switch (message) { case 'CHAT_IS_READY': chat.messages.startWidget(); break; case 'CHAT_INITIALIZE_COMMUNICATION_API': chat.actions.receiveCommunicationApi(payload); break; case 'CHAT_IS_MINIMIZED': chat.actions.minimizeChat(); break; case 'CHAT_IS_MAXIMIZED': chat.actions.maximizeChat(); break; case 'CHAT_IS_CLOSED': chat.actions.closeChat(); break; case 'CHAT_DIALOG_END': chat.actions.notOnDialog(); break; case 'CHAT_DIALOG_START': chat.actions.onDialog(); break; case 'CHAT_DIALOG_UNREADMESSAGE': chat.actions.handleUnreadMessages(payload); break; case 'CHAT_DRAGSTART': chat.actions.elementMouseStart(payload.px, payload.py); break; case 'CHAT_DRAGGING': chat.actions.elementMouseMove(payload.px, payload.py); break; case 'CHAT_DRAGEND': chat.actions.elementMouseStop(); break; case 'CHAT_SET_STATUS': chat.control.currentStatus = payload break case 'CHAT_STATUS': window.postMessage({ type: 'HI_CHAT_STATUS', message: payload }, '*'); break; default: break; } } }); }, messageSender: function (message) { if (!chat.iFrame.contentWindow) return null; chat.iFrame.contentWindow.postMessage({ type: 'HI_CHAT_WIDGET', message: message }, '*'); } }, container: null, control: { chatIsClosed: false, chatIsMinimized: true, chatIsOnDialog: false, chatStatus: 'initialSurvey', chatNotStarted: true, departmentId: '', currentStatus: '', dragabble: { mouse: { x: 0, y: 0 }, posCachedValues: { x: 0, y: 0 } }, initialSurveyData: '', unreadMessages: 0, tenantId: '' }, handlers: { widget: function () { chat.waiters.waitForWidget(10000, function () { document.querySelector('#hi-chat-widget').addEventListener('click', function () { !chat.helpers.chatIsPreview() && chat.actions.openChat(); }); }); } }, helpers: { getDomain: function () { const url = document.querySelector('#hi-chat-script').getAttribute('src'); return url.indexOf('/static') !== -1 ? url.split('/static')[0] : url.split('/hi-chat')[0]; }, getEmbedId: function () { return chat.helpers.getParams().widgetId; }, getParams: function () { var paramsArr = document.querySelector('#hi-chat-script').getAttribute('src').split('?')[1].split('&'); var params = paramsArr.reduce(function (obj, item, index) { obj[item.split('=')[0]] = item.split('=')[1]; return obj; }, {}) return params; }, getRelativePath: function () { var arraySplited = document.querySelector('#hi-chat-script').getAttribute('src').split('/'); arraySplited.splice(-1, 1); return arraySplited.join('/'); }, getStaticDomain: function () { var url = document.querySelector('#hi-chat-script').getAttribute('src'); return url.indexOf('/static') !== -1 ? (url.split('/static')[0] + '/static/') : (url.split('/hi-chat')[0] + '/'); }, getUrlWithParams: function (params) { if (chat.control.initialSurveyData && chat.control.currentStatus !== 'QUEUE') { var initialData = chat.control.initialSurveyData.charAt(0) == '&' ? chat.control.initialSurveyData : '&' + chat.control.initialSurveyData; return chat.helpers.getDomain() + '/1.0/api/chat/automatic?tenantId=' + params.tenantId + '&departmentId=' + params.departmentId + (params.templateId ? '&templateId=' + params.templateId : '') + initialData; } else { return chat.helpers.getStaticDomain() + '?tenantId=' + params.tenantId + '&departmentId=' + params.departmentId + '&templateId=' + params.templateId; } }, chatIsPreview: function (params) { return chat.helpers.getParams().preview == 'true'; } }, iFrame: null, insert: { style: function () { var link = document.createElement('link'); link.rel = 'stylesheet'; link.href = chat.helpers.getRelativePath() + '/style.css'; document.head.insertAdjacentElement('beforeend', link); }, widget: function (template, params) { var container = document.createElement('div'); var widget = document.createElement('div'); var notifications = document.createElement('span') container.id = 'hi-chat-container'; widget.id = 'hi-chat-widget'; notifications.id = 'hi-chat-notification'; notifications.textContent = 5; notifications.style.display = 'none'; widget.insertAdjacentElement('beforeend', notifications); container.insertAdjacentElement('beforeend', template); container.insertAdjacentElement('afterbegin', widget); document.body.insertAdjacentElement('beforeend', container); chat.iFrame = document.querySelector('#hi-chat-conversation'); chat.container = document.querySelector('#hi-chat-container'); this.style(); chat.actions.mergeParams(params); chat.actions.applyParams(params); chat.actions.verifyNotifications(); } }, messages: { rebootChat: function () { chat.communication.messageSender('REBOOT_CHAT'); }, maximizeChat: function () { chat.communication.messageSender('MAXIMIZE_CHAT'); }, quitDialog: function () { chat.communication.messageSender('QUIT_DIALOG'); }, startWidget: function () { chat.communication.messageSender('IS_CHAT_WIDGET'); }, quitQueue: function () { chat.communication.messageSender('QUIT_QUEUE'); }, chatStatus: function () { chat.communication.messageSender('CHAT_STATUS'); } }, template: function () { var iframe = document.createElement('iframe'); iframe.style.display = 'none' iframe.id = 'hi-chat-conversation'; if (chat.actions.haveLocalStorageInfos()) { iframe.src = chat.helpers.getUrlWithParams(chat.actions.getLocalStorageInfos()); } else { // loadingAssets param is used to avoid empty requests to back-end iframe.src = chat.helpers.getStaticDomain() + '?loadingAssets=true'; } return iframe; }, waiters: { waitForParams: function (maxTimeoutToWait, callback) { var interval = setInterval(function () { if (chat.actions.haveLocalStorageInfos()) { clearInterval(interval); clearTimeout(timeout); return callback(chat.actions.getLocalStorageInfos()); } if ( chat.control.departmentId !== '' && chat.control.tenantId !== '') { clearInterval(interval); clearTimeout(timeout); var params = { departmentId: chat.control.departmentId, tenantId: chat.control.tenantId, templateId: chat.control.templateId }; callback(params); } }, 10); var timeout = setTimeout(function () { clearInterval(interval); }, maxTimeoutToWait); }, waitForScriptLoad: function (maxTimeoutToWait, callback) { var interval = setInterval(function () { if (document.querySelectorAll('#hi-chat-script').length) { clearInterval(interval); clearTimeout(timeout); callback(); } }, 10); var timeout = setTimeout(function () { clearInterval(interval); }, maxTimeoutToWait); }, waitForWidget: function (maxTimeoutToWait, callback) { var interval = setInterval(function () { if (document.querySelectorAll('#hi-chat-widget').length) { clearInterval(interval); clearTimeout(timeout); callback(); } }, 10); var timeout = setTimeout(function () { clearInterval(interval); }, maxTimeoutToWait); } } }; if (chat.helpers.chatIsPreview()) { chat.waiters.waitForScriptLoad(100, function () { chat.init({ "id": "", "tenantId": "", "departmentId": "", "templateId": "", "widgetConfiguration": { "position": "BottomRight", "action": "Widget", "minimizedConfiguration": { "shape": "Circle", "primaryColor": "#ffffff", "secondaryColor": "#d45151", "textColor": "#143647", "imageUrl": "" } } }); chat.actions.minimizeChat(); }) } else { chat.waiters.waitForScriptLoad(1000, function () { getConfig(chat.helpers.getEmbedId(), function (result) { chat.init(result); chat.actions.minimizeChat(); }); }) } })();