2020-10-27 11:08:39 -04:00
|
|
|
import { toArray } from 'lodash';
|
2021-02-14 13:09:20 -05:00
|
|
|
import Vue from 'vue';
|
2020-10-27 11:08:39 -04:00
|
|
|
import PopoversComponent from './components/popovers.vue';
|
|
|
|
|
|
|
|
let app;
|
|
|
|
|
|
|
|
const APP_ELEMENT_ID = 'gl-popovers-app';
|
|
|
|
|
|
|
|
const getPopoversApp = () => {
|
|
|
|
if (!app) {
|
|
|
|
const container = document.createElement('div');
|
|
|
|
container.setAttribute('id', APP_ELEMENT_ID);
|
|
|
|
document.body.appendChild(container);
|
|
|
|
|
|
|
|
const Popovers = Vue.extend(PopoversComponent);
|
|
|
|
app = new Popovers();
|
|
|
|
app.$mount(`#${APP_ELEMENT_ID}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return app;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isPopover = (node, selector) => node.matches && node.matches(selector);
|
|
|
|
|
|
|
|
const handlePopoverEvent = (rootTarget, e, selector) => {
|
|
|
|
for (let { target } = e; target && target !== rootTarget; target = target.parentNode) {
|
|
|
|
if (isPopover(target, selector)) {
|
|
|
|
getPopoversApp().addPopovers([target]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const initPopovers = () => {
|
2020-12-23 19:10:25 -05:00
|
|
|
['mouseenter', 'focus', 'click'].forEach((event) => {
|
2020-10-27 11:08:39 -04:00
|
|
|
document.addEventListener(
|
|
|
|
event,
|
2020-12-23 19:10:25 -05:00
|
|
|
(e) => handlePopoverEvent(document, e, '[data-toggle="popover"]'),
|
2020-10-27 11:08:39 -04:00
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return getPopoversApp();
|
|
|
|
};
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
export const dispose = (elements) => toArray(elements).forEach(getPopoversApp().dispose);
|
2020-10-27 11:08:39 -04:00
|
|
|
|
|
|
|
export const destroy = () => {
|
|
|
|
getPopoversApp().$destroy();
|
|
|
|
app = null;
|
|
|
|
};
|