Skip to content

Commit

Permalink
put check for table classes into property on table class object.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeWannacott committed May 21, 2023
1 parent 9527bb5 commit 9a7be82
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,50 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
body: getTableBody(sortableTable),
head: sortableTable.querySelector("thead"),
};
if(table.body == null){ return }
if (table.body == null) {
return;
}
table.headers = table.head.querySelectorAll("th");
table.rows = table.body.querySelectorAll("tr");
table.hasClass = {
noClassInfer: sortableTable.classList.contains("no-class-infer"),
cellsSort: sortableTable.classList.contains("cells-sort"),
tableArrows: sortableTable.classList.contains("table-arrows"),
rememberSort: sortableTable.classList.contains("remember-sort"),
};

let columnIndexesClicked = [];

const isNoSortClassInference =
sortableTable.classList.contains("no-class-infer");

for (let [columnIndex, th] of table.headers.entries()) {
if (!th.classList.contains("disable-sort")) {
th.style.cursor = "pointer";
if (!isNoSortClassInference) {
if (!table.hasClass.noClassInfer) {
inferSortClasses(table.rows, columnIndex, th);
}
makeEachColumnSortable(
th,
columnIndex,
table,
sortableTable,
columnIndexesClicked
);
}
}
}

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

function sortDataAttributes(tableRows, column) {
for (let [i, tr] of tableRows.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]] = tr.outerHTML //
}
}

Expand Down Expand Up @@ -261,6 +273,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {

function getTableData(tableProperties, timesClickedColumn) {
const {
table,
tableRows,
fillValue,
column,
Expand All @@ -269,7 +282,6 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
isSortDates,
desc,
arrow,
tableArrows,
} = tableProperties;
for (let [i, tr] of tableRows.entries()) {
let tdTextContent = column.getColumn(
Expand Down Expand Up @@ -361,7 +373,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}

function changeTableArrow(arrowDirection) {
if (tableArrows) {
if (table.hasClass.tableArrows) {
clearArrows(arrow.up, arrow.down);
th.insertAdjacentText("beforeend", arrowDirection);
}
Expand Down Expand Up @@ -476,17 +488,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
th,
columnIndex,
table,
sortableTable,
columnIndexesClicked
) {
const desc = th.classList.contains("order-by-desc");
const tableArrows = sortableTable.classList.contains("table-arrows");
const arrow = { up: " ▲", down: " ▼" };
const fillValue = "!X!Y!Z!";

if (desc && tableArrows) {
if (desc && table.hasClass.tableArrows) {
th.insertAdjacentText("beforeend", arrow.down);
} else if (tableArrows) {
} else if (table.hasClass.tableArrows) {
th.insertAdjacentText("beforeend", arrow.up);
}

Expand Down Expand Up @@ -515,8 +525,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
);

const isRememberSort = sortableTable.classList.contains("remember-sort");
if (!isRememberSort) {
if (!table.hasClass.rememberSort) {
timesClickedColumn = rememberSort(
columnIndexesClicked,
timesClickedColumn,
Expand Down Expand Up @@ -556,6 +565,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}

const tableProperties = {
table,
tableRows: table.visibleRows,
fillValue,
column,
Expand All @@ -566,7 +576,6 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
desc,
timesClickedColumn,
arrow,
tableArrows,
};
timesClickedColumn = getTableData(tableProperties, timesClickedColumn);
updateTable(tableProperties);
Expand Down

0 comments on commit 9a7be82

Please sign in to comment.