debian-mirror-gitlab/app/assets/javascripts/issues_list/utils.js

229 lines
5.9 KiB
JavaScript
Raw Normal View History

2021-06-08 01:23:25 +05:30
import {
2021-09-30 23:02:18 +05:30
API_PARAM,
2021-06-08 01:23:25 +05:30
BLOCKING_ISSUES_DESC,
CREATED_ASC,
CREATED_DESC,
DUE_DATE_ASC,
DUE_DATE_DESC,
DUE_DATE_VALUES,
filters,
2021-09-30 23:02:18 +05:30
LABEL_PRIORITY_ASC,
2021-06-08 01:23:25 +05:30
LABEL_PRIORITY_DESC,
MILESTONE_DUE_ASC,
MILESTONE_DUE_DESC,
NORMAL_FILTER,
POPULARITY_ASC,
POPULARITY_DESC,
2021-09-30 23:02:18 +05:30
PRIORITY_ASC,
2021-06-08 01:23:25 +05:30
PRIORITY_DESC,
2021-09-30 23:02:18 +05:30
RELATIVE_POSITION_ASC,
2021-06-08 01:23:25 +05:30
SPECIAL_FILTER,
SPECIAL_FILTER_VALUES,
2021-09-04 01:27:46 +05:30
TOKEN_TYPE_ASSIGNEE,
2021-09-30 23:02:18 +05:30
TOKEN_TYPE_ITERATION,
2021-06-08 01:23:25 +05:30
UPDATED_ASC,
UPDATED_DESC,
2021-09-30 23:02:18 +05:30
URL_PARAM,
2021-06-08 01:23:25 +05:30
urlSortParams,
WEIGHT_ASC,
WEIGHT_DESC,
} from '~/issues_list/constants';
import { isPositiveInteger } from '~/lib/utils/number_utils';
import { __ } from '~/locale';
2021-09-30 23:02:18 +05:30
import {
FILTERED_SEARCH_TERM,
OPERATOR_IS_NOT,
} from '~/vue_shared/components/filtered_search_bar/constants';
2021-06-08 01:23:25 +05:30
export const getSortKey = (sort) =>
2021-09-30 23:02:18 +05:30
Object.keys(urlSortParams).find((key) => urlSortParams[key] === sort);
2021-06-08 01:23:25 +05:30
export const getDueDateValue = (value) => (DUE_DATE_VALUES.includes(value) ? value : undefined);
export const getSortOptions = (hasIssueWeightsFeature, hasBlockedIssuesFeature) => {
const sortOptions = [
{
id: 1,
title: __('Priority'),
sortDirection: {
2021-09-30 23:02:18 +05:30
ascending: PRIORITY_ASC,
2021-06-08 01:23:25 +05:30
descending: PRIORITY_DESC,
},
},
{
id: 2,
title: __('Created date'),
sortDirection: {
ascending: CREATED_ASC,
descending: CREATED_DESC,
},
},
{
id: 3,
title: __('Last updated'),
sortDirection: {
ascending: UPDATED_ASC,
descending: UPDATED_DESC,
},
},
{
id: 4,
title: __('Milestone due date'),
sortDirection: {
ascending: MILESTONE_DUE_ASC,
descending: MILESTONE_DUE_DESC,
},
},
{
id: 5,
title: __('Due date'),
sortDirection: {
ascending: DUE_DATE_ASC,
descending: DUE_DATE_DESC,
},
},
{
id: 6,
title: __('Popularity'),
sortDirection: {
ascending: POPULARITY_ASC,
descending: POPULARITY_DESC,
},
},
{
id: 7,
title: __('Label priority'),
sortDirection: {
2021-09-30 23:02:18 +05:30
ascending: LABEL_PRIORITY_ASC,
2021-06-08 01:23:25 +05:30
descending: LABEL_PRIORITY_DESC,
},
},
{
id: 8,
title: __('Manual'),
sortDirection: {
2021-09-30 23:02:18 +05:30
ascending: RELATIVE_POSITION_ASC,
descending: RELATIVE_POSITION_ASC,
2021-06-08 01:23:25 +05:30
},
},
];
if (hasIssueWeightsFeature) {
sortOptions.push({
id: 9,
title: __('Weight'),
sortDirection: {
ascending: WEIGHT_ASC,
descending: WEIGHT_DESC,
},
});
}
if (hasBlockedIssuesFeature) {
sortOptions.push({
id: 10,
title: __('Blocking'),
sortDirection: {
ascending: BLOCKING_ISSUES_DESC,
descending: BLOCKING_ISSUES_DESC,
},
});
}
return sortOptions;
};
const tokenTypes = Object.keys(filters);
const getUrlParams = (tokenType) =>
2021-09-30 23:02:18 +05:30
Object.values(filters[tokenType][URL_PARAM]).flatMap((filterObj) => Object.values(filterObj));
2021-06-08 01:23:25 +05:30
const urlParamKeys = tokenTypes.flatMap(getUrlParams);
const getTokenTypeFromUrlParamKey = (urlParamKey) =>
tokenTypes.find((tokenType) => getUrlParams(tokenType).includes(urlParamKey));
const getOperatorFromUrlParamKey = (tokenType, urlParamKey) =>
2021-09-30 23:02:18 +05:30
Object.entries(filters[tokenType][URL_PARAM]).find(([, filterObj]) =>
2021-06-08 01:23:25 +05:30
Object.values(filterObj).includes(urlParamKey),
)[0];
const convertToFilteredTokens = (locationSearch) =>
Array.from(new URLSearchParams(locationSearch).entries())
.filter(([key]) => urlParamKeys.includes(key))
.map(([key, data]) => {
const type = getTokenTypeFromUrlParamKey(key);
const operator = getOperatorFromUrlParamKey(type, key);
return {
type,
value: { data, operator },
};
});
const convertToFilteredSearchTerms = (locationSearch) =>
new URLSearchParams(locationSearch)
.get('search')
?.split(' ')
.map((word) => ({
type: FILTERED_SEARCH_TERM,
value: {
data: word,
},
})) || [];
export const getFilterTokens = (locationSearch) => {
if (!locationSearch) {
return [];
}
const filterTokens = convertToFilteredTokens(locationSearch);
const searchTokens = convertToFilteredSearchTerms(locationSearch);
return filterTokens.concat(searchTokens);
};
const getFilterType = (data, tokenType = '') =>
SPECIAL_FILTER_VALUES.includes(data) ||
2021-09-04 01:27:46 +05:30
(tokenType === TOKEN_TYPE_ASSIGNEE && isPositiveInteger(data))
2021-06-08 01:23:25 +05:30
? SPECIAL_FILTER
: NORMAL_FILTER;
2021-09-30 23:02:18 +05:30
const isIterationSpecialValue = (tokenType, value) =>
tokenType === TOKEN_TYPE_ITERATION && SPECIAL_FILTER_VALUES.includes(value);
export const convertToApiParams = (filterTokens) => {
const params = {};
const not = {};
filterTokens
.filter((token) => token.type !== FILTERED_SEARCH_TERM)
.forEach((token) => {
const filterType = getFilterType(token.value.data, token.type);
const field = filters[token.type][API_PARAM][filterType];
const obj = token.value.operator === OPERATOR_IS_NOT ? not : params;
const data = isIterationSpecialValue(token.type, token.value.data)
? token.value.data.toUpperCase()
: token.value.data;
Object.assign(obj, {
[field]: obj[field] ? [obj[field], data].flat() : data,
});
});
return Object.keys(not).length ? Object.assign(params, { not }) : params;
};
export const convertToUrlParams = (filterTokens) =>
2021-06-08 01:23:25 +05:30
filterTokens
.filter((token) => token.type !== FILTERED_SEARCH_TERM)
.reduce((acc, token) => {
const filterType = getFilterType(token.value.data, token.type);
2021-09-30 23:02:18 +05:30
const param = filters[token.type][URL_PARAM][token.value.operator]?.[filterType];
2021-06-08 01:23:25 +05:30
return Object.assign(acc, {
[param]: acc[param] ? [acc[param], token.value.data].flat() : token.value.data,
});
}, {});
export const convertToSearchQuery = (filterTokens) =>
filterTokens
.filter((token) => token.type === FILTERED_SEARCH_TERM && token.value.data)
.map((token) => token.value.data)
.join(' ');