summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/feature_highlight/feature_highlight_manager.js
blob: e7f5824b397ac94eb3c04288439e943d1d7e37f7 (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
59
60
61
62
import Cookies from 'js-cookie';
import {
  getCookieName,
  getSelector,
  hidePopover,
  setupDismissButton,
  mouseenter,
  mouseleave,
} from './feature_highlight';

export default class FeatureHighlightManager {
  constructor(highlightOrder) {
    this.highlightOrder = highlightOrder;
  }

  init() {
    const featureId = this.highlightOrder.find(FeatureHighlightManager.shouldHighlightFeature);

    if (featureId) {
      FeatureHighlightManager.highlightFeature(featureId);
    }
  }

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

    return element && !previouslyDismissed;
  }

  static highlightFeature(id) {
    const $selector = $(getSelector(id));
    const $parent = $selector.parent();
    const $popoverContent = $parent.siblings('.feature-highlight-popover-content');
    const hideOnScroll = hidePopover.bind($selector);

    $selector
      // Setup popover
      .data('content', $popoverContent[0].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', mouseleave)
      .on('inserted.bs.popover', setupDismissButton)
      .on('show.bs.popover', () => {
        window.addEventListener('scroll', hideOnScroll);
      })
      .on('hidden.bs.popover', () => {
        window.removeEventListener('scroll', hideOnScroll);
      })
      // Display feature highlight
      .removeAttr('disabled');
  }
}