-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcascade.js
143 lines (113 loc) · 3.23 KB
/
cascade.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
import has from 'underscore/modules/_has.js';
import isFunction from 'underscore/modules/isFunction.js';
import { applyOwn } from 'helpers/apply';
import { array } from 'helpers/array';
import { log, warn } from 'helpers/log';
import { model, opt } from './data';
import { single } from './model';
applyOwn(cascade, {
collect, propagate, spawn,
});
export async function cascade(data, fields, expeditor) {
if (data.length === 0 || fields.length === 0) {
return;
}
if (has(expeditor, 'children')) {
var previous = expeditor.children;
}
var children = expeditor.children = [];
if (previous) {
children._previous = previous;
}
cascade.collect(data, fields, expeditor);
var promises = children.map(expeditor =>
expeditor.sequent()
);
var res = await Promise.all(promises);
promises = children.map((expeditor, i) => {
if (expeditor.fields) {
return cascade(res[i], expeditor.fields, expeditor);
}
});
return Promise.all(promises);
}
function collect(data, fields, expeditor) {
var m = model(expeditor.model);
fields.forEach(field => {
var sequence = m._parse(field);
var step = sequence.find(step => step.field);
var index = sequence.indexOf(step);
var info;
var tailField = sequence
.slice(index + 1)
.map(step => step.chunk)
.join('.');
if (opt.collect(data, step, m, tailField, expeditor)) {
// custom propagate
}
else if ((info = m._info(step.field)).model) {
cascade.propagate(data, step, info, tailField, expeditor);
}
else if (isFunction(m[step.field])) {
var calculated =
m[step.field + 'Field'] ||
m[step.field].field;
if (calculated) {
fields = array(calculated);
fields = single(fields);
log('expand "' + step.field + '" to', { fields });
cascade.collect(data, fields, expeditor);
}
}
else if (m.fieldsMap[step.field] >= 0) {
// field belongs to model
}
else {
warn('model "' + m.aka + '"',
'has no property "' + step.field + '"',
'in field "' + field + '"');
}
});
}
function propagate(data, step, info, tailField, expeditor) {
var children = expeditor.children;
var k = [
info.model,
step.field,
step.filter && step.filter.list.map(filter => filter.expression),
];
var child = children[k];
if (!child) {
children[k] = child = cascade.spawn(data, step, info, expeditor);
children.push(child);
}
if (tailField) {
child.fields.push(tailField);
}
opt.propagate(child);
}
function spawn(data, step, info, expeditor) {
var fields = [];
var params = {};
params[info.index] = data.map(record => record[info.field]);
if (step.filter) {
step.filter.list.forEach(filter => {
if (filter.operator === '=' && filter.value.indexOf('..') === -1) {
params[filter.field] = filter.value.split(',');
}
else {
fields.push(filter.field);
}
});
}
opt.spawn(params, info);
return expeditor.spawn({
field: step.field,
nullable: step.nullable,
inversed: [ info.inverse || expeditor.model ].concat(expeditor.inversed), // copy
index: info.index,
model: info.model,
fields,
params,
});
}