Skip to content

Commit 6bfac8f

Browse files
Refactoring and ran prettier.
1 parent ce690aa commit 6bfac8f

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

public/table-sort.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
8888
}
8989

9090
function sortFileSize(tableRows, columnData) {
91+
// Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB.
9192
const numberWithUnitType =
9293
/[.0-9]+(\s?B|\s?KB|\s?KiB|\s?MB|\s?MiB|\s?GB|\s?GiB|T\s?B|\s?TiB)/i;
9394
const unitType =
@@ -154,6 +155,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
154155
let columnIndexesClicked = [];
155156

156157
function rememberSort(timesClickedColumn, columnIndexesClicked) {
158+
// Check if user has clicked different column from the first column if
159+
// yes reset times clicked.
157160
columnIndexesClicked.push(columnIndex);
158161
if (timesClickedColumn === 1 && columnIndexesClicked.length > 1) {
159162
const lastColumnClicked =
@@ -167,28 +170,31 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
167170
}
168171
}
169172

170-
function getColSpanData(sortableTable, colSpanData, colSpanSum){
171-
sortableTable.querySelectorAll("th").forEach(
172-
(th,index) => {
173-
colSpanData[index] = th.colSpan
174-
if(index===0)
175-
colSpanSum[index] = th.colSpan
176-
else
177-
colSpanSum[index] = colSpanSum[index-1] + th.colSpan
178-
})
173+
function getColSpanData(sortableTable, colSpanData, colSpanSum) {
174+
sortableTable.querySelectorAll("th").forEach((th, index) => {
175+
colSpanData[index] = th.colSpan;
176+
if (index === 0) colSpanSum[index] = th.colSpan;
177+
else colSpanSum[index] = colSpanSum[index - 1] + th.colSpan;
178+
});
179179
}
180180

181-
182-
function getTableData(tableRows, columnData, isFileSize, isDataAttribute, colSpanData, colSpanSum) {
181+
function getTableData(
182+
tableRows,
183+
columnData,
184+
isFileSize,
185+
isDataAttribute,
186+
colSpanData,
187+
colSpanSum
188+
) {
183189
for (let [i, tr] of tableRows.entries()) {
184190
// inner text for column we click on
185191
let tdTextContent = tr
186192
.querySelectorAll("td")
187193
.item(
188-
colSpanData[columnIndex] === 1 ?
189-
colSpanSum[columnIndex]-1 :
190-
colSpanSum[columnIndex]-colSpanData[columnIndex])
191-
.textContent;
194+
colSpanData[columnIndex] === 1
195+
? colSpanSum[columnIndex] - 1
196+
: colSpanSum[columnIndex] - colSpanData[columnIndex]
197+
).textContent;
192198

193199
if (tdTextContent.length === 0) {
194200
tdTextContent = "";
@@ -350,7 +356,6 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
350356
const colSpanData = {};
351357
const colSpanSum = {};
352358

353-
// To make it work even if there is a tr with display: none; in the table, only the tr that is currently displayed is subject to sorting.
354359
const visibleTableRows = Array.prototype.filter.call(
355360
tableBody.querySelectorAll("tr"),
356361
(tr) => {
@@ -359,29 +364,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
359364
);
360365

361366
let isDataAttribute = th.classList.contains("data-sort");
362-
// Check if using data-sort attribute; if so sort by value of data-sort
363-
// attribute.
364367
if (isDataAttribute) {
365368
sortDataAttributes(visibleTableRows, columnData);
366369
}
367370

368371
let isFileSize = th.classList.contains("file-size");
369-
// Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB.
370372
if (isFileSize) {
371373
sortFileSize(visibleTableRows, columnData);
372374
}
373375

374-
// Checking if user has clicked different column from the first column if
375-
// yes reset times clicked.
376376
let isRememberSort = sortableTable.classList.contains("remember-sort");
377377
if (!isRememberSort) {
378378
rememberSort(timesClickedColumn, columnIndexesClicked);
379379
}
380380

381381
timesClickedColumn += 1;
382382

383-
getColSpanData(sortableTable, colSpanData, colSpanSum)
384-
getTableData(visibleTableRows, columnData, isFileSize, isDataAttribute, colSpanData, colSpanSum);
383+
getColSpanData(sortableTable, colSpanData, colSpanSum);
384+
// TODO: refactor function to take object.
385+
getTableData(
386+
visibleTableRows,
387+
columnData,
388+
isFileSize,
389+
isDataAttribute,
390+
colSpanData,
391+
colSpanSum
392+
);
385393
updateTable(visibleTableRows, columnData, isFileSize);
386394
});
387395
}

0 commit comments

Comments
 (0)