2021-02-12 13:08:59 -05:00
|
|
|
import { toArray, isElement } from 'lodash';
|
2021-02-14 13:09:20 -05:00
|
|
|
import Vue from 'vue';
|
2020-08-26 11:10:29 -04: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-30 23:10:13 -04:00
|
|
|
const APP_ELEMENT_ID = 'gl-tooltips-app';
|
2020-08-26 11:10:29 -04:00
|
|
|
|
|
|
|
const tooltipsApp = () => {
|
|
|
|
if (!app) {
|
2020-08-30 23:10:13 -04:00
|
|
|
const container = document.createElement('div');
|
|
|
|
|
|
|
|
container.setAttribute('id', APP_ELEMENT_ID);
|
|
|
|
document.body.appendChild(container);
|
|
|
|
|
2020-08-26 11:10:29 -04:00
|
|
|
app = new Vue({
|
|
|
|
render(h) {
|
|
|
|
return h(Tooltips, {
|
|
|
|
props: {
|
|
|
|
elements: this.elements,
|
|
|
|
},
|
|
|
|
ref: 'tooltips',
|
|
|
|
});
|
|
|
|
},
|
2020-08-30 23:10:13 -04:00
|
|
|
}).$mount(container);
|
2020-08-26 11:10:29 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 05:08:44 -04:00
|
|
|
return app.$refs.tooltips;
|
2020-08-26 11:10:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const isTooltip = (node, selector) => node.matches && node.matches(selector);
|
|
|
|
|
|
|
|
const addTooltips = (elements, config) => {
|
2020-09-11 05:08:44 -04:00
|
|
|
tooltipsApp().addTooltips(toArray(elements), config);
|
2020-08-26 11:10:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleTooltipEvent = (rootTarget, e, selector, config = {}) => {
|
|
|
|
for (let { target } = e; target && target !== rootTarget; target = target.parentNode) {
|
|
|
|
if (isTooltip(target, selector)) {
|
2021-04-19 14:09:09 -04:00
|
|
|
addTooltips([target], config);
|
2020-08-26 11:10:29 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-14 11:08:42 -04:00
|
|
|
const applyToElements = (elements, handler) => {
|
|
|
|
const iterable = isElement(elements) ? [elements] : toArray(elements);
|
|
|
|
|
|
|
|
toArray(iterable).forEach(handler);
|
|
|
|
};
|
2020-08-26 11:10:29 -04:00
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
const createTooltipApiInvoker = (glHandler) => (elements) => {
|
|
|
|
applyToElements(elements, glHandler);
|
2020-08-26 11:10:29 -04:00
|
|
|
};
|
2020-08-30 23:10:13 -04:00
|
|
|
|
2020-09-16 20:09:34 -04:00
|
|
|
export const initTooltips = (config = {}) => {
|
2021-02-12 13:08:59 -05:00
|
|
|
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();
|
2020-10-21 17:09:00 -04:00
|
|
|
};
|
2021-02-12 13:08:59 -05:00
|
|
|
export const add = (elements, config = {}) => addTooltips(elements, config);
|
|
|
|
export const dispose = createTooltipApiInvoker((element) => tooltipsApp().dispose(element));
|
|
|
|
export const fixTitle = createTooltipApiInvoker((element) => tooltipsApp().fixTitle(element));
|
|
|
|
export const enable = createTooltipApiInvoker((element) =>
|
|
|
|
tooltipsApp().triggerEvent(element, 'enable'),
|
|
|
|
);
|
|
|
|
export const disable = createTooltipApiInvoker((element) =>
|
|
|
|
tooltipsApp().triggerEvent(element, 'disable'),
|
|
|
|
);
|
|
|
|
export const hide = createTooltipApiInvoker((element) =>
|
|
|
|
tooltipsApp().triggerEvent(element, 'close'),
|
|
|
|
);
|
|
|
|
export const show = createTooltipApiInvoker((element) =>
|
|
|
|
tooltipsApp().triggerEvent(element, 'open'),
|
|
|
|
);
|
2021-02-23 10:10:47 -05:00
|
|
|
export const once = (event, cb) => tooltipsApp().$once(event, cb);
|
2020-08-30 23:10:13 -04:00
|
|
|
export const destroy = () => {
|
|
|
|
tooltipsApp().$destroy();
|
|
|
|
app = null;
|
|
|
|
};
|