|
1 |
| -/* eslint-disable ember/no-actions-hash, ember/no-classic-classes, ember/no-classic-components, ember/require-tagless-components */ |
2 |
| -import Component from '@ember/component'; |
3 |
| -import { computed } from '@ember/object'; |
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { tracked } from '@glimmer/tracking'; |
4 | 3 | import { NavDownIcon } from '../icons/nav-down';
|
5 | 4 | import { NavUpIcon } from '../icons/nav-up';
|
6 | 5 | import { NavUpDownIcon } from '../icons/nav-up-down';
|
7 | 6 |
|
8 |
| -// Source: https://github.com/mu-semtech/ember-data-table/blob/c690a3948b2d9d5f91d69f0a935c6b5cdb4862ca/addon/components/th-sortable.js |
9 |
| -const ThSortable = Component.extend({ |
10 |
| - tagName: 'th', |
11 |
| - classNames: ['sortable'], |
12 |
| - classNameBindings: ['isSorted:sorted'], |
13 |
| - dasherizedField: computed('field', function () { |
14 |
| - return this.field.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); |
15 |
| - }), |
16 |
| - /** |
17 |
| - Inverses the sorting parameter |
18 |
| - E.g. inverseSorting('title') returns '-title' |
19 |
| - inverseSorting('-title') returns 'title' |
20 |
| - */ |
| 7 | +export default class AuDataTableThSortable extends Component { |
| 8 | + @tracked |
| 9 | + NavDownIcon = NavDownIcon; |
| 10 | + NavUpIcon = NavUpIcon; |
| 11 | + NavUpDownIcon = NavUpDownIcon; |
| 12 | + |
| 13 | + get dasherizedField() { |
| 14 | + return this.args.field.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); |
| 15 | + } |
| 16 | + |
21 | 17 | _inverseSorting(sorting) {
|
22 | 18 | if (sorting.substring(0, 1) === '-') {
|
23 | 19 | return sorting.substring(1);
|
24 | 20 | } else {
|
25 | 21 | return '-' + sorting;
|
26 | 22 | }
|
27 |
| - }, |
28 |
| - isSorted: computed('dasherizedField', 'currentSorting', function () { |
| 23 | + }; |
| 24 | + |
| 25 | + get isSorted() { |
29 | 26 | return (
|
30 | 27 | this.currentSorting === this.dasherizedField ||
|
31 | 28 | this.currentSorting === this._inverseSorting(this.dasherizedField)
|
32 | 29 | );
|
33 |
| - }), |
34 |
| - order: computed('dasherizedField', 'currentSorting', function () { |
| 30 | + } |
| 31 | + |
| 32 | + get order() { |
35 | 33 | if (this.currentSorting === this.dasherizedField) {
|
36 | 34 | return 'asc';
|
37 | 35 | } else if (this.currentSorting === `-${this.dasherizedField}`) {
|
38 | 36 | return 'desc';
|
39 | 37 | } else {
|
40 | 38 | return '';
|
41 | 39 | }
|
42 |
| - }), |
| 40 | + } |
43 | 41 |
|
44 |
| - actions: { |
45 |
| - /** |
46 |
| - Sets the current sorting parameter. |
47 |
| - Note: the current sorting parameter may contain another field than the given field. |
48 |
| - In case the given field is currently sorted ascending, change to descending. |
49 |
| - In case the given field is currently sorted descending, clean the sorting. |
50 |
| - Else, set the sorting to ascending on the given field. |
51 |
| - */ |
52 |
| - inverseSorting() { |
53 |
| - if (this.order === 'asc') { |
54 |
| - this.set('currentSorting', this._inverseSorting(this.currentSorting)); |
55 |
| - } else if (this.order === 'desc') { |
56 |
| - this.set('currentSorting', ''); |
57 |
| - } else { |
58 |
| - // if currentSorting is not set to this field |
59 |
| - this.set('currentSorting', this.dasherizedField); |
60 |
| - } |
61 |
| - }, |
62 |
| - }, |
63 |
| -}); |
| 42 | + /** |
| 43 | + Sets the current sorting parameter. |
| 44 | + Note: the current sorting parameter may contain another field than the given field. |
| 45 | + In case the given field is currently sorted ascending, change to descending. |
| 46 | + In case the given field is currently sorted descending, clean the sorting. |
| 47 | + Else, set the sorting to ascending on the given field. |
| 48 | + */ |
| 49 | + inverseSorting = () => { |
| 50 | + if (this.order === 'asc') { |
| 51 | + this.set('currentSorting', this._inverseSorting(this.currentSorting)); |
| 52 | + } else if (this.order === 'desc') { |
| 53 | + this.set('currentSorting', ''); |
| 54 | + } else { |
| 55 | + // if currentSorting is not set to this field |
| 56 | + this.set('currentSorting', this.dasherizedField); |
| 57 | + } |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +// Source: https://github.com/mu-semtech/ember-data-table/blob/c690a3948b2d9d5f91d69f0a935c6b5cdb4862ca/addon/components/th-sortable.js |
| 62 | +// const ThSortable = Component.extend({ |
| 63 | +// tagName: 'th', |
| 64 | +// classNames: ['sortable'], |
| 65 | +// classNameBindings: ['isSorted:sorted'], |
| 66 | +// dasherizedField: computed('field', function () { |
| 67 | +// return this.field.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); |
| 68 | +// }), |
| 69 | +// /** |
| 70 | +// Inverses the sorting parameter |
| 71 | +// E.g. inverseSorting('title') returns '-title' |
| 72 | +// inverseSorting('-title') returns 'title' |
| 73 | +// */ |
| 74 | +// _inverseSorting(sorting) { |
| 75 | +// if (sorting.substring(0, 1) === '-') { |
| 76 | +// return sorting.substring(1); |
| 77 | +// } else { |
| 78 | +// return '-' + sorting; |
| 79 | +// } |
| 80 | +// }, |
| 81 | +// isSorted: computed('dasherizedField', 'currentSorting', function () { |
| 82 | +// return ( |
| 83 | +// this.currentSorting === this.dasherizedField || |
| 84 | +// this.currentSorting === this._inverseSorting(this.dasherizedField) |
| 85 | +// ); |
| 86 | +// }), |
| 87 | +// order: computed('dasherizedField', 'currentSorting', function () { |
| 88 | +// if (this.currentSorting === this.dasherizedField) { |
| 89 | +// return 'asc'; |
| 90 | +// } else if (this.currentSorting === `-${this.dasherizedField}`) { |
| 91 | +// return 'desc'; |
| 92 | +// } else { |
| 93 | +// return ''; |
| 94 | +// } |
| 95 | +// }), |
64 | 96 |
|
65 |
| -export default ThSortable.extend({ |
66 |
| - NavDownIcon, |
67 |
| - NavUpIcon, |
68 |
| - NavUpDownIcon, |
69 |
| -}); |
| 97 | +// actions: { |
| 98 | +// /** |
| 99 | +// Sets the current sorting parameter. |
| 100 | +// Note: the current sorting parameter may contain another field than the given field. |
| 101 | +// In case the given field is currently sorted ascending, change to descending. |
| 102 | +// In case the given field is currently sorted descending, clean the sorting. |
| 103 | +// Else, set the sorting to ascending on the given field. |
| 104 | +// */ |
| 105 | +// inverseSorting() { |
| 106 | +// if (this.order === 'asc') { |
| 107 | +// this.set('currentSorting', this._inverseSorting(this.currentSorting)); |
| 108 | +// } else if (this.order === 'desc') { |
| 109 | +// this.set('currentSorting', ''); |
| 110 | +// } else { |
| 111 | +// // if currentSorting is not set to this field |
| 112 | +// this.set('currentSorting', this.dasherizedField); |
| 113 | +// } |
| 114 | +// }, |
| 115 | +// }, |
| 116 | +// }); |
0 commit comments