summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/feature_highlight/feature_highlight_helper.js
blob: f480e72961cba7c3bd0b47da557013952eb5ed00 (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
import $ from 'jquery';
import axios from '../lib/utils/axios_utils';
import { __ } from '../locale';
import Flash from '../flash';
import LazyLoader from '../lazy_loader';

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

export function togglePopover(show) {
  const isAlreadyShown = this.hasClass('js-popover-show');
  if ((show && isAlreadyShown) || (!show && !isAlreadyShown)) {
    return false;
  }
  this.popover(show ? 'show' : 'hide');
  this.toggleClass('disable-animation js-popover-show', show);

  return true;
}

export function dismiss(highlightId) {
  axios.post(this.attr('data-dismiss-endpoint'), {
    feature_name: highlightId,
  })
    .catch(() => Flash(__('An error occurred while dismissing the feature highlight. Refresh the page and try dismissing again.')));

  togglePopover.call(this, false);
  this.hide();
}

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

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

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

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

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

  const lazyImg = $(`#${popoverId} .feature-highlight-illustration`)[0];
  if (lazyImg) {
    LazyLoader.loadImage(lazyImg);
  }
}