Skip to content

Commit a08cca1

Browse files
committed
Fixes mistic100#9 Configurable operators
1 parent dea9b85 commit a08cca1

File tree

12 files changed

+94
-37
lines changed

12 files changed

+94
-37
lines changed

dist/i18n/en.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jQuery.fn.queryBuilder.defaults.set({ lang: {
44
"delete_rule": "Delete",
55
"delete_group": "Delete",
66

7-
"and_condition": "AND",
8-
"or_condition": "OR",
7+
"condition_and": "AND",
8+
"condition_or": "OR",
99

1010
"filter_select_placeholder": "------",
1111

dist/i18n/fr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jQuery.fn.queryBuilder.defaults.set({ lang: {
44
"delete_rule": "Supprimer",
55
"delete_group": "Supprimer",
66

7-
"and_condition": "ET",
8-
"or_condition": "OU",
7+
"condition_and": "ET",
8+
"condition_or": "OU",
99

1010
"filter_select_placeholder": "------",
1111

dist/i18n/ro.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jQuery.fn.queryBuilder.defaults.set({ lang: {
44
"delete_rule": "Şterge",
55
"delete_group": "Şterge",
66

7-
"and_condition": "ŞI",
8-
"or_condition": "SAU",
7+
"condition_and": "ŞI",
8+
"condition_or": "SAU",
99

1010
"filter_select_placeholder": "------",
1111

dist/query-builder.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@
144144
sortable: false,
145145
filters: [],
146146

147+
conditions: ['AND', 'OR'],
148+
default_condition: 'AND',
149+
147150
template: {
148151
group: null,
149152
rule: null
@@ -155,8 +158,8 @@
155158
delete_rule: 'Delete',
156159
delete_group: 'Delete',
157160

158-
and_condition: 'AND',
159-
or_condition: 'OR',
161+
condition_and: 'AND',
162+
condition_or: 'OR',
160163

161164
filter_select_placeholder: '------',
162165

@@ -331,11 +334,13 @@
331334
$buttons = $group.find('>.rules-group-header input[name$=_cond]');
332335

333336
if (!data.condition) {
334-
data.condition = 'AND';
337+
data.condition = that.settings.default_condition;
335338
}
336339

337-
$buttons.filter('[value=AND]').prop('checked', data.condition.toUpperCase() == 'AND');
338-
$buttons.filter('[value=OR]').prop('checked', data.condition.toUpperCase() == 'OR');
340+
for (var i=0, l=that.settings.conditions.length; i<l; i++) {
341+
var cond = that.settings.conditions[i];
342+
$buttons.filter('[value='+ cond +']').prop('checked', data.condition.toUpperCase() == cond.toUpperCase());
343+
}
339344
$buttons.trigger('change');
340345

341346
$.each(data.rules, function(i, rule) {
@@ -996,9 +1001,17 @@
9961001
<button type="button" class="btn btn-xs btn-success" data-add="group"><i class="glyphicon glyphicon-plus-sign"></i> '+ this.lang.add_group +'</button> \
9971002
<button type="button" class="btn btn-xs btn-danger" data-delete="group"><i class="glyphicon glyphicon-remove"></i> '+ this.lang.delete_group +'</button> \
9981003
</div> \
999-
<div class="btn-group"> \
1000-
<label class="btn btn-xs btn-primary active"><input type="radio" name="'+ group_id +'_cond" value="AND" checked> '+ this.lang.and_condition +'</label> \
1001-
<label class="btn btn-xs btn-primary"><input type="radio" name="'+ group_id +'_cond" value="OR"> '+ this.lang.or_condition +'</label> \
1004+
<div class="btn-group">';
1005+
1006+
for (var i=0, l=this.settings.conditions.length; i<l; i++) {
1007+
var cond = this.settings.conditions[i],
1008+
active = cond == this.settings.default_condition,
1009+
label = this.lang['condition_'+ cond.toLowerCase()] || cond;
1010+
1011+
h+= '<label class="btn btn-xs btn-primary '+ (active?'active':'') +'"><input type="radio" name="'+ group_id +'_cond" value="'+ cond +'" '+ (active?'checked':'') +'> '+ label +'</label>';
1012+
}
1013+
1014+
h+= '\
10021015
</div> \
10031016
'+ (this.settings.sortable ? '<div class="drag-handle"><i class="glyphicon glyphicon-sort"></i></div>' : '') +' \
10041017
</dt> \

dist/query-builder.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.

src/i18n/en.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jQuery.fn.queryBuilder.defaults.set({ lang: {
44
"delete_rule": "Delete",
55
"delete_group": "Delete",
66

7-
"and_condition": "AND",
8-
"or_condition": "OR",
7+
"condition_and": "AND",
8+
"condition_or": "OR",
99

1010
"filter_select_placeholder": "------",
1111

src/i18n/fr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jQuery.fn.queryBuilder.defaults.set({ lang: {
44
"delete_rule": "Supprimer",
55
"delete_group": "Supprimer",
66

7-
"and_condition": "ET",
8-
"or_condition": "OU",
7+
"condition_and": "ET",
8+
"condition_or": "OU",
99

1010
"filter_select_placeholder": "------",
1111

src/i18n/ro.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jQuery.fn.queryBuilder.defaults.set({ lang: {
44
"delete_rule": "Şterge",
55
"delete_group": "Şterge",
66

7-
"and_condition": "ŞI",
8-
"or_condition": "SAU",
7+
"condition_and": "ŞI",
8+
"condition_or": "SAU",
99

1010
"filter_select_placeholder": "------",
1111

src/query-builder.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@
145145
sortable: false,
146146
filters: [],
147147

148+
conditions: ['AND', 'OR'],
149+
default_condition: 'AND',
150+
148151
template: {
149152
group: null,
150153
rule: null
@@ -156,8 +159,8 @@
156159
delete_rule: 'Delete',
157160
delete_group: 'Delete',
158161

159-
and_condition: 'AND',
160-
or_condition: 'OR',
162+
condition_and: 'AND',
163+
condition_or: 'OR',
161164

162165
filter_select_placeholder: '------',
163166

@@ -332,11 +335,13 @@
332335
$buttons = $group.find('>.rules-group-header input[name$=_cond]');
333336

334337
if (!data.condition) {
335-
data.condition = 'AND';
338+
data.condition = that.settings.default_condition;
336339
}
337340

338-
$buttons.filter('[value=AND]').prop('checked', data.condition.toUpperCase() == 'AND');
339-
$buttons.filter('[value=OR]').prop('checked', data.condition.toUpperCase() == 'OR');
341+
for (var i=0, l=that.settings.conditions.length; i<l; i++) {
342+
var cond = that.settings.conditions[i];
343+
$buttons.filter('[value='+ cond +']').prop('checked', data.condition.toUpperCase() == cond.toUpperCase());
344+
}
340345
$buttons.trigger('change');
341346

342347
$.each(data.rules, function(i, rule) {
@@ -997,9 +1002,17 @@
9971002
<button type="button" class="btn btn-xs btn-success" data-add="group"><i class="glyphicon glyphicon-plus-sign"></i> '+ this.lang.add_group +'</button> \
9981003
<button type="button" class="btn btn-xs btn-danger" data-delete="group"><i class="glyphicon glyphicon-remove"></i> '+ this.lang.delete_group +'</button> \
9991004
</div> \
1000-
<div class="btn-group"> \
1001-
<label class="btn btn-xs btn-primary active"><input type="radio" name="'+ group_id +'_cond" value="AND" checked> '+ this.lang.and_condition +'</label> \
1002-
<label class="btn btn-xs btn-primary"><input type="radio" name="'+ group_id +'_cond" value="OR"> '+ this.lang.or_condition +'</label> \
1005+
<div class="btn-group">';
1006+
1007+
for (var i=0, l=this.settings.conditions.length; i<l; i++) {
1008+
var cond = this.settings.conditions[i],
1009+
active = cond == this.settings.default_condition,
1010+
label = this.lang['condition_'+ cond.toLowerCase()] || cond;
1011+
1012+
h+= '<label class="btn btn-xs btn-primary '+ (active?'active':'') +'"><input type="radio" name="'+ group_id +'_cond" value="'+ cond +'" '+ (active?'checked':'') +'> '+ label +'</label>';
1013+
}
1014+
1015+
h+= '\
10031016
</div> \
10041017
'+ (this.settings.sortable ? '<div class="drag-handle"><i class="glyphicon glyphicon-sort"></i></div>' : '') +' \
10051018
</dt> \

tests/core_tests.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $(function(){
1313
$('#container2').queryBuilder('setRules', basic_rules);
1414

1515
assert.ok(rulesMatch($('#container2').queryBuilder('getRules'), basic_rules), 'Should return object with rules');
16-
assert.deepEqual(getSelectOptions($('#container2_rule_1 [name$=_operator]')), basic_filters[1].operators, 'Should respect the order of operators');
16+
assert.deepEqual(getOptions($('#container2_rule_1 [name$=_operator] option')), basic_filters[1].operators, 'Should respect the order of operators');
1717
});
1818

1919
QUnit.test('Empty value check', function(assert) {
@@ -42,25 +42,39 @@ $(function(){
4242
});
4343
});
4444

45-
QUnit.test('Operators', function(assert) {
45+
QUnit.test('Delete/add operators', function(assert) {
4646
$('#container4').queryBuilder({
4747
filters: filters_for_custom_operators,
4848
operators: custom_operators
4949
});
5050

5151
$('#container4').queryBuilder('setRules', rules_for_custom_operators);
5252

53-
assert.deepEqual(getSelectOptions($('#container4_rule_0 [name$=_operator]')), ['equal', 'not_equal'], 'String type should have equal & not_equal operators');
54-
assert.deepEqual(getSelectOptions($('#container4_rule_1 [name$=_operator]')), ['less', 'greater'], 'Number type should have less & greater operators');
55-
assert.deepEqual(getSelectOptions($('#container4_rule_2 [name$=_operator]')), ['before', 'after'], 'Datetime type should have before & after operators');
53+
assert.deepEqual(getOptions($('#container4_rule_0 [name$=_operator] option')), ['equal', 'not_equal'], 'String type should have equal & not_equal operators');
54+
assert.deepEqual(getOptions($('#container4_rule_1 [name$=_operator] option')), ['less', 'greater'], 'Number type should have less & greater operators');
55+
assert.deepEqual(getOptions($('#container4_rule_2 [name$=_operator] option')), ['before', 'after'], 'Datetime type should have before & after operators');
56+
});
57+
58+
QUnit.test('Change conditions', function(assert) {
59+
$('#container5').queryBuilder({
60+
filters: basic_filters,
61+
conditions: ['NAND', 'XOR'],
62+
default_condition: 'NAND'
63+
});
64+
65+
$('#container5').queryBuilder('setRules', rules_for_custom_conditions);
66+
67+
assert.ok(rulesMatch($('#container5').queryBuilder('getRules'), rules_for_custom_conditions), 'Should return correct rules');
68+
69+
assert.deepEqual(getOptions($('#container5_group_0>.rules-group-header [name$=_cond]')), ['NAND', 'XOR'], 'Conditions should be NAND & XOR');
5670
});
5771

5872
});
5973

60-
function getSelectOptions($target) {
74+
function getOptions($target) {
6175
var options = [];
6276

63-
$target.find('option').each(function(){
77+
$target.each(function(){
6478
options.push($(this).val());
6579
});
6680

0 commit comments

Comments
 (0)