-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimple-table-manual.component.js
More file actions
379 lines (345 loc) · 14.7 KB
/
simple-table-manual.component.js
File metadata and controls
379 lines (345 loc) · 14.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { BaseComponent } from "../../base.component.js";
import {
buildFieldsForTable,
evaluateAllowRowAction,
getContext,
getReferenceList
} from "./template-utils.js";
export class SimpleTableManualComponent extends BaseComponent {
jsComponentPConnectData = {};
DISPLAY_ONLY = 'DISPLAY_ONLY';
EDITABLE_IN_MODAL = 'EDITABLE_IN_MODAL';
EDITABLE_IN_ROW = 'EDITABLE_IN_ROW';
props = {
visible: true,
label: "",
displayMode: this.EDITABLE_IN_ROW,
allowAddRows: true,
allowReorderRows: true,
addButtonLabel: "+ Add",
columnLabels: [],
rows: [],
}
isInitialized = false;
referenceListStr;
targetClassLabel;
referenceList;
readOnlyMode;
allowEditingInModal;
defaultView;
bUseSeparateViewForEdit;
editView;
prevReferenceList = null;
editableRows = [];
constructor(componentsManager, pConn) {
super(componentsManager, pConn);
this.type = "SimpleTableManual";
}
init() {
this.jsComponentPConnectData = this.jsComponentPConnect.registerAndSubscribeComponent(
this,
this.#checkAndUpdate
);
this.isInitialized = true;
this.componentsManager.onComponentAdded(this);
this.#updateSelf();
}
destroy() {
super.destroy();
this.jsComponentPConnectData.unsubscribeFn?.();
this.editableRows.forEach(row => {
row.cells.forEach(cell => {
cell.component.destroy();
});
})
this.editableRows = [];
this.props.rows = [];
this.componentsManager.onComponentPropsUpdate(this);
this.componentsManager.onComponentRemoved(this);
}
update(pConn) {
if (this.pConn !== pConn) {
this.pConn = pConn;
this.#updateSelf();
}
}
#checkAndUpdate() {
if (this.jsComponentPConnect.shouldComponentUpdate(this)) {
this.#updateSelf();
}
}
#updateSelf() {
const configProps = this.pConn.resolveConfigProps(this.pConn.getConfigProps())
if (configProps.visibility != null) {
this.props.visible = this.utils.getBooleanValue(configProps.visibility);
}
const {
referenceList = [], // if referenceList not in configProps$, default to empty list
renderMode,
children,
presets,
allowActions,
allowTableEdit,
allowRowDelete: allowRowDeleteExpression,
allowRowEdit: allowRowEditExpression,
label: labelProp,
propertyLabel,
editMode,
addAndEditRowsWithin,
viewForAddAndEditModal,
editModeConfig,
displayMode,
useSeparateViewForEdit,
viewForEditModal,
targetClassLabel
} = configProps;
const conditions = this.#calculateConditions(editMode, allowActions, allowTableEdit)
this.referenceListStr = getContext(this.pConn).referenceListStr;
this.props.label = this.pConn.getInheritedProps().label || labelProp || propertyLabel;
this.targetClassLabel = targetClassLabel;
this.props.addButtonLabel = targetClassLabel ? `+ Add ${targetClassLabel}` : "+ Add";
this.referenceList = referenceList.map((element) => {
element.allowEdit = conditions.allowEditRow && evaluateAllowRowAction(allowRowEditExpression, element)
return element;
});
this.contextClass = this.#getContextClass(configProps);
this.pConn.setReferenceList(getReferenceList(this.pConn));
this.readOnlyMode = renderMode === 'ReadOnly';
const editableMode = renderMode === 'Editable';
this.props.allowAddRows = editableMode && conditions.allowAddRow;
this.props.allowReorderRows = editableMode && conditions.allowDragDrop;
this.allowEditingInModal =
(editMode ? editMode === 'modal' : addAndEditRowsWithin === 'modal') && !(renderMode === 'ReadOnly' || displayMode === 'DISPLAY_ONLY');
if (this.readOnlyMode) {
this.props.displayMode = this.DISPLAY_ONLY;
} else if (editableMode && this.allowEditingInModal) {
this.props.displayMode = this.EDITABLE_IN_MODAL;
} else if (editableMode && !this.allowEditingInModal) {
this.props.displayMode = this.EDITABLE_IN_ROW;
}
this.defaultView = editModeConfig ? editModeConfig.defaultView : viewForAddAndEditModal;
this.bUseSeparateViewForEdit = editModeConfig ? editModeConfig.useSeparateViewForEdit : useSeparateViewForEdit;
this.editView = editModeConfig ? editModeConfig.editView : viewForEditModal;
const rawFields = this.#getRawFields(this.pConn.getRawMetadata())
const resolvedFields = children?.[0]?.children || presets?.[0].children?.[0].children;
const fieldDefs = this.#buildFieldDefs(rawFields, resolvedFields)
this.#initializeDefaultPageInstructions(fieldDefs);
this.props.columnLabels = this.#getColumnLabels(fieldDefs, resolvedFields);
if ((!this.#listsEqual(this.prevReferenceList, this.referenceList))) {
this.#buildRows(rawFields);
}
this.props.rows = this.editableRows.map((row, rowIndex) => {
const allowDelete = conditions.allowDeleteRow && evaluateAllowRowAction(allowRowDeleteExpression, this.referenceList[rowIndex])
const showEditButton = editableMode && this.allowEditingInModal && this.referenceList[rowIndex].allowEdit
const showDeleteButton = editableMode && allowDelete
return {
cellComponentIds: row.cells.map((cell) => cell.component.compId),
showEditButton: showEditButton,
showDeleteButton: showDeleteButton
}
});
this.prevReferenceList = this.referenceList;
this.componentsManager.onComponentPropsUpdate(this)
}
onEvent(event) {
if (this.readonlyMode) {
return;
}
if (event.type === "SimpleTableManualEvent") {
switch (event.eventData?.type) {
case "addRow":
this.#addSimpleTableRow();
break;
case "deleteRow":
this.#deleteSimpleTableRow(event.eventData?.rowId);
break;
case "reorderRow":
this.#reorderSimpleTableRow(event.eventData?.fromIndex, event.eventData?.toIndex);
break;
case "editRowInModal":
this.#editRowInModal(event.eventData?.rowId);
break;
case "addRowInModal":
this.#addRowInModal();
break;
default:
console.warn("SimpleTableManualComponent", "Unexpected event: ", event.eventData?.type);
}
return;
}
this.editableRows?.forEach((row) => {
row.cells.forEach(cell => {
cell.component.onEvent(event);
})
});
}
#checkIfAllowActionsOrRowEditingExist(newflagobject) {
return (newflagobject && Object.keys(newflagobject).length > 0) || this.pConn.getComponentConfig().allowRowEdit;
}
#initializeDefaultPageInstructions(fieldDefs) {
if (this.isInitialized) {
this.isInitialized = false;
if (this.allowEditingInModal) {
this.pConn.getListActions().initDefaultPageInstructions(
this.pConn.getReferenceList(),
fieldDefs.filter(item => item.name).map(item => item.name)
);
} else {
// @ts-ignore - An argument for 'propertyNames' was not provided.
this.pConn.getListActions().initDefaultPageInstructions(this.pConn.getReferenceList());
}
}
}
#buildRows(rawFields) {
const context = this.pConn.getContextName();
const newEditableRows = [];
this.referenceList.forEach((element, rowIndex) => {
const editableRow = this.editableRows[rowIndex];
const newEditableCells = [];
rawFields?.forEach((item, cellIndex) => {
if (!item?.config?.hide) {
item = {
...item,
config: {
...item.config,
label: '',
displayMode: this.readOnlyMode || this.allowEditingInModal || !element.allowEdit ? 'DISPLAY_ONLY' : undefined
}
};
const referenceListData = getReferenceList(this.pConn);
const pageReferenceValue = referenceListData.startsWith('D_')
? `${referenceListData}[${rowIndex}]`
: `${this.pConn.getPageReference()}${referenceListData}[${rowIndex}]`;
const config = {
meta: item,
options: {
context,
pageReference: pageReferenceValue,
referenceList: referenceListData,
hasForm: true
}
};
const cellPConn = PCore.createPConnect(config).getPConnect();
const oldComponent = editableRow?.cells?.[cellIndex]?.component;
const newComponent = this.componentsManager.upsert(oldComponent, cellPConn.meta.type, [cellPConn]);
newEditableCells.push({ component: newComponent })
}
});
newEditableRows.push({ cells: newEditableCells});
});
this.editableRows = newEditableRows;
}
#addSimpleTableRow() {
this.pConn.getListActions().insert({ classID: this.contextClass }, this.referenceList.length);
}
#deleteSimpleTableRow(index) {
if (index) {
this.pConn.getListActions().deleteEntry(parseInt(index));
} else {
console.error("SimpleTableManualComponent", "Cannot delete row - index not provided.");
}
}
#reorderSimpleTableRow(fromIndex, toIndex) {
if (fromIndex != null && toIndex != null) {
this.pConn.getListActions().reorder(parseInt(fromIndex), parseInt(toIndex));
} else {
console.error("SimpleTableManualComponent", "Cannot reorder row - correct indexes not provided.");
}
}
#editRowInModal(index) {
this.pConn.getActionsApi().openEmbeddedDataModal(
this.bUseSeparateViewForEdit ? this.editView : this.defaultView,
this.pConn,
this.referenceListStr,
index,
PCore.getConstants().RESOURCE_STATUS.UPDATE,
this.targetClassLabel
);
}
#addRowInModal() {
this.pConn.getActionsApi().openEmbeddedDataModal(
this.defaultView,
this.pConn,
this.referenceListStr,
this.referenceList.length,
PCore.getConstants().RESOURCE_STATUS.CREATE,
this.targetClassLabel
);
}
#getContextClass(configProps) {
let {contextClass} = configProps;
if (!contextClass) {
let listName = this.pConn.getComponentConfig().referenceList;
listName = PCore.getAnnotationUtils().getPropertyName(listName);
contextClass = this.pConn.getFieldMetadata(listName)?.pageClass;
}
return contextClass
}
#getRawFields(rawMetadata) {
// get raw config as @P and other annotations are processed and don't appear in the resolved config.
// Destructure "raw" children into array var: "rawFields"
// NOTE: when config.listType == "associated", the property can be found in either
// config.value (ex: "@P .DeclarantChoice") or
// config.datasource (ex: "@ASSOCIATED .DeclarantChoice")
// Neither of these appear in the resolved (this.configProps$)
const rawConfig = rawMetadata?.config;
return rawConfig?.children?.[0]?.children || rawConfig?.presets?.[0].children?.[0]?.children;
}
#buildFieldDefs(rawFields, resolvedFields) {
// fieldDefs will be an array where each entry will have a "name" which will be the
// "resolved" property name (that we can use as the colId) though it's not really
// resolved. The buildFieldsForTable helper just removes the "@P " (which is what
// Nebula does). It will also have the "label", and "meta" contains the original,
// unchanged config info. For now, much of the info here is carried over from
// Nebula and we may not end up using it all.
const primaryFieldsViewIndex = resolvedFields.findIndex(field => field.config.value === 'pyPrimaryFields');
const fieldDefs = buildFieldsForTable(rawFields, this.pConn, {
primaryFieldsViewIndex,
fields: resolvedFields
});
return fieldDefs?.filter(field => !(field.meta?.config?.hide === true));
}
#listsEqual(oldList, newList) {
if (oldList == null) {
return false;
}
if (oldList.length !== newList.length) {
return false;
}
for (let i = 0; i < oldList.length; i++) {
if (JSON.stringify(oldList[i]) !== JSON.stringify(newList[i])) {
return false;
}
}
return true;
}
#getColumnLabels(fieldDefs, resolvedFields) {
const displayedColumns = fieldDefs?.map(field => field.name);
const labelsMap = fieldDefs.reduce((acc, curr) => {
return {...acc, [curr.name]: curr.label};
}, {});
return resolvedFields.map((field, i) => {
const name = displayedColumns[i];
return labelsMap[name] || field.config.label;
});
}
#calculateConditions(editMode, allowActions, allowTableEdit) {
const conditions = {
allowAddRow: true,
allowDeleteRow: true,
allowEditRow: true,
allowDragDrop: true
};
if (this.#checkIfAllowActionsOrRowEditingExist(allowActions) && editMode) {
conditions.allowAddRow = (allowActions?.allowAdd ?? true) === true;
conditions.allowDeleteRow = (allowActions?.allowDelete ?? true) === true;
conditions.allowEditRow = (allowActions?.allowEdit ?? true) === true;
conditions.allowDragDrop = (allowActions?.allowDragDrop ?? true) === true;
} else if (allowTableEdit === false) {
conditions.allowAddRow = false;
conditions.allowDeleteRow = false;
conditions.allowDragDrop = false;
}
return conditions;
}
}