-
Notifications
You must be signed in to change notification settings - Fork 4
/
objectsRelated.js
356 lines (335 loc) · 11.6 KB
/
objectsRelated.js
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
/*
* @Author: baozhoutao@steedos.com
* @Date: 2022-07-05 15:55:39
* @LastEditors: liaodaxue
* @LastEditTime: 2023-08-21 14:50:21
* @Description:
*/
import { getObjectRecordDetailRelatedListHeader } from './converter/amis/header';
import { isEmpty, find, isString, forEach, keys, findKey, isArray, union, has } from "lodash";
import { getUISchema, getField, getListViewColumns, getListViewSort, getListViewFilter } from './objects'
import { getRecord } from './record';
import { i18next } from '../i18n'
function str2function(
contents,
...args
) {
try {
let fn = new Function(...args, contents);
return fn;
} catch (e) {
console.warn(e);
return null;
}
}
export const getRelatedFieldValue = (masterObjectName, record_id, uiSchema, foreign_key) => {
const relatedField = find(uiSchema.fields, (field) => {
return foreign_key === field?.name
});
// console.log(`getRelatedFieldValue`, relatedField, uiSchema, foreign_key)
if (!isString(relatedField.reference_to)) {
return { o: masterObjectName, ids: [record_id] }
} else if (relatedField.multiple) {
return [record_id]
} else {
return record_id
}
}
// 获取所有相关表
export async function getObjectRelatedList(
objectName,
recordId,
formFactor
) {
const uiSchema = await getUISchema(objectName);
const related = [];
const relatedLists = [].concat(uiSchema.related_lists || []);
if(!isEmpty(relatedLists)){
for (const relatedList of relatedLists) {
const arr = relatedList.related_field_fullname.split(".");
const foreign_key = arr[2] ? arr[1]+'.'+arr[2] : arr[1];
related.push({
masterObjectName: objectName,
object_name: arr[0],
foreign_key,
label: relatedList.label,
columns: relatedList.field_names,
sort: relatedList.sort,
filters: relatedList.filters,
visible_on: relatedList.visible_on,
page_size: relatedList.page_size
});
}
}else{
const details = [].concat(uiSchema.details || []);
for (const detail of details) {
const arr = detail.split(".");
const foreign_key = arr[2] ? arr[1]+'.'+arr[2] : arr[1];
related.push({
masterObjectName: objectName,
object_name: arr[0],
foreign_key
});
}
}
return related;
}
// 获取单个相关表
export async function getRecordDetailRelatedListSchema(objectName, recordId, relatedObjectName, relatedKey, ctx){
let { top, perPage, appId, relatedLabel, className, columns, sort, filters, visible_on } = ctx;
// console.log('getRecordDetailRelatedListSchema==>',objectName,recordId,relatedObjectName)
const relatedObjectUiSchema = await getUISchema(relatedObjectName);
if(!relatedObjectUiSchema){
// 子表对象不存在时直接返回空值.
return {
uiSchema: {},
amisSchema: {}
}
}
const { label , fields } = relatedObjectUiSchema;
if(!relatedLabel){
relatedLabel = label;
}
if(!relatedKey){
let mainRelated = {};
const mainObjectUiSchema = await getUISchema(objectName);
if(mainObjectUiSchema){
const mainRelatedLists = mainObjectUiSchema.related_lists || [];
if(!isEmpty(mainRelatedLists)){
for (const relatedList of mainRelatedLists) {
const arr = relatedList.related_field_fullname.split(".");
const foreign_key_value = arr[2] ? arr[1]+'.'+arr[2] : arr[1];
mainRelated[arr[0]] = foreign_key_value;
}
}else{
const details = union(mainObjectUiSchema.details,mainObjectUiSchema.lookup_details) || [];
for (const detail of details) {
const arr = detail.split(".");
const foreign_key_value = arr[2] ? arr[1]+'.'+arr[2] : arr[1];
if(!has(mainRelated,arr[0])){
mainRelated[arr[0]] = foreign_key_value;
}
}
}
}
relatedKey = mainRelated[relatedObjectName];
}
let globalFilter = null;
// TODO: refField变量去掉,写到amis运行时脚本中,uiSchema.fields[relatedKey];可以取到
const refField = await getField(relatedObjectName, relatedKey);
if(!refField){
return {
uiSchema: relatedObjectUiSchema,
amisSchema: {
"type": "alert",
"body": `${i18next.t('frontend_objects_related_alert_start')} ${relatedObjectName} ${i18next.t('frontend_objects_related_alert_end')}`,
"level": "warning",
"showIcon": true,
"className": "mb-3"
}
}
}
let relatedValue = "${recordId}";
// if(refField.reference_to_field && refField.reference_to_field != '_id'){
// const masterRecord = await getRecord(objectName, recordId, [refField.reference_to_field]);
// relatedValue = masterRecord[refField.reference_to_field]
// }
// if (
// refField._reference_to ||
// (refField.reference_to && !isString(refField.reference_to))
// ) {
// globalFilter = [
// [`${relatedKey}/o`, "=", objectName],
// [`${relatedKey}/ids`, "=", relatedValue],
// ];
// } else {
// globalFilter = [`${relatedKey}`, "=", relatedValue];
// }
const recordRelatedListHeader = await getObjectRecordDetailRelatedListHeader(relatedObjectUiSchema, relatedLabel, ctx);
const componentId = `steedos-record-related-list-${relatedObjectName}`;
const options = {
globalFilter,
defaults: {
listSchema: {
headerToolbar:["bulkActions"],
columnsTogglable: false,
onEvent: {
[`@data.changed.${relatedObjectName}`]: {
"actions": [
{
"actionType": "reload"
}
]
}
}
},
headerSchema: recordRelatedListHeader
},
showHeader: true,
top: top,
perPage: perPage,
setDataToComponentId: componentId,
// tableHiddenOn: hiddenEmptyTable ? "this.$count === 0" : null,
appId: appId,
crudClassName: 'border-t border-slate-300 hidden',
...ctx
}
const amisSchema= (await getRelatedListSchema(relatedObjectName, 'all', options)).amisSchema;
if(!amisSchema){
return;
}
return {
uiSchema: relatedObjectUiSchema,
amisSchema: {
type: "service",
id: componentId,
className: `steedos-record-related-list ${componentId} rounded border border-slate-300 bg-gray-100 mb-4 ${className}`,
data: {
relatedKey: relatedKey,
listViewId: `amis-\${appId}-${relatedObjectName}-listview`,
_isRelated: true
},
body:[
amisSchema
]
}
};
}
/**
*
* @param {*} uiSchema
* @param {*} ctx
* @returns
* 1 对象uiSchema
* 2 列表视图名称: (排除日历视图)当无视图传入,取is_name为true的字段显示。
* 3 options
*/
function getDefaultRelatedListProps(uiSchema, listName, ctx) {
// console.log('getDefaultRelatedListProps==>', uiSchema, listName, ctx)
const listView = find(
uiSchema.list_views,
(listView, name) => {
// 日历视图不在相关子表中出现
if(listView.type === 'calendar'){
return false;
}
// 传入listViewName空值则取第一个
if (!listName) {
listName = name;
}
return name === listName;
}
);
let columns = [];
let sort = '';
let filter = null;
let filtersFunction = null;
if(listView){
columns = getListViewColumns(listView, ctx.formFactor);
sort = getListViewSort(listView);
filter = getListViewFilter(listView);
// if(isArray(ctx.globalFilter) && ctx.globalFilter.length && isArray(filter) && filter.length){
// // 都有值
// filter = [ctx.globalFilter, 'and', filter]
// }else if(ctx.globalFilter && (!filter || !filter.length)){
// // globalFilter有值,filter无值
// filter = ctx.globalFilter;
// }
filtersFunction = listView && listView._filters;
}else{
const isNameField = find(
uiSchema.fields,
(field,name)=>{
return field.is_name;
}
)
columns = isNameField ? [isNameField.name] : ['name'];
// if(ctx.globalFilter){
// filter = ctx.globalFilter;
// }
}
return {
columns,
sort,
filter,
filtersFunction
}
};
function getRelatedListProps(uiSchema, listViewName, ctx) {
if (ctx.columns) {
const sort = getListViewSort({sort: ctx.sort});
let { filters , filtersFunction} = ctx;
if(!filtersFunction && filters){
filtersFunction = str2function(
filters,
'filters',
'data'
)
}
return {
columns: ctx.columns,
sort,
// filter: ctx.globalFilter,
filtersFunction: filtersFunction
}
} else {
return getDefaultRelatedListProps(uiSchema, listViewName, ctx);
}
}
// 仅提供给单个相关子表内部使用
export async function getRelatedListSchema(
objectName,
listViewName,
ctx
) {
const uiSchema = await getUISchema(objectName);
const listView = uiSchema.list_views;
const listViewProps = getRelatedListProps(uiSchema,listViewName, ctx);
// console.log('listViewProps==>', listViewProps)
const {columns: listViewColumns, sort: listViewSort, filter: listviewFilter, filtersFunction } = listViewProps;
const defaults = ctx.defaults || {};
if(!defaults.headerSchema && ctx.showHeader){
defaults.headerSchema = await getObjectListHeader(uiSchema, listViewName);
}
if(!ctx.showHeader){
defaults.headerSchema = null;
}
ctx.defaults = defaults;
// 由于steedos-object-table组件的上下文覆盖规则异常,此处手动删除无需的ctx参数
delete ctx.filtersFunction;
delete ctx.sort;
delete ctx.extraColumns;
delete ctx.filters;
delete ctx.globalFilter;
const adaptor = `
try{
if(setDataToComponentId){
if(payload.data.count){
setTimeout(function(){
window.$("." + setDataToComponentId + " .antd-Crud").removeClass("hidden");
}, 10);
}
};
}catch(e){
console.log(e);
}
`;
const amisSchema = {
"type": "steedos-object-table",
"objectApiName": objectName,
"columns": listViewColumns,
"extraColumns": listView.extra_columns,
"filters": listviewFilter,
"filtersFunction": filtersFunction,
"sort": listViewSort,
"filterVisible": false,
"requestAdaptor": ctx.requestAdaptor,
"adaptor": adaptor + ctx.adaptor || '',
"ctx": ctx
};
// console.log(`getRelatedListSchema amisSchema`, amisSchema);
return {
uiSchema,
amisSchema,
};
}