Skip to content

Commit

Permalink
Add direction paramater to sortFunction invocation (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
broldak authored Feb 19, 2020
1 parent 587351c commit e37ccd1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 23 deletions.
35 changes: 19 additions & 16 deletions addon/components/models-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {isArray, A} from '@ember/array';
import betterCompare from '../utils/better-compare';
import layout from '../templates/components/models-table';
import ModelsTableColumn, {propertyNameToTitle} from '../utils/column';
import SORT_CONSTANTS from 'ember-models-table/constants/sort-constants';

/**
* @typedef {object} groupedHeader
Expand Down Expand Up @@ -108,7 +109,7 @@ function chunkBy(collection, propertyName, sortOrder) {
const sortedValues = values.slice().sort((v1, v2) => {
let result = betterCompare(v1, v2);
if (result !== 0) {
return (sortOrder === 'desc') ? (-1 * result) : result;
return (sortOrder === SORT_CONSTANTS.DESC) ? (-1 * result) : result;
}
return 0;
});
Expand Down Expand Up @@ -205,10 +206,12 @@ export default Component.extend({
* @default {{ none: 'asc', asc: 'desc', desc: 'none' }}
*/
sortMap: computed(function() {
const { ASC, DESC, NONE } = SORT_CONSTANTS;

return {
none: 'asc',
asc: 'desc',
desc: 'none'
[NONE]: ASC,
[ASC]: DESC,
[DESC]: NONE
};
}),

Expand Down Expand Up @@ -413,7 +416,7 @@ export default Component.extend({
* @default 'asc'
* @private
*/
sortByGroupedFieldDirection: 'asc',
sortByGroupedFieldDirection: SORT_CONSTANTS.ASC,

/**
* Determines how grouped value will be displayed - as a row or column
Expand Down Expand Up @@ -1053,7 +1056,7 @@ export default Component.extend({
const filteredContent = get(this, 'filteredContent');
let sortProperties = get(this, 'sortProperties').map(p => {
let [prop, direction] = p.split(':');
direction = direction || 'asc';
direction = direction || SORT_CONSTANTS.ASC;

return [prop, direction];
});
Expand All @@ -1064,9 +1067,9 @@ export default Component.extend({
for (let i = 0; i < sortedPropsLength; i++) {
let [prop, direction] = sortProperties[i];
let sortFunction = get(this, `sortFunctions.${prop}`) || betterCompare;
let result = prop ? sortFunction(get(row1, prop), get(row2, prop)) : 0;
let result = prop ? sortFunction(get(row1, prop), get(row2, prop), direction) : 0;
if (result !== 0) {
return (direction === 'desc') ? (-1 * result) : result;
return (direction === SORT_CONSTANTS.DESC) ? (-1 * result) : result;
}
}

Expand Down Expand Up @@ -1101,7 +1104,7 @@ export default Component.extend({
}
let sortProperties = get(this, 'sortProperties').map(p => {
let [prop, direction] = p.split(':');
direction = direction || 'asc';
direction = direction || SORT_CONSTANTS.ASC;
return [prop, direction];
});

Expand All @@ -1115,7 +1118,7 @@ export default Component.extend({
let sortFunction = get(this, `sortFunctions.${prop}`) || betterCompare;
let result = prop ? sortFunction(get(row1, prop), get(row2, prop)) : 0;
if (result !== 0) {
return (direction === 'desc') ? (-1 * result) : result;
return (direction === SORT_CONSTANTS.DESC) ? (-1 * result) : result;
}
}
return 0;
Expand Down Expand Up @@ -1573,7 +1576,7 @@ export default Component.extend({
const { sortDirection, sortPrecedence } = column;
const hasSortPrecedence = !isNone(sortPrecedence) && sortPrecedence > NOT_SORTED;
const defaultSortPrecedence = hasSortPrecedence ? sortPrecedence : NOT_SORTED;
const defaultSorting = sortDirection && (sortPrecedence > NOT_SORTED) ? sortDirection.toLowerCase() : 'none';
const defaultSorting = sortDirection && (sortPrecedence > NOT_SORTED) ? sortDirection.toLowerCase() : SORT_CONSTANTS.NONE;

setProperties(c, {
defaultVisible: !get(c, 'isHidden'),
Expand Down Expand Up @@ -1664,12 +1667,12 @@ export default Component.extend({
* @private
*/
_singleColumnSorting(column, sortedBy, newSorting) {
get(this, 'processedColumns').setEach('sorting', 'none');
get(this, 'processedColumns').setEach('sorting', SORT_CONSTANTS.NONE);
set(column, 'sorting', newSorting);
let sortFunctions = Object.create(null);
sortFunctions[sortedBy] = get(column, 'sortFunction');
set(this, 'sortFunctions', sortFunctions);
set(this, 'sortProperties', 'none' === newSorting ? [] : [`${sortedBy}:${newSorting}`]);
set(this, 'sortProperties', SORT_CONSTANTS.NONE === newSorting ? [] : [`${sortedBy}:${newSorting}`]);
},

/**
Expand Down Expand Up @@ -1700,7 +1703,7 @@ export default Component.extend({
}
set(newSortFunctions, propertyName, get(column, 'sortFunction'));
});
if ('none' !== newSorting) {
if (SORT_CONSTANTS.NONE !== newSorting) {
newSortProperties.pushObject(`${sortedBy}:${newSorting}`);
newSortFunctions[sortedBy] = get(column, 'sortFunction');
}
Expand Down Expand Up @@ -2025,11 +2028,11 @@ export default Component.extend({
if (!sortedBy) {
return;
}
let currentSorting = get(column, 'sorting') || 'none';
let currentSorting = get(column, 'sorting') || SORT_CONSTANTS.NONE;
let newSorting = sortMap[currentSorting.toLowerCase()];
if (sortedBy === get(this, 'currentGroupingPropertyName')) {
const sortByGroupedFieldDirection = get(this, 'sortByGroupedFieldDirection');
newSorting = sortByGroupedFieldDirection === 'asc' ? 'desc' : 'asc';
newSorting = sortByGroupedFieldDirection === SORT_CONSTANTS.ASC ? SORT_CONSTANTS.DESC : SORT_CONSTANTS.ASC;
set(this, 'sortByGroupedFieldDirection', newSorting);
return;
}
Expand Down
5 changes: 5 additions & 0 deletions addon/constants/sort-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
ASC: 'asc',
DESC: 'desc',
NONE: 'none'
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-models-table",
"version": "2.12.0",
"version": "2.14.0",
"description": "Table with pagination, sorting and filtering",
"keywords": [
"data-table",
Expand Down
3 changes: 1 addition & 2 deletions tests/dummy/app/routes/examples/sort-by-filter-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ export default ExampleRoute.extend({
{propertyName: 'age'},
{propertyName: 'city'}
]);
}

},
});
55 changes: 51 additions & 4 deletions tests/integration/components/models-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ module('ModelsTable | Integration', function (hooks) {
useNumericPagination: false
});

await render(hbs`{{models-table
data=data
await render(hbs`{{models-table
data=data
columns=columns
showCurrentPageNumberSelect=showCurrentPageNumberSelect
useNumericPagination=useNumericPagination
Expand Down Expand Up @@ -295,8 +295,8 @@ module('ModelsTable | Integration', function (hooks) {
useNumericPagination: false
});

await render(hbs`{{models-table
data=data
await render(hbs`{{models-table
data=data
columns=columns
showCurrentPageNumberSelect=showCurrentPageNumberSelect
useNumericPagination=useNumericPagination
Expand Down Expand Up @@ -1399,6 +1399,53 @@ module('ModelsTable | Integration', function (hooks) {

});

test('sorting, custom sort function conditional on direction (multi `false`)', async function (assert) {

const columns = generateColumns(['index', 'index2']);
columns[0].sortFunction = function sortEvenFirstIfAscending(i1, i2, dir) {
if (dir !== 'asc') {
return compare(i1, i2);
}

if (i1 % 2 === 0) {
if (i2 % 2 === 0) {
return compare(i1, i2);
}
return -1
} else {
if (i2 % 2 === 0) {
return 1;
}
return compare(i1, i2);
}
};

this.setProperties({
columns: columns,
data: generateContent(10, 1)
});
await render(hbs`{{models-table columns=columns data=data multipleColumnsSorting=false}}`);
await this.ModelsTablePageObject.sorting.objectAt(0).click();

assert.deepEqual(this.ModelsTablePageObject.getColumnCells(0), ['2', '4', '6', '8', '10', '1', '3', '5', '7', '9'], 'Content is valid (sorting 1st column asc)');

await this.ModelsTablePageObject.sorting.objectAt(0).click();

assert.deepEqual(this.ModelsTablePageObject.getColumnCells(0), ['10', '9', '8', '7', '6', '5', '4', '3', '2', '1'], 'Content is valid (sorting 1st column desc)');

await this.ModelsTablePageObject.sorting.objectAt(0).click();
await this.ModelsTablePageObject.sorting.objectAt(1).click();

assert.deepEqual(this.ModelsTablePageObject.getColumnCells(0), oneTenArrayDig, 'Content is valid (sorting 1st column asc) - restore defaults');
assert.deepEqual(this.ModelsTablePageObject.getColumnCells(1), ['1', '1', '2', '2', '3', '3', '4', '4', '5', '5'], 'Content is valid (sorting 2nd column asc) - restore defaults');

await this.ModelsTablePageObject.sorting.objectAt(0).click();
await this.ModelsTablePageObject.sorting.objectAt(0).click();
assert.deepEqual(this.ModelsTablePageObject.getColumnCells(0), ['10', '9', '8', '7', '6', '5', '4', '3', '2', '1'], 'Content is valid (sorting 1st column desc)');
assert.deepEqual(this.ModelsTablePageObject.getColumnCells(1), ['5', '5', '4', '4', '3', '3', '2', '2', '1', '1'], 'Content is valid (sorting 2nd reverted)');

});

test('column is sorted with `sortedBy` when `propertyName` is not provided', async function (assert) {

const columns = generateColumns(['index', 'index2']);
Expand Down

0 comments on commit e37ccd1

Please sign in to comment.