assets/themes/j1/adapter/js/chatbot.js in j1-template-2024.1.5 vs assets/themes/j1/adapter/js/chatbot.js in j1-template-2024.2.0

- old
+ new

@@ -83,11 +83,11 @@ // ESLint shimming // ----------------------------------------------------------------------------- /* eslint indent: "off" */ // ----------------------------------------------------------------------------- 'use strict'; -j1.adapter.chatbot = (function (j1, window) { +j1.adapter.chatbot = ((j1, window) => { {% comment %} Set global variables -------------------------------------------------------------------------------- {% endcomment %} var url = new URL(window.location.href); var hostname = url.hostname; @@ -107,44 +107,54 @@ var chatBotWidget; var validChatbot; var validChatbotID; var user_consent; var apiExists; + var _this; var logger; var logText; +// date|time +var startTime; +var endTime; +var startTimeModule; +var endTimeModule; +var timeSeconds; + // --------------------------------------------------------------------------- - // Main object + // main // --------------------------------------------------------------------------- return { // ------------------------------------------------------------------------- // init() // adapter initializer // ------------------------------------------------------------------------- - init: function (options) { + init: (options) => { - var dependencies_met_page_ready = setInterval (function (options) { - var pageState = $('#no_flicker').css("display"); - var pageVisible = (pageState == 'block') ? true: false; - var atticFinished = (j1.adapter.attic.getState() == 'finished') ? true: false; + // ----------------------------------------------------------------------- + // adapter initializer + // ----------------------------------------------------------------------- + var dependencies_met_page_ready = setInterval (() => { + var pageState = $('#content').css("display"); + var pageVisible = (pageState === 'block') ? true: false; + var j1CoreFinished = (j1.getState() === 'finished') ? true : false; - if ( j1.getState() === 'finished' && pageVisible ) { -// if (j1.getState() == 'finished' && pageVisible && atticFinished) { -// if (j1.getState() == 'finished') { + if (j1CoreFinished && pageVisible) { + startTimeModule = Date.now(); {% if chat_enabled %} // Load module DEFAULTS|CONFIG // chatDefaults = $.extend({}, {{chat_defaults | replace: 'nil', 'null' | replace: '=>', ':' }}); chatSettings = $.extend({}, {{chat_settings | replace: 'nil', 'null' | replace: '=>', ':' }}); chatOptions = $.extend(true, {}, chatDefaults, chatSettings); // ----------------------------------------------------------------- - // Global variable settings + // global variable settings // ----------------------------------------------------------------- _this = j1.adapter.chatbot; logger = log4javascript.getLogger('j1.adapter.chatbot'); chatbot = chatOptions.provider; @@ -158,24 +168,28 @@ logger.info('\n' + 'invalid chatbot detected: ' + chatbot); logger.warn('\n' + 'module chat: disabled'); clearInterval(dependencies_met_page_ready); } - // [INFO ] [j1.adapter.chatbot ] [ detected chat provider (j1_config): {{chat_provider}}} ] - // [INFO ] [j1.adapter.chatbot ] [ start processing load region head, layout: {{page.layout}} ] - {% case chat_provider %} - {% when "webwhiz" %} - // [INFO ] [j1.adapter.chatbot ] [ place provider: WebWhiz ] - // ----------------------------------------------------------------- - // Default module settings + // default module settings // ----------------------------------------------------------------- var settings = $.extend({ module_name: 'j1.adapter.chatbot', generated: '{{site.time}}' }, options); + // ----------------------------------------------------------------- + // module initializer + // ----------------------------------------------------------------- + + // [INFO ] [j1.adapter.chatbot ] [ detected chat provider (j1_config): {{chat_provider}}} ] + // [INFO ] [j1.adapter.chatbot ] [ start processing load region head, layout: {{page.layout}} ] + {% case chat_provider %} + {% when "webwhiz" %} + // [INFO ] [j1.adapter.chatbot ] [ place provider: WebWhiz ] + // initialize state flag _this.setState('started'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: started'); @@ -197,10 +211,14 @@ _this.setState('finished'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: finished'); + + endTimeModule = Date.now(); + logger.info('\n' + 'module initializing time: ' + (endTimeModule-startTimeModule) + 'ms'); + clearInterval(dependencies_met_page_ready); {% comment %} Setup VivoChat -------------------------------------------------------------------- {% endcomment %} {% when "vivochat" %} @@ -209,19 +227,19 @@ // initialize state flag _this.setState('started'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: started'); - apiExists = document.getElementById("{{chatbotID}}") === null ? false : true; + apiExists = document.getElementById("{{chatbotID}}") === null ? false : true; user_consent = j1.readCookie(cookie_names.user_consent); if (user_consent.personalization) { logger.info('\n' + 'user consent on personalization: ' + user_consent.personalization); if (validChatbotID) { logger.info('\n' + 'enable VivoChat on ID: ' + chatbotID); - setTimeout (function() { + setTimeout(() => { logger.info('\n' + 'set fab button position'); $('.fab-btn').css('bottom', '5rem'); }, 1000); apiScript.id = 'VivoChat'; @@ -242,10 +260,14 @@ } _this.setState('finished'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: finished'); + + endTimeModule = Date.now(); + logger.info('\n' + 'module initializing time: ' + (endTimeModule-startTimeModule) + 'ms'); + clearInterval(dependencies_met_page_ready); {% comment %} Setup TutGPT -------------------------------------------------------------------- {% endcomment %} {% when "tutgpt" %} @@ -254,11 +276,11 @@ // initialize state flag _this.setState('started'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: started'); - setTimeout (function() { + setTimeout(() => { logger.info('\n' + 'set fab button position'); $('.fab-btn').css('bottom', '5rem'); }, 1000); user_consent = j1.readCookie(cookie_names.user_consent); @@ -277,17 +299,17 @@ // apiScript.src = "https://widget.tutgpt.com/chat.js"; apiScript.async = true; apiScript.id = "chatbot-api"; - apiScript.addEventListener("load", function (evt) { + apiScript.addEventListener("load", (evt) => { BotChat.init({ id: chatbotID, }); }) - setTimeout (function() { + setTimeout(() => { logger.info('\n' + 'Initialize TutGPT API'); // add the chatbotAPI to run the Bot (delayed) // document.head.appendChild(apiScript); logger.info('\n' + 'TutGPT API added in section: head'); @@ -303,10 +325,14 @@ } _this.setState('finished'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: finished'); + + endTimeModule = Date.now(); + logger.info('\n' + 'module initializing time: ' + (endTimeModule-startTimeModule) + 'ms'); + clearInterval(dependencies_met_page_ready); {% comment %} Setup ChatBob -------------------------------------------------------------------- {% endcomment %} {% when "chatbob" %} @@ -315,17 +341,18 @@ // initialize state flag _this.setState('started'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: started'); - setTimeout (function() { + setTimeout(() => { logger.info('\n' + 'set fab button position'); $('.fab-btn').css('bottom', '5rem'); }, 1000); - apiExists = document.getElementById("{{chatbotID}}") === null ? false : true; + apiExists = document.getElementById("{{chatbotID}}") === null ? false : true; user_consent = j1.readCookie(cookie_names.user_consent); + if (user_consent.personalization) { logger.info('\n' + 'user consent on personalization: ' + user_consent.personalization); if (validChatbotID) { logger.info('\n' + 'enable ChatBob on ID: ' + chatbotID); @@ -346,10 +373,14 @@ } _this.setState('finished'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: finished'); + + endTimeModule = Date.now(); + logger.info('\n' + 'module initializing time: ' + (endTimeModule-startTimeModule) + 'ms'); + clearInterval(dependencies_met_page_ready); {% comment %} Setup WebWhiz -------------------------------------------------------------------- {% endcomment %} {% when "webwhiz" %} @@ -358,17 +389,18 @@ // initialize state flag _this.setState('started'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: started'); - setTimeout (function() { + setTimeout(() => { logger.info('\n' + 'set fab button position'); $('.fab-btn').css('bottom', '5rem'); }, 1000); - apiExists = document.getElementById("{{chatbotID}}") === null ? false : true; + apiExists = document.getElementById("{{chatbotID}}") === null ? false : true; user_consent = j1.readCookie(cookie_names.user_consent); + if (user_consent.personalization) { logger.info('\n' + 'user consent on personalization: ' + user_consent.personalization); if (validChatbotID) { logger.info('\n' + 'enable WebWhiz on ID: ' + chatbotID); @@ -389,10 +421,14 @@ } _this.setState('finished'); logger.debug('\n' + 'state: ' + _this.getState()); logger.info('\n' + 'module initializing: finished'); + + endTimeModule = Date.now(); + logger.info('\n' + 'module initializing time: ' + (endTimeModule-startTimeModule) + 'ms'); + clearInterval(dependencies_met_page_ready); {% comment %} Setup Custom Bot -------------------------------------------------------------------- {% endcomment %} {% when "custom" %} @@ -401,10 +437,14 @@ {% endcase %} // [INFO ] [j1.adapter.analytics ] [ end processing ] {% else %} logger = log4javascript.getLogger('j1.adapter.chatbot'); logger.info('\n' + 'chatbot: disabled'); + + endTimeModule = Date.now(); + logger.info('\n' + 'module initializing time: ' + (endTimeModule-startTimeModule) + 'ms'); + clearInterval(dependencies_met_page_ready); {% endif %} } }, 10); @@ -413,49 +453,51 @@ // ------------------------------------------------------------------------- // messageHandler() // manage messages send from other J1 modules // ------------------------------------------------------------------------- - messageHandler: function (sender, message) { + messageHandler: (sender, message) => { var json_message = JSON.stringify(message, undefined, 2); logText = '\n' + 'received message from ' + sender + ': ' + json_message; logger.debug(logText); // ----------------------------------------------------------------------- - // Process commands|actions + // process commands|actions // ----------------------------------------------------------------------- if (message.type === 'command' && message.action === 'module_initialized') { + // - // Place handling of command|action here + // place handling of command|action here // + logger.info('\n' + message.text); } // - // Place handling of other command|action here + // place handling of other command|action here // return true; }, // END messageHandler // ------------------------------------------------------------------------- // setState() - // Sets the current (processing) state of the module + // sets the current (processing) state of the module // ------------------------------------------------------------------------- - setState: function (stat) { + setState: (stat) => { _this.state = stat; }, // END setState // ------------------------------------------------------------------------- // getState() // Returns the current (processing) state of the module // ------------------------------------------------------------------------- - getState: function () { + getState: () => { return _this.state; } // END getState - }; // END return + }; // END main (return) })(j1, window); {% endcapture %} {% if production %} {{ cache | minifyJS }}