-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrenderer.ts
More file actions
151 lines (143 loc) · 5.39 KB
/
renderer.ts
File metadata and controls
151 lines (143 loc) · 5.39 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { format } from 'prettier';
import { readFileSync } from 'fs';
import { compile, registerHelper } from 'handlebars';
import { getAttributeType } from './fieldtypes';
import { EntityMetadata, LocalOptionSet, FormObject } from './types';
export const render = (
data: any,
meta: EntityMetadata,
templateName: 'template' | 'template-earlybound-entity' | 'template-earlybound-form',
localOptionSet?: LocalOptionSet[],
): string => {
const templateBuffer = readFileSync(`${__dirname}/${templateName}.hbs`);
const template = compile(templateBuffer.toString());
const getFieldName = (value: FormObject.Control) => {
if (value.DataFieldName)
return value.DataFieldName;
if (value.ClassId && value.ClassId.toLowerCase() !== '06375649-c143-495e-a496-c962e5b4488e') {
return value.Id;
}
if (value.Parameters && value.Parameters.$values.some(parameter => parameter.Name === 'UClientUniqueName')) {
return value.Parameters.$values.find(parameter => parameter.Name === 'UClientUniqueName')?.Value;
}
return "Timeline"
};
registerHelper('formtype', (value) => (value === 2 ? 'main' : 'quickcreate'));
// eslint-disable-next-line no-confusing-arrow
registerHelper(
'toTypeName',
// eslint-disable-next-line no-confusing-arrow
(value) => {
const typeName = value ? value.replace(/[^a-z^A-Z^0-9^_]+/g, '') : null;
if (/^\d/.test(typeName) || typeName === 'import') {
return `_${typeName}`;
}
return typeName;
},
// eslint-disable-next-line implicit-arrow-linebreak
// eslint-disable-next-line function-paren-newline
);
// eslint-disable-next-line no-nested-ternary
registerHelper('getFieldName', getFieldName);
registerHelper('jsonStringify', (value) => JSON.stringify(value));
registerHelper(
'getAttributeType',
(metadata, name: string, classid: string, fieldtype: 'attribute' | 'control') => {
const attributetype = getAttributeType(metadata, name, classid, fieldtype);
return attributetype;
},
);
registerHelper('hasControls', (section, metadata) => {
const Rows = section.Rows.$values as any[];
return Rows.some((row) => {
const Cells = row.Cells.$values as any[];
return Cells.some(
(cell) =>
cell.Control !== null &&
getAttributeType(
metadata,
cell.Control.DataFieldName ? cell.Control.DataFieldName : cell.Control.Id,
cell.Control.classid,
'formControl',
) !== undefined &&
getFieldName(cell.Control),
);
});
});
registerHelper('getFormControl', (formdata: FormObject.Form, formmeta) => {
const controls: FormObject.Control[] = [];
formdata.Tabs.$values.forEach((tab) => {
tab.Columns.$values.forEach((col) => {
col.Sections.$values.forEach((sec) => {
sec.Rows.$values.forEach((row) => {
row.Cells.$values.forEach((cell) => {
if (
cell.Control !== null &&
getAttributeType(
formmeta,
cell.Control.DataFieldName ? cell.Control.DataFieldName : cell.Control.Id,
cell.Control.ClassId,
'formControl',
) !== undefined &&
getFieldName(cell.Control) &&
!controls.some((control) => getFieldName(control) === getFieldName(cell.Control))
) {
controls.push(cell.Control);
}
});
});
});
});
});
controls.sort((a, b) => getFieldName(a)?.localeCompare(getFieldName(b) || '') || 0);
return controls;
});
registerHelper('sectionCollector', (formdata: FormObject.Tab) => {
const sections: FormObject.Section[] = [];
formdata.Columns.$values.forEach((col) => {
col.Sections.$values.forEach((sec) => {
sections.push(sec);
});
});
sections.sort((a, b) => a.Name?.localeCompare(b.Name));
return sections;
});
registerHelper('cellsCollector', (formdata: FormObject.Section) => {
const cells: FormObject.Cell[] = [];
formdata.Rows.$values.forEach((row) => {
row.Cells.$values.forEach((cell) => {
if (getFieldName(cell.Control)) {
cells.push(cell);
}
});
});
cells.sort((a, b) => getFieldName(a.Control)?.localeCompare(getFieldName(b.Control) || '') || 0);
return cells;
});
const formObj: FormObject.Form = data.formjson ? JSON.parse(data.formjson) : null;
if (
formObj !== null &&
formObj.Tabs.$values[0].Columns.$values[0].Sections.$values[0].Name !== null
) {
formObj.Tabs.$values.sort((a, b) => a.Name.localeCompare(b.Name));
formObj.Tabs.$values[0].Columns.$values[0].Sections.$values.sort((a, b) =>
a.Name.localeCompare(b.Name),
);
formObj.Tabs.$values[0].Columns.$values[0].Sections.$values[0].Rows.$values[0].Cells.$values.sort(
(a, b) => a.Control.DataFieldName?.localeCompare(b.Control.DataFieldName),
);
}
// eslint-disable-next-line no-param-reassign
meta.Attributes = meta.Attributes.sort((a, b) => a.SchemaName.localeCompare(b.SchemaName));
// eslint-disable-next-line no-param-reassign
localOptionSet = localOptionSet?.sort((a, b) => a.LogicalName.localeCompare(b.LogicalName));
const dts = template({
data,
formObj,
meta,
localOptionSet,
});
const formatted = format(dts, { parser: 'typescript' });
return formatted;
};
export default render;