feat: 支持迭代器容器
This commit is contained in:
@@ -21,10 +21,16 @@ import EventEmitter from 'events';
|
||||
import { cloneDeep, template } from 'lodash-es';
|
||||
|
||||
import type { AppCore, DataSourceSchema, Id, MNode } from '@tmagic/schema';
|
||||
import { compiledCond, compiledNode, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, isObject } from '@tmagic/utils';
|
||||
import {
|
||||
compiledNode,
|
||||
DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX,
|
||||
DSL_NODE_KEY_COPY_PREFIX,
|
||||
getValueByKeyPath,
|
||||
} from '@tmagic/utils';
|
||||
|
||||
import { DataSource, HttpDataSource } from './data-sources';
|
||||
import type { ChangeEvent, DataSourceManagerData, DataSourceManagerOptions } from './types';
|
||||
import { compliedConditions, createIteratorContentData } from './utils';
|
||||
|
||||
class DataSourceManager extends EventEmitter {
|
||||
private static dataSourceClassMap = new Map<string, typeof DataSource>();
|
||||
@@ -189,15 +195,7 @@ class DataSourceManager extends EventEmitter {
|
||||
|
||||
if (!data) return value;
|
||||
|
||||
return fields.reduce((accumulator, currentValue: any) => {
|
||||
if (Array.isArray(accumulator)) return accumulator;
|
||||
|
||||
if (isObject(accumulator)) {
|
||||
return accumulator[currentValue];
|
||||
}
|
||||
|
||||
return '';
|
||||
}, data);
|
||||
return getValueByKeyPath(fields.join('.'), data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,34 +208,39 @@ class DataSourceManager extends EventEmitter {
|
||||
}
|
||||
|
||||
public compliedConds(node: MNode) {
|
||||
if (!node.displayConds || !Array.isArray(node.displayConds) || !node.displayConds.length) return true;
|
||||
return compliedConditions(node, this.data);
|
||||
}
|
||||
|
||||
for (const { cond } of node.displayConds) {
|
||||
if (!cond) continue;
|
||||
public compliedIteratorItems(itemData: any, items: MNode[], dataSourceField: string[] = []) {
|
||||
return items.map((item) => {
|
||||
const keys: string[] = [];
|
||||
const [dsId, ...fields] = dataSourceField;
|
||||
|
||||
let result = true;
|
||||
for (const { op, value, range, field } of cond) {
|
||||
const [sourceId, fieldKey] = field;
|
||||
|
||||
const dsData = this.data[sourceId];
|
||||
|
||||
if (!dsData || !fieldKey) {
|
||||
break;
|
||||
Object.entries(item).forEach(([key, value]) => {
|
||||
if (
|
||||
typeof value === 'string' &&
|
||||
!key.startsWith(DSL_NODE_KEY_COPY_PREFIX) &&
|
||||
value.includes(`${dsId}`) &&
|
||||
/\$\{([\s\S]+?)\}/.test(value)
|
||||
) {
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
|
||||
const fieldValue = dsData[fieldKey];
|
||||
if (!compiledCond(op, fieldValue, value, range)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (result) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return compiledNode(
|
||||
(value: string) => template(value)(createIteratorContentData(itemData, dsId, fields)),
|
||||
cloneDeep(item),
|
||||
{
|
||||
[dsId]: {
|
||||
[item.id]: {
|
||||
name: '',
|
||||
keys,
|
||||
},
|
||||
},
|
||||
},
|
||||
dsId,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
|
||||
Reference in New Issue
Block a user