summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/feature_highlight/feature_highlight_helper.js
blob: 9f741355cd74322c38d817a4eed70728851d4c06 (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
import Cookies from 'js-cookie';

export const getCookieName = cookieId => `feature-highlighted-${cookieId}`;
export const getSelector = highlightId => `.js-feature-highlight[data-highlight=${highlightId}]`;

export const showPopover = function showPopover() {
  if (this.hasClass('js-popover-show')) {
    return false;
  }
  this.popover('show');
  this.addClass('disable-animation js-popover-show');

  return true;
};

export const hidePopover = function hidePopover() {
  if (!this.hasClass('js-popover-show')) {
    return false;
  }
  this.popover('hide');
  this.removeClass('disable-animation js-popover-show');

  return true;
};

export const dismiss = function dismiss(cookieId) {
  Cookies.set(getCookieName(cookieId), true);
  hidePopover.call(this);
  this.hide();
};

export const mouseleave = function mouseleave() {
  if (!$('.popover:hover').length > 0) {
    const $featureHighlight = $(this);
    hidePopover.call($featureHighlight);
  }
};

export const mouseenter = function mouseenter() {
  const $featureHighlight = $(this);

  const showedPopover = showPopover.call($featureHighlight);
  if (showedPopover) {
    $('.popover')
      .on('mouseleave', mouseleave.bind($featureHighlight));
  }
};

export const setupDismissButton = function setupDismissButton() {
  const popoverId = this.getAttribute('aria-describedby');
  const cookieId = this.dataset.highlight;
  const $popover = $(this);
  const dismissWrapper = dismiss.bind($popover, cookieId);

  $(`#${popoverId} .dismiss-feature-highlight`)
    .on('click', dismissWrapper);
};