debian-mirror-gitlab/app/assets/javascripts/code_navigation/store/actions.js

78 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-04-22 19:07:51 +05:30
import axios from '~/lib/utils/axios_utils';
2020-03-13 15:44:24 +05:30
import * as types from './mutation_types';
import { getCurrentHoverElement, setCurrentHoverElement, addInteractionClass } from '../utils';
export default {
setInitialData({ commit }, data) {
commit(types.SET_INITIAL_DATA, data);
},
requestDataError({ commit }) {
commit(types.REQUEST_DATA_ERROR);
},
fetchData({ commit, dispatch, state }) {
commit(types.REQUEST_DATA);
2020-04-22 19:07:51 +05:30
state.blobs.forEach(({ path, codeNavigationPath }) => {
axios
.get(codeNavigationPath)
.then(({ data }) => {
const normalizedData = data.reduce((acc, d) => {
if (d.hover) {
acc[`${d.start_line}:${d.start_char}`] = d;
addInteractionClass(path, d);
}
return acc;
}, {});
commit(types.REQUEST_DATA_SUCCESS, { path, normalizedData });
})
.catch(() => dispatch('requestDataError'));
});
},
showBlobInteractionZones({ state }, path) {
Object.values(state.data[path]).forEach(d => addInteractionClass(path, d));
2020-03-13 15:44:24 +05:30
},
showDefinition({ commit, state }, { target: el }) {
let definition;
let position;
if (!state.data) return;
const isCurrentElementPopoverOpen = el.classList.contains('hll');
if (getCurrentHoverElement()) {
getCurrentHoverElement().classList.remove('hll');
}
2020-04-22 19:07:51 +05:30
const blobEl = el.closest('[data-path]');
if (!blobEl) {
commit(types.SET_CURRENT_DEFINITION, { definition, position });
return;
}
const data = state.data[blobEl.dataset.path];
if (!data) return;
if (el.closest('.js-code-navigation') && !isCurrentElementPopoverOpen) {
2020-03-13 15:44:24 +05:30
const { lineIndex, charIndex } = el.dataset;
2020-04-22 19:07:51 +05:30
const { x, y } = el.getBoundingClientRect();
2020-03-13 15:44:24 +05:30
position = {
2020-04-22 19:07:51 +05:30
x: x || 0,
y: y + window.scrollY || 0,
2020-03-13 15:44:24 +05:30
height: el.offsetHeight,
};
2020-04-22 19:07:51 +05:30
definition = data[`${lineIndex}:${charIndex}`];
2020-03-13 15:44:24 +05:30
el.classList.add('hll');
setCurrentHoverElement(el);
}
commit(types.SET_CURRENT_DEFINITION, { definition, position });
},
};