"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } var MAX_CHECK_COUNT = 5000; var PING_INTERVAL = 1000; var CHAT_WINDOW_MIN_WIDTH = 385; var CHAT_WINDOW_MAX_WIDTH = 450; var CHAT_WINDOW_MIN_HEIGHT = 350; var CHAT_WINDOW_MAX_HEIGHT = 800; var ACCOUNT_CODE = "e5100d999cc9"; var HTML_PAGE_CODE = '
'; var CANAL_CODE = "mf-zGGYksuiQWXgRQAay-mbionic-live"; /** * * @param {{tdCode: string, params?: any}} data * @returns {string} */ function messageDataToSting(data) { return JSON.stringify(data); } /** * * @param {string} data * @returns {{tdCode: string, params?: any}} */ function parseMessageData(data) { return typeof data === 'string' && data.includes('tdCode') ? JSON.parse(data) : { tdCode: 'UNKNOWN' }; } function isMobileDevice() { return navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i); } var TextDigoLiveChatClass = /*#__PURE__*/function () { function TextDigoLiveChatClass() { _classCallCheck(this, TextDigoLiveChatClass); /** * @type {HTMLIFrameElement} */ // this.IFRAME_ID = 'textdigo-live-chat'; this.iframeNode = null; this.isOpen = false; this.isReady = false; this.isInit = false; this.showIconLabel = false; this.heightAvailable = false; this.widthAvailable = false; this.openWidth = 0; this.openHeight = 0; this.currentRoute = 'start'; this.showWidget = true; this.visibleOnDesktop = true; this.visibleOnMobile = false; this.showWidgetWhenOffline = true; this.pagesWheresNotVisible = []; var pushState = history.pushState; var replaceState = history.replaceState; history.pushState = function () { pushState.apply(history, arguments); window.dispatchEvent(new Event('pushstate')); window.dispatchEvent(new Event('locationchange')); }; history.replaceState = function () { replaceState.apply(history, arguments); window.dispatchEvent(new Event('replacestate')); window.dispatchEvent(new Event('locationchange')); }; } _createClass(TextDigoLiveChatClass, [{ key: "_onResizeEventHandler", value: function _onResizeEventHandler() { this.heightAvailable = window.innerHeight - this.bottomPosition >= CHAT_WINDOW_MIN_HEIGHT; this.widthAvailable = window.innerWidth - this.rightPosition >= CHAT_WINDOW_MAX_WIDTH; this._updateChatPosition(); } }, { key: "_onSaveLocationChange", value: function _onSaveLocationChange() { this.iframeNode.contentWindow.postMessage(messageDataToSting({ tdCode: 'CHAT_LOCATION_HREF_CHANGE', params: { href: location.href, baseUrl: location.origin, accountCode: ACCOUNT_CODE, canalCode: CANAL_CODE } }), this.baseUrl); } }, { key: "_onLocationChangeEventHandler", value: function _onLocationChangeEventHandler() { this._updateChatPosition(); this._onSaveLocationChange(); } }, { key: "_onMessageEventHandler", value: function _onMessageEventHandler(msgEvent) { var _data$params$pagesWhe; var data = parseMessageData(msgEvent.data); switch (data.tdCode) { case 'CHAT_INIT_CONFIRM': this.isInit = true; break; case 'CHAT_READY': this.isReady = true; this.rightPosition = data.params.rightPosition; this.leftPosition = data.params.leftPosition; this.bottomPosition = data.params.bottomPosition; this.iconPosition = data.params.iconPosition; this.showIconLabel = data.params.showIconLabel; this.showWidget = data.params.showWidget; this.visibleOnDesktop = data.params.visibleOnDesktop; this.visibleOnMobile = data.params.visibleOnMobile; this.showWidgetWhenOffline = data.params.showWidgetWhenOffline; this.pagesWheresNotVisible = (_data$params$pagesWhe = data.params.pagesWheresNotVisible) !== null && _data$params$pagesWhe !== void 0 ? _data$params$pagesWhe : []; this._onChatReady(); this._onChatClose(); break; case 'CHAT_OPEN': this._onChatOpen(); break; case 'CHAT_CLOSE': this._onChatClose(); break; case 'CHAT_ROUTE_CHANGE': this.currentRoute = data.params.currentRoute; this._onRouteChange(); break; } } }, { key: "_updateChatPosition", value: function _updateChatPosition() { var rightPosition = this.widthAvailable || !this.isOpen ? "".concat(this.rightPosition, "px") : '-20px'; var leftPosition = this.widthAvailable ? "".concat(this.leftPosition, "px") : '10px'; var bottomPosition = this.heightAvailable ? "".concat(this.bottomPosition, "px") : '10px'; if (this.widthAvailable) { if (this.iconPosition === 'RIGHT') { this.iframeNode.style.right = rightPosition; this.openWidth = "calc(100% - ".concat(rightPosition, ")"); } else { this.iframeNode.style.left = leftPosition; this.openWidth = "calc(100% - ".concat(leftPosition, ")"); } } else { this.openWidth = "".concat(window.innerWidth + 10, "px"); if (this.iconPosition === 'RIGHT') { this.iframeNode.style.right = rightPosition; } else { this.iframeNode.style.left = leftPosition; } } this.iframeNode.style.bottom = bottomPosition; } }, { key: "_onChatReady", value: function _onChatReady() { var display = 'none'; if (this.showWidget && !this.pagesWheresNotVisible.includes(window.location.href) && !this.pagesWheresNotVisible.includes("".concat(window.location.origin).concat(window.location.pathname))) { if (isMobileDevice()) { if (this.visibleOnMobile) { display = 'initial'; } } else { if (this.visibleOnDesktop) { display = 'initial'; } } } if (display === 'initial') { this._updateChatPosition(); this.iframeNode.style.display = 'initial'; } } }, { key: "_onRouteChange", value: function _onRouteChange() { var bottomPosition = this.heightAvailable ? "".concat(this.bottomPosition, "px") : '10px'; this.openHeight = this.currentRoute === 'start' ? "".concat(CHAT_WINDOW_MIN_HEIGHT, "px") : "calc(calc(100% - ".concat(bottomPosition, ") - 26px)"); this.iframeNode.style.height = this.openHeight; } }, { key: "_onChatOpen", value: function _onChatOpen() { this.isOpen = true; this.iframeNode.style.width = this.openWidth; this.iframeNode.style.minWidth = this.widthAvailable ? "".concat(CHAT_WINDOW_MIN_WIDTH, "PX") : this.openWidth; this.iframeNode.style.height = this.openHeight; this.iframeNode.style.minHeight = "".concat(CHAT_WINDOW_MIN_HEIGHT, "px"); this.iframeNode.style.maxHeight = "".concat(CHAT_WINDOW_MAX_HEIGHT, "px"); this._updateChatPosition(); this.iframeNode.contentWindow.postMessage(messageDataToSting({ tdCode: 'CHAT_OPEN_CONFIRM' }), this.baseUrl); } }, { key: "_onChatClose", value: function _onChatClose() { this.isOpen = false; this.iframeNode.style.minWidth = '66px'; this.iframeNode.style.width = this.showIconLabel ? '270px' : '85px'; this.iframeNode.style.height = '66px'; this.iframeNode.style.minHeight = '0px'; this._updateChatPosition(); } }, { key: "_checkChatReady", value: function _checkChatReady() { var _this = this; var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; if (!this.isInit && this.iframeNode.contentWindow) { this.iframeNode.contentWindow.postMessage(messageDataToSting({ tdCode: 'CHAT_INIT', params: { accountCode: ACCOUNT_CODE, canalCode: CANAL_CODE, href: location.href, baseUrl: location.origin } }), this.baseUrl); if (index < MAX_CHECK_COUNT) { setTimeout(function () { _this._checkChatReady(index + 1); }, PING_INTERVAL); } } } }, { key: "init", value: function init() { var _this2 = this; this.rightPosition = 50; this.leftPosition = 50; this.bottomPosition = 45; this.iconPosition = 'RIGHT'; this.openWidth = "calc(100% - ".concat(this.rightPosition, ")"); this.openHeight = "calc(100% - ".concat(this.bottomPosition, ")"); this.iframeNode = document.createElement('iframe'); this.iframeNode.srcdoc = HTML_PAGE_CODE; this.iframeNode.setAttribute('title', 'Textdigo Chat'); this.iframeNode.setAttribute('style', "\n position: fixed;\n max-width: ".concat(CHAT_WINDOW_MAX_WIDTH, "px;\n border: 0;\n z-index: 5000;\n bottom: ").concat(this.bottomPosition, ";\n display: none;\n ")); document.getElementsByTagName('body').item(0).append(this.iframeNode); this.iframeNode.addEventListener('load', function () { _this2._onResizeEventHandler(); _this2._checkChatReady(); }); } }, { key: "closeView", value: function closeView() { this.iframeNode.contentWindow.postMessage('MINIMIZE_CHAT', this.baseUrl); } }]); return TextDigoLiveChatClass; }(); var textDigoChatLib = function () { var TextDigoLiveChat = new TextDigoLiveChatClass(); var onLocationChangeEventHandler = function onLocationChangeEventHandler() { TextDigoLiveChat._onLocationChangeEventHandler(); }; var onMessageEventHandler = function onMessageEventHandler(event) { TextDigoLiveChat._onMessageEventHandler(event); }; var onResizeEventHandler = function onResizeEventHandler(event) { TextDigoLiveChat._onResizeEventHandler(event); }; var onDOMContentLoaded = function onDOMContentLoaded() { textDigoChatLib.TextDigoLiveChat.init(); textDigoChatLib.attachEventsHandler(); }; return { TextDigoLiveChat: TextDigoLiveChat, start: function start() { if (document.readyState === 'loading') { window.addEventListener('DOMContentLoaded', onDOMContentLoaded); } else { onDOMContentLoaded(); } }, attachEventsHandler: function attachEventsHandler() { window.addEventListener('locationchange', onLocationChangeEventHandler); window.addEventListener('message', onMessageEventHandler); window.addEventListener('resize', onResizeEventHandler); }, removeEventsHandler: function removeEventsHandler() { window.removeEventListener('DOMContentLoaded', onDOMContentLoaded); window.removeEventListener('locationchange', onLocationChangeEventHandler); window.removeEventListener('message', onMessageEventHandler); window.removeEventListener('resize', onResizeEventHandler); } }; }(); textDigoChatLib.start();