| 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 | 1×
1×
1×
1×
510×
510×
510×
510×
510×
510×
510×
510×
510×
510×
510×
47×
47×
46×
46×
509×
509×
505×
505×
505×
38×
38×
1×
37×
504×
504×
9450×
504×
504×
503×
35×
64×
67×
67×
67×
53×
11×
503×
7×
1×
1639×
2×
1×
3006×
3006×
1×
129×
129×
129×
1×
128×
134×
128×
9×
119×
1×
32×
123×
1×
65×
1×
97×
97×
96×
96×
8×
96×
2×
4×
2×
94×
3×
4×
4×
3×
3×
4×
4×
91×
91×
5×
5×
2×
2×
3×
3×
2×
2×
91×
91×
91×
91×
4×
90×
90×
90×
414×
208×
206×
324×
90×
9×
90×
1×
557×
557×
469×
88×
1×
87×
47×
47×
48×
43×
43×
40×
5×
35×
1×
35×
35×
34×
1×
33×
33×
33×
33×
48×
33×
33×
40×
40×
23×
17×
17×
17×
17×
17×
1×
16×
15×
16×
33×
33×
7×
32×
32×
32×
2×
32×
32×
32×
42×
42×
5×
5×
37×
37×
32×
1×
524×
524×
524×
843×
830×
874×
523×
1353×
523×
523×
523×
523×
452×
71×
4×
1×
70×
2335×
2335×
4×
2331×
1×
524×
524×
51×
47×
524×
40×
524×
524×
82×
524×
523×
1×
134×
67×
67×
67×
8×
59×
59×
1×
20×
20×
20×
20×
20×
20×
71×
71×
71×
64×
64×
20×
1×
1×
| define(["require", "exports", "@syncfusion/ej2-querybuilder", "@syncfusion/ej2-base", "@syncfusion/ej2-data"], function (require, exports, ej2_querybuilder_1, ej2_base_1, ej2_data_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var FlatQuery = (function () {
function FlatQuery(store, settings, locale) {
var _this = this;
this.ordinals = [];
this.groups = new Map();
this.store = store;
this.settings = settings;
this.locale = locale;
this.columns = settings.columns || [];
this.collator = new Intl.Collator(locale);
var filter = settings.filterSettings || {};
Iif (filter.mode && filter.mode !== 'OnEnter' || filter.type && ['Menu', 'Excel'].indexOf(filter.type) === -1) {
this.invalid();
}
(filter.columns || []).forEach(function (model) {
Iif (model.predicate && ['and', 'or'].indexOf(model.predicate) === -1) {
_this.invalid();
}
var predicate = _this.predicate(model);
var previous = _this.groups.get(model.field);
_this.groups.set(model.field, !previous ? predicate : model.predicate === 'or'
? previous.or(predicate) : previous.and(predicate));
});
var builder = settings.advancedFilterSettings && settings.advancedFilterSettings.queryBuilderSettings;
this.advanced = this.compileRule(builder && builder.rule, []);
var sorts = settings.sortSettings && settings.sortSettings.columns || [];
var sortedFields = [];
sorts.forEach(function (sort) {
_this.requireField(sort.field);
if (['Ascending', 'Descending'].indexOf(sort.direction) === -1 || sortedFields.indexOf(sort.field) !== -1) {
_this.invalid();
}
sortedFields.push(sort.field);
});
var ordinals = [];
for (var ordinal = 0; ordinal < store.length; ordinal++) {
ordinals.push(ordinal);
}
this.manager = new ej2_data_1.DataManager(ordinals);
var query = this.createQuery();
if (sorts.length) {
query.sortBy('', function (left, right) {
for (var _i = 0, sorts_1 = sorts; _i < sorts_1.length; _i++) {
var sort = sorts_1[_i];
var result = _this.compare(store.read(left, sort.field), store.read(right, sort.field), sort.direction);
if (result) {
return result;
}
}
return left - right;
});
}
this.ordinals = this.manager.executeLocal(query);
}
FlatQuery.prototype.invalid = function () { throw new Error('Flat layout settings contain an invalid value.'); };
FlatQuery.prototype.requireField = function (field) {
if (typeof field !== 'string' || this.columns.every(function (column) { return column.field !== field; }) ||
this.store.length > 0 && this.store.fields.indexOf(field) === -1) {
this.invalid();
}
};
FlatQuery.prototype.normalize = function (text, ignoreCase, ignoreAccent) {
var normalized = ignoreAccent ? ej2_data_1.DataUtil.ignoreDiacritics(text) : text;
return ignoreCase ? normalized.toLocaleLowerCase.call(normalized, this.locale) : normalized;
};
FlatQuery.prototype.validatePredicate = function (predicate) {
this.requireField(predicate.field);
var operators = ['equal', 'notequal', 'contains', 'doesnotcontain', 'startswith', 'doesnotstartwith',
'endswith', 'doesnotendwith', 'greaterthan', 'greaterthanorequal', 'lessthan', 'lessthanorequal',
'isnull', 'isnotnull', 'isempty', 'isnotempty', 'in', 'notin', 'between', 'notbetween'];
if (operators.indexOf(predicate.operator) === -1) {
this.invalid();
}
var value = predicate.value;
var scalar = function (operand) { return operand === null || operand === undefined ||
['string', 'boolean'].indexOf(typeof operand) !== -1 || typeof operand === 'number' && isFinite(operand) ||
operand instanceof Date && isFinite(operand.getTime()); };
if (Array.isArray(value)) {
Iif (['in', 'notin', 'between', 'notbetween'].indexOf(predicate.operator) === -1 ||
!value.every(scalar) || /between/.test(predicate.operator) && value.length !== 2) {
this.invalid();
}
}
else Iif (!scalar(value) || ['in', 'notin', 'between', 'notbetween'].indexOf(predicate.operator) !== -1) {
this.invalid();
}
};
FlatQuery.prototype.check = function (read, expected) {
if (expected === void 0) { expected = true; }
return new ej2_data_1.Predicate('__flat', 'equal', expected, false, false, false, function (_field, ordinal) { return read(ordinal); });
};
FlatQuery.prototype.negate = function (predicate) {
return this.check(function (ordinal) { return !!predicate.validate(ordinal); }, false);
};
FlatQuery.prototype.predicate = function (model) {
var _this = this;
this.validatePredicate(model);
var operator = model.operator;
var leaf = function (op, value) {
return _this.predicate(Object.assign({}, model, { operator: op, value: value }));
};
if (operator === 'in' || operator === 'notin') {
var choices = model.value.map(function (value) { return leaf('equal', value); });
var any = choices.length ? ej2_data_1.Predicate.or(choices) : this.check(function () { return false; });
return operator === 'notin' ? this.negate(any) : any;
}
if (operator === 'between' || operator === 'notbetween') {
var bounds = model.value;
var dates_1 = bounds.every(function (bound) { return bound instanceof Date; });
Iif (!dates_1 && !bounds.every(function (bound) { return typeof bound === 'number'; })) {
return this.check(function () { return false; });
}
var range = ej2_data_1.Predicate.and(leaf('greaterthanorequal', bounds[0]), leaf('lessthanorequal', bounds[1]));
return operator === 'between' ? range : ej2_data_1.Predicate.and(this.check(function (ordinal) {
var value = _this.store.read(ordinal, model.field);
return dates_1 ? value instanceof Date && isFinite(value.getTime()) : typeof value === 'number' && isFinite(value);
}), this.negate(range));
}
var nativeOperator = operator;
switch (operator) {
case 'notequal':
case 'isnull':
case 'isnotnull':
case 'isempty':
case 'isnotempty':
nativeOperator = 'equal';
break;
case 'doesnotcontain':
nativeOperator = 'contains';
break;
case 'doesnotstartwith':
nativeOperator = 'startswith';
break;
case 'doesnotendwith':
nativeOperator = 'endswith';
break;
}
var operand = operator === 'isnull' || operator === 'isnotnull' ? null :
operator === 'isempty' || operator === 'isnotempty' ? '' : model.value;
var text = ['contains', 'startswith', 'endswith'].indexOf(nativeOperator) !== -1;
var ordered = /^(greater|less)than/.test(operator);
if (text && typeof operand !== 'string' || ordered && !(operand instanceof Date || typeof operand === 'number')) {
return this.check(function () { return false; });
}
var dates = operand instanceof Date;
var ignoreCase = text ? model.matchCase !== true : model.matchCase === false;
var normalize = function (value) {
if (ordered || dates) {
return dates ? value instanceof Date ? value.getTime() : NaN :
typeof value === 'number' && isFinite(value) ? value : NaN;
}
return typeof value === 'string' ? _this.normalize(value, ignoreCase, (text || ignoreCase) && model.ignoreAccent) :
value === undefined ? null : value;
};
var result = new ej2_data_1.Predicate(model.field, nativeOperator, normalize(operand), false, false, false, function (field, ordinal) { return normalize(_this.store.read(ordinal, field)); });
if (['notequal', 'isnotnull', 'isnotempty'].indexOf(operator) !== -1 || operator.indexOf('doesnot') === 0) {
result = this.negate(result);
}
return text ? ej2_data_1.Predicate.and(this.check(function (ordinal) { return typeof _this.store.read(ordinal, model.field) === 'string'; }), result) : result;
};
FlatQuery.prototype.compileRule = function (rule, ancestors) {
var _this = this;
if (!rule) {
return undefined;
}
if (ancestors.indexOf(rule) !== -1 || ancestors.length > 64) {
this.invalid();
}
if (rule.rules) {
Iif (!Array.isArray(rule.rules) || ['and', 'or'].indexOf(rule.condition) === -1) {
this.invalid();
}
var children = rule.rules.map(function (child) {
return _this.compileRule(child, ancestors.concat(rule)) || _this.check(function () { return true; });
});
var group = !children.length ? this.check(function () { return true; }) : rule.condition === 'and'
? ej2_data_1.Predicate.and(children) : ej2_data_1.Predicate.or(children);
return rule.not ? this.negate(group) : group;
}
if (!rule.type) {
return this.predicate({ field: rule.field, operator: rule.operator, value: rule.value });
}
return this.nativeRule(rule);
};
FlatQuery.prototype.nativeRule = function (rule) {
var _this = this;
this.requireField(rule.field);
if (['string', 'number', 'boolean', 'date'].indexOf(rule.type) === -1) {
this.invalid();
}
var aliases = {
notcontains: 'doesnotcontain', notstartswith: 'doesnotstartwith', notendswith: 'doesnotendwith'
};
var alias = Object.getOwnPropertyDescriptor(aliases, rule.operator);
var operator = alias ? alias.value : rule.operator;
var date = rule.type === 'date';
var column = this.columns.filter(function (entry) { return entry.field === rule.field; })[0];
var intl = date ? new ej2_base_1.Internationalization(this.locale) : undefined;
var value = function (operand, end) {
if (end === void 0) { end = false; }
if (!date || operand === null || operand === undefined) {
return operand;
}
Iif (operand instanceof Date) {
return operand.getTime();
}
Iif (typeof operand !== 'string') {
_this.invalid();
}
var format = typeof column.format === 'string' ?
column.format === 'yMd' || column.format === 'short' ? { skeleton: column.format, type: 'date' } :
{ format: column.format } : column.format || { skeleton: 'yMd', type: 'date' };
var parsed = intl.parseDate(operand, format);
if (!parsed || !isFinite(parsed.getTime())) {
_this.invalid();
}
if (operand.indexOf(':') === -1) {
parsed.setHours(end ? 23 : 0, end ? 59 : 0, end ? 59 : 0, end ? 999 : 0);
}
return parsed.getTime();
};
var original = rule.value;
var normalized = Array.isArray(original) ? original.map(function (entry, index) {
return value(entry, /between/.test(operator) && index === 1);
}) :
value(original, operator === 'greaterthan' || operator === 'lessthanorequal');
this.validatePredicate({ field: rule.field, operator: operator, value: normalized });
var leaf = { field: rule.field, type: date ? 'number' : rule.type, operator: operator,
value: normalized };
if (date && typeof original === 'string' && original.indexOf(':') === -1 &&
(operator === 'equal' || operator === 'notequal')) {
leaf = { condition: operator === 'equal' ? 'and' : 'or', rules: [
{ field: rule.field, type: 'number', operator: operator === 'equal' ? 'greaterthanorequal' : 'lessthan',
value: value(original) },
{ field: rule.field, type: 'number', operator: operator === 'equal' ? 'lessthanorequal' : 'greaterthan',
value: value(original, true) }
] };
}
var builder = new ej2_querybuilder_1.QueryBuilder({ columns: [{ field: rule.field, type: date ? 'number' : rule.type }] });
var native = builder.getPredicate({ condition: 'and', rules: [leaf] });
var bind = function (entry) {
Iif (!entry) {
return _this.check(function () { return true; });
}
if (entry.isComplex) {
var children = entry.predicates.map(bind);
return entry.condition === 'or' ? ej2_data_1.Predicate.or(children) : ej2_data_1.Predicate.and(children);
}
var operand = date && typeof entry.value === 'number' ? new Date(entry.value) : entry.value;
return _this.predicate({ field: rule.field, operator: entry.operator, value: operand, matchCase: !entry.ignoreCase });
};
return bind(native);
};
FlatQuery.prototype.addSearch = function (query) {
var _this = this;
var search = this.settings.searchSettings || {};
var fields = search.fields && search.fields.length ? search.fields : this.columns
.filter(function (column) { return column.visible !== false && column.allowSearching !== false; })
.map(function (column) { return column.field; });
fields.forEach(function (field) { return _this.requireField(field); });
var eligible = fields.filter(function (field) {
return _this.columns.some(function (column) { return column.field === field && column.allowSearching !== false; });
});
var operator = search.operator || 'contains';
Iif (['contains', 'startswith', 'endswith', 'equal', 'notequal'].indexOf(operator) === -1) {
this.invalid();
}
var key = this.normalize(search.key || '', search.ignoreCase !== false, search.ignoreAccent);
if (!key) {
return;
}
if (!eligible.length) {
query.where(this.check(function () { return false; }));
return;
}
query.search(key, eligible, operator, false, false, function (field, ordinal) {
var value = _this.store.read(ordinal, field);
if (value === null || typeof value === 'number' && !isFinite(value) ||
value instanceof Date && !isFinite(value.getTime())) {
return operator === 'notequal' ? key : null;
}
return _this.normalize(value instanceof Date ? value.toISOString() : String(value), search.ignoreCase !== false, search.ignoreAccent);
});
};
FlatQuery.prototype.createQuery = function (omitField) {
var predicates = [];
this.groups.forEach(function (predicate, field) {
if (field !== omitField) {
predicates.push(predicate);
}
});
if (this.advanced) {
predicates.push(this.advanced);
}
var query = new ej2_data_1.Query();
if (predicates.length) {
query.where(ej2_data_1.Predicate.and(predicates));
}
this.addSearch(query);
return query;
};
FlatQuery.prototype.compare = function (left, right, direction) {
var rank = function (value) { return value === null ||
typeof value === 'number' && isNaN(value) || value instanceof Date && !isFinite(value.getTime()) ? 4 :
typeof value === 'boolean' ? 0 : typeof value === 'number' ? 1 : value instanceof Date ? 2 : 3; };
var a = rank(left);
var b = rank(right);
if (a === 4 || b === 4) {
return a === b ? 0 : a === 4 ? 1 : -1;
}
var result = a !== b ? a - b : typeof left === 'string' ? this.collator.compare(left, right) :
Number(left) - Number(right);
return direction === 'Descending' ? -result : result;
};
FlatQuery.prototype.facet = function (field) {
var _this = this;
this.requireField(field);
var result = [];
var keys = new Set();
var ordinals = this.manager.executeLocal(this.createQuery(field));
ordinals.forEach(function (ordinal) {
var value = _this.store.read(ordinal, field);
var key = value instanceof Date ? 'date:' + value.getTime() : typeof value + ':' + String(value);
if (!keys.has(key)) {
keys.add(key);
result.push(value instanceof Date ? new Date(value.getTime()) : value);
}
});
return result;
};
return FlatQuery;
}());
exports.FlatQuery = FlatQuery;
});
|