| 1 |
import { scopeColTransformer, emptyCol, mapProps, emptyTD, parentId } from './transformers'; |
| 2 |
import { selectable, cellActions, collapsible, expandedRow, editable, favoritable, sortableFavorites } from './decorators'; |
| 3 |
import { defaultTitle } from './formatters'; |
| 4 |
/** |
| 5 |
* Generate header with transforms and formatters from custom header object. |
| 6 |
* |
| 7 |
* @param {*} header with transforms, formatters, columnTransforms, and rest of header object. |
| 8 |
* @param {*} title to be used as label in header config. |
| 9 |
* @returns {*} header, label, transforms: Array, formatters: Array. |
| 10 |
*/ |
| 11 |
const generateHeader = ({ transforms: origTransforms, formatters: origFormatters, columnTransforms, header }, title) => (Object.assign(Object.assign({}, header), { label: title, transforms: [ |
| 12 |
scopeColTransformer, |
| 13 |
emptyCol, |
| 14 |
...(origTransforms || []), |
| 15 |
...(columnTransforms || []), |
| 16 |
...(header && header.hasOwnProperty('transforms') ? header.transforms : []) |
| 17 |
], formatters: [...(origFormatters || []), ...(header && header.hasOwnProperty('formatters') ? header.formatters : [])] })); |
| 18 |
/** |
| 19 |
* Function to generate cell for header config to change look of each cell. |
| 20 |
* |
| 21 |
* @param {*} customCell config with cellFormatters, cellTransforms, columnTransforms and rest of cell config. |
| 22 |
* @param {*} extra - extra |
| 23 |
* @returns {*} cell, transforms: Array, formatters: Array. |
| 24 |
*/ |
| 25 |
const generateCell = ({ cellFormatters, cellTransforms, columnTransforms, cell }, |
| 26 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 27 |
extra) => (Object.assign(Object.assign({}, cell), { transforms: [ |
| 28 |
...(cellTransforms || []), |
| 29 |
...(columnTransforms || []), |
| 30 |
...(cell && cell.hasOwnProperty('transforms') ? cell.transforms : []), |
| 31 |
mapProps // This transform should be applied last so that props that are manually defined at the cell level will override all other transforms. |
| 32 |
], formatters: [ |
| 33 |
defaultTitle, |
| 34 |
...(cellFormatters || []), |
| 35 |
...(cell && cell.hasOwnProperty('formatters') ? cell.formatters : []) |
| 36 |
] })); |
| 37 |
/** |
| 38 |
* Function to map custom simple object properties to expected format with property, header, cell, extra params |
| 39 |
* and props. |
| 40 |
* |
| 41 |
* @param {*} column to be shown in header - either string or object with title, transformers and formatters (for cells as well). |
| 42 |
* @param {*} extra additional object with callbacks for specific formatters. |
| 43 |
* @param {*} key cell key to be shown in data-key. |
| 44 |
* @param {*} props additional props for each cell. |
| 45 |
* @returns {*} object with property, extraParams, header, cell and props. |
| 46 |
*/ |
| 47 |
const mapHeader = (column, extra, key, ...props) => { |
| 48 |
const title = (column.hasOwnProperty('title') ? column.title : column); |
| 49 |
let dataLabel = `column-${key}`; |
| 50 |
if (column.hasOwnProperty('dataLabel')) { |
| 51 |
dataLabel = column.dataLabel; |
| 52 |
} |
| 53 |
else if (typeof title === 'string') { |
| 54 |
dataLabel = title; |
| 55 |
} |
| 56 |
return { |
| 57 |
property: (typeof title === 'string' && title.toLowerCase().trim().replace(/\s/g, '-')) || `column-${key}`, |
| 58 |
extraParams: extra, |
| 59 |
data: column.data, |
| 60 |
header: generateHeader(column, title), |
| 61 |
cell: generateCell(column, extra), |
| 62 |
props: Object.assign(Object.assign({ 'data-label': dataLabel, 'data-key': key }, (column.hasOwnProperty('props') ? column.props : {})), props) |
| 63 |
}; |
| 64 |
}; |
| 65 |
/** |
| 66 |
* Function to define select cell in first column. |
| 67 |
* |
| 68 |
* @param {*} extraObject with onSelect callback. |
| 69 |
* @returns {*} object with empty title, tranforms - Array, cellTransforms - Array. |
| 70 |
*/ |
| 71 |
const selectableTransforms = ({ onSelect, canSelectAll }) => [ |
| 72 |
...(onSelect |
| 73 |
? [ |
| 74 |
{ |
| 75 |
title: '', |
| 76 |
transforms: (canSelectAll && [selectable]) || null, |
| 77 |
cellTransforms: [selectable] |
| 78 |
} |
| 79 |
] |
| 80 |
: []) |
| 81 |
]; |
| 82 |
/** |
| 83 |
* Function to define favorites cell in first column (or second column if rows are also selectable). |
| 84 |
* |
| 85 |
* @param {*} extraObject with onFavorite callback. |
| 86 |
* @returns {*} object with empty title, tranforms - Array, cellTransforms - Array. |
| 87 |
*/ |
| 88 |
const favoritesTransforms = ({ onFavorite, onSort, sortBy, canSortFavorites, firstUserColumnIndex }) => [ |
| 89 |
...(onFavorite |
| 90 |
? [ |
| 91 |
{ |
| 92 |
title: '', |
| 93 |
transforms: onSort && canSortFavorites |
| 94 |
? [ |
| 95 |
scopeColTransformer, |
| 96 |
sortableFavorites({ |
| 97 |
onSort, |
| 98 |
// favorites should be just before the first user-defined column |
| 99 |
columnIndex: firstUserColumnIndex - 1, |
| 100 |
sortBy |
| 101 |
}) |
| 102 |
] |
| 103 |
: [emptyTD], |
| 104 |
cellTransforms: [favoritable] |
| 105 |
} |
| 106 |
] |
| 107 |
: []) |
| 108 |
]; |
| 109 |
/** |
| 110 |
* Function to define actions in last column. |
| 111 |
* |
| 112 |
* @param {*} extraObject with actions array. |
| 113 |
* @returns {*} object with empty title, tranforms - Array, cellTransforms - Array. |
| 114 |
*/ |
| 115 |
const actionsTransforms = ({ actions, actionResolver, areActionsDisabled }) => [ |
| 116 |
...(actionResolver || actions |
| 117 |
? [ |
| 118 |
{ |
| 119 |
title: '', |
| 120 |
transforms: [emptyTD], |
| 121 |
cellTransforms: [cellActions(actions, actionResolver, areActionsDisabled)] |
| 122 |
} |
| 123 |
] |
| 124 |
: []) |
| 125 |
]; |
| 126 |
/** |
| 127 |
* Function to define collapsible in first column. |
| 128 |
* |
| 129 |
* @param {*} header info with cellTransforms. |
| 130 |
* @param {*} extraObject with onCollapse callback. |
| 131 |
* @returns {*} object with empty title, tranforms - Array, cellTransforms - Array. |
| 132 |
*/ |
| 133 |
const collapsibleTransforms = (header, { onCollapse, canCollapseAll, firstUserColumnIndex }) => [ |
| 134 |
...(onCollapse |
| 135 |
? [ |
| 136 |
{ |
| 137 |
title: '', |
| 138 |
transforms: (canCollapseAll && [collapsible]) || null, |
| 139 |
cellTransforms: [collapsible, expandedRow(header.length, firstUserColumnIndex)] |
| 140 |
} |
| 141 |
] |
| 142 |
: []) |
| 143 |
]; |
| 144 |
/** |
| 145 |
* Function to add additional cell transforms to object. |
| 146 |
* |
| 147 |
* @param {*} cell to be expanded. |
| 148 |
* @param {*} additional thing to be added to cellTransforms. |
| 149 |
* @returns {*} object with title from cell and cellTransforms with additional in. |
| 150 |
*/ |
| 151 |
const addAdditionalCellTranforms = (cell, additional) => (Object.assign(Object.assign({}, (cell.hasOwnProperty('title') ? cell : { title: cell })), { cellTransforms: [...(cell.hasOwnProperty('cellTransforms') ? cell.cellTransforms : []), additional] })); |
| 152 |
/** |
| 153 |
* Function to change expanded row with additional transforms. |
| 154 |
* |
| 155 |
* @param {*} header info with cellTransforms. |
| 156 |
* @param {*} extra object with onCollapse/onExpand function. |
| 157 |
*/ |
| 158 |
const expandContent = (header, extra) => { |
| 159 |
if (!extra.onCollapse && !extra.onExpand) { |
| 160 |
return header; |
| 161 |
} |
| 162 |
return header.map((cell) => { |
| 163 |
const parentIdCell = addAdditionalCellTranforms(cell, parentId); |
| 164 |
return addAdditionalCellTranforms(parentIdCell, expandedRow(header.length)); |
| 165 |
}); |
| 166 |
}; |
| 167 |
/** |
| 168 |
* Function to join parent and their children so they can be rendered in tbody. |
| 169 |
* |
| 170 |
* @param {*} rows raw data to find out if it's child or parent. |
| 171 |
* @param {*} children data to render (array of react children). |
| 172 |
*/ |
| 173 |
export const mapOpenedRows = (rows, children) => rows.reduce((acc, curr, key) => { |
| 174 |
if (curr.hasOwnProperty('parent')) { |
| 175 |
const parent = acc.length > 0 && acc[acc.length - 1]; |
| 176 |
if (parent) { |
| 177 |
acc[acc.length - 1].rows = [...acc[acc.length - 1].rows, children[key]]; |
| 178 |
if (curr.hasOwnProperty('compoundParent')) { |
| 179 |
// if this is compound expand, check for any open child cell |
| 180 |
acc[acc.length - 1].isOpen = acc[acc.length - 1].rows.some((oneRow) => oneRow.props.rowData.cells.some((oneCell) => oneCell.props && oneCell.props.isOpen)); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
else { |
| 185 |
acc = [...acc, Object.assign(Object.assign({}, curr), { rows: [children[key]] })]; |
| 186 |
} |
| 187 |
return acc; |
| 188 |
}, []); |
| 189 |
const rowEditTransforms = ({ onRowEdit }) => [ |
| 190 |
...(onRowEdit |
| 191 |
? [ |
| 192 |
{ |
| 193 |
title: '', |
| 194 |
cellTransforms: [editable] |
| 195 |
} |
| 196 |
] |
| 197 |
: []) |
| 198 |
]; |
| 199 |
/** |
| 200 |
* Function to calculate columns based on custom config. |
| 201 |
* It adds some custom cells for collapse, select, if expanded row and actions. |
| 202 |
* |
| 203 |
* @param {*} headerRows custom object with described table header cells. |
| 204 |
* @param {*} extra object with custom callbacks. |
| 205 |
* @returns {*} expected object for react tabular table. |
| 206 |
*/ |
| 207 |
export const calculateColumns = (headerRows, extra) => headerRows && |
| 208 |
[ |
| 209 |
...collapsibleTransforms(headerRows, extra), |
| 210 |
...selectableTransforms(extra), |
| 211 |
...favoritesTransforms(extra), |
| 212 |
...expandContent(headerRows, extra), |
| 213 |
...rowEditTransforms(extra), |
| 214 |
...actionsTransforms(extra) |
| 215 |
].map((oneCol, key) => (Object.assign({}, mapHeader(oneCol, extra, key)))); |
| 216 |
//# sourceMappingURL=headerUtils.js.map |