summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diffs/store/mutations.js
blob: f7f99b5d62adbe5b619e2bb79c5aadbded6e63d0 (plain)
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
import Vue from 'vue';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import * as utils from './utils';
import * as types from './mutation_types';
import { COMMENT_FORM_TYPE, PARALLEL_DIFF_VIEW_TYPE, LINE_POSITION_LEFT } from '../constants';

export default {
  [types.SET_ENDPOINT](state, endpoint) {
    Object.assign(state, { endpoint });
  },

  [types.SET_LOADING](state, isLoading) {
    Object.assign(state, { isLoading });
  },

  [types.SET_DIFF_FILES](state, diffFiles) {
    Object.assign(state, {
      diffFiles: convertObjectPropsToCamelCase(diffFiles, {
        deep: true,
      }),
    });
  },

  [types.SET_DIFF_VIEW_TYPE](state, diffViewType) {
    Object.assign(state, { diffViewType });
  },

  [types.ADD_COMMENT_FORM_LINE](state, params) {
    const { lineCode } = params;
    let linePosition = params.linePosition;

    if (!linePosition) {
      throw new Error('you should not be here. I expect a line position');
      return;
    }

    // Always render context line comment form on the left side in parallel view
    if (state.diffViewType === PARALLEL_DIFF_VIEW_TYPE && window.TODO) {
      linePosition = LINE_POSITION_LEFT;
    }

    // We add forms as another diff line so they have to have a unique id
    // We later use this id to remove the form from diff lines
    const id = `${lineCode}_CommentForm_${linePosition || ''}`;

    // Unique comment form object as a diff line
    const formObj = {
      id,
      type: COMMENT_FORM_TYPE,
    };

    if (!state.diffLineCommentForms[lineCode]) {
      Vue.set(state.diffLineCommentForms, lineCode, { linePosition: {} });
    }

    Vue.set(state.diffLineCommentForms[lineCode], linePosition, formObj);
  },

  [types.REMOVE_COMMENT_FORM_LINE](state, { lineCode, linePosition }) {
    if (linePosition) {
      Vue.set(state.diffLineCommentForms[lineCode], linePosition, null);
    } else {
      Vue.set(state.diffLineCommentForms, lineCode, null);
    }
  },

  [types.ADD_CONTEXT_LINES](state, options) {
    const { lineNumbers, contextLines, fileHash } = options;
    const { bottom } = options.params;
    const diffFile = utils.findDiffFile(state.diffFiles, fileHash);
    const { highlightedDiffLines, parallelDiffLines } = diffFile;

    utils.removeMatchLine(diffFile, lineNumbers, bottom);
    const lines = utils.addLineReferences(contextLines, lineNumbers, bottom);
    utils.addContextLines({
      inlineLines: highlightedDiffLines,
      parallelLines: parallelDiffLines,
      contextLines: lines,
      bottom,
      lineNumbers,
    });
  },
};