Welcome to mirror list, hosted at ThFree Co, Russian Federation.

table_utility.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d66335b83214afed62fdd2e1434b56588be6f2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { convertToSnakeCase, convertToCamelCase } from '~/lib/utils/text_utility';
import { DEFAULT_TH_CLASSES } from './constants';

/**
 * Generates the table header classes to be used for GlTable fields.
 *
 * @param {Number} width - The column width as a percentage.
 * @returns {String} The classes to be used in GlTable fields object.
 */
export const thWidthClass = (width) => `gl-w-${width}p ${DEFAULT_TH_CLASSES}`;

/**
 * Converts a GlTable sort-changed event object into string format.
 * This string can be used as a sort argument on GraphQL queries.
 *
 * @param {Object} - The table state context object.
 * @returns {String} A string with the sort key and direction, for example 'NAME_DESC'.
 */
export const sortObjectToString = ({ sortBy, sortDesc }) => {
  const sortingDirection = sortDesc ? 'DESC' : 'ASC';
  const sortingColumn = convertToSnakeCase(sortBy).toUpperCase();

  return `${sortingColumn}_${sortingDirection}`;
};

/**
 * Converts a sort string into a sort state object that can be used to
 * set the sort order on GlTable.
 *
 * @param {String} - The string with the sort key and direction, for example 'NAME_DESC'.
 * @returns {Object} An object with the sortBy and sortDesc properties.
 */
export const sortStringToObject = (sortString) => {
  let sortBy = null;
  let sortDesc = null;

  if (sortString && sortString.includes('_')) {
    const [key, direction] = sortString.split(/_(ASC|DESC)$/);
    sortBy = convertToCamelCase(key.toLowerCase());
    sortDesc = direction === 'DESC';
  }

  return { sortBy, sortDesc };
};