@@ -136,16 +136,16 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
136
136
th . insertAdjacentText ( "beforeend" , arrowUp ) ;
137
137
}
138
138
139
- function sortDataAttributes ( tableRows , columnData ) {
139
+ function sortDataAttributes ( tableRows , column ) {
140
140
for ( let [ i , tr ] of tableRows . entries ( ) ) {
141
- const dataAttributeTd = tr . querySelectorAll ( "td" ) . item ( columnIndex )
142
- . dataset . sort ;
143
- columnData . push ( `${ dataAttributeTd } #${ i } ` ) ;
144
- columnIndexAndTableRow [ columnData [ i ] ] = tr . outerHTML ;
141
+ let dataAttributeTd = getColumn ( tr , column . spanSum , column . span ) . dataset
142
+ . sort ;
143
+ column . toBeSorted . push ( `${ dataAttributeTd } #${ i } ` ) ;
144
+ columnIndexAndTableRow [ column . toBeSorted [ i ] ] = tr . outerHTML ;
145
145
}
146
146
}
147
147
148
- function sortFileSize ( tableRows , columnData ) {
148
+ function sortFileSize ( tableRows , column ) {
149
149
let unitToMultiplier = {
150
150
b : 1 ,
151
151
kb : 1000 ,
@@ -167,25 +167,23 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
167
167
let number = parseFloat ( match [ 1 ] ) ;
168
168
let unit = match [ 2 ] . toLowerCase ( ) ;
169
169
let multiplier = unitToMultiplier [ unit ] ;
170
- columnData . push ( `${ number * multiplier } #${ i } ` ) ;
170
+ column . toBeSorted . push ( `${ number * multiplier } #${ i } ` ) ;
171
171
} else {
172
- columnData . push ( `${ fillValue } #${ i } ` ) ;
172
+ column . toBeSorted . push ( `${ fillValue } #${ i } ` ) ;
173
173
}
174
174
}
175
175
}
176
176
177
- function sortByRuntime ( tableRows , columnData ) {
177
+ function sortByRuntime ( tableRows , column ) {
178
178
try {
179
179
for ( let [ i , tr ] of tableRows . entries ( ) ) {
180
180
const regexMinutesAndSeconds = / ^ ( \d + h ) ? \s ? ( \d + m ) ? \s ? ( \d + s ) ? $ / i;
181
181
let columnOfTd = "" ;
182
182
// TODO: github actions runtime didn't like textContent, tests didn't like innerText?
183
183
if ( testingTableSortJS ) {
184
- columnOfTd = tr
185
- . querySelectorAll ( "td" )
186
- . item ( columnIndex ) . textContent ;
184
+ columnOfTd = getColumn ( tr , column . spanSum , column . span ) . textContent ;
187
185
} else {
188
- columnOfTd = tr . querySelectorAll ( "td" ) . item ( columnIndex ) . innerText ;
186
+ columnOfTd = getColumn ( tr , column . spanSum , column . span ) . innerText ;
189
187
}
190
188
let match = columnOfTd . match ( regexMinutesAndSeconds ) ;
191
189
let [ minutesInSeconds , hours , seconds ] = [ 0 , 0 , 0 ] ;
@@ -205,15 +203,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
205
203
}
206
204
timeinSeconds = hours + minutesInSeconds + seconds ;
207
205
}
208
- columnData . push ( `${ timeinSeconds } #${ i } ` ) ;
209
- columnIndexAndTableRow [ columnData [ i ] ] = tr . outerHTML ;
206
+ column . toBeSorted . push ( `${ timeinSeconds } #${ i } ` ) ;
207
+ columnIndexAndTableRow [ column . toBeSorted [ i ] ] = tr . outerHTML ;
210
208
}
211
209
} catch ( e ) {
212
210
console . log ( e ) ;
213
211
}
214
212
}
215
213
216
- function sortDates ( datesFormat , tableRows , columnData ) {
214
+ function sortDates ( datesFormat , tableRows , column ) {
217
215
try {
218
216
for ( let [ i , tr ] of tableRows . entries ( ) ) {
219
217
let columnOfTd , datesRegex ;
@@ -222,7 +220,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
222
220
} else if ( datesFormat === "ymd" ) {
223
221
datesRegex = / ^ ( \d \d \d \d ) [ . / - ] ( \d \d ? ) [ . / - ] ( \d \d ? ) / ;
224
222
}
225
- columnOfTd = tr . querySelectorAll ( "td" ) . item ( columnIndex ) . textContent ;
223
+ columnOfTd = getColumn ( tr , column . spanSum , column . span ) . textContent ;
226
224
let match = columnOfTd . match ( datesRegex ) ;
227
225
let [ years , days , months ] = [ 0 , 0 , 0 ] ;
228
226
let numberToSort = columnOfTd ;
@@ -243,8 +241,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
243
241
String ( days ) . padStart ( 2 , "0" )
244
242
) ;
245
243
}
246
- columnData . push ( `${ numberToSort } #${ i } ` ) ;
247
- columnIndexAndTableRow [ columnData [ i ] ] = tr . outerHTML ;
244
+ column . toBeSorted . push ( `${ numberToSort } #${ i } ` ) ;
245
+ columnIndexAndTableRow [ column . toBeSorted [ i ] ] = tr . outerHTML ;
248
246
}
249
247
} catch ( e ) {
250
248
console . log ( e ) ;
@@ -268,41 +266,47 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
268
266
}
269
267
}
270
268
271
- function getColSpanData ( sortableTable , colSpanData , colSpanSum ) {
269
+ function getColSpanData ( sortableTable , column ) {
272
270
sortableTable . querySelectorAll ( "th" ) . forEach ( ( th , index ) => {
273
- colSpanData [ index ] = th . colSpan ;
274
- if ( index === 0 ) colSpanSum [ index ] = th . colSpan ;
275
- else colSpanSum [ index ] = colSpanSum [ index - 1 ] + th . colSpan ;
271
+ column . span [ index ] = th . colSpan ;
272
+ if ( index === 0 ) column . spanSum [ index ] = th . colSpan ;
273
+ else column . spanSum [ index ] = column . spanSum [ index - 1 ] + th . colSpan ;
276
274
} ) ;
277
275
}
278
276
277
+ function getColumn ( tr , colSpanSum , colSpanData ) {
278
+ return tr
279
+ . querySelectorAll ( "td" )
280
+ . item (
281
+ colSpanData [ columnIndex ] === 1
282
+ ? colSpanSum [ columnIndex ] - 1
283
+ : colSpanSum [ columnIndex ] - colSpanData [ columnIndex ]
284
+ ) ;
285
+ }
286
+
279
287
function getTableData ( tableProperties ) {
280
288
const {
281
289
tableRows,
282
- columnData ,
290
+ column ,
283
291
isFileSize,
284
292
isTimeSort,
285
293
isSortDateDayMonthYear,
286
294
isSortDateMonthDayYear,
287
295
isSortDateYearMonthDay,
288
296
isDataAttribute,
289
- colSpanData,
290
- colSpanSum,
291
297
} = tableProperties ;
292
298
for ( let [ i , tr ] of tableRows . entries ( ) ) {
293
- let tdTextContent = tr
294
- . querySelectorAll ( "td" )
295
- . item (
296
- colSpanData [ columnIndex ] === 1
297
- ? colSpanSum [ columnIndex ] - 1
298
- : colSpanSum [ columnIndex ] - colSpanData [ columnIndex ]
299
- ) . textContent ;
299
+ let tdTextContent = getColumn (
300
+ tr ,
301
+ column . spanSum ,
302
+ column . span
303
+ ) . textContent ;
300
304
if ( tdTextContent . length === 0 ) {
301
305
tdTextContent = "" ;
302
306
}
303
307
if ( tdTextContent . trim ( ) !== "" ) {
304
308
if ( isFileSize ) {
305
- fileSizeColumnTextAndRow [ columnData [ i ] ] = tr . outerHTML ;
309
+ fileSizeColumnTextAndRow [ column . toBeSorted [ i ] ] = tr . outerHTML ;
306
310
}
307
311
// These classes already handle pushing to column and setting the tr html.
308
312
if (
@@ -313,12 +317,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
313
317
! isSortDateYearMonthDay &&
314
318
! isSortDateMonthDayYear
315
319
) {
316
- columnData . push ( `${ tdTextContent } #${ i } ` ) ;
320
+ column . toBeSorted . push ( `${ tdTextContent } #${ i } ` ) ;
317
321
columnIndexAndTableRow [ `${ tdTextContent } #${ i } ` ] = tr . outerHTML ;
318
322
}
319
323
} else {
320
324
// Fill in blank table cells dict key with filler value.
321
- columnData . push ( `${ fillValue } #${ i } ` ) ;
325
+ column . toBeSorted . push ( `${ fillValue } #${ i } ` ) ;
322
326
columnIndexAndTableRow [ `${ fillValue } #${ i } ` ] = tr . outerHTML ;
323
327
}
324
328
}
@@ -348,7 +352,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
348
352
th . innerHTML = th . innerHTML . replace ( arrowDown , "" ) ;
349
353
}
350
354
351
- if ( columnData [ 0 ] === undefined ) {
355
+ if ( column . toBeSorted [ 0 ] === undefined ) {
352
356
return ;
353
357
}
354
358
@@ -360,7 +364,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
360
364
}
361
365
362
366
function sortColumn ( sortDirection ) {
363
- columnData . sort ( sortDirection , {
367
+ column . toBeSorted . sort ( sortDirection , {
364
368
numeric : ! isAlphaSort ,
365
369
ignorePunctuation : ! isPunctSort ,
366
370
} ) ;
@@ -387,19 +391,19 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
387
391
}
388
392
389
393
function updateTable ( tableProperties ) {
390
- const { tableRows, columnData , isFileSize } = tableProperties ;
394
+ const { tableRows, column , isFileSize } = tableProperties ;
391
395
for ( let [ i , tr ] of tableRows . entries ( ) ) {
392
396
if ( isFileSize ) {
393
- tr . innerHTML = fileSizeColumnTextAndRow [ columnData [ i ] ] ;
397
+ tr . innerHTML = fileSizeColumnTextAndRow [ column . toBeSorted [ i ] ] ;
394
398
let fileSizeInBytesHTML = tr
395
399
. querySelectorAll ( "td" )
396
400
. item ( columnIndex ) . innerHTML ;
397
401
const fileSizeInBytesText = tr
398
402
. querySelectorAll ( "td" )
399
403
. item ( columnIndex ) . textContent ;
400
404
// Remove the unique identifyer for duplicate values(#number).
401
- columnData [ i ] = columnData [ i ] . replace ( / # [ 0 - 9 ] * / , "" ) ;
402
- const fileSize = parseFloat ( columnData [ i ] ) ;
405
+ column . toBeSorted [ i ] = column . toBeSorted [ i ] . replace ( / # [ 0 - 9 ] * / , "" ) ;
406
+ const fileSize = parseFloat ( column . toBeSorted [ i ] ) ;
403
407
let prefixes = [ "" , "Ki" , "Mi" , "Gi" , "Ti" , "Pi" ] ;
404
408
let replaced = false ;
405
409
for ( let i = 0 ; i < prefixes . length ; ++ i ) {
@@ -423,13 +427,19 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
423
427
tr . querySelectorAll ( "td" ) . item ( columnIndex ) . innerHTML =
424
428
fileSizeInBytesHTML ;
425
429
} else if ( ! isFileSize ) {
426
- tr . outerHTML = columnIndexAndTableRow [ columnData [ i ] ] ;
430
+ tr . outerHTML = columnIndexAndTableRow [ column . toBeSorted [ i ] ] ;
427
431
}
428
432
}
429
433
}
430
434
431
435
th . addEventListener ( "click" , function ( ) {
432
- const [ columnData , colSpanData , colSpanSum ] = [ [ ] , { } , { } ] ;
436
+ timesClickedColumn += 1 ;
437
+ const column = {
438
+ // column used for sorting; better name?
439
+ toBeSorted : [ ] ,
440
+ span : { } ,
441
+ spanSum : { } ,
442
+ } ;
433
443
434
444
const visibleTableRows = Array . prototype . filter . call (
435
445
tableBody . querySelectorAll ( "tr" ) ,
@@ -438,52 +448,49 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
438
448
}
439
449
) ;
440
450
451
+ getColSpanData ( sortableTable , column ) ;
452
+
441
453
const isDataAttribute = th . classList . contains ( "data-sort" ) ;
442
454
if ( isDataAttribute ) {
443
- sortDataAttributes ( visibleTableRows , columnData ) ;
455
+ sortDataAttributes ( visibleTableRows , column ) ;
444
456
}
445
457
446
458
const isFileSize = th . classList . contains ( "file-size-sort" ) ;
447
459
if ( isFileSize ) {
448
- sortFileSize ( visibleTableRows , columnData ) ;
460
+ sortFileSize ( visibleTableRows , column ) ;
449
461
}
450
462
451
463
const isTimeSort = th . classList . contains ( "runtime-sort" ) ;
452
464
if ( isTimeSort ) {
453
- sortByRuntime ( visibleTableRows , columnData ) ;
465
+ sortByRuntime ( visibleTableRows , column ) ;
454
466
}
455
467
456
468
const isSortDateDayMonthYear = th . classList . contains ( "dates-dmy-sort" ) ;
457
469
const isSortDateMonthDayYear = th . classList . contains ( "dates-mdy-sort" ) ;
458
470
const isSortDateYearMonthDay = th . classList . contains ( "dates-ymd-sort" ) ;
459
471
// pick mdy first to override the inferred default class which is dmy.
460
472
if ( isSortDateMonthDayYear ) {
461
- sortDates ( "mdy" , visibleTableRows , columnData ) ;
473
+ sortDates ( "mdy" , visibleTableRows , column ) ;
462
474
} else if ( isSortDateYearMonthDay ) {
463
- sortDates ( "ymd" , visibleTableRows , columnData ) ;
475
+ sortDates ( "ymd" , visibleTableRows , column ) ;
464
476
} else if ( isSortDateDayMonthYear ) {
465
- sortDates ( "dmy" , visibleTableRows , columnData ) ;
477
+ sortDates ( "dmy" , visibleTableRows , column ) ;
466
478
}
467
479
468
480
const isRememberSort = sortableTable . classList . contains ( "remember-sort" ) ;
469
481
if ( ! isRememberSort ) {
470
482
rememberSort ( timesClickedColumn , columnIndexesClicked ) ;
471
483
}
472
484
473
- timesClickedColumn += 1 ;
474
-
475
- getColSpanData ( sortableTable , colSpanData , colSpanSum ) ;
476
485
const tableProperties = {
477
486
tableRows : visibleTableRows ,
478
- columnData ,
487
+ column ,
479
488
isFileSize,
480
489
isSortDateDayMonthYear,
481
490
isSortDateMonthDayYear,
482
491
isSortDateYearMonthDay,
483
492
isDataAttribute,
484
493
isTimeSort,
485
- colSpanData,
486
- colSpanSum,
487
494
} ;
488
495
getTableData ( tableProperties ) ;
489
496
updateTable ( tableProperties ) ;
0 commit comments