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

574 lines
17 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import Vue from 'vue';
import Cookies from 'js-cookie';
2020-01-01 13:55:28 +05:30
import axios from '~/lib/utils/axios_utils';
2018-12-05 23:21:45 +05:30
import createFlash from '~/flash';
import { s__ } from '~/locale';
2019-02-15 15:39:39 +05:30
import { handleLocationHash, historyPushState, scrollToElement } from '~/lib/utils/common_utils';
2018-11-20 20:47:30 +05:30
import { mergeUrlParams, getLocationHash } from '~/lib/utils/url_utility';
2019-03-02 22:35:43 +05:30
import TreeWorker from '../workers/tree_worker';
2019-02-15 15:39:39 +05:30
import eventHub from '../../notes/event_hub';
2019-07-31 22:56:46 +05:30
import {
getDiffPositionByLineCode,
getNoteFormData,
convertExpandLines,
idleCallback,
2019-09-30 21:07:59 +05:30
allDiscussionWrappersExpanded,
2020-01-01 13:55:28 +05:30
prepareDiffData,
2019-07-31 22:56:46 +05:30
} from './utils';
2018-11-08 19:23:39 +05:30
import * as types from './mutation_types';
import {
PARALLEL_DIFF_VIEW_TYPE,
INLINE_DIFF_VIEW_TYPE,
DIFF_VIEW_COOKIE_NAME,
2018-12-05 23:21:45 +05:30
MR_TREE_SHOW_KEY,
2019-03-02 22:35:43 +05:30
TREE_LIST_STORAGE_KEY,
WHITESPACE_STORAGE_KEY,
2019-07-07 11:18:12 +05:30
TREE_LIST_WIDTH_STORAGE_KEY,
2019-07-31 22:56:46 +05:30
OLD_LINE_KEY,
NEW_LINE_KEY,
TYPE_KEY,
LEFT_LINE_KEY,
MAX_RENDERING_DIFF_LINES,
MAX_RENDERING_BULK_ROWS,
MIN_RENDERING_MS,
START_RENDERING_INDEX,
INLINE_DIFF_LINES_KEY,
PARALLEL_DIFF_LINES_KEY,
2020-01-01 13:55:28 +05:30
DIFFS_PER_PAGE,
2018-11-08 19:23:39 +05:30
} from '../constants';
2019-07-07 11:18:12 +05:30
import { diffViewerModes } from '~/ide/constants';
2018-11-08 19:23:39 +05:30
export const setBaseConfig = ({ commit }, options) => {
2020-01-01 13:55:28 +05:30
const {
endpoint,
endpointMetadata,
endpointBatch,
projectPath,
dismissEndpoint,
showSuggestPopover,
useSingleDiffStyle,
} = options;
commit(types.SET_BASE_CONFIG, {
endpoint,
endpointMetadata,
endpointBatch,
projectPath,
dismissEndpoint,
showSuggestPopover,
useSingleDiffStyle,
});
2018-11-08 19:23:39 +05:30
};
export const fetchDiffFiles = ({ state, commit }) => {
2019-03-02 22:35:43 +05:30
const worker = new TreeWorker();
2020-01-01 13:55:28 +05:30
const urlParams = {
w: state.showWhitespace ? '0' : '1',
};
2019-03-02 22:35:43 +05:30
2018-11-08 19:23:39 +05:30
commit(types.SET_LOADING, true);
2019-03-02 22:35:43 +05:30
worker.addEventListener('message', ({ data }) => {
commit(types.SET_TREE_DATA, data);
worker.terminate();
});
2018-11-08 19:23:39 +05:30
return axios
2020-01-01 13:55:28 +05:30
.get(mergeUrlParams(urlParams, state.endpoint))
2018-11-08 19:23:39 +05:30
.then(res => {
commit(types.SET_LOADING, false);
2020-01-01 13:55:28 +05:30
2018-11-08 19:23:39 +05:30
commit(types.SET_MERGE_REQUEST_DIFFS, res.data.merge_request_diffs || []);
commit(types.SET_DIFF_DATA, res.data);
2019-03-02 22:35:43 +05:30
worker.postMessage(state.diffFiles);
2018-11-08 19:23:39 +05:30
return Vue.nextTick();
})
2019-03-02 22:35:43 +05:30
.then(handleLocationHash)
.catch(() => worker.terminate());
2018-11-08 19:23:39 +05:30
};
2020-01-01 13:55:28 +05:30
export const fetchDiffFilesBatch = ({ commit, state }) => {
commit(types.SET_BATCH_LOADING, true);
const getBatch = page =>
axios
.get(state.endpointBatch, {
params: { page, per_page: DIFFS_PER_PAGE, w: state.showWhitespace ? '0' : '1' },
})
.then(({ data: { pagination, diff_files } }) => {
commit(types.SET_DIFF_DATA_BATCH, { diff_files });
commit(types.SET_BATCH_LOADING, false);
return pagination.next_page;
})
.then(nextPage => nextPage && getBatch(nextPage));
return getBatch()
.then(handleLocationHash)
.catch(() => null);
};
export const fetchDiffFilesMeta = ({ commit, state }) => {
const worker = new TreeWorker();
commit(types.SET_LOADING, true);
worker.addEventListener('message', ({ data }) => {
commit(types.SET_TREE_DATA, data);
worker.terminate();
});
return axios
.get(state.endpointMetadata)
.then(({ data }) => {
const strippedData = { ...data };
delete strippedData.diff_files;
commit(types.SET_LOADING, false);
commit(types.SET_MERGE_REQUEST_DIFFS, data.merge_request_diffs || []);
commit(types.SET_DIFF_DATA, strippedData);
prepareDiffData(data);
worker.postMessage(data.diff_files);
})
.catch(() => worker.terminate());
};
2019-02-15 15:39:39 +05:30
export const setHighlightedRow = ({ commit }, lineCode) => {
2019-07-07 11:18:12 +05:30
const fileHash = lineCode.split('_')[0];
2019-02-15 15:39:39 +05:30
commit(types.SET_HIGHLIGHTED_ROW, lineCode);
2019-07-07 11:18:12 +05:30
commit(types.UPDATE_CURRENT_DIFF_FILE_ID, fileHash);
2019-02-15 15:39:39 +05:30
};
2018-11-20 20:47:30 +05:30
// This is adding line discussions to the actual lines in the diff tree
// once for parallel and once for inline mode
2018-12-13 13:39:08 +05:30
export const assignDiscussionsToDiff = (
{ commit, state, rootState },
discussions = rootState.notes.discussions,
) => {
2018-11-20 20:47:30 +05:30
const diffPositionByLineCode = getDiffPositionByLineCode(state.diffFiles);
2019-09-30 21:07:59 +05:30
const hash = getLocationHash();
2018-11-20 20:47:30 +05:30
2019-02-15 15:39:39 +05:30
discussions
.filter(discussion => discussion.diff_discussion)
.forEach(discussion => {
commit(types.SET_LINE_DISCUSSIONS_FOR_FILE, {
discussion,
diffPositionByLineCode,
2019-09-30 21:07:59 +05:30
hash,
2019-02-15 15:39:39 +05:30
});
2018-12-13 13:39:08 +05:30
});
2019-02-15 15:39:39 +05:30
Vue.nextTick(() => {
eventHub.$emit('scrollToDiscussion');
2018-11-20 20:47:30 +05:30
});
};
export const removeDiscussionsFromDiff = ({ commit }, removeDiscussion) => {
2019-02-15 15:39:39 +05:30
const { file_hash, line_code, id } = removeDiscussion;
commit(types.REMOVE_LINE_DISCUSSIONS_FOR_FILE, { fileHash: file_hash, lineCode: line_code, id });
};
2019-09-30 21:07:59 +05:30
export const toggleLineDiscussions = ({ commit }, options) => {
commit(types.TOGGLE_LINE_DISCUSSIONS, options);
};
2019-02-15 15:39:39 +05:30
export const renderFileForDiscussionId = ({ commit, rootState, state }, discussionId) => {
const discussion = rootState.notes.discussions.find(d => d.id === discussionId);
2019-10-12 21:52:04 +05:30
if (discussion && discussion.diff_file) {
2019-02-15 15:39:39 +05:30
const file = state.diffFiles.find(f => f.file_hash === discussion.diff_file.file_hash);
if (file) {
if (!file.renderIt) {
commit(types.RENDER_FILE, file);
}
2019-07-07 11:18:12 +05:30
if (file.viewer.collapsed) {
2019-02-15 15:39:39 +05:30
eventHub.$emit(`loadCollapsedDiff/${file.file_hash}`);
scrollToElement(document.getElementById(file.file_hash));
} else {
eventHub.$emit('scrollToDiscussion');
}
}
}
2018-11-20 20:47:30 +05:30
};
export const startRenderDiffsQueue = ({ state, commit }) => {
const checkItem = () =>
new Promise(resolve => {
const nextFile = state.diffFiles.find(
2019-07-07 11:18:12 +05:30
file =>
2019-09-04 21:01:54 +05:30
!file.renderIt &&
(file.viewer && (!file.viewer.collapsed || !file.viewer.name === diffViewerModes.text)),
2018-11-20 20:47:30 +05:30
);
if (nextFile) {
requestAnimationFrame(() => {
commit(types.RENDER_FILE, nextFile);
});
requestIdleCallback(
() => {
checkItem()
.then(resolve)
.catch(() => {});
},
{ timeout: 1000 },
);
} else {
resolve();
}
});
return checkItem();
};
2019-07-07 11:18:12 +05:30
export const setRenderIt = ({ commit }, file) => commit(types.RENDER_FILE, file);
2018-11-08 19:23:39 +05:30
export const setInlineDiffViewType = ({ commit }) => {
commit(types.SET_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE);
Cookies.set(DIFF_VIEW_COOKIE_NAME, INLINE_DIFF_VIEW_TYPE);
const url = mergeUrlParams({ view: INLINE_DIFF_VIEW_TYPE }, window.location.href);
historyPushState(url);
};
export const setParallelDiffViewType = ({ commit }) => {
commit(types.SET_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE);
Cookies.set(DIFF_VIEW_COOKIE_NAME, PARALLEL_DIFF_VIEW_TYPE);
const url = mergeUrlParams({ view: PARALLEL_DIFF_VIEW_TYPE }, window.location.href);
historyPushState(url);
};
2019-02-15 15:39:39 +05:30
export const showCommentForm = ({ commit }, { lineCode, fileHash }) => {
commit(types.TOGGLE_LINE_HAS_FORM, { lineCode, fileHash, hasForm: true });
2018-11-08 19:23:39 +05:30
};
2019-02-15 15:39:39 +05:30
export const cancelCommentForm = ({ commit }, { lineCode, fileHash }) => {
commit(types.TOGGLE_LINE_HAS_FORM, { lineCode, fileHash, hasForm: false });
2018-11-08 19:23:39 +05:30
};
export const loadMoreLines = ({ commit }, options) => {
2019-10-12 21:52:04 +05:30
const { endpoint, params, lineNumbers, fileHash, isExpandDown, nextLineNumbers } = options;
2018-11-08 19:23:39 +05:30
params.from_merge_request = true;
return axios.get(endpoint, { params }).then(res => {
const contextLines = res.data || [];
commit(types.ADD_CONTEXT_LINES, {
lineNumbers,
contextLines,
params,
fileHash,
2019-10-12 21:52:04 +05:30
isExpandDown,
nextLineNumbers,
2018-11-08 19:23:39 +05:30
});
});
};
2018-11-20 20:47:30 +05:30
export const scrollToLineIfNeededInline = (_, line) => {
const hash = getLocationHash();
2019-02-15 15:39:39 +05:30
if (hash && line.line_code === hash) {
2018-11-20 20:47:30 +05:30
handleLocationHash();
}
};
export const scrollToLineIfNeededParallel = (_, line) => {
const hash = getLocationHash();
if (
hash &&
2019-02-15 15:39:39 +05:30
((line.left && line.left.line_code === hash) || (line.right && line.right.line_code === hash))
2018-11-20 20:47:30 +05:30
) {
handleLocationHash();
}
};
2019-09-04 21:01:54 +05:30
export const loadCollapsedDiff = ({ commit, getters, state }, file) =>
2019-02-15 15:39:39 +05:30
axios
.get(file.load_collapsed_diff_url, {
params: {
commit_id: getters.commitId,
2019-09-04 21:01:54 +05:30
w: state.showWhitespace ? '0' : '1',
2019-02-15 15:39:39 +05:30
},
})
.then(res => {
commit(types.ADD_COLLAPSED_DIFFS, {
file,
data: res.data,
});
2018-11-08 19:23:39 +05:30
});
export const expandAllFiles = ({ commit }) => {
commit(types.EXPAND_ALL_FILES);
};
/**
* Toggles the file discussions after user clicked on the toggle discussions button.
*
* Gets the discussions for the provided diff.
*
* If all discussions are expanded, it will collapse all of them
* If all discussions are collapsed, it will expand all of them
* If some discussions are open and others closed, it will expand the closed ones.
*
* @param {Object} diff
*/
export const toggleFileDiscussions = ({ getters, dispatch }, diff) => {
const discussions = getters.getDiffFileDiscussions(diff);
const shouldCloseAll = getters.diffHasAllExpandedDiscussions(diff);
2019-02-15 15:39:39 +05:30
const shouldExpandAll = getters.diffHasAllCollapsedDiscussions(diff);
2018-11-08 19:23:39 +05:30
discussions.forEach(discussion => {
const data = { discussionId: discussion.id };
if (shouldCloseAll) {
dispatch('collapseDiscussion', data, { root: true });
} else if (shouldExpandAll || (!shouldCloseAll && !shouldExpandAll && !discussion.expanded)) {
dispatch('expandDiscussion', data, { root: true });
}
});
};
2019-09-30 21:07:59 +05:30
export const toggleFileDiscussionWrappers = ({ commit }, diff) => {
const discussionWrappersExpanded = allDiscussionWrappersExpanded(diff);
let linesWithDiscussions;
if (diff.highlighted_diff_lines) {
linesWithDiscussions = diff.highlighted_diff_lines.filter(line => line.discussions.length);
}
if (diff.parallel_diff_lines) {
linesWithDiscussions = diff.parallel_diff_lines.filter(
line =>
(line.left && line.left.discussions.length) ||
(line.right && line.right.discussions.length),
);
}
if (linesWithDiscussions.length) {
linesWithDiscussions.forEach(line => {
commit(types.TOGGLE_LINE_DISCUSSIONS, {
fileHash: diff.file_hash,
lineCode: line.line_code,
expanded: !discussionWrappersExpanded,
});
});
}
};
2019-02-15 15:39:39 +05:30
export const saveDiffDiscussion = ({ state, dispatch }, { note, formData }) => {
2018-12-05 23:21:45 +05:30
const postData = getNoteFormData({
2019-02-15 15:39:39 +05:30
commit: state.commit,
2018-12-05 23:21:45 +05:30
note,
...formData,
});
return dispatch('saveNote', postData, { root: true })
.then(result => dispatch('updateDiscussion', result.discussion, { root: true }))
2018-12-13 13:39:08 +05:30
.then(discussion => dispatch('assignDiscussionsToDiff', [discussion]))
2019-09-30 21:07:59 +05:30
.then(() => dispatch('updateResolvableDiscussionsCounts', null, { root: true }))
2019-02-15 15:39:39 +05:30
.then(() => dispatch('closeDiffFileCommentForm', formData.diffFile.file_hash))
2018-12-05 23:21:45 +05:30
.catch(() => createFlash(s__('MergeRequests|Saving the comment failed')));
};
export const toggleTreeOpen = ({ commit }, path) => {
commit(types.TOGGLE_FOLDER_OPEN, path);
};
export const scrollToFile = ({ state, commit }, path) => {
const { fileHash } = state.treeEntries[path];
document.location.hash = fileHash;
commit(types.UPDATE_CURRENT_DIFF_FILE_ID, fileHash);
};
2019-07-07 11:18:12 +05:30
export const toggleShowTreeList = ({ commit, state }, saving = true) => {
2018-12-05 23:21:45 +05:30
commit(types.TOGGLE_SHOW_TREE_LIST);
2019-07-07 11:18:12 +05:30
if (saving) {
localStorage.setItem(MR_TREE_SHOW_KEY, state.showTreeList);
}
2018-12-05 23:21:45 +05:30
};
2018-12-13 13:39:08 +05:30
export const openDiffFileCommentForm = ({ commit, getters }, formData) => {
const form = getters.getCommentFormForDiffFile(formData.fileHash);
if (form) {
commit(types.UPDATE_DIFF_FILE_COMMENT_FORM, formData);
} else {
commit(types.OPEN_DIFF_FILE_COMMENT_FORM, formData);
}
};
export const closeDiffFileCommentForm = ({ commit }, fileHash) => {
commit(types.CLOSE_DIFF_FILE_COMMENT_FORM, fileHash);
};
2019-03-02 22:35:43 +05:30
export const setRenderTreeList = ({ commit }, renderTreeList) => {
commit(types.SET_RENDER_TREE_LIST, renderTreeList);
localStorage.setItem(TREE_LIST_STORAGE_KEY, renderTreeList);
};
export const setShowWhitespace = ({ commit }, { showWhitespace, pushState = false }) => {
commit(types.SET_SHOW_WHITESPACE, showWhitespace);
localStorage.setItem(WHITESPACE_STORAGE_KEY, showWhitespace);
if (pushState) {
2019-09-04 21:01:54 +05:30
historyPushState(mergeUrlParams({ w: showWhitespace ? '0' : '1' }, window.location.href));
2019-03-02 22:35:43 +05:30
}
2019-09-04 21:01:54 +05:30
eventHub.$emit('refetchDiffData');
2019-03-02 22:35:43 +05:30
};
export const toggleFileFinder = ({ commit }, visible) => {
commit(types.TOGGLE_FILE_FINDER_VISIBLE, visible);
};
2019-07-07 11:18:12 +05:30
export const cacheTreeListWidth = (_, size) => {
localStorage.setItem(TREE_LIST_WIDTH_STORAGE_KEY, size);
};
export const requestFullDiff = ({ commit }, filePath) => commit(types.REQUEST_FULL_DIFF, filePath);
2019-07-31 22:56:46 +05:30
export const receiveFullDiffSucess = ({ commit }, { filePath }) =>
commit(types.RECEIVE_FULL_DIFF_SUCCESS, { filePath });
2019-07-07 11:18:12 +05:30
export const receiveFullDiffError = ({ commit }, filePath) => {
commit(types.RECEIVE_FULL_DIFF_ERROR, filePath);
createFlash(s__('MergeRequest|Error loading full diff. Please try again.'));
};
2019-07-31 22:56:46 +05:30
export const setExpandedDiffLines = ({ commit, state }, { file, data }) => {
const expandedDiffLines = {
highlighted_diff_lines: convertExpandLines({
diffLines: file.highlighted_diff_lines,
typeKey: TYPE_KEY,
oldLineKey: OLD_LINE_KEY,
newLineKey: NEW_LINE_KEY,
data,
mapLine: ({ line, oldLine, newLine }) =>
Object.assign(line, {
old_line: oldLine,
new_line: newLine,
line_code: `${file.file_hash}_${oldLine}_${newLine}`,
}),
}),
parallel_diff_lines: convertExpandLines({
diffLines: file.parallel_diff_lines,
typeKey: [LEFT_LINE_KEY, TYPE_KEY],
oldLineKey: [LEFT_LINE_KEY, OLD_LINE_KEY],
newLineKey: [LEFT_LINE_KEY, NEW_LINE_KEY],
data,
mapLine: ({ line, oldLine, newLine }) => ({
left: {
...line,
old_line: oldLine,
line_code: `${file.file_hash}_${oldLine}_${newLine}`,
},
right: {
...line,
new_line: newLine,
line_code: `${file.file_hash}_${newLine}_${oldLine}`,
},
}),
}),
};
const currentDiffLinesKey =
state.diffViewType === INLINE_DIFF_VIEW_TYPE ? INLINE_DIFF_LINES_KEY : PARALLEL_DIFF_LINES_KEY;
const hiddenDiffLinesKey =
state.diffViewType === INLINE_DIFF_VIEW_TYPE ? PARALLEL_DIFF_LINES_KEY : INLINE_DIFF_LINES_KEY;
commit(types.SET_HIDDEN_VIEW_DIFF_FILE_LINES, {
filePath: file.file_path,
lines: expandedDiffLines[hiddenDiffLinesKey],
});
if (expandedDiffLines[currentDiffLinesKey].length > MAX_RENDERING_DIFF_LINES) {
let index = START_RENDERING_INDEX;
commit(types.SET_CURRENT_VIEW_DIFF_FILE_LINES, {
filePath: file.file_path,
lines: expandedDiffLines[currentDiffLinesKey].slice(0, index),
});
commit(types.TOGGLE_DIFF_FILE_RENDERING_MORE, file.file_path);
const idleCb = t => {
const startIndex = index;
while (
t.timeRemaining() >= MIN_RENDERING_MS &&
index !== expandedDiffLines[currentDiffLinesKey].length &&
index - startIndex !== MAX_RENDERING_BULK_ROWS
) {
const line = expandedDiffLines[currentDiffLinesKey][index];
if (line) {
commit(types.ADD_CURRENT_VIEW_DIFF_FILE_LINES, { filePath: file.file_path, line });
index += 1;
}
}
if (index !== expandedDiffLines[currentDiffLinesKey].length) {
idleCallback(idleCb);
} else {
commit(types.TOGGLE_DIFF_FILE_RENDERING_MORE, file.file_path);
}
};
idleCallback(idleCb);
} else {
commit(types.SET_CURRENT_VIEW_DIFF_FILE_LINES, {
filePath: file.file_path,
lines: expandedDiffLines[currentDiffLinesKey],
});
}
};
2019-07-07 11:18:12 +05:30
export const fetchFullDiff = ({ dispatch }, file) =>
axios
.get(file.context_lines_path, {
params: {
full: true,
from_merge_request: true,
},
})
2019-07-31 22:56:46 +05:30
.then(({ data }) => {
dispatch('receiveFullDiffSucess', { filePath: file.file_path });
dispatch('setExpandedDiffLines', { file, data });
})
2019-07-07 11:18:12 +05:30
.catch(() => dispatch('receiveFullDiffError', file.file_path));
export const toggleFullDiff = ({ dispatch, getters, state }, filePath) => {
const file = state.diffFiles.find(f => f.file_path === filePath);
dispatch('requestFullDiff', filePath);
if (file.isShowingFullFile) {
dispatch('loadCollapsedDiff', file)
.then(() => dispatch('assignDiscussionsToDiff', getters.getDiffFileDiscussions(file)))
.catch(() => dispatch('receiveFullDiffError', filePath));
} else {
dispatch('fetchFullDiff', file);
}
};
export const setFileCollapsed = ({ commit }, { filePath, collapsed }) =>
commit(types.SET_FILE_COLLAPSED, { filePath, collapsed });
2019-09-04 21:01:54 +05:30
export const setSuggestPopoverDismissed = ({ commit, state }) =>
axios
.post(state.dismissEndpoint, {
feature_name: 'suggest_popover_dismissed',
})
.then(() => {
commit(types.SET_SHOW_SUGGEST_POPOVER);
})
.catch(() => {
createFlash(s__('MergeRequest|Error dismissing suggestion popover. Please try again.'));
});
2018-11-08 19:23:39 +05:30
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};