Debian Codegraph @

node/@patternfly/react-table/dist/js/components/Table/utils/headerUtils.js

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