Skip to content

Commit ba6d404

Browse files
committed
Fix #223 Add triggers in "Change filters", "Invert" and "Sortable" plugins
1 parent b5f05ef commit ba6d404

File tree

5 files changed

+70
-36
lines changed

5 files changed

+70
-36
lines changed

src/core.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,23 @@ QueryBuilder.prototype.bindEvents = function() {
269269
node.$el.remove();
270270
},
271271
'add': function(e, node, index) {
272-
node.$el.detach();
273-
274272
if (index === 0) {
275273
node.$el.prependTo(node.parent.$el.find('>' + Selectors.rules_list));
276274
}
277275
else {
278276
node.$el.insertAfter(node.parent.rules[index-1].$el);
279277
}
280278
},
279+
'move': function(e, node, group, index) {
280+
node.$el.detach();
281+
282+
if (index === 0) {
283+
node.$el.prependTo(group.$el.find('>' + Selectors.rules_list));
284+
}
285+
else {
286+
node.$el.insertAfter(group.rules[index-1].$el);
287+
}
288+
},
281289
'update': function(e, node, field, value, oldValue) {
282290
if (node instanceof Rule) {
283291
switch (field) {

src/model.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Node.prototype.drop = function() {
150150
}
151151

152152
if (!this.isRoot()) {
153-
this.parent._dropNode(this);
153+
this.parent._removeNode(this);
154154
this.parent = null;
155155
}
156156
};
@@ -163,8 +163,8 @@ Node.prototype.drop = function() {
163163
Node.prototype.moveAfter = function(node) {
164164
if (this.isRoot()) return;
165165

166-
this.parent._dropNode(this);
167-
node.parent._addNode(this, node.getPos()+1);
166+
this._move(node.parent, node.getPos()+1);
167+
168168
return this;
169169
};
170170

@@ -179,9 +179,9 @@ Node.prototype.moveAtBegin = function(target) {
179179
if (target === undefined) {
180180
target = this.parent;
181181
}
182-
183-
this.parent._dropNode(this);
184-
target._addNode(this, 0);
182+
183+
this._move(target, 0);
184+
185185
return this;
186186
};
187187

@@ -196,12 +196,26 @@ Node.prototype.moveAtEnd = function(target) {
196196
if (target === undefined) {
197197
target = this.parent;
198198
}
199-
200-
this.parent._dropNode(this);
201-
target._addNode(this, target.length());
199+
200+
this._move(target, target.length());
201+
202202
return this;
203203
};
204204

205+
/**
206+
* Move itself at specific position of Group
207+
* @param {Group}
208+
* @param {int}
209+
*/
210+
Node.prototype._move = function(group, index) {
211+
this.parent._removeNode(this);
212+
group._appendNode(this, index, false);
213+
214+
if (this.model !== null) {
215+
this.model.trigger('move', this, group, index);
216+
}
217+
};
218+
205219

206220
// GROUP CLASS
207221
// ===============================
@@ -256,17 +270,18 @@ Group.prototype.length = function() {
256270
* Add a Node at specified index
257271
* @param {Node}
258272
* @param {int,optional}
273+
* @param {boolean,optional}
259274
* @return {Node} the inserted node
260275
*/
261-
Group.prototype._addNode = function(node, index) {
276+
Group.prototype._appendNode = function(node, index, trigger) {
262277
if (index === undefined) {
263278
index = this.length();
264279
}
265280

266281
this.rules.splice(index, 0, node);
267282
node.parent = this;
268283

269-
if (this.model !== null) {
284+
if (trigger && this.model !== null) {
270285
this.model.trigger('add', node, index);
271286
}
272287

@@ -280,7 +295,7 @@ Group.prototype._addNode = function(node, index) {
280295
* @return {Group} the inserted group
281296
*/
282297
Group.prototype.addGroup = function($el, index) {
283-
return this._addNode(new Group(this, $el), index);
298+
return this._appendNode(new Group(this, $el), index, true);
284299
};
285300

286301
/**
@@ -290,15 +305,15 @@ Group.prototype.addGroup = function($el, index) {
290305
* @return {Rule} the inserted rule
291306
*/
292307
Group.prototype.addRule = function($el, index) {
293-
return this._addNode(new Rule(this, $el), index);
308+
return this._appendNode(new Rule(this, $el), index, true);
294309
};
295310

296311
/**
297312
* Delete a specific Node
298313
* @param {Node}
299314
* @return {Group} self
300315
*/
301-
Group.prototype._dropNode = function(node) {
316+
Group.prototype._removeNode = function(node) {
302317
var index = this.getNodePos(node);
303318
if (index !== -1) {
304319
node.parent = null;

src/plugins/change-filters/plugin.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ QueryBuilder.extend({
1212
* @param {object[]} new filters
1313
*/
1414
setFilters: function(delete_orphans, filters) {
15-
var that = this;
15+
var self = this;
1616

1717
if (filters === undefined) {
1818
filters = delete_orphans;
1919
delete_orphans = false;
2020
}
2121

2222
filters = this.checkFilters(filters);
23+
filters = this.change('setFilters', filters);
2324

2425
var filtersIds = filters.map(function(filter) {
2526
return filter.id;
@@ -29,12 +30,12 @@ QueryBuilder.extend({
2930
if (!delete_orphans) {
3031
(function checkOrphans(node) {
3132
node.each(
32-
function(rule) {
33-
if (rule.filter && filtersIds.indexOf(rule.filter.id) === -1) {
34-
Utils.error('ChangeFilter', 'A rule is using filter "{0}"', rule.filter.id);
35-
}
36-
},
37-
checkOrphans
33+
function(rule) {
34+
if (rule.filter && filtersIds.indexOf(rule.filter.id) === -1) {
35+
Utils.error('ChangeFilter', 'A rule is using filter "{0}"', rule.filter.id);
36+
}
37+
},
38+
checkOrphans
3839
);
3940
}(this.model.root));
4041
}
@@ -50,7 +51,7 @@ QueryBuilder.extend({
5051
rule.drop();
5152
}
5253
else {
53-
that.createRuleFilters(rule);
54+
self.createRuleFilters(rule);
5455

5556
rule.$el.find(Selectors.rule_filter).val(rule.filter ? rule.filter.id : '-1');
5657
}
@@ -60,11 +61,11 @@ QueryBuilder.extend({
6061
}(this.model.root));
6162

6263
// update plugins
63-
if (that.settings.plugins) {
64-
if (that.settings.plugins['unique-filter']) {
64+
if (this.settings.plugins) {
65+
if (this.settings.plugins['unique-filter']) {
6566
this.updateDisabledFilters();
6667
}
67-
else if (this.settings.plugins['bt-selectpicker']) {
68+
if (this.settings.plugins['bt-selectpicker']) {
6869
this.$el.find(Selectors.rule_filter).selectpicker('render');
6970
}
7071
}
@@ -78,6 +79,8 @@ QueryBuilder.extend({
7879
this.settings.default_filter = null;
7980
}
8081
}
82+
83+
this.trigger('afterSetFilters', filters);
8184
},
8285

8386
/**

src/plugins/invert/plugin.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ QueryBuilder.define('invert', function(options) {
7171
});
7272
}
7373
}, {
74-
icon: 'glyphicon glyphicon-random',
75-
recursive: true,
76-
invert_rules: true,
77-
display_rules_button: false,
78-
silent_fail: false
74+
icon: 'glyphicon glyphicon-random',
75+
recursive: true,
76+
invert_rules: true,
77+
display_rules_button: false,
78+
silent_fail: false
7979
});
8080

8181
QueryBuilder.extend({
@@ -96,6 +96,7 @@ QueryBuilder.extend({
9696
if (options.recursive === undefined) options.recursive = true;
9797
if (options.invert_rules === undefined) options.invert_rules = true;
9898
if (options.silent_fail === undefined) options.silent_fail = false;
99+
if (options.trigger === undefined) options.trigger = true;
99100

100101
if (node instanceof Group) {
101102
// invert group condition
@@ -108,12 +109,13 @@ QueryBuilder.extend({
108109

109110
// recursive call
110111
if (options.recursive) {
112+
var tempOpts = $.extend({}, options, {trigger: false});
111113
node.each(function(rule) {
112114
if (options.invert_rules) {
113-
this.invert(rule, options);
115+
this.invert(rule, tempOpts);
114116
}
115117
}, function(group) {
116-
this.invert(group, options);
118+
this.invert(group, tempOpts);
117119
}, this);
118120
}
119121
}
@@ -132,5 +134,9 @@ QueryBuilder.extend({
132134
}
133135
}
134136
}
137+
138+
if (options.trigger) {
139+
this.trigger('afterInvert', node, options);
140+
}
135141
}
136142
});

src/plugins/sortable/plugin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ QueryBuilder.define('sortable', function(options) {
8080
src.$el.show();
8181
placeholder.drop();
8282

83-
src = placeholder = null;
84-
8583
self.$el.find(Selectors.rule_and_group_containers).removeAttr('draggable');
84+
85+
self.trigger('afterMove', src);
86+
87+
src = placeholder = null;
8688
});
8789
});
8890

0 commit comments

Comments
 (0)