All files / app/assets/javascripts/code_navigation/store actions.js

91.67% Statements 33/36
87.5% Branches 21/24
100% Functions 10/10
90.91% Lines 30/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85            1x     1x     3x   3x 3x     2x 4x 2x       2x   4x     2x   1x       2x 2x             5x   4x   4x       4x   4x           4x 4x   4x   3x 3x 3x   3x           3x   3x   3x     3x      
import axios from '~/lib/utils/axios_utils';
import { getCurrentHoverElement, setCurrentHoverElement, addInteractionClass } from '../utils';
import * as types from './mutation_types';
 
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);
 
    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,
                definitionLineNumber: parseInt(d.definition_path?.split('#L').pop() || 0, 10),
              };
              addInteractionClass(path, d);
            }
            return acc;
          }, {});
 
          commit(types.REQUEST_DATA_SUCCESS, { path, normalizedData });
        })
        .catch(() => dispatch('requestDataError'));
    });
  },
  showBlobInteractionZones({ state }, path) {
    if (state.data && state.data[path]) {
      Object.values(state.data[path]).forEach((d) => addInteractionClass(path, d));
    }
  },
  showDefinition({ commit, state }, { target: el }) {
    let definition;
    let position;
 
    if (!state.data) return;
 
    const isCurrentElementPopoverOpen = el.classList.contains('hll');
 
    Iif (getCurrentHoverElement()) {
      getCurrentHoverElement().classList.remove('hll');
    }
 
    const blobEl = el.closest('[data-path]');
 
    Iif (!blobEl) {
      commit(types.SET_CURRENT_DEFINITION, { definition, position });
 
      return;
    }
 
    const blobPath = blobEl.dataset.path;
    const data = state.data[blobPath];
 
    if (!data) return;
 
    Eif (el.closest('.js-code-navigation') && !isCurrentElementPopoverOpen) {
      const { lineIndex, charIndex } = el.dataset;
      const { x, y } = el.getBoundingClientRect();
 
      position = {
        x: x || 0,
        y: y + window.scrollY || 0,
        height: el.offsetHeight,
        lineIndex: parseInt(lineIndex, 10),
      };
      definition = data[`${lineIndex}:${charIndex}`];
 
      el.classList.add('hll');
 
      setCurrentHoverElement(el);
    }
 
    commit(types.SET_CURRENT_DEFINITION, { definition, position, blobPath });
  },
};