-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathplugin.js
More file actions
165 lines (153 loc) · 6.09 KB
/
plugin.js
File metadata and controls
165 lines (153 loc) · 6.09 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
/**
* @class Invert
* @memberof module:plugins
* @description Allows to invert a rule operator, a group condition or the entire builder.
* @param {object} [options]
* @param {string} [options.icon='bi-shuffle']
* @param {boolean} [options.recursive=true]
* @param {boolean} [options.invert_rules=true]
* @param {boolean} [options.display_rules_button=false]
* @param {boolean} [options.silent_fail=false]
*/
QueryBuilder.define('invert', function(options) {
var self = this;
var Selectors = QueryBuilder.selectors;
// Bind events
this.on('afterInit', function() {
self.$el.on('click.queryBuilder', '[data-invert=group]', function() {
var $group = $(this).closest(Selectors.group_container);
self.invert(self.getModel($group), options);
});
if (options.display_rules_button && options.invert_rules) {
self.$el.on('click.queryBuilder', '[data-invert=rule]', function() {
var $rule = $(this).closest(Selectors.rule_container);
self.invert(self.getModel($rule), options);
});
}
});
// Modify templates
if (!options.disable_template) {
this.on('getGroupTemplate.filter', function(h) {
var $h = $($.parseHTML(h.value));
$h.find(Selectors.condition_container).after(
'<button type="button" class="btn btn-sm btn-default" data-invert="group">' +
'<i class="' + options.icon + '"></i> ' + self.translate('invert') +
'</button>'
);
h.value = $h.prop('outerHTML');
});
if (options.display_rules_button && options.invert_rules) {
this.on('getRuleTemplate.filter', function(h) {
var $h = $($.parseHTML(h.value));
$h.find(Selectors.rule_actions).prepend(
'<button type="button" class="btn btn-sm btn-default" data-invert="rule">' +
'<i class="' + options.icon + '"></i> ' + self.translate('invert') +
'</button>'
);
h.value = $h.prop('outerHTML');
});
}
}
}, {
icon: 'bi-shuffle',
recursive: true,
invert_rules: true,
display_rules_button: false,
silent_fail: false,
disable_template: false
});
QueryBuilder.defaults({
operatorOpposites: {
'equal': 'not_equal',
'not_equal': 'equal',
'in': 'not_in',
'not_in': 'in',
'less': 'greater_or_equal',
'less_or_equal': 'greater',
'greater': 'less_or_equal',
'greater_or_equal': 'less',
'between': 'not_between',
'not_between': 'between',
'begins_with': 'not_begins_with',
'not_begins_with': 'begins_with',
'contains': 'not_contains',
'not_contains': 'contains',
'ends_with': 'not_ends_with',
'not_ends_with': 'ends_with',
'is_empty': 'is_not_empty',
'is_not_empty': 'is_empty',
'is_null': 'is_not_null',
'is_not_null': 'is_null'
},
conditionOpposites: {
'AND': 'OR',
'OR': 'AND'
}
});
QueryBuilder.extend(/** @lends module:plugins.Invert.prototype */ {
/**
* Invert a Group, a Rule or the whole builder
* @param {Node} [node]
* @param {object} [options] {@link module:plugins.Invert}
* @fires module:plugins.Invert.afterInvert
* @throws InvertConditionError, InvertOperatorError
*/
invert: function(node, options) {
if (!(node instanceof Node)) {
if (!this.model.root) return;
options = node;
node = this.model.root;
}
if (typeof options != 'object') options = {};
if (options.recursive === undefined) options.recursive = true;
if (options.invert_rules === undefined) options.invert_rules = true;
if (options.silent_fail === undefined) options.silent_fail = false;
if (options.trigger === undefined) options.trigger = true;
if (node instanceof Group) {
// invert group condition
if (this.settings.conditionOpposites[node.condition]) {
node.condition = this.settings.conditionOpposites[node.condition];
}
else if (!options.silent_fail) {
Utils.error('InvertCondition', 'Unknown inverse of condition "{0}"', node.condition);
}
// recursive call
if (options.recursive) {
var tempOpts = $.extend({}, options, { trigger: false });
node.each(function(rule) {
if (options.invert_rules) {
this.invert(rule, tempOpts);
}
}, function(group) {
this.invert(group, tempOpts);
}, this);
}
}
else if (node instanceof Rule) {
if (node.operator && !node.filter.no_invert) {
// invert rule operator
if (this.settings.operatorOpposites[node.operator.type]) {
var invert = this.settings.operatorOpposites[node.operator.type];
// check if the invert is "authorized"
if (!node.filter.operators || node.filter.operators.indexOf(invert) != -1) {
node.operator = this.getOperatorByType(invert);
}
}
else if (!options.silent_fail) {
Utils.error('InvertOperator', 'Unknown inverse of operator "{0}"', node.operator.type);
}
}
}
if (options.trigger) {
/**
* After {@link module:plugins.Invert.invert} method
* @event afterInvert
* @memberof module:plugins.Invert
* @param {Node} node - the main group or rule that has been modified
* @param {object} options
*/
this.trigger('afterInvert', node, options);
this.trigger('rulesChanged');
}
}
});