2018-01-20 00:46:47 -08:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { computed } from '@ember/object';
|
2017-03-24 21:21:00 -07:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: 'th',
|
|
|
|
classNameBindings: ['isColumnActive'],
|
|
|
|
columnName: '',
|
|
|
|
sortColumnWithName: '',
|
|
|
|
sortDirection: null,
|
|
|
|
|
2018-01-20 00:46:47 -08:00
|
|
|
isColumnActive: computed('sortColumnWithName', 'columnName', function() {
|
|
|
|
const { sortColumnWithName, columnName } = this.getProperties('sortColumnWithName', 'columnName');
|
2017-03-24 21:21:00 -07:00
|
|
|
return sortColumnWithName && sortColumnWithName === columnName;
|
|
|
|
}),
|
|
|
|
|
|
|
|
click() {
|
2018-01-20 00:46:47 -08:00
|
|
|
const { columnName, sortDirection, isColumnActive } = this.getProperties(
|
|
|
|
'columnName',
|
|
|
|
'sortDirection',
|
|
|
|
'isColumnActive'
|
|
|
|
);
|
2017-03-24 21:21:00 -07:00
|
|
|
|
|
|
|
if (columnName) {
|
|
|
|
let updatedSortDirection;
|
|
|
|
// if this column is not active, default sort direction to ascending, else toggle sort direction
|
|
|
|
if (!isColumnActive) {
|
|
|
|
updatedSortDirection = 'asc';
|
|
|
|
} else {
|
|
|
|
updatedSortDirection = sortDirection === 'asc' ? 'desc' : 'asc';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('sortDidChange')(columnName, updatedSortDirection);
|
|
|
|
this.set('sortDirection', updatedSortDirection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|