Skip to content

Commit 11259a9

Browse files
committed
Fix #288 trigger afterAddGroup for root group
1 parent dfb7c19 commit 11259a9

File tree

8 files changed

+41
-58
lines changed

8 files changed

+41
-58
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
language: node_js
22
node_js:
3-
- "0.12"
3+
- "5"
44
before_install:
55
- gem install sass
66
- gem install scss_lint
77
- npm install -g grunt-cli
88
- npm install -g bower
99
before_script:
1010
- bower install
11-
install: npm install
12-
after_success: grunt coveralls
11+
after_success: grunt coveralls

dist/js/query-builder.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,10 @@ QueryBuilder.prototype.bindEvents = function() {
677677
* Create the root group
678678
* @param addRule {bool,optional} add a default empty rule
679679
* @param data {mixed,optional} group custom data
680+
* @param flags {object,optional} flags to apply to the group
680681
* @return group {Root}
681682
*/
682-
QueryBuilder.prototype.setRoot = function(addRule, data) {
683+
QueryBuilder.prototype.setRoot = function(addRule, data, flags) {
683684
addRule = (addRule === undefined || addRule === true);
684685

685686
var group_id = this.nextGroupId();
@@ -688,12 +689,13 @@ QueryBuilder.prototype.setRoot = function(addRule, data) {
688689
this.$el.append($group);
689690
this.model.root = new Group(null, $group);
690691
this.model.root.model = this.model;
691-
this.model.root.condition = this.settings.default_condition;
692-
this.model.root.flags = $.extend({}, this.settings.default_group_flags);
693692

694-
if (data !== undefined) {
695-
this.model.root.data = data;
696-
}
693+
this.model.root.data = data;
694+
this.model.root.flags = $.extend({}, this.settings.default_group_flags, flags);
695+
696+
this.trigger('afterAddGroup', this.model.root);
697+
698+
this.model.root.condition = this.settings.default_condition;
697699

698700
if (addRule) {
699701
this.addRule(this.model.root);
@@ -707,7 +709,7 @@ QueryBuilder.prototype.setRoot = function(addRule, data) {
707709
* @param parent {Group}
708710
* @param addRule {bool,optional} add a default empty rule
709711
* @param data {mixed,optional} group custom data
710-
* @param {object,optional} flags to apply to the group
712+
* @param flags {object,optional} flags to apply to the group
711713
* @return group {Group}
712714
*/
713715
QueryBuilder.prototype.addGroup = function(parent, addRule, data, flags) {
@@ -724,10 +726,7 @@ QueryBuilder.prototype.addGroup = function(parent, addRule, data, flags) {
724726
var $group = $(this.getGroupTemplate(group_id, level));
725727
var model = parent.addGroup($group);
726728

727-
if (data !== undefined) {
728-
model.data = data;
729-
}
730-
729+
model.data = data;
731730
model.flags = $.extend({}, this.settings.default_group_flags, flags);
732731

733732
this.trigger('afterAddGroup', model);
@@ -1316,9 +1315,7 @@ QueryBuilder.prototype.setRules = function(data) {
13161315
}
13171316

13181317
this.clear();
1319-
this.setRoot(false, data.data);
1320-
1321-
this.model.root.flags = this.parseGroupFlags(data);
1318+
this.setRoot(false, data.data, this.parseGroupFlags(data));
13221319

13231320
data = this.change('setRules', data);
13241321

@@ -1346,13 +1343,11 @@ QueryBuilder.prototype.setRules = function(data) {
13461343
Utils.error('RulesParse', 'No more than {0} groups are allowed', self.settings.allow_groups);
13471344
}
13481345
else {
1349-
model = self.addGroup(group, false, item.data);
1346+
model = self.addGroup(group, false, item.data, self.parseGroupFlags(item));
13501347
if (model === null) {
13511348
return;
13521349
}
13531350

1354-
model.flags = self.parseGroupFlags(item);
1355-
13561351
add(item, model);
13571352
}
13581353
}

dist/js/query-builder.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/query-builder.standalone.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -944,9 +944,10 @@ QueryBuilder.prototype.bindEvents = function() {
944944
* Create the root group
945945
* @param addRule {bool,optional} add a default empty rule
946946
* @param data {mixed,optional} group custom data
947+
* @param flags {object,optional} flags to apply to the group
947948
* @return group {Root}
948949
*/
949-
QueryBuilder.prototype.setRoot = function(addRule, data) {
950+
QueryBuilder.prototype.setRoot = function(addRule, data, flags) {
950951
addRule = (addRule === undefined || addRule === true);
951952

952953
var group_id = this.nextGroupId();
@@ -955,12 +956,13 @@ QueryBuilder.prototype.setRoot = function(addRule, data) {
955956
this.$el.append($group);
956957
this.model.root = new Group(null, $group);
957958
this.model.root.model = this.model;
958-
this.model.root.condition = this.settings.default_condition;
959-
this.model.root.flags = $.extend({}, this.settings.default_group_flags);
960959

961-
if (data !== undefined) {
962-
this.model.root.data = data;
963-
}
960+
this.model.root.data = data;
961+
this.model.root.flags = $.extend({}, this.settings.default_group_flags, flags);
962+
963+
this.trigger('afterAddGroup', this.model.root);
964+
965+
this.model.root.condition = this.settings.default_condition;
964966

965967
if (addRule) {
966968
this.addRule(this.model.root);
@@ -974,7 +976,7 @@ QueryBuilder.prototype.setRoot = function(addRule, data) {
974976
* @param parent {Group}
975977
* @param addRule {bool,optional} add a default empty rule
976978
* @param data {mixed,optional} group custom data
977-
* @param {object,optional} flags to apply to the group
979+
* @param flags {object,optional} flags to apply to the group
978980
* @return group {Group}
979981
*/
980982
QueryBuilder.prototype.addGroup = function(parent, addRule, data, flags) {
@@ -991,10 +993,7 @@ QueryBuilder.prototype.addGroup = function(parent, addRule, data, flags) {
991993
var $group = $(this.getGroupTemplate(group_id, level));
992994
var model = parent.addGroup($group);
993995

994-
if (data !== undefined) {
995-
model.data = data;
996-
}
997-
996+
model.data = data;
998997
model.flags = $.extend({}, this.settings.default_group_flags, flags);
999998

1000999
this.trigger('afterAddGroup', model);
@@ -1583,9 +1582,7 @@ QueryBuilder.prototype.setRules = function(data) {
15831582
}
15841583

15851584
this.clear();
1586-
this.setRoot(false, data.data);
1587-
1588-
this.model.root.flags = this.parseGroupFlags(data);
1585+
this.setRoot(false, data.data, this.parseGroupFlags(data));
15891586

15901587
data = this.change('setRules', data);
15911588

@@ -1613,13 +1610,11 @@ QueryBuilder.prototype.setRules = function(data) {
16131610
Utils.error('RulesParse', 'No more than {0} groups are allowed', self.settings.allow_groups);
16141611
}
16151612
else {
1616-
model = self.addGroup(group, false, item.data);
1613+
model = self.addGroup(group, false, item.data, self.parseGroupFlags(item));
16171614
if (model === null) {
16181615
return;
16191616
}
16201617

1621-
model.flags = self.parseGroupFlags(item);
1622-
16231618
add(item, model);
16241619
}
16251620
}

dist/js/query-builder.standalone.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ <h3>Output</h3>
403403
$('#builder').queryBuilder(options);
404404

405405
$('#builder').on('afterCreateRuleInput.queryBuilder', function(e, rule) {
406-
console.log(rule);
407406
if (rule.filter.plugin == 'selectize') {
408407
rule.$el.find('.rule-value-container').css('min-width', '200px')
409408
.find('.selectize-control').removeClass('form-control');

src/core.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,10 @@ QueryBuilder.prototype.bindEvents = function() {
356356
* Create the root group
357357
* @param addRule {bool,optional} add a default empty rule
358358
* @param data {mixed,optional} group custom data
359+
* @param flags {object,optional} flags to apply to the group
359360
* @return group {Root}
360361
*/
361-
QueryBuilder.prototype.setRoot = function(addRule, data) {
362+
QueryBuilder.prototype.setRoot = function(addRule, data, flags) {
362363
addRule = (addRule === undefined || addRule === true);
363364

364365
var group_id = this.nextGroupId();
@@ -367,12 +368,13 @@ QueryBuilder.prototype.setRoot = function(addRule, data) {
367368
this.$el.append($group);
368369
this.model.root = new Group(null, $group);
369370
this.model.root.model = this.model;
370-
this.model.root.condition = this.settings.default_condition;
371-
this.model.root.flags = $.extend({}, this.settings.default_group_flags);
372371

373-
if (data !== undefined) {
374-
this.model.root.data = data;
375-
}
372+
this.model.root.data = data;
373+
this.model.root.flags = $.extend({}, this.settings.default_group_flags, flags);
374+
375+
this.trigger('afterAddGroup', this.model.root);
376+
377+
this.model.root.condition = this.settings.default_condition;
376378

377379
if (addRule) {
378380
this.addRule(this.model.root);
@@ -386,7 +388,7 @@ QueryBuilder.prototype.setRoot = function(addRule, data) {
386388
* @param parent {Group}
387389
* @param addRule {bool,optional} add a default empty rule
388390
* @param data {mixed,optional} group custom data
389-
* @param {object,optional} flags to apply to the group
391+
* @param flags {object,optional} flags to apply to the group
390392
* @return group {Group}
391393
*/
392394
QueryBuilder.prototype.addGroup = function(parent, addRule, data, flags) {
@@ -403,10 +405,7 @@ QueryBuilder.prototype.addGroup = function(parent, addRule, data, flags) {
403405
var $group = $(this.getGroupTemplate(group_id, level));
404406
var model = parent.addGroup($group);
405407

406-
if (data !== undefined) {
407-
model.data = data;
408-
}
409-
408+
model.data = data;
410409
model.flags = $.extend({}, this.settings.default_group_flags, flags);
411410

412411
this.trigger('afterAddGroup', model);

src/public.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ QueryBuilder.prototype.setRules = function(data) {
217217
}
218218

219219
this.clear();
220-
this.setRoot(false, data.data);
221-
222-
this.model.root.flags = this.parseGroupFlags(data);
220+
this.setRoot(false, data.data, this.parseGroupFlags(data));
223221

224222
data = this.change('setRules', data);
225223

@@ -247,13 +245,11 @@ QueryBuilder.prototype.setRules = function(data) {
247245
Utils.error('RulesParse', 'No more than {0} groups are allowed', self.settings.allow_groups);
248246
}
249247
else {
250-
model = self.addGroup(group, false, item.data);
248+
model = self.addGroup(group, false, item.data, self.parseGroupFlags(item));
251249
if (model === null) {
252250
return;
253251
}
254252

255-
model.flags = self.parseGroupFlags(item);
256-
257253
add(item, model);
258254
}
259255
}

0 commit comments

Comments
 (0)