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

85 lines
2.3 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 { getCurrentHoverElement, setCurrentHoverElement, addInteractionClass } from '../utils';
2021-03-11 19:13:27 +05:30
import * as types from './mutation_types';
2020-03-13 15:44:24 +05:30
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) {
2020-07-28 23:09:34 +05:30
acc[`${d.start_line}:${d.start_char}`] = {
...d,
definitionLineNumber: parseInt(d.definition_path?.split('#L').pop() || 0, 10),
};
2020-04-22 19:07:51 +05:30
addInteractionClass(path, d);
}
return acc;
}, {});
commit(types.REQUEST_DATA_SUCCESS, { path, normalizedData });
})
.catch(() => dispatch('requestDataError'));
});
},
showBlobInteractionZones({ state }, path) {
2020-05-24 23:13:21 +05:30
if (state.data && state.data[path]) {
2021-03-08 18:12:59 +05:30
Object.values(state.data[path]).forEach((d) => addInteractionClass(path, d));
2020-05-24 23:13:21 +05:30
}
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;
}
2020-05-24 23:13:21 +05:30
const blobPath = blobEl.dataset.path;
const data = state.data[blobPath];
2020-04-22 19:07:51 +05:30
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-07-28 23:09:34 +05:30
lineIndex: parseInt(lineIndex, 10),
2020-03-13 15:44:24 +05:30
};
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);
}
2020-05-24 23:13:21 +05:30
commit(types.SET_CURRENT_DEFINITION, { definition, position, blobPath });
2020-03-13 15:44:24 +05:30
},
};