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

115 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import _ from 'underscore';
import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '../constants';
export const isParallelView = state => state.diffViewType === PARALLEL_DIFF_VIEW_TYPE;
export const isInlineView = state => state.diffViewType === INLINE_DIFF_VIEW_TYPE;
export const areAllFilesCollapsed = state => state.diffFiles.every(file => file.collapsed);
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
};
/**
* Checks if the diff has all discussions collpased
* @param {Object} diff
* @returns {Boolean}
*/
export const diffHasAllCollpasedDiscussions = (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
};
/**
* 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(
discussion =>
discussion.diff_discussion && _.isEqual(discussion.diff_file.file_hash, diff.fileHash),
) || [];
2018-11-20 20:47:30 +05:30
export const shouldRenderParallelCommentRow = state => line => {
const hasDiscussion =
(line.left && line.left.discussions && line.left.discussions.length) ||
(line.right && line.right.discussions && line.right.discussions.length);
2018-11-18 11:00:15 +05:30
2018-11-20 20:47:30 +05:30
const hasExpandedDiscussionOnLeft =
line.left && line.left.discussions && line.left.discussions.length
? line.left.discussions.every(discussion => discussion.expanded)
: false;
const hasExpandedDiscussionOnRight =
line.right && line.right.discussions && line.right.discussions.length
? line.right.discussions.every(discussion => discussion.expanded)
: false;
2018-11-18 11:00:15 +05:30
if (hasDiscussion && (hasExpandedDiscussionOnLeft || hasExpandedDiscussionOnRight)) {
return true;
}
2018-11-20 20:47:30 +05:30
const hasCommentFormOnLeft = line.left && state.diffLineCommentForms[line.left.lineCode];
const hasCommentFormOnRight = line.right && state.diffLineCommentForms[line.right.lineCode];
2018-11-18 11:00:15 +05:30
return hasCommentFormOnLeft || hasCommentFormOnRight;
};
2018-11-20 20:47:30 +05:30
export const shouldRenderInlineCommentRow = state => line => {
2018-11-18 11:00:15 +05:30
if (state.diffLineCommentForms[line.lineCode]) return true;
2018-11-20 20:47:30 +05:30
if (!line.discussions || line.discussions.length === 0) {
2018-11-18 11:00:15 +05:30
return false;
}
2018-11-20 20:47:30 +05:30
return line.discussions.every(discussion => discussion.expanded);
2018-11-18 11:00:15 +05:30
};
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 =>
state.diffFiles.find(file => file.fileHash === fileHash);
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};