UNPKG

genesys-chat-rr

Version:

Class iteration chat genesys royalresorts.com

152 lines (124 loc) 4.66 kB
import {CONFIG_CHAT,FORMOPT} from "./utilities/variables"; class GenesysChat{ chatInstance; flagOpenChat = false; session = null; options = { "languageConfig":null, "formConfig":FORMOPT, } constructor(options) { this.options = options === null ? this.options:options; if(this.options.languageConfig !== null){ CONFIG_CHAT.widgets.main.i18n = this.options.languageConfig; } this.loadFileChat(); } initConfigurations(){ let instance = this.chatInstance; instance.subscribe('WebChat.ready',event =>{ this.common = window._genesys.widgets.common; }); instance.subscribe('App.i18n',evt =>{ if (typeof this.chatTitle !== "undefined") if (typeof evt.data.webchat !== "undefined") evt.data.webchat.ChatTitle = this.chatTitle; }); this.chatInstance.subscribe('WebChat.opened',event => { this.updateSession(); }); instance.subscribe('WebChat.started',event => { this.flagOpenChat = true; }); instance.subscribe('WebChat.completed',event => { this.flagOpenChat = false; this.updateSession(); this.closeChat(); }); instance.subscribe('WebChat.cancelled',event => { this.flagOpenChat = false; this.updateSession(); this.closeChat(); }); } startChat(channel,title){ if(!this.flagOpenChat && this.session === null){ this.setChannel(channel); this.setTitle(title); this.chatInstance.command('WebChat.open',this.options.formConfig); }else{ alert(language === "en" ? "Please, close your previously chat before open a new one.":"Por favor, termine su sesión previa de chat antes de iniciar una nueva."); } } closeChat(){ this.chatInstance.command('WebChat.close'); this.session = null; this.flagOpenChat = false; } endChat(){ this.chatInstance.command('WebChat.endChat'); this.flagOpenChat = false; } setChannel(channel){ window._genesys.widgets.webchat.transport.interactionData.routing.targetAddress = channel; } setTitle(title) { this.chatTitle = title; this.chatInstance.command("App.setLanguage",{"lang" :window.language}); } setValidation(){ this.options.formConfig.formJSON.inputs[0]["validate"] = (event, form, input, label, $, CXBus, Common) => { if (input){ return (/^[a-zA-ZáéíóúýÁÉÍÓÚñÑ\s]+$/).test(input.val()); }else{ return false; } }; this.options.formConfig.formJSON.inputs[1]["validate"] = (event, form, input, label, $, CXBus, Common) => { if (input){ return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/).test(input.val()); }else{ return false; } }; } getCookie(cName) { const name = cName + "="; const cDecoded = decodeURIComponent(document.cookie); //to be careful const cArr = cDecoded.split('; '); let res; cArr.forEach(val => { if (val.indexOf(name) === 0) res = val.substring(name.length); }) return res } updateSession(){ this.session = this.getCookie('_genesys.widgets.webchat.metaData'); if (typeof this.session === "undefined") this.session = null else this.session = JSON.parse(this.session); } loadFileChat(){ window._genesys = CONFIG_CHAT; const scriptChat = document.createElement('script'); scriptChat.src = 'https://apps.mypurecloud.com/widgets/9.0/cxbus.min.js'; scriptChat.onload = () => { CXBus.configure({debug:false,pluginsPath:'https://apps.mypurecloud.com/widgets/9.0/plugins/'}); CXBus.loadPlugin('widgets-core'); this.chatInstance = CXBus.registerPlugin('RoyalResort'); /*init settings*/ this.setValidation(); this.chatInstance.subscribe('App.ready', evt => { this.initConfigurations(); }); } document.querySelector('head').append(scriptChat); } getInstance(){ return this.chatInstance; } } export{ GenesysChat }