Skip to content

Commit

Permalink
Merge pull request #428 Fix #426 add "skip_empty" flag to "getRules"
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruud-cb authored and mistic100 committed Feb 1, 2017
1 parent e5d48f8 commit 62f6f44
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
5 changes: 4 additions & 1 deletion examples/index.html
Expand Up @@ -595,7 +595,10 @@ <h3>Output</h3>
$('.parse-json').on('click', function() {
$('#result').removeClass('hide')
.find('pre').html(JSON.stringify(
$('#builder').queryBuilder('getRules', { get_flags: true }),
$('#builder').queryBuilder('getRules', {
get_flags: true,
skip_empty: true
}),
undefined, 2
));
});
Expand Down
35 changes: 29 additions & 6 deletions src/public.js
Expand Up @@ -72,9 +72,15 @@ QueryBuilder.prototype.getModel = function(target) {

/**
* Validate the whole builder
* @param {object} options
* - skip_empty: false[default] | true(skips validating rules that have no filter selected)
* @return {boolean}
*/
QueryBuilder.prototype.validate = function() {
QueryBuilder.prototype.validate = function(options) {
options = $.extend({
skip_empty: false
}, options);

this.clearErrors();

var self = this;
Expand All @@ -84,6 +90,10 @@ QueryBuilder.prototype.validate = function() {
var errors = 0;

group.each(function(rule) {
if (!rule.filter && options.skip_empty) {
return;
}

if (!rule.filter) {
self.triggerValidationError(rule, 'no_filter', null);
errors++;
Expand All @@ -109,17 +119,21 @@ QueryBuilder.prototype.validate = function() {
done++;

}, function(group) {
if (parse(group)) {
var res = parse(group);
if (res === true) {
done++;
}
else {
else if (res === false) {
errors++;
}
});

if (errors > 0) {
return false;
}
else if (done === 0 && !group.isRoot() && options.skip_empty) {
return null;
}
else if (done === 0 && (!self.settings.allow_empty || !group.isRoot())) {
self.triggerValidationError(group, 'empty_group', null);
return false;
Expand All @@ -137,15 +151,17 @@ QueryBuilder.prototype.validate = function() {
* @param {object} options
* - get_flags: false[default] | true(only changes from default flags) | 'all'
* - allow_invalid: false[default] | true(returns rules even if they are invalid)
* - skip_empty: false[default] | true(remove rules that have no filter selected)
* @return {object}
*/
QueryBuilder.prototype.getRules = function(options) {
options = $.extend({
get_flags: false,
allow_invalid: false
allow_invalid: false,
skip_empty: false
}, options);

var valid = this.validate();
var valid = this.validate(options);
if (!valid && !options.allow_invalid) {
return null;
}
Expand All @@ -170,6 +186,10 @@ QueryBuilder.prototype.getRules = function(options) {
}

group.each(function(rule) {
if (!rule.filter && options.skip_empty) {
return;
}

var value = null;
if (!rule.operator || rule.operator.nb_inputs !== 0) {
value = rule.value;
Expand Down Expand Up @@ -198,7 +218,10 @@ QueryBuilder.prototype.getRules = function(options) {
groupData.rules.push(self.change('ruleToJson', ruleData, rule));

}, function(model) {
groupData.rules.push(parse(model));
var data = parse(model);
if (data.rules.length !== 0 || !options.skip_empty) {
groupData.rules.push(data);
}
}, this);

return self.change('groupToJson', groupData, group);
Expand Down
51 changes: 43 additions & 8 deletions tests/data.module.js
@@ -1,4 +1,4 @@
$(function(){
$(function() {
var $b = $('#builder');

QUnit.module('data', {
Expand Down Expand Up @@ -31,9 +31,9 @@ $(function(){
type: 'string',
input: 'select',
values: [
{one: 'One'},
{two: 'Two'},
{three: 'Three'}
{ one: 'One' },
{ two: 'Two' },
{ three: 'Three' }
]
}],
rules: {
Expand Down Expand Up @@ -207,7 +207,7 @@ $(function(){
QUnit.test('custom data', function(assert) {
var rules = {
condition: 'AND',
data: [1,2,3],
data: [1, 2, 3],
rules: [{
id: 'name',
value: 'Mistic',
Expand Down Expand Up @@ -361,13 +361,13 @@ $(function(){
};

assert.rulesMatch(
$b.queryBuilder('getRules', {get_flags: true}),
$b.queryBuilder('getRules', { get_flags: true }),
rules_changed_flags,
'Should export rules with changed flags'
);

assert.rulesMatch(
$b.queryBuilder('getRules', {get_flags: 'all'}),
$b.queryBuilder('getRules', { get_flags: 'all' }),
rules_all_flags,
'Should export rules with all flags'
);
Expand Down Expand Up @@ -455,13 +455,48 @@ $(function(){
);
});

/**
* Test skip_empty option
*/
QUnit.test('skip empty', function(assert) {
$b.queryBuilder({
filters: basic_filters
});

$b.queryBuilder('setRules', {
condition: 'AND',
rules: [{
id: 'name',
operator: 'equal',
value: 'Mistic'
}, {
empty: true
}]
});

assert.rulesMatch(
$b.queryBuilder('getRules', {
skip_empty: true
}),
{
condition: 'AND',
rules: [{
id: 'name',
operator: 'equal',
value: 'Mistic'
}]
},
'Should skip empty rules for getRules'
);
});

/**
* Test allow_empty_value option
*/
QUnit.test('allow empty value', function(assert) {
var filters = $.extend(true, [], basic_filters);
filters.forEach(function(filter) {
filter.validation = $.extend({allow_empty_value: true}, filter.validation);
filter.validation = $.extend({ allow_empty_value: true }, filter.validation);
});

$b.queryBuilder({
Expand Down

0 comments on commit 62f6f44

Please sign in to comment.