@@ -2,39 +2,28 @@ import { action } from '@ember/object';
22import Component from '@glimmer/component' ;
33
44export default class ThSortableComponent extends Component {
5- // TODO: this is a json-api translation and it should be configurable on the data-table
6- get dasherizedField ( ) {
7- return this . args . field . attribute . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, '$1-$2' ) . toLowerCase ( ) ;
5+ get sortParameters ( ) {
6+ return this . args . field . sortParameters ;
87 }
98
10- /**
11- Inverses the sorting parameter
12- E.g. inverseSorting('title') returns '-title'
13- inverseSorting('-title') returns 'title'
14- */
15- _inverseSorting ( sorting ) {
16- if ( sorting . substring ( 0 , 1 ) === '-' ) {
17- return sorting . substring ( 1 ) ;
18- } else {
19- return '-' + sorting ;
20- }
9+ get sortDirection ( ) {
10+ for ( const key in this . sortParameters )
11+ if ( this . args . sort == this . sortParameters [ key ] )
12+ return key ;
13+
14+ return '' ;
2115 }
2216
23- get isSorted ( ) {
24- return (
25- this . args . currentSorting === this . dasherizedField ||
26- this . args . currentSorting === this . _inverseSorting ( this . dasherizedField )
27- ) ;
17+ get isAscending ( ) {
18+ return this . sortDirection === "asc" ;
19+ }
20+
21+ get isDescending ( ) {
22+ return this . sortDirection === "desc" ;
2823 }
2924
30- get order ( ) {
31- if ( this . args . currentSorting === this . dasherizedField ) {
32- return 'asc' ;
33- } else if ( this . args . currentSorting === `-${ this . dasherizedField } ` ) {
34- return 'desc' ;
35- } else {
36- return '' ;
37- }
25+ get isSorted ( ) {
26+ return this . sortDirection !== '' ;
3827 }
3928
4029 get renderCustomBlock ( ) {
@@ -55,22 +44,27 @@ export default class ThSortableComponent extends Component {
5544 return this . args . fields . find ( ( { hasCustomHeader} ) => hasCustomHeader ) || false ;
5645 }
5746
47+ get availableSortOptions ( ) {
48+ const options = [ ] ;
49+ Object
50+ . keys ( this . sortParameters )
51+ . sort ( ) // for asc and desc, asc first then desc, the rest also sorted for now
52+ . map ( ( key ) => options . push ( key ) ) ;
53+ options . push ( '' ) ; // no sorting
54+ return options ;
55+ }
56+
57+ get nextSort ( ) {
58+ // wrapping loop over availableSortOptions
59+ const opts = this . availableSortOptions ;
60+ return opts [ ( opts . indexOf ( this . sortDirection ) + 1 ) % opts . length ] ;
61+ }
62+
5863 /**
59- Sets the current sorting parameter.
60- Note: the current sorting parameter may contain another field than the given field.
61- In case the given field is currently sorted ascending, change to descending.
62- In case the given field is currently sorted descending, clean the sorting.
63- Else, set the sorting to ascending on the given field.
64- */
64+ * Wraps around possible sorting directions.
65+ */
6566 @action
66- inverseSorting ( ) {
67- if ( this . order === 'asc' ) {
68- this . args . updateSort ( this . _inverseSorting ( this . args . currentSorting ) ) ;
69- } else if ( this . order === 'desc' ) {
70- this . args . updateSort ( '' ) ;
71- } else {
72- // if currentSorting is not set to this field
73- this . args . updateSort ( this . dasherizedField ) ;
74- }
67+ toggleSort ( ) {
68+ this . args . updateSort ( this . sortParameters [ this . nextSort ] ) ;
7569 }
7670}
0 commit comments