summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/tooltips/index.js
blob: f7cad6639ae65007b8d888a73392c735c25df0c3 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Vue from 'vue';
import jQuery from 'jquery';
import { toArray, isFunction, isElement } from 'lodash';
import Tooltips from './components/tooltips.vue';

let app;

const EVENTS_MAP = {
  hover: 'mouseenter',
  click: 'click',
  focus: 'focus',
};

const DEFAULT_TRIGGER = 'hover focus';
const APP_ELEMENT_ID = 'gl-tooltips-app';

const tooltipsApp = () => {
  if (!app) {
    const container = document.createElement('div');

    container.setAttribute('id', APP_ELEMENT_ID);
    document.body.appendChild(container);

    app = new Vue({
      render(h) {
        return h(Tooltips, {
          props: {
            elements: this.elements,
          },
          ref: 'tooltips',
        });
      },
    }).$mount(container);
  }

  return app.$refs.tooltips;
};

const isTooltip = (node, selector) => node.matches && node.matches(selector);

const addTooltips = (elements, config) => {
  tooltipsApp().addTooltips(toArray(elements), config);
};

const handleTooltipEvent = (rootTarget, e, selector, config = {}) => {
  for (let { target } = e; target && target !== rootTarget; target = target.parentNode) {
    if (isTooltip(target, selector)) {
      addTooltips([target], {
        show: true,
        ...config,
      });
      break;
    }
  }
};

const applyToElements = (elements, handler) => {
  const iterable = isElement(elements) ? [elements] : toArray(elements);

  toArray(iterable).forEach(handler);
};

const invokeBootstrapApi = (elements, method) => {
  if (isFunction(elements.tooltip)) {
    elements.tooltip(method);
  } else {
    jQuery(elements).tooltip(method);
  }
};

const isGlTooltipsEnabled = () => Boolean(window.gon.glTooltipsEnabled);

const tooltipApiInvoker = ({ glHandler, bsHandler }) => (elements, ...params) => {
  if (isGlTooltipsEnabled()) {
    applyToElements(elements, glHandler);
  } else {
    bsHandler(elements, ...params);
  }
};

export const initTooltips = (config = {}) => {
  if (isGlTooltipsEnabled()) {
    const triggers = config?.triggers || DEFAULT_TRIGGER;
    const events = triggers.split(' ').map(trigger => EVENTS_MAP[trigger]);

    events.forEach(event => {
      document.addEventListener(
        event,
        e => handleTooltipEvent(document, e, config.selector, config),
        true,
      );
    });

    return tooltipsApp();
  }

  return invokeBootstrapApi(document.body, config);
};
export const add = (elements, config = {}) => {
  if (isGlTooltipsEnabled()) {
    return addTooltips(elements, config);
  }
  return invokeBootstrapApi(elements, config);
};
export const dispose = tooltipApiInvoker({
  glHandler: element => tooltipsApp().dispose(element),
  bsHandler: elements => invokeBootstrapApi(elements, 'dispose'),
});
export const fixTitle = tooltipApiInvoker({
  glHandler: element => tooltipsApp().fixTitle(element),
  bsHandler: elements => invokeBootstrapApi(elements, '_fixTitle'),
});
export const enable = tooltipApiInvoker({
  glHandler: element => tooltipsApp().triggerEvent(element, 'enable'),
  bsHandler: elements => invokeBootstrapApi(elements, 'enable'),
});
export const disable = tooltipApiInvoker({
  glHandler: element => tooltipsApp().triggerEvent(element, 'disable'),
  bsHandler: elements => invokeBootstrapApi(elements, 'disable'),
});
export const hide = tooltipApiInvoker({
  glHandler: element => tooltipsApp().triggerEvent(element, 'close'),
  bsHandler: elements => invokeBootstrapApi(elements, 'hide'),
});
export const show = tooltipApiInvoker({
  glHandler: element => tooltipsApp().triggerEvent(element, 'open'),
  bsHandler: elements => invokeBootstrapApi(elements, 'show'),
});
export const destroy = () => {
  tooltipsApp().$destroy();
  app = null;
};