forked from ukrbublik/react-awesome-query-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryString.js
More file actions
323 lines (284 loc) · 11 KB
/
queryString.js
File metadata and controls
323 lines (284 loc) · 11 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
import {
getFieldConfig, getOperatorConfig, getFieldWidgetConfig, getFuncConfig
} from "../utils/configUtils";
import {
getFieldPath, getFieldPathLabels, getWidgetForFieldOp, formatFieldName
} from "../utils/ruleUtils";
import omit from "lodash/omit";
import pick from "lodash/pick";
import {defaultValue} from "../utils/stuff";
import {defaultConjunction} from "../utils/defaultUtils";
import {completeValue} from "../utils/funcUtils";
import {List, Map} from "immutable";
export const queryString = (item, config, isForDisplay = false) => {
//meta is mutable
let meta = {
errors: []
};
const res = formatItem(item, config, meta, isForDisplay, null);
if (meta.errors.length)
console.warn("Errors while exporting to string:", meta.errors);
return res;
};
const formatItem = (item, config, meta, isForDisplay = false, parentField = null) => {
if (!item) return undefined;
const type = item.get("type");
const children = item.get("children1");
if ((type === "group" || type === "rule_group") ) {
return formatGroup(item, config, meta, isForDisplay, parentField);
} else if (type === "rule") {
return formatRule(item, config, meta, isForDisplay, parentField);
}
return undefined;
};
const formatGroup = (item, config, meta, isForDisplay = false, parentField = null) => {
const type = item.get("type");
const properties = item.get("properties") || new Map();
const mode = properties.get("mode");
const children = item.get("children1") || new List();
const isRuleGroup = (type === "rule_group");
// TIP: don't cut group for mode == 'struct' and don't do aggr format (maybe later)
const groupField = isRuleGroup && mode == "array" ? properties.get("field") : null;
const canHaveEmptyChildren = isRuleGroup && mode == "array";
const not = properties.get("not");
const list = children
.map((currentChild) => formatItem(currentChild, config, meta, isForDisplay, groupField))
.filter((currentChild) => typeof currentChild !== "undefined");
if (!canHaveEmptyChildren && !list.size)
return undefined;
let conjunction = properties.get("conjunction");
if (!conjunction)
conjunction = defaultConjunction(config);
const conjunctionDefinition = config.conjunctions[conjunction];
const conjStr = list.size ? conjunctionDefinition.formatConj(list, conjunction, not, isForDisplay) : null;
let ret;
if (groupField) {
const aggrArgs = formatRule(item, config, meta, isForDisplay, parentField, true);
if (aggrArgs) {
const isRev = aggrArgs.pop();
const args = [
conjStr,
...aggrArgs
];
ret = config.settings.formatAggr(...args);
if (isRev) {
ret = config.settings.formatReverse(ret, null, null, null, null, isForDisplay);
}
}
} else {
ret = conjStr;
}
return ret;
};
const formatItemValue = (config, properties, meta, _operator, isForDisplay, parentField) => {
const field = properties.get("field");
const iValueSrc = properties.get("valueSrc");
const iValueType = properties.get("valueType");
const fieldDef = getFieldConfig(config, field) || {};
const operator = _operator || properties.get("operator");
const operatorDef = getOperatorConfig(config, operator, field) || {};
const cardinality = defaultValue(operatorDef.cardinality, 1);
const iValue = properties.get("value");
const asyncListValues = properties.get("asyncListValues");
let valueSrcs = [];
let valueTypes = [];
let formattedValue;
if (iValue != undefined) {
const fvalue = iValue.map((currentValue, ind) => {
const valueSrc = iValueSrc ? iValueSrc.get(ind) : null;
const valueType = iValueType ? iValueType.get(ind) : null;
const cValue = completeValue(currentValue, valueSrc, config);
const widget = getWidgetForFieldOp(config, field, operator, valueSrc);
const fieldWidgetDef = omit(getFieldWidgetConfig(config, field, operator, widget, valueSrc), ["factory"]);
let fv = formatValue(
config, meta, cValue, valueSrc, valueType, fieldWidgetDef, fieldDef, operator, operatorDef, isForDisplay, parentField, asyncListValues
);
if (fv !== undefined) {
valueSrcs.push(valueSrc);
valueTypes.push(valueType);
}
return fv;
});
const hasUndefinedValues = fvalue.filter(v => v === undefined).size > 0;
if (!( hasUndefinedValues || fvalue.size < cardinality )) {
formattedValue = (cardinality == 1 ? fvalue.first() : fvalue);
}
}
return [
formattedValue,
(valueSrcs.length > 1 ? valueSrcs : valueSrcs[0]),
(valueTypes.length > 1 ? valueTypes : valueTypes[0]),
];
};
const buildFnToFormatOp = (operator, operatorDefinition) => {
const fop = operatorDefinition.labelForFormat || operator;
const cardinality = defaultValue(operatorDefinition.cardinality, 1);
let fn;
if (cardinality == 0) {
fn = (field, op, values, valueSrc, valueType, opDef, operatorOptions, isForDisplay) => {
return `${field} ${fop}`;
};
} else if (cardinality == 1) {
fn = (field, op, values, valueSrc, valueType, opDef, operatorOptions, isForDisplay) => {
return `${field} ${fop} ${values}`;
};
} else if (cardinality == 2) {
// between
fn = (field, op, values, valueSrc, valueType, opDef, operatorOptions, isForDisplay) => {
const valFrom = values.first();
const valTo = values.get(1);
return `${field} ${fop} ${valFrom} AND ${valTo}`;
};
}
return fn;
};
const formatRule = (item, config, meta, isForDisplay = false, parentField = null, returnArgs = false) => {
const properties = item.get("properties") || new Map();
const field = properties.get("field");
let operator = properties.get("operator");
let operatorOptions = properties.get("operatorOptions");
if (field == null || operator == null)
return undefined;
const fieldDef = getFieldConfig(config, field) || {};
let operatorDef = getOperatorConfig(config, operator, field) || {};
let reversedOp = operatorDef.reversedOp;
let revOperatorDef = getOperatorConfig(config, reversedOp, field) || {};
//check op
let isRev = false;
let fn = operatorDef.formatOp;
if (!fn && reversedOp) {
fn = revOperatorDef.formatOp;
if (fn) {
isRev = true;
[operator, reversedOp] = [reversedOp, operator];
[operatorDef, revOperatorDef] = [revOperatorDef, operatorDef];
}
}
//find fn to format expr
if (!fn)
fn = buildFnToFormatOp(operator, operatorDef);
if (!fn)
return undefined;
//format field
const formattedField = formatField(config, meta, field, isForDisplay, parentField);
//format value
const [formattedValue, valueSrc, valueType] = formatItemValue(
config, properties, meta, operator, isForDisplay, parentField
);
if (formattedValue === undefined)
return undefined;
const args = [
formattedField,
operator,
formattedValue,
valueSrc,
valueType,
omit(operatorDef, ["formatOp", "mongoFormatOp", "sqlFormatOp", "jsonLogic", "spelFormatOp"]),
operatorOptions,
isForDisplay,
fieldDef,
isRev,
];
if (returnArgs) {
return args;
} else {
//format expr
let ret = fn.call(config.ctx, ...args);
//rev
if (isRev) {
ret = config.settings.formatReverse(ret, operator, reversedOp, operatorDef, revOperatorDef, isForDisplay);
}
return ret;
}
};
const formatValue = (config, meta, value, valueSrc, valueType, fieldWidgetDef, fieldDef, operator, opDef, isForDisplay, parentField = null, asyncListValues) => {
if (value === undefined)
return undefined;
let ret;
if (valueSrc == "field") {
ret = formatField(config, meta, value, isForDisplay, parentField);
} else if (valueSrc == "func") {
ret = formatFunc(config, meta, value, isForDisplay, parentField);
} else {
if (typeof fieldWidgetDef.formatValue === "function") {
const fn = fieldWidgetDef.formatValue;
const args = [
value,
{
...pick(fieldDef, ["fieldSettings", "listValues"]),
asyncListValues
},
//useful options: valueFormat for date/time
omit(fieldWidgetDef, ["formatValue", "mongoFormatValue", "sqlFormatValue", "jsonLogic", "elasticSearchFormatValue", "spelFormatValue"]),
isForDisplay
];
if (operator) {
args.push(operator);
args.push(opDef);
}
if (valueSrc == "field") {
const valFieldDefinition = getFieldConfig(config, value) || {};
args.push(valFieldDefinition);
}
ret = fn.call(config.ctx, ...args);
} else {
ret = value;
}
}
return ret;
};
const formatField = (config, meta, field, isForDisplay, parentField = null, cutParentField = true) => {
const {fieldSeparator, fieldSeparatorDisplay} = config.settings;
let ret = null;
if (field) {
const fieldDefinition = getFieldConfig(config, field) || {};
const fieldParts = Array.isArray(field) ? field : field.split(fieldSeparator);
const _fieldKeys = getFieldPath(field, config);
const fieldPartsLabels = getFieldPathLabels(field, config, cutParentField ? parentField : null);
const fieldFullLabel = fieldPartsLabels ? fieldPartsLabels.join(fieldSeparatorDisplay) : null;
const fieldLabel2 = fieldDefinition.label2 || fieldFullLabel;
const formatFieldFn = config.settings.formatField;
const fieldName = formatFieldName(field, config, meta, cutParentField ? parentField : null, {useTableName: true});
ret = formatFieldFn(fieldName, fieldParts, fieldLabel2, fieldDefinition, config, isForDisplay);
}
return ret;
};
const formatFunc = (config, meta, funcValue, isForDisplay, parentField = null) => {
const funcKey = funcValue.get("func");
const args = funcValue.get("args");
const funcConfig = getFuncConfig(config, funcKey);
const funcName = isForDisplay && funcConfig.label || funcKey;
let formattedArgs = {};
let formattedArgsWithNames = {};
for (const argKey in funcConfig.args) {
const argConfig = funcConfig.args[argKey];
const fieldDef = getFieldConfig(config, argConfig);
const argVal = args ? args.get(argKey) : undefined;
const argValue = argVal ? argVal.get("value") : undefined;
const argValueSrc = argVal ? argVal.get("valueSrc") : undefined;
const argAsyncListValues = argVal ? argVal.get("asyncListValues") : undefined;
const formattedArgVal = formatValue.call(config.ctx,
config, meta, argValue, argValueSrc, argConfig.type, fieldDef, argConfig, null, null, isForDisplay, parentField, argAsyncListValues
);
const argName = isForDisplay && argConfig.label || argKey;
if (formattedArgVal !== undefined) { // skip optional in the end
formattedArgs[argKey] = formattedArgVal;
formattedArgsWithNames[argName] = formattedArgVal;
}
}
let ret = null;
if (typeof funcConfig.formatFunc === "function") {
const fn = funcConfig.formatFunc;
const args = [
formattedArgs,
isForDisplay
];
ret = fn.call(config.ctx, ...args);
} else {
const argsStr = Object.entries(formattedArgsWithNames)
.map(([k, v]) => (isForDisplay ? `${k}: ${v}` : `${v}`))
.join(", ");
ret = `${funcName}(${argsStr})`;
}
return ret;
};