debian-mirror-gitlab/app/assets/javascripts/diffs/store/mutations.js

243 lines
7.2 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
2018-12-05 23:21:45 +05:30
import { sortTree } from '~/ide/stores/utils';
2018-11-20 20:47:30 +05:30
import {
findDiffFile,
addLineReferences,
removeMatchLine,
addContextLines,
prepareDiffData,
isDiscussionApplicableToLine,
2018-12-05 23:21:45 +05:30
generateTreeList,
2018-11-20 20:47:30 +05:30
} from './utils';
2018-11-08 19:23:39 +05:30
import * as types from './mutation_types';
export default {
[types.SET_BASE_CONFIG](state, options) {
const { endpoint, projectPath } = options;
Object.assign(state, { endpoint, projectPath });
},
[types.SET_LOADING](state, isLoading) {
Object.assign(state, { isLoading });
},
[types.SET_DIFF_DATA](state, data) {
2019-02-13 22:33:31 +05:30
prepareDiffData(data);
const { tree, treeEntries } = generateTreeList(data.diff_files);
2018-11-20 20:47:30 +05:30
2018-11-08 19:23:39 +05:30
Object.assign(state, {
2019-02-13 22:33:31 +05:30
...convertObjectPropsToCamelCase(data),
2018-12-05 23:21:45 +05:30
tree: sortTree(tree),
treeEntries,
2018-11-20 20:47:30 +05:30
});
},
[types.RENDER_FILE](state, file) {
Object.assign(file, {
renderIt: true,
2018-11-08 19:23:39 +05:30
});
},
[types.SET_MERGE_REQUEST_DIFFS](state, mergeRequestDiffs) {
Object.assign(state, {
2019-02-13 22:33:31 +05:30
mergeRequestDiffs,
2018-11-08 19:23:39 +05:30
});
},
[types.SET_DIFF_VIEW_TYPE](state, diffViewType) {
Object.assign(state, { diffViewType });
},
2019-02-13 22:33:31 +05:30
[types.TOGGLE_LINE_HAS_FORM](state, { lineCode, fileHash, hasForm }) {
const diffFile = state.diffFiles.find(f => f.file_hash === fileHash);
if (!diffFile) return;
if (diffFile.highlighted_diff_lines) {
diffFile.highlighted_diff_lines.find(l => l.line_code === lineCode).hasForm = hasForm;
}
if (diffFile.parallel_diff_lines) {
const line = diffFile.parallel_diff_lines.find(l => {
const { left, right } = l;
2018-12-23 12:14:25 +05:30
2019-02-13 22:33:31 +05:30
return (left && left.line_code === lineCode) || (right && right.line_code === lineCode);
});
if (line.left && line.left.line_code === lineCode) {
line.left.hasForm = hasForm;
}
if (line.right && line.right.line_code === lineCode) {
line.right.hasForm = hasForm;
}
}
2018-11-08 19:23:39 +05:30
},
[types.ADD_CONTEXT_LINES](state, options) {
const { lineNumbers, contextLines, fileHash } = options;
const { bottom } = options.params;
const diffFile = findDiffFile(state.diffFiles, fileHash);
removeMatchLine(diffFile, lineNumbers, bottom);
2018-12-13 13:39:08 +05:30
const lines = addLineReferences(contextLines, lineNumbers, bottom).map(line => ({
...line,
2019-02-13 22:33:31 +05:30
line_code: line.line_code || `${fileHash}_${line.old_line}_${line.new_line}`,
2018-12-13 13:39:08 +05:30
discussions: line.discussions || [],
2019-02-13 22:33:31 +05:30
hasForm: false,
2018-12-13 13:39:08 +05:30
}));
2018-11-08 19:23:39 +05:30
addContextLines({
2019-02-13 22:33:31 +05:30
inlineLines: diffFile.highlighted_diff_lines,
parallelLines: diffFile.parallel_diff_lines,
2018-11-08 19:23:39 +05:30
contextLines: lines,
bottom,
lineNumbers,
});
},
[types.ADD_COLLAPSED_DIFFS](state, { file, data }) {
2019-02-13 22:33:31 +05:30
prepareDiffData(data);
const [newFileData] = data.diff_files.filter(f => f.file_hash === file.file_hash);
const selectedFile = state.diffFiles.find(f => f.file_hash === file.file_hash);
2018-11-20 20:47:30 +05:30
Object.assign(selectedFile, { ...newFileData });
2018-11-08 19:23:39 +05:30
},
[types.EXPAND_ALL_FILES](state) {
state.diffFiles = state.diffFiles.map(file => ({
...file,
collapsed: false,
}));
},
2018-11-20 20:47:30 +05:30
2018-12-13 13:39:08 +05:30
[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { discussion, diffPositionByLineCode }) {
const { latestDiff } = state;
const discussionLineCode = discussion.line_code;
const fileHash = discussion.diff_file.file_hash;
2019-02-13 22:33:31 +05:30
const lineCheck = line =>
line.line_code === discussionLineCode &&
2018-12-05 23:21:45 +05:30
isDiscussionApplicableToLine({
2018-12-13 13:39:08 +05:30
discussion,
2019-02-13 22:33:31 +05:30
diffPosition: diffPositionByLineCode[line.line_code],
2018-12-13 13:39:08 +05:30
latestDiff,
});
2019-02-13 22:33:31 +05:30
const mapDiscussions = (line, extraCheck = () => true) => ({
...line,
discussions: extraCheck()
? line.discussions
.filter(() => !line.discussions.some(({ id }) => discussion.id === id))
.concat(lineCheck(line) ? discussion : line.discussions)
: [],
});
2018-12-13 13:39:08 +05:30
state.diffFiles = state.diffFiles.map(diffFile => {
2019-02-13 22:33:31 +05:30
if (diffFile.file_hash === fileHash) {
2018-12-13 13:39:08 +05:30
const file = { ...diffFile };
2019-02-13 22:33:31 +05:30
if (file.highlighted_diff_lines) {
file.highlighted_diff_lines = file.highlighted_diff_lines.map(line =>
lineCheck(line) ? mapDiscussions(line) : line,
);
2018-11-20 20:47:30 +05:30
}
2019-02-13 22:33:31 +05:30
if (file.parallel_diff_lines) {
file.parallel_diff_lines = file.parallel_diff_lines.map(line => {
2018-12-13 13:39:08 +05:30
const left = line.left && lineCheck(line.left);
const right = line.right && lineCheck(line.right);
if (left || right) {
return {
2019-02-13 22:33:31 +05:30
left: line.left ? mapDiscussions(line.left) : null,
right: line.right ? mapDiscussions(line.right, () => !left) : null,
2018-12-13 13:39:08 +05:30
};
}
return line;
2018-11-20 20:47:30 +05:30
});
}
2018-12-13 13:39:08 +05:30
2019-02-13 22:33:31 +05:30
if (!file.parallel_diff_lines || !file.highlighted_diff_lines) {
file.discussions = (file.discussions || []).concat(discussion);
2018-12-13 13:39:08 +05:30
}
return file;
2018-11-20 20:47:30 +05:30
}
2018-12-13 13:39:08 +05:30
return diffFile;
});
2018-11-20 20:47:30 +05:30
},
2019-02-13 22:33:31 +05:30
[types.REMOVE_LINE_DISCUSSIONS_FOR_FILE](state, { fileHash, lineCode }) {
const selectedFile = state.diffFiles.find(f => f.file_hash === fileHash);
2018-11-20 20:47:30 +05:30
if (selectedFile) {
2019-02-13 22:33:31 +05:30
if (selectedFile.parallel_diff_lines) {
const targetLine = selectedFile.parallel_diff_lines.find(
2018-12-13 13:39:08 +05:30
line =>
2019-02-13 22:33:31 +05:30
(line.left && line.left.line_code === lineCode) ||
(line.right && line.right.line_code === lineCode),
2018-12-13 13:39:08 +05:30
);
if (targetLine) {
2019-02-13 22:33:31 +05:30
const side = targetLine.left && targetLine.left.line_code === lineCode ? 'left' : 'right';
2018-12-13 13:39:08 +05:30
Object.assign(targetLine[side], {
2019-02-13 22:33:31 +05:30
discussions: targetLine[side].discussions.filter(discussion => discussion.notes.length),
2018-12-13 13:39:08 +05:30
});
}
2018-11-20 20:47:30 +05:30
}
2019-02-13 22:33:31 +05:30
if (selectedFile.highlighted_diff_lines) {
const targetInlineLine = selectedFile.highlighted_diff_lines.find(
line => line.line_code === lineCode,
2018-11-20 20:47:30 +05:30
);
if (targetInlineLine) {
Object.assign(targetInlineLine, {
2019-02-13 22:33:31 +05:30
discussions: targetInlineLine.discussions.filter(discussion => discussion.notes.length),
2018-11-20 20:47:30 +05:30
});
}
}
2018-12-13 13:39:08 +05:30
if (selectedFile.discussions && selectedFile.discussions.length) {
selectedFile.discussions = selectedFile.discussions.filter(
2019-02-13 22:33:31 +05:30
discussion => discussion.notes.length,
2018-12-13 13:39:08 +05:30
);
}
2018-11-20 20:47:30 +05:30
}
},
2018-12-05 23:21:45 +05:30
[types.TOGGLE_FOLDER_OPEN](state, path) {
state.treeEntries[path].opened = !state.treeEntries[path].opened;
},
[types.TOGGLE_SHOW_TREE_LIST](state) {
state.showTreeList = !state.showTreeList;
},
[types.UPDATE_CURRENT_DIFF_FILE_ID](state, fileId) {
state.currentDiffFileId = fileId;
},
2018-12-13 13:39:08 +05:30
[types.OPEN_DIFF_FILE_COMMENT_FORM](state, formData) {
state.commentForms.push({
...formData,
});
},
[types.UPDATE_DIFF_FILE_COMMENT_FORM](state, formData) {
const { fileHash } = formData;
state.commentForms = state.commentForms.map(form => {
if (form.fileHash === fileHash) {
return {
...formData,
};
}
return form;
});
},
[types.CLOSE_DIFF_FILE_COMMENT_FORM](state, fileHash) {
state.commentForms = state.commentForms.filter(form => form.fileHash !== fileHash);
},
2019-02-13 22:33:31 +05:30
[types.SET_HIGHLIGHTED_ROW](state, lineCode) {
state.highlightedRow = lineCode;
},
2018-11-08 19:23:39 +05:30
};