summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/directives/tooltip.js
blob: 0eb505bfce8b8991e982ad5949031ddf52c8a941 (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
import $ from 'jquery';
import '~/commons/bootstrap';
import { parseBoolean } from '~/lib/utils/common_utils';

export default {
  bind(el) {
    const glTooltipDelay = localStorage.getItem('gl-tooltip-delay');
    const delay = glTooltipDelay ? JSON.parse(glTooltipDelay) : 0;

    $(el).tooltip({
      trigger: 'hover',
      delay,
      // By default, sanitize is run even if there is no `html` or `template` present
      // so let's optimize to only run this when necessary.
      // https://github.com/twbs/bootstrap/blob/c5966de27395a407f9a3d20d0eb2ff8e8fb7b564/js/src/tooltip.js#L716
      sanitize: parseBoolean(el.dataset.html) || Boolean(el.dataset.template),
    });
  },

  componentUpdated(el) {
    $(el).tooltip('_fixTitle');

    // update visible tooltips
    const tooltipInstance = $(el).data('bs.tooltip');
    const tip = tooltipInstance.getTipElement();
    tooltipInstance.setElementContent(
      $(tip.querySelectorAll('.tooltip-inner')),
      tooltipInstance.getTitle(),
    );
  },

  unbind(el) {
    $(el).tooltip('dispose');
  },
};