7
7
* Desc: Adds sorting to a HTML data table that implements ARIA Authoring Practices
8
8
*/
9
9
10
- 'use strict' ;
10
+ // 'use strict';
11
+
12
+ var columnIndex = 2 ;
13
+ const sortEvent = new Event ( 'sort' ) ;
11
14
12
15
class SortableTable {
13
16
constructor ( tableNode ) {
14
17
this . tableNode = tableNode ;
15
18
16
19
this . columnHeaders = tableNode . querySelectorAll ( 'thead th' ) ;
17
-
18
- this . sortColumns = [ ] ;
19
-
20
- for ( var i = 0 ; i < this . columnHeaders . length ; i ++ ) {
21
- var ch = this . columnHeaders [ i ] ;
22
- var buttonNode = ch . querySelector ( 'button' ) ;
23
- if ( buttonNode ) {
24
- this . sortColumns . push ( i ) ;
25
- buttonNode . setAttribute ( 'data-column-index' , i ) ;
26
- buttonNode . addEventListener ( 'click' , this . handleClick . bind ( this ) ) ;
27
- }
28
- }
29
-
30
- this . optionCheckbox = document . querySelector (
31
- 'input[type="checkbox"][value="show-unsorted-icon"]'
32
- ) ;
33
-
34
- if ( this . optionCheckbox ) {
35
- this . optionCheckbox . addEventListener (
36
- 'change' ,
37
- this . handleOptionChange . bind ( this )
38
- ) ;
39
- if ( this . optionCheckbox . checked ) {
40
- this . tableNode . classList . add ( 'show-unsorted-icon' ) ;
41
- }
42
- }
43
20
}
44
21
45
22
setColumnHeaderSort ( columnIndex ) {
46
- if ( typeof columnIndex === 'string' ) {
47
- columnIndex = parseInt ( columnIndex ) ;
48
- }
49
-
50
- for ( var i = 0 ; i < this . columnHeaders . length ; i ++ ) {
51
- var ch = this . columnHeaders [ i ] ;
52
- var buttonNode = ch . querySelector ( 'button' ) ;
53
- if ( i === columnIndex ) {
54
- var value = ch . getAttribute ( 'aria-sort' ) ;
55
- if ( value === 'descending' ) {
56
- ch . setAttribute ( 'aria-sort' , 'ascending' ) ;
57
- this . sortColumn (
58
- columnIndex ,
59
- 'ascending' ,
60
- ch . classList . contains ( 'num' )
61
- ) ;
62
- } else {
63
- ch . setAttribute ( 'aria-sort' , 'descending' ) ;
64
- this . sortColumn (
65
- columnIndex ,
66
- 'descending' ,
67
- ch . classList . contains ( 'num' )
68
- ) ;
69
- }
70
- } else {
71
- if ( ch . hasAttribute ( 'aria-sort' ) && buttonNode ) {
72
- ch . removeAttribute ( 'aria-sort' ) ;
73
- }
74
- }
75
- }
23
+ var ch = this . columnHeaders [ columnIndex ] ;
24
+ this . sortColumn ( columnIndex ) ;
76
25
}
77
26
78
- sortColumn ( columnIndex , sortValue , isNumber ) {
27
+ sortColumn ( columnIndex ) {
79
28
function compareValues ( a , b ) {
80
- if ( sortValue === 'ascending' ) {
81
29
if ( a . value === b . value ) {
82
30
return 0 ;
83
31
} else {
84
- if ( isNumber ) {
85
- return a . value - b . value ;
86
- } else {
87
- return a . value < b . value ? - 1 : 1 ;
88
- }
32
+ return a . value < b . value ? - 1 : 1 ;
89
33
}
90
- } else {
91
- if ( a . value === b . value ) {
92
- return 0 ;
93
- } else {
94
- if ( isNumber ) {
95
- return b . value - a . value ;
96
- } else {
97
- return a . value > b . value ? - 1 : 1 ;
98
- }
99
- }
100
- }
101
- }
102
-
103
- if ( typeof isNumber !== 'boolean' ) {
104
- isNumber = false ;
105
34
}
106
35
107
36
var tbodyNode = this . tableNode . querySelector ( 'tbody' ) ;
@@ -119,9 +48,6 @@ class SortableTable {
119
48
var data = { } ;
120
49
data . index = index ;
121
50
data . value = dataCell . textContent . toLowerCase ( ) . trim ( ) ;
122
- if ( isNumber ) {
123
- data . value = parseFloat ( data . value ) ;
124
- }
125
51
dataCells . push ( data ) ;
126
52
rowNode = rowNode . nextElementSibling ;
127
53
index += 1 ;
@@ -142,30 +68,14 @@ class SortableTable {
142
68
143
69
/* EVENT HANDLERS */
144
70
145
- handleClick ( event ) {
146
- var tgt = event . currentTarget ;
147
- this . setColumnHeaderSort ( tgt . getAttribute ( 'data-column-index' ) ) ;
148
- }
149
-
150
- handleOptionChange ( event ) {
151
- var tgt = event . currentTarget ;
152
-
153
- if ( tgt . checked ) {
154
- this . tableNode . classList . add ( 'show-unsorted-icon' ) ;
155
- } else {
156
- this . tableNode . classList . remove ( 'show-unsorted-icon' ) ;
157
- }
71
+ handleSort ( event ) {
72
+ this . setColumnHeaderSort ( tgt . getAttribute ( columnIndex ) ) ;
158
73
}
159
74
}
160
75
161
- // Initialize sortable table buttons
162
- window . addEventListener ( 'load' , function ( ) {
163
- var sortableTables = document . querySelectorAll ( 'table.sortable' ) ;
164
- for ( var i = 0 ; i < sortableTables . length ; i ++ ) {
165
- new SortableTable ( sortableTables [ i ] ) ;
166
- }
167
- } ) ;
168
-
76
+ var sortable_directory = document . querySelector ( 'table.sortable' ) ;
77
+ const sd_class = { sortable_dir : new SortableTable ( sortable_directory ) } ;
78
+ sortable_directory . addEventListener ( 'sort' , function ( ) { sd_class [ "sortable_dir" ] . setColumnHeaderSort ( columnIndex ) ; } ) ;
169
79
170
80
let new_directory_name = document . getElementById ( "name" ) ;
171
81
let files = document . getElementById ( "files" ) ;
@@ -271,6 +181,8 @@ async function refresh_list() {
271
181
}
272
182
var tbody = document . querySelector ( "tbody" ) ;
273
183
tbody . replaceChildren ( ...new_children ) ;
184
+
185
+ sortable_directory . dispatchEvent ( sortEvent ) ;
274
186
}
275
187
276
188
async function find_devices ( ) {
0 commit comments