Skip to content

Commit

Permalink
Add support for default custom search function
Browse files Browse the repository at this point in the history
  • Loading branch information
javve committed Feb 3, 2014
1 parent f5f6817 commit 4590a7b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ var naturalSort = require('natural-sort'),
getAttribute = require('get-attribute');

module.exports = function(list) {
var sortButtons;
var sortButtons, options, valueName;

list.sortFunction = list.sortFunction || function(itemA, itemB) {
return naturalSort(itemA.values()[valueName], itemB.values()[valueName], options);
};

var buttons = {
els: undefined,
Expand Down Expand Up @@ -41,10 +45,10 @@ module.exports = function(list) {
};
var sort = function() {
list.trigger('sortStart');
options = {},
valueName = "";

var options = {},
valueName,
target = arguments[0].currentTarget || arguments[0].srcElement || undefined;
var target = arguments[0].currentTarget || arguments[0].srcElement || undefined;

if (target) {
valueName = getAttribute(target, 'data-sort');
Expand All @@ -60,11 +64,10 @@ module.exports = function(list) {
}

options.desc = options.order == "desc" ? true : false;
options.sortFunction = options.sortFunction || function(a, b) {
return naturalSort(a.values()[valueName], b.values()[valueName], options);
};

list.items.sort(options.sortFunction);
options.sortFunction = options.sortFunction || list.sortFunction;
list.items.sort(function(a, b) {
return options.sortFunction(a, b, options);
});
list.update();
list.trigger('sortComplete');
};
Expand Down
58 changes: 58 additions & 0 deletions test/test.sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,62 @@ describe('Sort', function() {
});
*/
});

describe('Custom sort function', function() {
it('should use custom sort option', function() {
i1.values({ val: "<input value='b' />" });
i2.values({ val: "<input value='a' />" });
i3.values({ val: "<input value='c' />" });
i4.values({ val: "<input value='z' />" });
i5.values({ val: "<input value='s' />" });
i6.values({ val: "<input value='y' />" });
list.sort('val', {
sortFunction: function(itemA, itemB, options) {
return list.helpers.naturalSort($(itemA.values().val).val(), $(itemB.values().val).val(), options);
}
});
expect(list.items[0].values().val).to.be.equal("<input value='a' />");
expect(list.items[1].values().val).to.be.equal("<input value='b' />");
expect(list.items[2].values().val).to.be.equal("<input value='c' />");
expect(list.items[3].values().val).to.be.equal("<input value='s' />");
expect(list.items[4].values().val).to.be.equal("<input value='y' />");
expect(list.items[5].values().val).to.be.equal("<input value='z' />");
});
it('should use default custom sort function', function() {
list.sortFunction = function(itemA, itemB, options) {
return list.helpers.naturalSort($(itemA.values().val).val(), $(itemB.values().val).val(), options);
};
i1.values({ val: "<input value='b' />" });
i2.values({ val: "<input value='a' />" });
i3.values({ val: "<input value='c' />" });
i4.values({ val: "<input value='z' />" });
i5.values({ val: "<input value='s' />" });
i6.values({ val: "<input value='y' />" });
list.sort('val');
expect(list.items[0].values().val).to.be.equal("<input value='a' />");
expect(list.items[1].values().val).to.be.equal("<input value='b' />");
expect(list.items[2].values().val).to.be.equal("<input value='c' />");
expect(list.items[3].values().val).to.be.equal("<input value='s' />");
expect(list.items[4].values().val).to.be.equal("<input value='y' />");
expect(list.items[5].values().val).to.be.equal("<input value='z' />");
});
it('should use default custom sort function with options', function() {
list.sortFunction = function(itemA, itemB, options) {
return list.helpers.naturalSort($(itemA.values().val).val(), $(itemB.values().val).val(), options);
};
i1.values({ val: "<input value='b' />" });
i2.values({ val: "<input value='a' />" });
i3.values({ val: "<input value='c' />" });
i4.values({ val: "<input value='z' />" });
i5.values({ val: "<input value='s' />" });
i6.values({ val: "<input value='y' />" });
list.sort('val', { order: "desc"});
expect(list.items[0].values().val).to.be.equal("<input value='z' />");
expect(list.items[1].values().val).to.be.equal("<input value='y' />");
expect(list.items[2].values().val).to.be.equal("<input value='s' />");
expect(list.items[3].values().val).to.be.equal("<input value='c' />");
expect(list.items[4].values().val).to.be.equal("<input value='b' />");
expect(list.items[5].values().val).to.be.equal("<input value='a' />");
});
});
});

0 comments on commit 4590a7b

Please sign in to comment.