@@ -35,28 +35,88 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
35
35
}
36
36
}
37
37
38
- function makeTableSortable ( sortableTable ) {
38
+ function createMissingTableHead ( sortableTable ) {
39
39
let createTableHead ;
40
- let tableBody ;
40
+ if ( testingTableSortJS === true ) {
41
+ createTableHead = domDocumentWindow . createElement ( "thead" ) ;
42
+ } else {
43
+ createTableHead = document . createElement ( "thead" ) ;
44
+ }
45
+ createTableHead . appendChild ( sortableTable . rows [ 0 ] ) ;
46
+ sortableTable . insertBefore ( createTableHead , sortableTable . firstChild ) ;
47
+ }
48
+
49
+ function getTableBody ( sortableTable ) {
41
50
if ( sortableTable . getElementsByTagName ( "thead" ) . length === 0 ) {
42
- if ( testingTableSortJS === true ) {
43
- createTableHead = domDocumentWindow . createElement ( "thead" ) ;
44
- } else {
45
- createTableHead = document . createElement ( "thead" ) ;
46
- }
47
- createTableHead . appendChild ( sortableTable . rows [ 0 ] ) ;
48
- sortableTable . insertBefore ( createTableHead , sortableTable . firstChild ) ;
51
+ createMissingTableHead ( sortableTable ) ;
49
52
if ( sortableTable . querySelectorAll ( "tbody" ) . length > 1 ) {
50
- tableBody = sortableTable . querySelectorAll ( "tbody" ) [ 1 ] ;
53
+ return sortableTable . querySelectorAll ( "tbody" ) [ 1 ] ;
51
54
} else {
52
- tableBody = sortableTable . querySelector ( "tbody" ) ;
55
+ return sortableTable . querySelector ( "tbody" ) ;
53
56
}
54
57
} else {
55
- tableBody = sortableTable . querySelector ( "tbody" ) ;
58
+ return sortableTable . querySelector ( "tbody" ) ;
59
+ }
60
+ }
61
+
62
+ function addInferredClass ( th , columnLength , currentCount , classToAdd ) {
63
+ const threshold = columnLength / 2 ;
64
+ if ( currentCount >= threshold ) {
65
+ th . classList . add ( classToAdd ) ;
66
+ }
67
+ }
68
+
69
+ function inferSortClasses ( tableRows , tableHeadHeaders ) {
70
+ for ( let [ columnIndex , th ] of tableHeadHeaders . entries ( ) ) {
71
+ const regexMinutesAndSeconds = / ^ ( \d + h ) ? \s ? ( \d + m ) ? \s ? ( \d + s ) ? $ / i;
72
+ const regexFileSizeSort = / ^ ( [ . 0 - 9 ] + ) \s ? ( B | K B | K i B | M B | M i B | G B | G i B | T B | T i B ) / i;
73
+ let runtimeSortCounter = 0 ,
74
+ fileSizeSortCounter = 0 ;
75
+
76
+ let tableColumnLength = th . parentElement . childElementCount ;
77
+ for ( let tr of tableRows ) {
78
+ let runtimeSortMatch , fileSizeSortMatch ;
79
+ const tableColumn = tr . querySelectorAll ( "td" ) . item ( columnIndex ) ;
80
+ if ( tableColumn . innerText ) {
81
+ runtimeSortMatch = tableColumn . innerText . match (
82
+ regexMinutesAndSeconds
83
+ ) ;
84
+ fileSizeSortMatch = tableColumn . innerText . match ( regexFileSizeSort ) ;
85
+ }
86
+ if ( runtimeSortMatch ) {
87
+ runtimeSortCounter ++ ;
88
+ }
89
+ if ( fileSizeSortMatch ) {
90
+ fileSizeSortCounter ++ ;
91
+ }
92
+ }
93
+ // TODO: refactor this into one function called addInferredClasses that loops over sort classes and counters
94
+ addInferredClass (
95
+ th ,
96
+ tableColumnLength ,
97
+ runtimeSortCounter ,
98
+ "runtime-sort"
99
+ ) ;
100
+ addInferredClass (
101
+ th ,
102
+ tableColumnLength ,
103
+ fileSizeSortCounter ,
104
+ "file-size-sort"
105
+ ) ;
56
106
}
107
+ }
57
108
109
+ function makeTableSortable ( sortableTable ) {
110
+ const tableBody = getTableBody ( sortableTable ) ;
58
111
const tableHead = sortableTable . querySelector ( "thead" ) ;
59
112
const tableHeadHeaders = tableHead . querySelectorAll ( "th" ) ;
113
+ const tableRows = tableBody . querySelectorAll ( "tr" ) ;
114
+
115
+ const isNoSortClassInference =
116
+ sortableTable . classList . contains ( "no-class-infer" ) ;
117
+ if ( ! isNoSortClassInference ) {
118
+ inferSortClasses ( tableRows , tableHeadHeaders ) ;
119
+ }
60
120
61
121
for ( let [ columnIndex , th ] of tableHeadHeaders . entries ( ) ) {
62
122
if ( ! th . classList . contains ( "disable-sort" ) ) {
@@ -116,6 +176,34 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
116
176
}
117
177
}
118
178
179
+ function sortByRuntime ( tableRows , columnData ) {
180
+ for ( let [ i , tr ] of tableRows . entries ( ) ) {
181
+ const regexMinutesAndSeconds = / ^ ( \d + h ) ? \s ? ( \d + m ) ? \s ? ( \d + s ) ? $ / i;
182
+ let columnOfTd = tr
183
+ . querySelectorAll ( "td" )
184
+ . item ( columnIndex ) . textContent ;
185
+ let match = columnOfTd . match ( regexMinutesAndSeconds ) ;
186
+ let [ minutesInSeconds , hours , seconds , timeinSeconds ] = [ 0 , 0 , 0 , 0 ] ;
187
+ if ( match ) {
188
+ const regexHours = match [ 1 ] ;
189
+ if ( regexHours ) {
190
+ hours = Number ( regexHours . replace ( "h" , "" ) ) * 60 * 60 ;
191
+ }
192
+ const regexMinutes = match [ 2 ] ;
193
+ if ( regexMinutes ) {
194
+ minutesInSeconds = Number ( regexMinutes . replace ( "m" , "" ) ) * 60 ;
195
+ }
196
+ const regexSeconds = match [ 3 ] ;
197
+ if ( regexSeconds ) {
198
+ seconds = Number ( regexSeconds . replace ( "s" , "" ) ) ;
199
+ }
200
+ timeinSeconds = hours + minutesInSeconds + seconds ;
201
+ }
202
+ columnData . push ( `${ timeinSeconds } #${ i } ` ) ;
203
+ columnIndexAndTableRow [ columnData [ i ] ] = tr . innerHTML ;
204
+ }
205
+ }
206
+
119
207
let [ timesClickedColumn , columnIndexesClicked ] = [ 0 , [ ] ] ;
120
208
121
209
function rememberSort ( timesClickedColumn , columnIndexesClicked ) {
@@ -146,6 +234,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
146
234
tableRows,
147
235
columnData,
148
236
isFileSize,
237
+ isTimeSort,
149
238
isDataAttribute,
150
239
colSpanData,
151
240
colSpanSum,
@@ -165,7 +254,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
165
254
if ( isFileSize ) {
166
255
fileSizeColumnTextAndRow [ columnData [ i ] ] = tr . innerHTML ;
167
256
}
168
- if ( ! isFileSize && ! isDataAttribute ) {
257
+ if ( ! isFileSize && ! isDataAttribute && ! isTimeSort ) {
169
258
columnData . push ( `${ tdTextContent } #${ i } ` ) ;
170
259
columnIndexAndTableRow [ `${ tdTextContent } #${ i } ` ] = tr . innerHTML ;
171
260
}
@@ -301,6 +390,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
301
390
sortFileSize ( visibleTableRows , columnData ) ;
302
391
}
303
392
393
+ const isTimeSort = th . classList . contains ( "runtime-sort" ) ;
394
+ if ( isTimeSort ) {
395
+ sortByRuntime ( visibleTableRows , columnData ) ;
396
+ }
397
+
304
398
const isRememberSort = sortableTable . classList . contains ( "remember-sort" ) ;
305
399
if ( ! isRememberSort ) {
306
400
rememberSort ( timesClickedColumn , columnIndexesClicked ) ;
@@ -314,12 +408,17 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
314
408
columnData,
315
409
isFileSize,
316
410
isDataAttribute,
411
+ isTimeSort,
317
412
colSpanData,
318
413
colSpanSum,
319
414
} ;
320
415
getTableData ( tableProperties ) ;
321
416
updateTable ( tableProperties ) ;
322
417
} ) ;
418
+ let isOnloadSort = th . classList . contains ( "onload-sort" ) ;
419
+ if ( isOnloadSort ) {
420
+ th . click ( ) ;
421
+ }
323
422
}
324
423
}
325
424
0 commit comments