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

136 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { __, n__ } from '~/locale';
2018-11-08 19:23:39 +05:30
import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '../constants';
2020-04-22 19:07:51 +05:30
export * from './getters_versions_dropdowns';
2018-11-08 19:23:39 +05:30
export const isParallelView = state => state.diffViewType === PARALLEL_DIFF_VIEW_TYPE;
export const isInlineView = state => state.diffViewType === INLINE_DIFF_VIEW_TYPE;
2019-07-07 11:18:12 +05:30
export const hasCollapsedFile = state =>
state.diffFiles.some(file => file.viewer && file.viewer.collapsed);
2018-11-08 19:23:39 +05:30
export const commitId = state => (state.commit && state.commit.id ? state.commit.id : null);
/**
* Checks if the diff has all discussions expanded
* @param {Object} diff
* @returns {Boolean}
*/
export const diffHasAllExpandedDiscussions = (state, getters) => diff => {
const discussions = getters.getDiffFileDiscussions(diff);
2018-11-20 20:47:30 +05:30
return (
(discussions && discussions.length && discussions.every(discussion => discussion.expanded)) ||
false
);
2018-11-08 19:23:39 +05:30
};
/**
2019-02-15 15:39:39 +05:30
* Checks if the diff has all discussions collapsed
2018-11-08 19:23:39 +05:30
* @param {Object} diff
* @returns {Boolean}
*/
2019-02-15 15:39:39 +05:30
export const diffHasAllCollapsedDiscussions = (state, getters) => diff => {
2018-11-08 19:23:39 +05:30
const discussions = getters.getDiffFileDiscussions(diff);
2018-11-20 20:47:30 +05:30
return (
(discussions && discussions.length && discussions.every(discussion => !discussion.expanded)) ||
false
);
2018-11-08 19:23:39 +05:30
};
/**
* Checks if the diff has any open discussions
* @param {Object} diff
* @returns {Boolean}
*/
export const diffHasExpandedDiscussions = (state, getters) => diff => {
const discussions = getters.getDiffFileDiscussions(diff);
return (
2018-11-20 20:47:30 +05:30
(discussions &&
discussions.length &&
discussions.find(discussion => discussion.expanded) !== undefined) ||
2018-11-08 19:23:39 +05:30
false
);
};
2018-11-18 11:00:15 +05:30
/**
* Checks if the diff has any discussion
* @param {Boolean} diff
* @returns {Boolean}
*/
export const diffHasDiscussions = (state, getters) => diff =>
getters.getDiffFileDiscussions(diff).length > 0;
2018-11-08 19:23:39 +05:30
/**
* Returns an array with the discussions of the given diff
* @param {Object} diff
* @returns {Array}
*/
export const getDiffFileDiscussions = (state, getters, rootState, rootGetters) => diff =>
rootGetters.discussions.filter(
2019-02-15 15:39:39 +05:30
discussion => discussion.diff_discussion && discussion.diff_file.file_hash === diff.file_hash,
2018-11-08 19:23:39 +05:30
) || [];
// prevent babel-plugin-rewire from generating an invalid default during karma∂ tests
export const getDiffFileByHash = state => fileHash =>
2019-02-15 15:39:39 +05:30
state.diffFiles.find(file => file.file_hash === fileHash);
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
export const flatBlobsList = state =>
Object.values(state.treeEntries).filter(f => f.type === 'blob');
export const allBlobs = (state, getters) =>
getters.flatBlobsList.reduce((acc, file) => {
const { parentPath } = file;
if (parentPath && !acc.some(f => f.path === parentPath)) {
acc.push({
path: parentPath,
isHeader: true,
tree: [],
});
}
acc.find(f => f.path === parentPath).tree.push(file);
return acc;
}, []);
2018-12-05 23:21:45 +05:30
2018-12-13 13:39:08 +05:30
export const getCommentFormForDiffFile = state => fileHash =>
state.commentForms.find(form => form.fileHash === fileHash);
2020-04-08 14:13:33 +05:30
/**
* Returns the test coverage hits for a specific line of a given file
* @param {string} file
* @param {number} line
* @returns {number}
*/
export const fileLineCoverage = state => (file, line) => {
if (!state.coverageFiles.files) return {};
const fileCoverage = state.coverageFiles.files[file];
if (!fileCoverage) return {};
const lineCoverage = fileCoverage[String(line)];
if (lineCoverage === 0) {
return { text: __('No test coverage'), class: 'no-coverage' };
} else if (lineCoverage >= 0) {
return {
text: n__('Test coverage: %d hit', 'Test coverage: %d hits', lineCoverage),
class: 'coverage',
};
}
return {};
};
2019-07-07 11:18:12 +05:30
/**
* Returns index of a currently selected diff in diffFiles
* @returns {number}
*/
export const currentDiffIndex = state =>
Math.max(0, state.diffFiles.findIndex(diff => diff.file_hash === state.currentDiffFileId));
2018-11-08 19:23:39 +05:30
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};