debian-mirror-gitlab/app/assets/javascripts/mr_popover/index.js

68 lines
2 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createDefaultClient from '~/lib/graphql';
2021-03-11 19:13:27 +05:30
import MRPopover from './components/mr_popover.vue';
2019-07-07 11:18:12 +05:30
let renderedPopover;
let renderFn;
const handleUserPopoverMouseOut = ({ target }) => {
target.removeEventListener('mouseleave', handleUserPopoverMouseOut);
if (renderFn) {
clearTimeout(renderFn);
}
if (renderedPopover) {
renderedPopover.$destroy();
renderedPopover = null;
}
};
/**
* Adds a MergeRequestPopover component to the body, hands over as much data as the target element has in data attributes.
* loads based on data-project-path and data-iid more data about an MR from the API and sets it on the popover
*/
const handleMRPopoverMount = ({ apolloProvider, projectPath, mrTitle, iid }) => ({ target }) => {
// Add listener to actually remove it again
target.addEventListener('mouseleave', handleUserPopoverMouseOut);
renderFn = setTimeout(() => {
const MRPopoverComponent = Vue.extend(MRPopover);
renderedPopover = new MRPopoverComponent({
propsData: {
target,
projectPath,
mergeRequestIID: iid,
mergeRequestTitle: mrTitle,
},
apolloProvider,
});
renderedPopover.$mount();
}, 200); // 200ms delay so not every mouseover triggers Popover + API Call
};
2021-03-08 18:12:59 +05:30
export default (elements) => {
2019-07-07 11:18:12 +05:30
const mrLinks = elements || [...document.querySelectorAll('.gfm-merge_request')];
if (mrLinks.length > 0) {
Vue.use(VueApollo);
const apolloProvider = new VueApollo({
defaultClient: createDefaultClient(),
});
const listenerAddedAttr = 'data-mr-listener-added';
2021-03-08 18:12:59 +05:30
mrLinks.forEach((el) => {
2019-07-07 11:18:12 +05:30
const { projectPath, mrTitle, iid } = el.dataset;
if (!el.getAttribute(listenerAddedAttr) && projectPath && mrTitle && iid) {
el.addEventListener(
'mouseenter',
handleMRPopoverMount({ apolloProvider, projectPath, mrTitle, iid }),
);
el.setAttribute(listenerAddedAttr, true);
}
});
}
};