2017-09-19 14:47:10 +00:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
|
|
|
const { Mixin, computed } = Ember;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Sortable mixin
|
|
|
|
|
|
|
|
Simple sorting behavior for a list of objects.
|
|
|
|
|
|
|
|
Properties to override:
|
|
|
|
- sortProperty: the property to sort by
|
|
|
|
- sortDescending: when true, the list is reversed
|
|
|
|
- listToSort: the list of objects to sort
|
|
|
|
|
|
|
|
Properties provided:
|
|
|
|
- listSorted: a copy of listToSort that has been sorted
|
|
|
|
*/
|
|
|
|
export default Mixin.create({
|
|
|
|
// Override in mixin consumer
|
|
|
|
sortProperty: null,
|
|
|
|
sortDescending: true,
|
|
|
|
listToSort: computed(() => []),
|
|
|
|
|
|
|
|
listSorted: computed('listToSort.[]', 'sortProperty', 'sortDescending', function() {
|
2017-10-18 23:42:08 +00:00
|
|
|
const sorted = this.get('listToSort')
|
|
|
|
.compact()
|
|
|
|
.sortBy(this.get('sortProperty'));
|
2017-09-19 14:47:10 +00:00
|
|
|
if (this.get('sortDescending')) {
|
|
|
|
return sorted.reverse();
|
|
|
|
}
|
|
|
|
return sorted;
|
|
|
|
}),
|
|
|
|
});
|