summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/feature_highlight/feature_highlight.js
blob: f5e38fc8879276a7aa3daade471a48afb642ff61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Cookies from 'js-cookie';
import _ from 'underscore';
import {
  getCookieName,
  getSelector,
  hidePopover,
  setupDismissButton,
  mouseenter,
  mouseleave,
} from './feature_highlight_helper';

export const setupFeatureHighlightPopover = (id) => {
  const $selector = $(getSelector(id));
  const $parent = $selector.parent();
  const $popoverContent = $parent.siblings('.feature-highlight-popover-content');
  const hideOnScroll = hidePopover.bind($selector);
  const debouncedMouseleave = _.debounce(mouseleave, 300);

  $selector
    // Setup popover
    .data('content', $popoverContent.prop('outerHTML'))
    .popover({
      html: true,
      // Override the existing template to add custom CSS classes
      template: `
        <div class="popover feature-highlight-popover" role="tooltip">
          <div class="arrow"></div>
          <div class="popover-content"></div>
        </div>
      `,
    })
    .on('mouseenter', mouseenter)
    .on('mouseleave', debouncedMouseleave)
    .on('inserted.bs.popover', setupDismissButton)
    .on('show.bs.popover', () => {
      window.addEventListener('scroll', hideOnScroll);
    })
    .on('hide.bs.popover', () => {
      window.removeEventListener('scroll', hideOnScroll);
    })
    // Display feature highlight
    .removeAttr('disabled');
};

export const shouldHighlightFeature = (id) => {
  const element = document.querySelector(getSelector(id));
  const previouslyDismissed = Cookies.get(getCookieName(id)) === 'true';

  return element && !previouslyDismissed;
};

export const highlightFeatures = (highlightOrder) => {
  const featureId = highlightOrder.find(shouldHighlightFeature);

  if (featureId) {
    setupFeatureHighlightPopover(featureId);
  }
};