Skip to content

Commit

Permalink
Use select inner or outer html on tr depending on if cells-sort class…
Browse files Browse the repository at this point in the history
… is present.
  • Loading branch information
LeeWannacott committed May 21, 2023
1 parent 9a7be82 commit dc6167e
Showing 1 changed file with 59 additions and 38 deletions.
97 changes: 59 additions & 38 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,34 +131,29 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
if (!table.hasClass.noClassInfer) {
inferSortClasses(table.rows, columnIndex, th);
}
makeEachColumnSortable(
th,
columnIndex,
table,
columnIndexesClicked
);
makeEachColumnSortable(th, columnIndex, table, columnIndexesClicked);
}
}
}

function sortByCellsOrRows(table, tr) {
function innerOrOuterHTML(table, tr) {
if (table.hasClass.cellsSort) {
return tr.innerHTML;
} else {
return tr.outerHTML;
}
}

function sortDataAttributes(tableRows, column) {
for (let [i, tr] of tableRows.entries()) {
function sortDataAttributes(table, column) {
for (let [i, tr] of table.visibleRows.entries()) {
let dataAttributeTd = column.getColumn(tr, column.spanSum, column.span)
.dataset.sort;
column.toBeSorted.push(`${dataAttributeTd}#${i}`);
columnIndexAndTableRow[column.toBeSorted[i]] = tr.outerHTML //
columnIndexAndTableRow[column.toBeSorted[i]] = innerOrOuterHTML(table, tr);
}
}

function sortFileSize(tableRows, column, columnIndex) {
function sortFileSize(table, column, columnIndex) {
let unitToMultiplier = {
b: 1,
kb: 1000,
Expand All @@ -171,22 +166,25 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
tib: 2 ** 40,
};
const numberWithUnitType = /([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
for (let [i, tr] of tableRows.entries()) {
for (let [i, tr] of table.visibleRows.entries()) {
let fileSizeTd = tr.querySelectorAll("td").item(columnIndex).textContent;
let match = fileSizeTd.match(numberWithUnitType);
if (match) {
let number = parseFloat(match[1]);
let unit = match[2].toLowerCase();
let multiplier = unitToMultiplier[unit];
column.toBeSorted.push(`${number * multiplier}#${i}`);
columnIndexAndTableRow[column.toBeSorted[i]] = tr.outerHTML;
columnIndexAndTableRow[column.toBeSorted[i]] = innerOrOuterHTML(
table,
tr
);
}
}
}

function sortDates(datesFormat, tableRows, column) {
function sortDates(datesFormat, table, column) {
try {
for (let [i, tr] of tableRows.entries()) {
for (let [i, tr] of table.visibleRows.entries()) {
let columnOfTd, datesRegex;
if (datesFormat === "mdy" || datesFormat === "dmy") {
datesRegex = /^(\d\d?)[./-](\d\d?)[./-]((\d\d)?\d\d)/;
Expand Down Expand Up @@ -219,16 +217,19 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
);
}
column.toBeSorted.push(`${numberToSort}#${i}`);
columnIndexAndTableRow[column.toBeSorted[i]] = tr.outerHTML;
columnIndexAndTableRow[column.toBeSorted[i]] = innerOrOuterHTML(
table,
tr
);
}
} catch (e) {
console.log(e);
}
}

function sortByRuntime(tableRows, column) {
function sortByRuntime(table, column) {
try {
for (let [i, tr] of tableRows.entries()) {
for (let [i, tr] of table.visibleRows.entries()) {
const regexMinutesAndSeconds = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
let columnOfTd = "";
// TODO: github actions runtime didn't like textContent, tests didn't like innerText?
Expand Down Expand Up @@ -264,7 +265,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
timeinSeconds = hours + minutesInSeconds + seconds;
}
column.toBeSorted.push(`${timeinSeconds}#${i}`);
columnIndexAndTableRow[column.toBeSorted[i]] = tr.outerHTML;
columnIndexAndTableRow[column.toBeSorted[i]] = innerOrOuterHTML(
table,
tr
);
}
} catch (e) {
console.log(e);
Expand Down Expand Up @@ -303,12 +307,18 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
!isSortDates.monthDayYear
) {
column.toBeSorted.push(`${tdTextContent}#${i}`);
columnIndexAndTableRow[`${tdTextContent}#${i}`] = tr.outerHTML;
columnIndexAndTableRow[`${tdTextContent}#${i}`] = innerOrOuterHTML(
table,
tr
);
}
} else {
// Fill in blank table cells dict key with filler value.
column.toBeSorted.push(`${fillValue}#${i}`);
columnIndexAndTableRow[`${fillValue}#${i}`] = tr.outerHTML;
columnIndexAndTableRow[`${fillValue}#${i}`] = innerOrOuterHTML(
table,
tr
);
}
}

Expand Down Expand Up @@ -407,11 +417,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
return timesClickedColumn;
}

function updateFilesize(i, tr, column, columnIndex) {
// We do this to sort rows rather than cells:
const template = document.createElement("template");
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
tr = template.content.firstChild;
function updateFilesize(i, table, tr, column, columnIndex) {
if (table.hasClass.cellsSort) {
tr.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
} else {
// We do this to sort rows rather than cells:
const template = document.createElement("template");
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
tr = template.content.firstChild;
}
let fileSizeInBytesHTML = column.getColumn(
tr,
column.spanSum,
Expand Down Expand Up @@ -445,17 +459,24 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
);
}
tr.querySelectorAll("td").item(columnIndex).innerHTML = fileSizeInBytesHTML;
return tr.outerHTML;
return table.hasClass.cellsSort ? tr.innerHTML : tr.outerHTML;
}

function updateTable(tableProperties) {
const { tableRows, column, columnIndex, hasThClass } = tableProperties;
for (let [i, tr] of tableRows.entries()) {
const { column, table, columnIndex, hasThClass } = tableProperties;
for (let [i, tr] of table.visibleRows.entries()) {
if (hasThClass.fileSize) {
tr.outerHTML = updateFilesize(i, tr, column, columnIndex);
// console.log(9, tr.outerHTML);
if (table.hasClass.cellsSort) {
tr.innerHTML = updateFilesize(i, table, tr, column, columnIndex);
} else {
tr.outerHTML = updateFilesize(i, table, tr, column, columnIndex);
}
} else if (!hasThClass.fileSize) {
tr.outerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
if (table.hasClass.cellsSort) {
tr.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
} else {
tr.outerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
}
}
}
}
Expand Down Expand Up @@ -541,13 +562,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
};

if (hasThClass.dataSort) {
sortDataAttributes(table.visibleRows, column);
sortDataAttributes(table, column);
}
if (hasThClass.fileSize) {
sortFileSize(table.visibleRows, column, columnIndex, fillValue);
sortFileSize(table, column, columnIndex, fillValue);
}
if (hasThClass.runtime) {
sortByRuntime(table.visibleRows, column);
sortByRuntime(table, column);
}

const isSortDates = {
Expand All @@ -557,11 +578,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
};
// pick mdy first to override the inferred default class which is dmy.
if (isSortDates.monthDayYear) {
sortDates("mdy", table.visibleRows, column);
sortDates("mdy", table, column);
} else if (isSortDates.yearMonthDay) {
sortDates("ymd", table.visibleRows, column);
sortDates("ymd", table, column);
} else if (isSortDates.dayMonthYear) {
sortDates("dmy", table.visibleRows, column);
sortDates("dmy", table, column);
}

const tableProperties = {
Expand Down

0 comments on commit dc6167e

Please sign in to comment.