debian-mirror-gitlab/app/assets/javascripts/boards/boards_util.js

173 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import { sortBy } from 'lodash';
2020-10-24 23:57:45 +05:30
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
2021-03-11 19:13:27 +05:30
import { ListType, NOT_FILTER } from './constants';
2020-10-24 23:57:45 +05:30
2019-07-31 22:56:46 +05:30
export function getMilestone() {
return null;
}
2021-02-22 17:27:13 +05:30
export function updateListPosition(listObj) {
const { listType } = listObj;
let { position } = listObj;
if (listType === ListType.closed) {
position = Infinity;
} else if (listType === ListType.backlog) {
position = -Infinity;
}
return { ...listObj, position };
}
2021-01-03 14:25:43 +05:30
export function formatBoardLists(lists) {
2021-02-22 17:27:13 +05:30
return lists.nodes.reduce((map, list) => {
2021-01-03 14:25:43 +05:30
return {
...map,
2021-02-22 17:27:13 +05:30
[list.id]: updateListPosition(list),
2021-01-03 14:25:43 +05:30
};
}, {});
}
2020-11-24 15:15:51 +05:30
export function formatIssue(issue) {
2021-02-22 17:27:13 +05:30
return {
2020-11-24 15:15:51 +05:30
...issue,
labels: issue.labels?.nodes || [],
assignees: issue.assignees?.nodes || [],
2021-02-22 17:27:13 +05:30
};
2020-11-24 15:15:51 +05:30
}
2020-10-24 23:57:45 +05:30
export function formatListIssues(listIssues) {
2020-11-24 15:15:51 +05:30
const issues = {};
2021-01-03 14:25:43 +05:30
let listIssuesCount;
2020-11-24 15:15:51 +05:30
const listData = listIssues.nodes.reduce((map, list) => {
2021-01-03 14:25:43 +05:30
listIssuesCount = list.issues.count;
2021-03-08 18:12:59 +05:30
let sortedIssues = list.issues.edges.map((issueNode) => ({
2021-01-03 14:25:43 +05:30
...issueNode.node,
}));
sortedIssues = sortBy(sortedIssues, 'relativePosition');
2020-10-24 23:57:45 +05:30
return {
...map,
2021-03-08 18:12:59 +05:30
[list.id]: sortedIssues.map((i) => {
2020-11-24 15:15:51 +05:30
const id = getIdFromGraphQLId(i.id);
2021-02-22 17:27:13 +05:30
const listIssue = {
2020-11-24 15:15:51 +05:30
...i,
id,
labels: i.labels?.nodes || [],
assignees: i.assignees?.nodes || [],
2021-02-22 17:27:13 +05:30
};
2020-11-24 15:15:51 +05:30
issues[id] = listIssue;
return id;
}),
2020-10-24 23:57:45 +05:30
};
}, {});
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
return { listData, issues, listIssuesCount };
}
export function formatListsPageInfo(lists) {
const listData = lists.nodes.reduce((map, list) => {
return {
...map,
[list.id]: list.issues.pageInfo,
};
}, {});
return listData;
2020-11-24 15:15:51 +05:30
}
export function fullBoardId(boardId) {
return `gid://gitlab/Board/${boardId}`;
}
2021-03-08 18:12:59 +05:30
export function fullIterationId(id) {
return `gid://gitlab/Iteration/${id}`;
}
export function fullUserId(id) {
return `gid://gitlab/User/${id}`;
}
export function fullMilestoneId(id) {
return `gid://gitlab/Milestone/${id}`;
}
2021-01-03 14:25:43 +05:30
export function fullLabelId(label) {
2021-03-08 18:12:59 +05:30
if (label.project_id && label.project_id !== null) {
2021-01-03 14:25:43 +05:30
return `gid://gitlab/ProjectLabel/${label.id}`;
}
return `gid://gitlab/GroupLabel/${label.id}`;
}
2021-03-08 18:12:59 +05:30
export function formatIssueInput(issueInput, boardConfig) {
const { labelIds = [], assigneeIds = [] } = issueInput;
const { labels, assigneeId, milestoneId } = boardConfig;
return {
milestoneId: milestoneId ? fullMilestoneId(milestoneId) : null,
...issueInput,
labelIds: [...labelIds, ...(labels?.map((l) => fullLabelId(l)) || [])],
assigneeIds: [...assigneeIds, ...(assigneeId ? [fullUserId(assigneeId)] : [])],
};
}
2020-11-24 15:15:51 +05:30
export function moveIssueListHelper(issue, fromList, toList) {
2021-02-22 17:27:13 +05:30
const updatedIssue = issue;
if (
toList.listType === ListType.label &&
2021-03-08 18:12:59 +05:30
!updatedIssue.labels.find((label) => label.id === toList.label.id)
2021-02-22 17:27:13 +05:30
) {
updatedIssue.labels.push(toList.label);
2020-11-24 15:15:51 +05:30
}
2021-02-22 17:27:13 +05:30
if (fromList?.label && fromList.listType === ListType.label) {
2021-03-08 18:12:59 +05:30
updatedIssue.labels = updatedIssue.labels.filter((label) => fromList.label.id !== label.id);
2020-11-24 15:15:51 +05:30
}
2021-02-22 17:27:13 +05:30
if (
toList.listType === ListType.assignee &&
2021-03-08 18:12:59 +05:30
!updatedIssue.assignees.find((assignee) => assignee.id === toList.assignee.id)
2021-02-22 17:27:13 +05:30
) {
updatedIssue.assignees.push(toList.assignee);
}
if (fromList?.assignee && fromList.listType === ListType.assignee) {
updatedIssue.assignees = updatedIssue.assignees.filter(
2021-03-08 18:12:59 +05:30
(assignee) => assignee.id !== fromList.assignee.id,
2021-02-22 17:27:13 +05:30
);
2020-11-24 15:15:51 +05:30
}
2021-02-22 17:27:13 +05:30
return updatedIssue;
}
export function isListDraggable(list) {
return list.listType !== ListType.backlog && list.listType !== ListType.closed;
}
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
export function transformNotFilters(filters) {
return Object.keys(filters)
.filter((key) => key.startsWith(NOT_FILTER))
.reduce((obj, key) => {
return {
...obj,
[key.substring(4, key.length - 1)]: filters[key],
};
}, {});
}
2021-02-22 17:27:13 +05:30
// EE-specific feature. Find the implementation in the `ee/`-folder
export function transformBoardConfig() {
return '';
2020-10-24 23:57:45 +05:30
}
2019-07-31 22:56:46 +05:30
export default {
getMilestone,
2020-11-24 15:15:51 +05:30
formatIssue,
2020-10-24 23:57:45 +05:30
formatListIssues,
2020-11-24 15:15:51 +05:30
fullBoardId,
2021-01-03 14:25:43 +05:30
fullLabelId,
2021-03-08 18:12:59 +05:30
fullIterationId,
2021-02-22 17:27:13 +05:30
isListDraggable,
2021-03-11 19:13:27 +05:30
transformNotFilters,
2019-07-31 22:56:46 +05:30
};