Skip to content

Commit 7f25bb0

Browse files
committed
Fix #626 Add "disable_template" option for plugins invert, not-group, sortable
1 parent 51fceae commit 7f25bb0

File tree

7 files changed

+111
-32
lines changed

7 files changed

+111
-32
lines changed

src/plugins/invert/plugin.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,36 @@ QueryBuilder.define('invert', function(options) {
2929
});
3030

3131
// Modify templates
32-
this.on('getGroupTemplate.filter', function(h, level) {
33-
var $h = $(h.value);
34-
$h.find(Selectors.condition_container).after('<button type="button" class="btn btn-xs btn-default" data-invert="group"><i class="' + options.icon + '"></i> ' + self.translate('invert') + '</button>');
35-
h.value = $h.prop('outerHTML');
36-
});
37-
38-
if (options.display_rules_button && options.invert_rules) {
39-
this.on('getRuleTemplate.filter', function(h) {
32+
if (!options.disable_template) {
33+
this.on('getGroupTemplate.filter', function(h) {
4034
var $h = $(h.value);
41-
$h.find(Selectors.rule_actions).prepend('<button type="button" class="btn btn-xs btn-default" data-invert="rule"><i class="' + options.icon + '"></i> ' + self.translate('invert') + '</button>');
35+
$h.find(Selectors.condition_container).after(
36+
'<button type="button" class="btn btn-xs btn-default" data-invert="group">' +
37+
'<i class="' + options.icon + '"></i> ' + self.translate('invert') +
38+
'</button>'
39+
);
4240
h.value = $h.prop('outerHTML');
4341
});
42+
43+
if (options.display_rules_button && options.invert_rules) {
44+
this.on('getRuleTemplate.filter', function(h) {
45+
var $h = $(h.value);
46+
$h.find(Selectors.rule_actions).prepend(
47+
'<button type="button" class="btn btn-xs btn-default" data-invert="rule">' +
48+
'<i class="' + options.icon + '"></i> ' + self.translate('invert') +
49+
'</button>'
50+
);
51+
h.value = $h.prop('outerHTML');
52+
});
53+
}
4454
}
4555
}, {
4656
icon: 'glyphicon glyphicon-random',
4757
recursive: true,
4858
invert_rules: true,
4959
display_rules_button: false,
50-
silent_fail: false
60+
silent_fail: false,
61+
disable_template: false
5162
});
5263

5364
QueryBuilder.defaults({

src/plugins/not-group/plugin.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ QueryBuilder.define('not-group', function(options) {
3030
});
3131

3232
// Modify templates
33-
this.on('getGroupTemplate.filter', function(h, level) {
34-
var $h = $(h.value);
35-
$h.find(QueryBuilder.selectors.condition_container).prepend(
36-
'<button type="button" class="btn btn-xs btn-default" data-not="group">' +
37-
'<i class="' + options.icon_unchecked + '"></i> ' + self.translate('NOT') +
38-
'</button>'
39-
);
40-
h.value = $h.prop('outerHTML');
41-
});
33+
if (!options.disable_template) {
34+
this.on('getGroupTemplate.filter', function(h) {
35+
var $h = $(h.value);
36+
$h.find(QueryBuilder.selectors.condition_container).prepend(
37+
'<button type="button" class="btn btn-xs btn-default" data-not="group">' +
38+
'<i class="' + options.icon_unchecked + '"></i> ' + self.translate('NOT') +
39+
'</button>'
40+
);
41+
h.value = $h.prop('outerHTML');
42+
});
43+
}
4244

4345
// Export "not" to JSON
4446
this.on('groupToJson.filter', function(e, group) {
@@ -94,7 +96,8 @@ QueryBuilder.define('not-group', function(options) {
9496
});
9597
}, {
9698
icon_unchecked: 'glyphicon glyphicon-unchecked',
97-
icon_checked: 'glyphicon glyphicon-check'
99+
icon_checked: 'glyphicon glyphicon-check',
100+
disable_template: false
98101
});
99102

100103
/**

src/plugins/sortable/plugin.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,26 @@ QueryBuilder.define('sortable', function(options) {
146146
});
147147

148148
// Modify templates
149-
this.on('getGroupTemplate.filter', function(h, level) {
150-
if (level > 1) {
149+
if (!options.disable_template) {
150+
this.on('getGroupTemplate.filter', function(h, level) {
151+
if (level > 1) {
152+
var $h = $(h.value);
153+
$h.find(QueryBuilder.selectors.condition_container).after('<div class="drag-handle"><i class="' + options.icon + '"></i></div>');
154+
h.value = $h.prop('outerHTML');
155+
}
156+
});
157+
158+
this.on('getRuleTemplate.filter', function(h) {
151159
var $h = $(h.value);
152-
$h.find(QueryBuilder.selectors.condition_container).after('<div class="drag-handle"><i class="' + options.icon + '"></i></div>');
160+
$h.find(QueryBuilder.selectors.rule_header).after('<div class="drag-handle"><i class="' + options.icon + '"></i></div>');
153161
h.value = $h.prop('outerHTML');
154-
}
155-
});
156-
157-
this.on('getRuleTemplate.filter', function(h) {
158-
var $h = $(h.value);
159-
$h.find(QueryBuilder.selectors.rule_header).after('<div class="drag-handle"><i class="' + options.icon + '"></i></div>');
160-
h.value = $h.prop('outerHTML');
161-
});
162+
});
163+
}
162164
}, {
163165
inherit_no_sortable: true,
164166
inherit_no_drop: true,
165-
icon: 'glyphicon glyphicon-sort'
167+
icon: 'glyphicon glyphicon-sort',
168+
disable_template: false
166169
});
167170

168171
QueryBuilder.selectors.rule_and_group_containers = QueryBuilder.selectors.rule_container + ', ' + QueryBuilder.selectors.group_container;

tests/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<script src="../bower_components/sql-parser/browser/sql-parser.js"></script>
2828
<script src="../bower_components/jquery-extendext/jQuery.extendext.min.js"></script>
2929
<script src="../bower_components/doT/doT.js"></script>
30+
<script src="../bower_components/interact/dist/interact.js"></script>
3031

3132
<script src="common.js"></script>
3233

tests/plugins-gui.module.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,35 @@ $(function(){
203203
'Rule should contain a new button enabled with Bootbox'
204204
);
205205
});
206+
207+
/**
208+
* Test sortable
209+
*/
210+
QUnit.test('sortable', function(assert) {
211+
$b.queryBuilder({
212+
filters: basic_filters,
213+
rules: basic_rules,
214+
plugins: ['sortable']
215+
});
216+
217+
assert.ok(
218+
$b.find('.drag-handle').length > 0,
219+
'Should add the drag handles'
220+
);
221+
222+
$b.queryBuilder('destroy');
223+
224+
$b.queryBuilder({
225+
plugins: {
226+
'sortable': {disable_template: true}
227+
},
228+
filters: basic_filters,
229+
rules: basic_rules
230+
});
231+
232+
assert.ok(
233+
$b.find('.drag-handle').length === 0,
234+
'Should not have added the handles with disable_template=true'
235+
);
236+
});
206237
});

tests/plugins.module.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ $(function(){
138138
},
139139
'Should have inverted all conditions and operators'
140140
);
141+
142+
$b.queryBuilder('destroy');
143+
144+
$b.queryBuilder({
145+
plugins: {
146+
invert: {disable_template: true}
147+
},
148+
filters: basic_filters,
149+
rules: basic_rules
150+
});
151+
152+
assert.ok(
153+
$b.find('[data-invert="group"]').length === 0,
154+
'Should not have added the button with disable_template=true'
155+
);
141156
});
142157

143158
/**

tests/plugins.not-group.module.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ $(function () {
3030
$b.queryBuilder('getRules').not,
3131
'The root json should have "not" flag set to true'
3232
);
33+
34+
$b.queryBuilder('destroy');
35+
36+
$b.queryBuilder({
37+
plugins: {
38+
'not-group': {disable_template: true}
39+
},
40+
filters: basic_filters,
41+
rules: basic_rules
42+
});
43+
44+
assert.ok(
45+
$b.find('[data-not="group"]').length === 0,
46+
'Should not have added the button with disable_template=true'
47+
);
3348
});
3449

3550
QUnit.test('SQL export', function (assert) {

0 commit comments

Comments
 (0)