1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | QueryBuilder.prototype.validateValue = function(rule, value) {
|
9 | var validation = rule.filter.validation || {};
|
10 | var result = true;
|
11 |
|
12 | if (validation.callback) {
|
13 | result = validation.callback.call(this, value, rule);
|
14 | }
|
15 | else {
|
16 | result = this._validateValue(rule, value);
|
17 | }
|
18 |
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | return this.change('validateValue', result, value, rule);
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | QueryBuilder.prototype._validateValue = function(rule, value) {
|
40 | var filter = rule.filter;
|
41 | var operator = rule.operator;
|
42 | var validation = filter.validation || {};
|
43 | var result = true;
|
44 | var tmp, tempValue;
|
45 |
|
46 | if (rule.operator.nb_inputs === 1) {
|
47 | value = [value];
|
48 | }
|
49 |
|
50 | for (var i = 0; i < operator.nb_inputs; i++) {
|
51 | if (!operator.multiple && $.isArray(value[i]) && value[i].length > 1) {
|
52 | result = ['operator_not_multiple', operator.type, this.translate('operators', operator.type)];
|
53 | break;
|
54 | }
|
55 |
|
56 | switch (filter.input) {
|
57 | case 'radio':
|
58 | if (value[i] === undefined || value[i].length === 0) {
|
59 | if (!validation.allow_empty_value) {
|
60 | result = ['radio_empty'];
|
61 | }
|
62 | break;
|
63 | }
|
64 | break;
|
65 |
|
66 | case 'checkbox':
|
67 | if (value[i] === undefined || value[i].length === 0) {
|
68 | if (!validation.allow_empty_value) {
|
69 | result = ['checkbox_empty'];
|
70 | }
|
71 | break;
|
72 | }
|
73 | break;
|
74 |
|
75 | case 'select':
|
76 | if (value[i] === undefined || value[i].length === 0 || (filter.placeholder && value[i] == filter.placeholder_value)) {
|
77 | if (!validation.allow_empty_value) {
|
78 | result = ['select_empty'];
|
79 | }
|
80 | break;
|
81 | }
|
82 | break;
|
83 |
|
84 | default:
|
85 | tempValue = $.isArray(value[i]) ? value[i] : [value[i]];
|
86 |
|
87 | for (var j = 0; j < tempValue.length; j++) {
|
88 | switch (QueryBuilder.types[filter.type]) {
|
89 | case 'string':
|
90 | if (tempValue[j] === undefined || tempValue[j].length === 0) {
|
91 | if (!validation.allow_empty_value) {
|
92 | result = ['string_empty'];
|
93 | }
|
94 | break;
|
95 | }
|
96 | if (validation.min !== undefined) {
|
97 | if (tempValue[j].length < parseInt(validation.min)) {
|
98 | result = [this.getValidationMessage(validation, 'min', 'string_exceed_min_length'), validation.min];
|
99 | break;
|
100 | }
|
101 | }
|
102 | if (validation.max !== undefined) {
|
103 | if (tempValue[j].length > parseInt(validation.max)) {
|
104 | result = [this.getValidationMessage(validation, 'max', 'string_exceed_max_length'), validation.max];
|
105 | break;
|
106 | }
|
107 | }
|
108 | if (validation.format) {
|
109 | if (typeof validation.format == 'string') {
|
110 | validation.format = new RegExp(validation.format);
|
111 | }
|
112 | if (!validation.format.test(tempValue[j])) {
|
113 | result = [this.getValidationMessage(validation, 'format', 'string_invalid_format'), validation.format];
|
114 | break;
|
115 | }
|
116 | }
|
117 | break;
|
118 |
|
119 | case 'number':
|
120 | if (tempValue[j] === undefined || tempValue[j].length === 0) {
|
121 | if (!validation.allow_empty_value) {
|
122 | result = ['number_nan'];
|
123 | }
|
124 | break;
|
125 | }
|
126 | if (isNaN(tempValue[j])) {
|
127 | result = ['number_nan'];
|
128 | break;
|
129 | }
|
130 | if (filter.type == 'integer') {
|
131 | if (parseInt(tempValue[j]) != tempValue[j]) {
|
132 | result = ['number_not_integer'];
|
133 | break;
|
134 | }
|
135 | }
|
136 | else {
|
137 | if (parseFloat(tempValue[j]) != tempValue[j]) {
|
138 | result = ['number_not_double'];
|
139 | break;
|
140 | }
|
141 | }
|
142 | if (validation.min !== undefined) {
|
143 | if (tempValue[j] < parseFloat(validation.min)) {
|
144 | result = [this.getValidationMessage(validation, 'min', 'number_exceed_min'), validation.min];
|
145 | break;
|
146 | }
|
147 | }
|
148 | if (validation.max !== undefined) {
|
149 | if (tempValue[j] > parseFloat(validation.max)) {
|
150 | result = [this.getValidationMessage(validation, 'max', 'number_exceed_max'), validation.max];
|
151 | break;
|
152 | }
|
153 | }
|
154 | if (validation.step !== undefined && validation.step !== 'any') {
|
155 | var v = (tempValue[j] / validation.step).toPrecision(14);
|
156 | if (parseInt(v) != v) {
|
157 | result = [this.getValidationMessage(validation, 'step', 'number_wrong_step'), validation.step];
|
158 | break;
|
159 | }
|
160 | }
|
161 | break;
|
162 |
|
163 | case 'datetime':
|
164 | if (tempValue[j] === undefined || tempValue[j].length === 0) {
|
165 | if (!validation.allow_empty_value) {
|
166 | result = ['datetime_empty'];
|
167 | }
|
168 | break;
|
169 | }
|
170 |
|
171 |
|
172 | if (validation.format) {
|
173 | if (!('moment' in window)) {
|
174 | Utils.error('MissingLibrary', 'MomentJS is required for Date/Time validation. Get it here http://momentjs.com');
|
175 | }
|
176 |
|
177 | var datetime = moment(tempValue[j], validation.format);
|
178 | if (!datetime.isValid()) {
|
179 | result = [this.getValidationMessage(validation, 'format', 'datetime_invalid'), validation.format];
|
180 | break;
|
181 | }
|
182 | else {
|
183 | if (validation.min) {
|
184 | if (datetime < moment(validation.min, validation.format)) {
|
185 | result = [this.getValidationMessage(validation, 'min', 'datetime_exceed_min'), validation.min];
|
186 | break;
|
187 | }
|
188 | }
|
189 | if (validation.max) {
|
190 | if (datetime > moment(validation.max, validation.format)) {
|
191 | result = [this.getValidationMessage(validation, 'max', 'datetime_exceed_max'), validation.max];
|
192 | break;
|
193 | }
|
194 | }
|
195 | }
|
196 | }
|
197 | break;
|
198 |
|
199 | case 'boolean':
|
200 | if (tempValue[j] === undefined || tempValue[j].length === 0) {
|
201 | if (!validation.allow_empty_value) {
|
202 | result = ['boolean_not_valid'];
|
203 | }
|
204 | break;
|
205 | }
|
206 | tmp = ('' + tempValue[j]).trim().toLowerCase();
|
207 | if (tmp !== 'true' && tmp !== 'false' && tmp !== '1' && tmp !== '0' && tempValue[j] !== 1 && tempValue[j] !== 0) {
|
208 | result = ['boolean_not_valid'];
|
209 | break;
|
210 | }
|
211 | }
|
212 |
|
213 | if (result !== true) {
|
214 | break;
|
215 | }
|
216 | }
|
217 | }
|
218 |
|
219 | if (result !== true) {
|
220 | break;
|
221 | }
|
222 | }
|
223 |
|
224 | return result;
|
225 | };
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | QueryBuilder.prototype.nextGroupId = function() {
|
233 | return this.status.id + '_group_' + (this.status.group_id++);
|
234 | };
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | QueryBuilder.prototype.nextRuleId = function() {
|
242 | return this.status.id + '_rule_' + (this.status.rule_id++);
|
243 | };
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | QueryBuilder.prototype.getOperators = function(filter) {
|
253 | if (typeof filter == 'string') {
|
254 | filter = this.getFilterById(filter);
|
255 | }
|
256 |
|
257 | var result = [];
|
258 |
|
259 | for (var i = 0, l = this.operators.length; i < l; i++) {
|
260 |
|
261 | if (filter.operators) {
|
262 | if (filter.operators.indexOf(this.operators[i].type) == -1) {
|
263 | continue;
|
264 | }
|
265 | }
|
266 |
|
267 | else if (this.operators[i].apply_to.indexOf(QueryBuilder.types[filter.type]) == -1) {
|
268 | continue;
|
269 | }
|
270 |
|
271 | result.push(this.operators[i]);
|
272 | }
|
273 |
|
274 |
|
275 | if (filter.operators) {
|
276 | result.sort(function(a, b) {
|
277 | return filter.operators.indexOf(a.type) - filter.operators.indexOf(b.type);
|
278 | });
|
279 | }
|
280 |
|
281 | |
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | return this.change('getOperators', result, filter);
|
290 | };
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | QueryBuilder.prototype.getFilterById = function(id, doThrow) {
|
301 | if (id == '-1') {
|
302 | return null;
|
303 | }
|
304 |
|
305 | for (var i = 0, l = this.filters.length; i < l; i++) {
|
306 | if (this.filters[i].id == id) {
|
307 | return this.filters[i];
|
308 | }
|
309 | }
|
310 |
|
311 | Utils.error(doThrow !== false, 'UndefinedFilter', 'Undefined filter "{0}"', id);
|
312 |
|
313 | return null;
|
314 | };
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | QueryBuilder.prototype.getOperatorByType = function(type, doThrow) {
|
325 | if (type == '-1') {
|
326 | return null;
|
327 | }
|
328 |
|
329 | for (var i = 0, l = this.operators.length; i < l; i++) {
|
330 | if (this.operators[i].type == type) {
|
331 | return this.operators[i];
|
332 | }
|
333 | }
|
334 |
|
335 | Utils.error(doThrow !== false, 'UndefinedOperator', 'Undefined operator "{0}"', type);
|
336 |
|
337 | return null;
|
338 | };
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | QueryBuilder.prototype.getRuleInputValue = function(rule) {
|
348 | var filter = rule.filter;
|
349 | var operator = rule.operator;
|
350 | var value = [];
|
351 |
|
352 | if (filter.valueGetter) {
|
353 | value = filter.valueGetter.call(this, rule);
|
354 | }
|
355 | else {
|
356 | var $value = rule.$el.find(QueryBuilder.selectors.value_container);
|
357 |
|
358 | for (var i = 0; i < operator.nb_inputs; i++) {
|
359 | var name = Utils.escapeElementId(rule.id + '_value_' + i);
|
360 | var tmp;
|
361 |
|
362 | switch (filter.input) {
|
363 | case 'radio':
|
364 | value.push($value.find('[name=' + name + ']:checked').val());
|
365 | break;
|
366 |
|
367 | case 'checkbox':
|
368 | tmp = [];
|
369 |
|
370 | $value.find('[name=' + name + ']:checked').each(function() {
|
371 | tmp.push($(this).val());
|
372 | });
|
373 |
|
374 | value.push(tmp);
|
375 | break;
|
376 |
|
377 | case 'select':
|
378 | if (filter.multiple) {
|
379 | tmp = [];
|
380 |
|
381 | $value.find('[name=' + name + '] option:selected').each(function() {
|
382 | tmp.push($(this).val());
|
383 | });
|
384 |
|
385 | value.push(tmp);
|
386 | }
|
387 | else {
|
388 | value.push($value.find('[name=' + name + '] option:selected').val());
|
389 | }
|
390 | break;
|
391 |
|
392 | default:
|
393 | value.push($value.find('[name=' + name + ']').val());
|
394 | }
|
395 | }
|
396 |
|
397 | if (operator.multiple && filter.value_separator) {
|
398 | value = value.map(function(val) {
|
399 | return val.split(filter.value_separator);
|
400 | });
|
401 | }
|
402 |
|
403 | if (operator.nb_inputs === 1) {
|
404 | value = value[0];
|
405 | }
|
406 |
|
407 |
|
408 | if (filter.valueParser) {
|
409 | value = filter.valueParser.call(this, rule, value);
|
410 | }
|
411 | }
|
412 |
|
413 | |
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | return this.change('getRuleValue', value, rule);
|
422 | };
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 | QueryBuilder.prototype.setRuleInputValue = function(rule, value) {
|
431 | var filter = rule.filter;
|
432 | var operator = rule.operator;
|
433 |
|
434 | if (!filter || !operator) {
|
435 | return;
|
436 | }
|
437 |
|
438 | this._updating_input = true;
|
439 |
|
440 | if (filter.valueSetter) {
|
441 | filter.valueSetter.call(this, rule, value);
|
442 | }
|
443 | else {
|
444 | var $value = rule.$el.find(QueryBuilder.selectors.value_container);
|
445 |
|
446 | if (operator.nb_inputs == 1) {
|
447 | value = [value];
|
448 | }
|
449 |
|
450 | for (var i = 0; i < operator.nb_inputs; i++) {
|
451 | var name = Utils.escapeElementId(rule.id + '_value_' + i);
|
452 |
|
453 | switch (filter.input) {
|
454 | case 'radio':
|
455 | $value.find('[name=' + name + '][value="' + value[i] + '"]').prop('checked', true).trigger('change');
|
456 | break;
|
457 |
|
458 | case 'checkbox':
|
459 | if (!$.isArray(value[i])) {
|
460 | value[i] = [value[i]];
|
461 | }
|
462 |
|
463 | value[i].forEach(function(value) {
|
464 | $value.find('[name=' + name + '][value="' + value + '"]').prop('checked', true).trigger('change');
|
465 | });
|
466 |
|
467 | break;
|
468 |
|
469 | default:
|
470 | if (operator.multiple && filter.value_separator && $.isArray(value[i])) {
|
471 | value[i] = value[i].join(filter.value_separator);
|
472 | }
|
473 | $value.find('[name=' + name + ']').val(value[i]).trigger('change');
|
474 | break;
|
475 | }
|
476 | }
|
477 | }
|
478 |
|
479 | this._updating_input = false;
|
480 | };
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 | QueryBuilder.prototype.parseRuleFlags = function(rule) {
|
490 | var flags = $.extend({}, this.settings.default_rule_flags);
|
491 |
|
492 | if (rule.readonly) {
|
493 | $.extend(flags, {
|
494 | filter_readonly: true,
|
495 | operator_readonly: true,
|
496 | value_readonly: true,
|
497 | no_delete: true
|
498 | });
|
499 | }
|
500 |
|
501 | if (rule.flags) {
|
502 | $.extend(flags, rule.flags);
|
503 | }
|
504 |
|
505 | |
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 | return this.change('parseRuleFlags', flags, rule);
|
514 | };
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 | QueryBuilder.prototype.getRuleFlags = function(flags, all) {
|
524 | if (all) {
|
525 | return $.extend({}, flags);
|
526 | }
|
527 | else {
|
528 | var ret = {};
|
529 | $.each(this.settings.default_rule_flags, function(key, value) {
|
530 | if (flags[key] !== value) {
|
531 | ret[key] = flags[key];
|
532 | }
|
533 | });
|
534 | return ret;
|
535 | }
|
536 | };
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 | QueryBuilder.prototype.parseGroupFlags = function(group) {
|
546 | var flags = $.extend({}, this.settings.default_group_flags);
|
547 |
|
548 | if (group.readonly) {
|
549 | $.extend(flags, {
|
550 | condition_readonly: true,
|
551 | no_add_rule: true,
|
552 | no_add_group: true,
|
553 | no_delete: true
|
554 | });
|
555 | }
|
556 |
|
557 | if (group.flags) {
|
558 | $.extend(flags, group.flags);
|
559 | }
|
560 |
|
561 | |
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 | return this.change('parseGroupFlags', flags, group);
|
570 | };
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 | QueryBuilder.prototype.getGroupFlags = function(flags, all) {
|
580 | if (all) {
|
581 | return $.extend({}, flags);
|
582 | }
|
583 | else {
|
584 | var ret = {};
|
585 | $.each(this.settings.default_group_flags, function(key, value) {
|
586 | if (flags[key] !== value) {
|
587 | ret[key] = flags[key];
|
588 | }
|
589 | });
|
590 | return ret;
|
591 | }
|
592 | };
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 | QueryBuilder.prototype.translate = function(category, key) {
|
602 | if (!key) {
|
603 | key = category;
|
604 | category = undefined;
|
605 | }
|
606 |
|
607 | var translation;
|
608 | if (typeof key === 'object') {
|
609 | translation = key[this.settings.lang_code] || key['en'];
|
610 | }
|
611 | else {
|
612 | translation = (category ? this.lang[category] : this.lang)[key] || key;
|
613 | }
|
614 |
|
615 | |
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 | return this.change('translate', translation, key, category);
|
625 | };
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 | QueryBuilder.prototype.getValidationMessage = function(validation, type, def) {
|
636 | return validation.messages && validation.messages[type] || def;
|
637 | };
|