2020-08-26 15:10:29 +00:00
|
|
|
import Vue from 'vue';
|
2020-09-11 09:08:44 +00:00
|
|
|
import { toArray } from 'lodash';
|
2020-08-26 15:10:29 +00:00
|
|
|
import Tooltips from './components/tooltips.vue';
|
|
|
|
|
|
|
|
let app;
|
|
|
|
|
|
|
|
const EVENTS_MAP = {
|
|
|
|
hover: 'mouseenter',
|
|
|
|
click: 'click',
|
|
|
|
focus: 'focus',
|
|
|
|
};
|
|
|
|
|
|
|
|
const DEFAULT_TRIGGER = 'hover focus';
|
2020-08-31 03:10:13 +00:00
|
|
|
const APP_ELEMENT_ID = 'gl-tooltips-app';
|
2020-08-26 15:10:29 +00:00
|
|
|
|
|
|
|
const tooltipsApp = () => {
|
|
|
|
if (!app) {
|
2020-08-31 03:10:13 +00:00
|
|
|
const container = document.createElement('div');
|
|
|
|
|
|
|
|
container.setAttribute('id', APP_ELEMENT_ID);
|
|
|
|
document.body.appendChild(container);
|
|
|
|
|
2020-08-26 15:10:29 +00:00
|
|
|
app = new Vue({
|
|
|
|
render(h) {
|
|
|
|
return h(Tooltips, {
|
|
|
|
props: {
|
|
|
|
elements: this.elements,
|
|
|
|
},
|
|
|
|
ref: 'tooltips',
|
|
|
|
});
|
|
|
|
},
|
2020-08-31 03:10:13 +00:00
|
|
|
}).$mount(container);
|
2020-08-26 15:10:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 09:08:44 +00:00
|
|
|
return app.$refs.tooltips;
|
2020-08-26 15:10:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const isTooltip = (node, selector) => node.matches && node.matches(selector);
|
|
|
|
|
|
|
|
const addTooltips = (elements, config) => {
|
2020-09-11 09:08:44 +00:00
|
|
|
tooltipsApp().addTooltips(toArray(elements), config);
|
2020-08-26 15:10:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const initTooltips = (selector, config = {}) => {
|
|
|
|
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, selector, config), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return tooltipsApp();
|
|
|
|
};
|
2020-08-31 03:10:13 +00:00
|
|
|
|
2020-09-11 09:08:44 +00:00
|
|
|
const elementsIterator = handler => elements => toArray(elements).forEach(handler);
|
|
|
|
|
|
|
|
export const dispose = elementsIterator(element => tooltipsApp().dispose(element));
|
|
|
|
export const fixTitle = elementsIterator(element => tooltipsApp().fixTitle(element));
|
|
|
|
export const enable = elementsIterator(element => tooltipsApp().triggerEvent(element, 'enable'));
|
|
|
|
export const disable = elementsIterator(element => tooltipsApp().triggerEvent(element, 'disable'));
|
|
|
|
export const hide = elementsIterator(element => tooltipsApp().triggerEvent(element, 'close'));
|
|
|
|
export const show = elementsIterator(element => tooltipsApp().triggerEvent(element, 'open'));
|
2020-08-31 03:10:13 +00:00
|
|
|
|
|
|
|
export const destroy = () => {
|
|
|
|
tooltipsApp().$destroy();
|
|
|
|
app = null;
|
|
|
|
};
|