Skip to content

Commit

Permalink
Close #264 ass 'sort_filters' option
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Mar 30, 2016
1 parent b96c02b commit eda478d
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 48 deletions.
12 changes: 12 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ QueryBuilder.prototype.checkFilters = function(filters) {
}
}, this);

if (this.settings.sort_filters) {
if (typeof this.settings.sort_filters == 'function') {
filters.sort(this.settings.sort_filters);
}
else {
var self = this;
filters.sort(function(a, b) {
return self.translateLabel(a.label).localeCompare(self.translateLabel(b.label));
});
}
}

if (this.status.has_optgroup) {
filters = Utils.groupSort(filters, 'optgroup');
}
Expand Down
2 changes: 1 addition & 1 deletion src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,5 +497,5 @@ QueryBuilder.prototype.getGroupFlags = function(flags, all) {
* @return string
*/
QueryBuilder.prototype.translateLabel = function(label) {
return typeof label == 'string' ? label : label[this.settings.lang_code] || label['en'];
return typeof label == 'object' ? (label[this.settings.lang_code] || label['en']) : label;
};
1 change: 1 addition & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ QueryBuilder.DEFAULTS = {
filters: [],
plugins: [],

sort_filters: false,
display_errors: true,
allow_groups: -1,
allow_empty: false,
Expand Down
78 changes: 78 additions & 0 deletions tests/core.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,84 @@ $(function(){
);
});

/**
* Test filters ordering
*/
QUnit.test('Sort filters', function(assert) {
$b.queryBuilder({
filters: [{
id: '3',
label: {
fr: 'ccc',
en: 'Ccc'
}
}, {
id: '1',
label: 'AAA'
}, {
id: '5',
label: 'eee'
}, {
id: '2',
label: 'bbb'
}, {
id: '4',
label: {
fr: 'ddd',
en: 'Ddd'
}
}],
sort_filters: true,
lang_code: 'fr'
});

var options = [];
$('[name=builder_rule_0_filter]>*').each(function() {
options.push($(this).val());
});

assert.deepEqual(
options,
['-1', '1', '2', '3', '4', '5'],
'Filters should be sorted by alphabetical order'
);

$b.queryBuilder('destroy');

$b.queryBuilder({
filters: [{
id: '3',
label: 'ccc'
}, {
id: '1',
label: 'AAA'
}, {
id: '5',
label: 'eee'
}, {
id: '2',
label: 'bbb'
}, {
id: '4',
label: 'ddd'
}],
sort_filters: function(a, b) {
return parseInt(b.id) - parseInt(a.id);
}
});

options = [];
$('[name=builder_rule_0_filter]>*').each(function() {
options.push($(this).val());
});

assert.deepEqual(
options,
['-1', '5', '4', '3', '2', '1'],
'Filters should be sorted by custom order'
);
});

/**
* Test access to defaults
*/
Expand Down
203 changes: 156 additions & 47 deletions tests/utils.module.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,168 @@
$(function(){
$(function () {

QUnit.module('utils', {
afterEach: function() {
QUnit.module('utils');

/**
* Test iterateOptions
*/
QUnit.test('iterateOptions', function (assert) {
var output;
function callback(a, b) {
output.push(a, b);
}

output = [];
Utils.iterateOptions(['one', 'foo', 'bar'], callback);
assert.deepEqual(
output,
['one', 'one', 'foo', 'foo', 'bar', 'bar'],
'Should iterate simple array'
);

output = [];
Utils.iterateOptions({1: 'one', 2: 'foo', 3: 'bar'}, callback);
assert.deepEqual(
output,
['1', 'one', '2', 'foo', '3', 'bar'],
'Should iterate simple hash-map'
);

output = [];
Utils.iterateOptions([{1: 'one'}, {2: 'foo'}, {3: 'bar'}], callback);
assert.deepEqual(
output,
['1', 'one', '2', 'foo', '3', 'bar'],
'Should iterate array of hash-maps'
);
});

/**
* Test
* Test groupSort
*/
QUnit.test('utils-escapeElementId', function(assert) {
QUnit.test('groupSort', function (assert) {
var input = [
{id: '1'},
{id: '1.1', group: '1'},
{id: '2'},
{id: '2.1', group: '2'},
{id: '1.2', group: '1'},
{id: '2.2', group: '2'},
{id: '3'},
{id: '1.3', group: '1'}
];

var output = [
{id: '1'},
{id: '1.1', group: '1'},
{id: '1.2', group: '1'},
{id: '1.3', group: '1'},
{id: '2'},
{id: '2.1', group: '2'},
{id: '2.2', group: '2'},
{id: '3'}
];

assert.deepEqual(
Utils.escapeElementId('abc123'),
'abc123',
'Should not escape id'
);
Utils.groupSort(input, 'group'),
output,
'Should sort items by group'
);
});

/**
* Test fmt
*/
QUnit.test('fmt', function (assert) {
assert.equal(
Utils.fmt('{0} is equal to {1}', 1, 'one'),
'1 is equal to one',
'Should replace arguments'
);

assert.equal(
Utils.fmt('{0} is equal to {0}', 1),
'1 is equal to 1',
'Should replace one argument multiple times'
);
});

/**
* Test changeType
*/
QUnit.test('changeType', function (assert) {
assert.ok(
Utils.changeType('10', 'integer') === 10,
'"10" should be parsed as integer'
);

assert.ok(
Utils.changeType('10.5', 'integer') === 10,
'"10.5" should be parsed as integer'
);

assert.ok(
Utils.changeType('10.5', 'double') === 10.5,
'"10.5" should be parsed as double'
);

assert.ok(
Utils.changeType('true', 'boolean') === true,
'"true" should be parsed as boolean'
);

assert.ok(
Utils.changeType('false', 'boolean', true) === 0,
'"false" should be parsed as integer'
);
});

/**
* Test escapeElementId
*/
QUnit.test('escapeElementId', function (assert) {
assert.equal(
Utils.escapeElementId('abc123'),
'abc123',
'Should not alter id'
);

var chars = ':.[],';
for (var i = 0; i < chars.length; ++i)
{
assert.deepEqual(
Utils.escapeElementId('abc' + chars[i] + '123'),
'abc\\' + chars[i] + '123',
'Should escape \'' + chars[i] + '\' in id'
);

assert.deepEqual(
Utils.escapeElementId('abc\\' + chars[i] + '123'),
'abc\\' + chars[i] + '123',
'Should not escape \'\\' + chars[i] + '\' in id'
);

assert.deepEqual(
Utils.escapeElementId( chars[i] + 'abc123' ),
'\\' + chars[i] + 'abc123',
'Should escape \'' + chars[i] + '\' prefixing id'
);

assert.deepEqual(
Utils.escapeElementId( '\\' + chars[i] + 'abc123' ),
'\\' + chars[i] + 'abc123',
'Should not escape \'\\' + chars[i] + '\' prefixing id'
);

assert.deepEqual(
Utils.escapeElementId('abc123' + chars[i] ),
'abc123\\' + chars[i],
'Should escape \'' + chars[i] + '\' trailing in id'
);

assert.deepEqual(
Utils.escapeElementId('abc123\\' + chars[i] ),
'abc123\\' + chars[i],
'Should not escape \'\\' + chars[i] + '\' trailing in id'
);
for (var i = 0; i < chars.length; ++i) {
assert.equal(
Utils.escapeElementId('abc' + chars[i] + '123'),
'abc\\' + chars[i] + '123',
'Should escape \'' + chars[i] + '\' in id'
);

assert.equal(
Utils.escapeElementId('abc\\' + chars[i] + '123'),
'abc\\' + chars[i] + '123',
'Should not escape \'\\' + chars[i] + '\' in id'
);

assert.equal(
Utils.escapeElementId(chars[i] + 'abc123'),
'\\' + chars[i] + 'abc123',
'Should escape \'' + chars[i] + '\' prefixing id'
);

assert.equal(
Utils.escapeElementId('\\' + chars[i] + 'abc123'),
'\\' + chars[i] + 'abc123',
'Should not escape \'\\' + chars[i] + '\' prefixing id'
);

assert.equal(
Utils.escapeElementId('abc123' + chars[i]),
'abc123\\' + chars[i],
'Should escape \'' + chars[i] + '\' trailing in id'
);

assert.equal(
Utils.escapeElementId('abc123\\' + chars[i]),
'abc123\\' + chars[i],
'Should not escape \'\\' + chars[i] + '\' trailing in id'
);
}
});
});
});

0 comments on commit eda478d

Please sign in to comment.