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

233 lines
6 KiB
JavaScript
Raw Normal View History

2021-04-29 21:17:54 +05:30
import { sortBy, cloneDeep } from 'lodash';
2020-10-24 23:57:45 +05:30
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
2021-06-08 01:23:25 +05:30
import { ListType, NOT_FILTER, AssigneeIdParamValues } 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) {
2021-04-17 20:07:23 +05:30
const boardItems = {};
let listItemsCount;
2020-11-24 15:15:51 +05:30
const listData = listIssues.nodes.reduce((map, list) => {
2021-04-17 20:07:23 +05:30
listItemsCount = 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
2021-04-17 20:07:23 +05:30
boardItems[id] = listIssue;
2020-11-24 15:15:51 +05:30
return id;
}),
2020-10-24 23:57:45 +05:30
};
}, {});
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
return { listData, boardItems, listItemsCount };
2021-01-03 14:25:43 +05:30
}
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)] : [])],
};
}
2021-04-29 21:17:54 +05:30
export function shouldCloneCard(fromListType, toListType) {
const involvesClosed = fromListType === ListType.closed || toListType === ListType.closed;
const involvesBacklog = fromListType === ListType.backlog || toListType === ListType.backlog;
if (involvesClosed || involvesBacklog) {
return false;
}
if (fromListType !== toListType) {
return true;
}
return false;
}
export function getMoveData(state, params) {
const { boardItems, boardItemsByListId, boardLists } = state;
const { itemId, fromListId, toListId } = params;
const fromListType = boardLists[fromListId].listType;
const toListType = boardLists[toListId].listType;
return {
reordering: fromListId === toListId,
shouldClone: shouldCloneCard(fromListType, toListType),
itemNotInToList: !boardItemsByListId[toListId].includes(itemId),
originalIssue: cloneDeep(boardItems[itemId]),
originalIndex: boardItemsByListId[fromListId].indexOf(itemId),
...params,
};
}
2021-04-17 20:07:23 +05:30
export function moveItemListHelper(item, fromList, toList) {
const updatedItem = item;
2021-02-22 17:27:13 +05:30
if (
toList.listType === ListType.label &&
2021-04-17 20:07:23 +05:30
!updatedItem.labels.find((label) => label.id === toList.label.id)
2021-02-22 17:27:13 +05:30
) {
2021-04-17 20:07:23 +05:30
updatedItem.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-04-17 20:07:23 +05:30
updatedItem.labels = updatedItem.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-04-17 20:07:23 +05:30
!updatedItem.assignees.find((assignee) => assignee.id === toList.assignee.id)
2021-02-22 17:27:13 +05:30
) {
2021-04-17 20:07:23 +05:30
updatedItem.assignees.push(toList.assignee);
2021-02-22 17:27:13 +05:30
}
if (fromList?.assignee && fromList.listType === ListType.assignee) {
2021-04-17 20:07:23 +05:30
updatedItem.assignees = updatedItem.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
2021-04-17 20:07:23 +05:30
return updatedItem;
2021-02-22 17:27:13 +05:30
}
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-06-08 01:23:25 +05:30
export function getSupportedParams(filters, supportedFilters) {
return supportedFilters.reduce((acc, f) => {
/**
* TODO the API endpoint for the classic boards
* accepts assignee wildcard value as 'assigneeId' param -
* while the GraphQL query accepts the value in 'assigneWildcardId' field.
* Once we deprecate the classics boards,
* we should change the filtered search bar to use 'asssigneeWildcardId' as a token name.
*/
if (f === 'assigneeId' && filters[f]) {
return AssigneeIdParamValues.includes(filters[f])
? {
...acc,
assigneeWildcardId: filters[f].toUpperCase(),
}
: acc;
}
if (filters[f]) {
return {
...acc,
[f]: filters[f],
};
}
return acc;
}, {});
}
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
};