-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtreeFormatter.ts
58 lines (47 loc) · 2.86 KB
/
treeFormatter.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { createDomElement } from '@slickgrid-universal/utils';
import { Constants } from '../constants.js';
import { type Formatter } from './../interfaces/index.js';
import { parseFormatterWhenExist } from './formatterUtilities.js';
import { getCellValueFromQueryFieldGetter } from '../services/utilities.js';
/** Formatter that must be use with a Tree Data column */
export const treeFormatter: Formatter = (row, cell, value, columnDef, dataContext, grid) => {
const gridOptions = grid.getOptions();
const treeDataOptions = gridOptions?.treeDataOptions;
const indentMarginLeft = treeDataOptions?.indentMarginLeft ?? 15;
const collapsedPropName = treeDataOptions?.collapsedPropName ?? Constants.treeDataProperties.COLLAPSED_PROP;
const hasChildrenPropName = treeDataOptions?.hasChildrenPropName ?? Constants.treeDataProperties.HAS_CHILDREN_PROP;
const treeLevelPropName = treeDataOptions?.levelPropName ?? Constants.treeDataProperties.TREE_LEVEL_PROP;
let outputValue = value;
// when a queryFieldNameGetterFn is defined, then get the value from that getter callback function
outputValue = getCellValueFromQueryFieldGetter(columnDef, dataContext, value);
if (outputValue === null || outputValue === undefined || dataContext === undefined) {
return '';
}
if (!dataContext.hasOwnProperty(treeLevelPropName)) {
throw new Error(
'[Slickgrid-Universal] You must provide valid "treeDataOptions" in your Grid Options, however it seems that we could not find any tree level info on the current item datacontext row.'
);
}
const treeLevel = dataContext?.[treeLevelPropName] ?? 0;
const indentSpacerElm = document.createElement('span');
indentSpacerElm.style.display = 'inline-block';
indentSpacerElm.style.width = `${indentMarginLeft * treeLevel}px`;
const slickTreeLevelClass = `slick-tree-level-${treeLevel}`;
let toggleClass = '';
if (dataContext[hasChildrenPropName]) {
toggleClass = dataContext?.[collapsedPropName] ? 'collapsed' : 'expanded'; // parent with child will have a toggle icon
}
if (treeDataOptions?.titleFormatter) {
outputValue = parseFormatterWhenExist(treeDataOptions.titleFormatter, row, cell, columnDef, dataContext, grid);
}
const spanToggleClass = `slick-group-toggle ${toggleClass}`.trim();
const spanIconElm = createDomElement('div', { className: spanToggleClass, ariaExpanded: String(toggleClass === 'expanded') });
const spanTitleElm = createDomElement('span', { className: 'slick-tree-title' });
grid.applyHtmlCode(spanTitleElm, outputValue);
spanTitleElm.setAttribute('level', treeLevel);
const containerElm = gridOptions?.preventDocumentFragmentUsage ? document.createElement('span') : new DocumentFragment();
containerElm.appendChild(indentSpacerElm);
containerElm.appendChild(spanIconElm);
containerElm.appendChild(spanTitleElm);
return { addClasses: slickTreeLevelClass, html: containerElm };
};