Skip to content

Commit 08b478e

Browse files
committed
invert plug, add "silent_fail" option
1 parent d9c8018 commit 08b478e

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/plugins/invert/plugin.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ QueryBuilder.define('invert', function(options) {
4343
this.on('afterInit', function() {
4444
that.$el.on('click.queryBuilder', '[data-invert=group]', function() {
4545
var $group = $(this).closest('.rules-group-container');
46-
that.invert(Model($group), options.recursive, options.invert_rules);
46+
that.invert(Model($group), options);
4747
});
4848

4949
if (options.display_rules_button && options.invert_rules) {
5050
that.$el.on('click.queryBuilder', '[data-invert=rule]', function() {
5151
var $rule = $(this).closest('.rule-container');
52-
that.invert(Model($rule));
52+
that.invert(Model($rule), options);
5353
});
5454
}
5555
});
@@ -74,31 +74,43 @@ QueryBuilder.define('invert', function(options) {
7474
icon: 'glyphicon glyphicon-random',
7575
recursive: true,
7676
invert_rules: true,
77-
display_rules_button: false
77+
display_rules_button: false,
78+
silent_fail: false
7879
});
7980

8081
QueryBuilder.extend({
81-
invert: function(node, recursive, invert_rules) {
82-
if (typeof node != 'object') {
83-
if (this.model.root) {
84-
this.invert(this.model.root, node, recursive);
85-
}
82+
/**
83+
* Invert a Group, a Rule or the whole builder
84+
* @param {Node,optional}
85+
* @param {object,optional}
86+
*/
87+
invert: function(node, options) {
88+
if (!(node instanceof Node)) {
89+
if (!this.model.root) return;
90+
options = node;
91+
node = this.model.root;
8692
}
87-
else if (node instanceof Group) {
93+
94+
if (typeof options != 'object') options = {};
95+
if (options.recursive === undefined) options.recursive = true;
96+
if (options.invert_rules === undefined) options.invert_rules = true;
97+
if (options.silent_fail === undefined) options.silent_fail = false;
98+
99+
if (node instanceof Group) {
88100
if (this.settings.conditionOpposites[node.condition]) {
89101
node.condition = this.settings.conditionOpposites[node.condition];
90102
}
91-
else {
103+
else if (!options.silent_fail) {
92104
error('Unknown inverse of condition "{0}"', node.condition);
93105
}
94106

95-
if (recursive === true || recursive === undefined) {
107+
if (options.recursive) {
96108
node.each(function(rule) {
97-
if (invert_rules === true || invert_rules === undefined) {
98-
this.invert(rule);
109+
if (options.invert_rules) {
110+
this.invert(rule, options);
99111
}
100112
}, function(group) {
101-
this.invert(group, true);
113+
this.invert(group, options);
102114
}, this);
103115
}
104116
}
@@ -107,7 +119,7 @@ QueryBuilder.extend({
107119
if (this.settings.operatorOpposites[node.operator.type]) {
108120
node.operator = this.getOperatorByType(this.settings.operatorOpposites[node.operator.type]);
109121
}
110-
else {
122+
else if (!options.silent_fail){
111123
error('Unknown inverse of operator "{0}"', node.operator.type);
112124
}
113125
}

tests/plugins.module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ $(function(){
392392
rules: basic_rules
393393
});
394394

395-
$b.queryBuilder('invert', true, true);
395+
$b.queryBuilder('invert');
396396

397397
assert.rulesMatch(
398398
$b.queryBuilder('getRules'),

0 commit comments

Comments
 (0)