AI Assistant

by Hugo Carneiro

HTML

<link rel="stylesheet" href="https://cdn.usebootstrap.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

CSS

body {
  font-family: Open Sans, Helvetica Neue, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.428571429;
  color: #333;
  background-color: #fff;
}

img {
  max-width: 100%;
  height: auto;
}

.chat-iframe-holder {
  position: fixed;
  right: 20px;
  bottom: 200px;
  z-index: 2147483000;
  display: flex;
  flex-flow: column nowrap;
  height: 592px;
  width: 385px;
  border: 1px solid #EBEBEB;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: -4px 4px 10px 0 rgba(0, 0, 0, .06);

  opacity: 0;
  transform: translateY(15px);
  transition: opacity 0.25s, transform 0.25s;
}

.chat-iframe-holder.active {
  opacity: 1;
  transform: translateY(0);
}

.chat-iframe-holder .chat-minimize {
  position: absolute;
  top: 0;
  right: 15px;
  padding: 15px;
  cursor: pointer;
  font-size: 30px;
  line-height: 1;
}

.chat-iframe-holder .chat-iframe-header {
  display: flex;
  flex-flow: column nowrap;
  padding: 30px 16px 20px 16px;
  background-color: #F2F6F7;
  color: #474974;
}

.chat-iframe-holder .chat-iframe-header h2 {
  margin-top: 0;
}

.chat-iframe-holder iframe {
  width: 100%;
  flex: 1;
}

.fl-assistant-btn {
  position: fixed;
  bottom: 125px;
  right: 20px;
  background-color: #19cd9d;
  color: #fff;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  font-size: 1.2em;
  cursor: pointer;
  padding: 10px;
  z-index: 2147483000;
  box-shadow: 0 1px 6px 0 rgba(0, 0, 0, .06), 0 2px 32px 0 rgba(0, 0, 0, .16);
}

JavaScript

var flUser = window.flUser || {
  data: {
    id: 1,
    firstName: 'Hugo'
  }
};

function toggleChatHolder() {
  $('.chat-iframe-holder').toggleClass('active');
}

function hideAssistant() {
  $('.chat-iframe-holder').addClass('hidden');
  $('.fl-assistant-btn').addClass('hidden');

  if ($('.chat-iframe-holder').hasClass('active')) {
    toggleChatHolder();
  }
}

function showAssistant() {
  $('.chat-iframe-holder').removeClass('hidden');
  $('.fl-assistant-btn').removeClass('hidden');

  toggleChatHolder();
}

// Function to create and start the observer
function watchForElementAppearance(targetSelector, callback) {
  // Configuration of the observer
  var config = {
    childList: true,
    subtree: true
  };

  // Callback function to execute when mutations are observed
  function observerCallback(mutationsList, observer) {
    for (var i = 0; i < mutationsList.length; i++) {
      var mutation = mutationsList[i];
      if (mutation.type === 'childList') {
        for (var j = 0; j < mutation.addedNodes.length; j++) {
          var node = mutation.addedNodes[j];
          if (node.nodeType === Node.ELEMENT_NODE) {
            var foundElement = node.querySelector(targetSelector);
            if (foundElement) {
              callback(foundElement);
              observer.disconnect(); // Optional: stop observing after the first detection
              return; // Exit once the target is found
            } else if (node.matches(targetSelector)) { // If the node itself matches the selector
              callback(node);
              observer.disconnect();
              return;
            }
          }
        }
      }
    }
  }

  // Create an observer instance linked to the callback function
  var observer = new MutationObserver(observerCallback);

  // Start observing the document with the configured parameters
  observer.observe(document.body, config);
}

function attachOverlayClick() {
  $('.overlay.data-source-overlay .overlay-close a').on('click',...