(function (window, document, GPTConfig) { var GPT = GPT || {}; GPTConfig.callbacksTemplate = function () { return { before: [], response: { success: [], error: [] }, ajax: { done: [], fail: [], always: [] } } }; GPT.Callbacks = { Chat: { addComment: GPTConfig.callbacksTemplate(), loadHistory: GPTConfig.callbacksTemplate(), } } GPT.Callbacks.add = function (path, name, func) { if (typeof func != 'function') { return false; } path = path.split('.'); var obj = GPT.Callbacks; for (var i = 0; i < path.length; i++) { if (obj[path[i]] == undefined) { return false; } obj = obj[path[i]]; } if (typeof obj != 'object') { obj = [obj]; } if (name != undefined) { obj[name] = func; } else { obj.push(func); } return true; } GPT.setup = function () { $ = jQuery; this.btn; this.$doc = $(document); this.$body = this.$doc.find('body'); this.ajaxProgress = false; this.response; this.container = '#gpt_container'; this.sendData = { $form: null, action: null, formData: null }; }; GPT.load = { config: {}, script: function (url, callback) { var script = document.createElement('script'); if (typeof callback !== 'undefined'){ if (script.readyState){ //IE script.onreadystatechange = function(){ if (script.readyState == 'loaded' || script.readyState == 'complete'){ script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function(){ callback(); }; } } script.src = url + '?v=' + GPT.Utils.uniqueid(); document.body.appendChild(script); }, style: function (url) { var link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css' link.href = url + '?v=' + GPT.Utils.uniqueid(); document.getElementsByTagName('head')[0].appendChild(link); }, scripts: function () { this.isLoad = true; for (let library in this.config.scripts){ let libdata = this.config.scripts[library]; if (libdata.depend && typeof window[libdata.depend] === 'undefined') continue; let check = typeof (libdata.depend === 'jQuery' ? jQuery.fn[library] : window[library]); if (check === 'undefined'){ this.isLoad = false; if (libdata.loading !== true) { libdata.loading = true; if (libdata.style) this.style(libdata.style); this.script(libdata.url); } } } if (this.isLoad){ GPT.Initialize.config(); } else { setTimeout(function() { GPT.load.scripts(); }, 50); } }, styles: function () { for (let library in this.config.styles){ this.style(this.config.styles[library]); } }, svg: function (url, event) { GPT.Loader.create(); var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.onload = function (e) { if (xhr.readyState === 4) { if (xhr.status !== 200) console.error(xhr.statusText); GPT.loadData = xhr.responseXML.querySelector('svg').outerHTML; if (event) event(); GPT.Loader.destroy(); } }; xhr.onerror = function (e) { GPT.Message.log(xhr.statusText); }; xhr.send(null); }, } GPT.Event = { list: {}, on: function (event, func) { this.list[event] = func; }, start: function (name) { if (this.list[name] && typeof this.list[name] === 'function') this.list[name](); }, } GPT.Initialize = { events: {}, load: function () { GPT.load.scripts(); GPT.load.styles(); }, config: function () { GPT.setup(); var callbacks = GPTConfig.callbacksTemplate(); callbacks.response.success = function (response) { if (response.data.timeout) GPT.Push.config.timeout = response.data.timeout; if (response.data.message) GPT.Push.config.message = response.data.message; if (response.data.push_type) GPT.Push.config.type = response.data.push_type; if (response.data.tooltips) GPT.Chat.config.tooltips = response.data.tooltips; GPT.Initialize.start(); } GPT.Request.toSystem({ url: document.URL, action: 'getConfig', }, callbacks); if (GPT.Chat.reaction) { for (let i in GPT.Chat.reaction){ var item = GPT.Chat.reaction[i]; if (!item.src) continue; GPT.Utils.preLoadVideo(item); } } }, start: function () { GPT.Chat.initialize(); GPT.Push.initialize(); }, } GPT.Chat = { config: { moduleId: 'gpt', limitChars: 2000, timeLimit: 30, action: '', type: 'popup', resize: true, }, initialize: function () { if (this.module) return; this.setSession(); if (GPTConfig.container && $(GPTConfig.container).length) { this.module = $(GPTConfig.container); this.config.type = 'inline'; this.config.resize = false; } else { this.module = $('
', {id: this.config.moduleId}).appendTo(GPT.$body); } this.icon = $('
', {id: this.config.moduleId + '_icon'}) .appendTo(this.module); $('
', {class: this.config.moduleId + '_icon_wrap'}) .html(GPT.Icons.assistent) .on('click', function () { GPT.Hello.config.show ? GPT.Hello.show() : GPT.Chat.show(); }) .appendTo(this.icon); this.window = $('
', {id: this.config.moduleId + '_window', style: 'display: none;', 'data-type': this.config.type}) .appendTo(this.module); let btns = $('
', {class: this.config.moduleId + '_window_buttons'}) .appendTo(this.window); $('
', {class: this.config.moduleId + '_window_actions'}) .html(this.config.action) .appendTo(this.window); if (this.config.resize) { $('', {class: this.config.moduleId + '_window_resize'}) .html(GPT.Icons.resize) .on('click', () => this.resize()) .appendTo(btns); } $('', {class: this.config.moduleId + '_window_close'}) .html(GPT.Icons.close) .on('click', (e) => this.close(e)) .appendTo(btns); // this.info = $('
', {id: this.config.moduleId + '_info'}) // .appendTo(this.window); this.chat = $('
', {id: this.config.moduleId + '_chat'}) .appendTo(this.window); this.commentsWrap = $('
', {id: this.config.moduleId + '_comments'}) .appendTo(this.chat); this.comments = $('
', {class: this.config.moduleId + '_comments'}) .on('scroll', function (){ var $wrap = $(this).parent(); ($(this).scrollTop() !== 0) ? $wrap.addClass('scroll') : $wrap.removeClass('scroll'); }) .appendTo(this.commentsWrap); if (this.config.tooltips){ this.tplWrap = $('
', {id: this.config.moduleId + '_tooltips'}) .appendTo(this.chat); this.config.tooltips.forEach((t) => { let p = $('

').html(t.text) .on('click', (e) => this.addTooltip(t.text, e.target, t.file)) .appendTo(this.tplWrap); }); } this.form = $('

', {id: this.config.moduleId + '_form'}) .on('submit', (e) => this.submit(e)) .appendTo(this.chat); this.text = $('