Skip to content

Commit 877d57f

Browse files
npm release 1.10.2; added onload-sort class for <th>.
1 parent b53196f commit 877d57f

File tree

8 files changed

+44
-34
lines changed

8 files changed

+44
-34
lines changed

browser-extension/manifest.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{
22
"manifest_version": 2,
3-
"author":"Lee Wannacott",
3+
"author": "Lee Wannacott",
44
"name": "table-sort-js",
55
"version": "0.0",
66
"description": "Makes tables sortable using https://github.com/LeeWannacott/table-sort-js",
7-
"icons":
8-
{ "48": "icons/t.png" }
9-
,
7+
"icons": { "48": "icons/t.png" },
108
"browser_action": {
119
"default_icon": "./icons/t.png",
1210
"default_title": "table-sort",
@@ -19,7 +17,7 @@
1917
"*://*.mozilla.org/*",
2018
"https://github.com/*/*/actions/runs/*/usage"
2119
],
22-
"js": ["addTableSortClass.js","getTableHeaders.js","table-sort.js"]
20+
"js": ["addTableSortClass.js", "getTableHeaders.js", "table-sort.js"]
2321
}
2422
]
2523
}

browser-extension/popup/table-options.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
<button>table3</button>
1313
<button>table4</button>
1414
</div>
15-
<script src="../setButtons.js"> </script>
15+
<script src="../setButtons.js"></script>
1616
</body>
1717
</html>

browser-extension/setButtons.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// let tables = window.document.querySelectorAll("table");
22
// console.log(getTableHeaders())
33
let headers = document.querySelectorAll("table th");
4-
console.log(headers)
4+
console.log(headers);
55
let headerNames = Array.from(headers).map((header) => header.innerText);
66
console.log("nnnn");
77

8-
98
headerNames.forEach((name) => {
109
const btn = document.createElement("button");
1110
console.log(name);
@@ -14,4 +13,4 @@ headerNames.forEach((name) => {
1413
document.querySelector("#popup-content").appendChild(btn);
1514
let test = document.querySelector("#popup-content");
1615
console.log(test);
17-
})
16+
});

npm/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ Refer to the documenation for examples on how to use table-sort-js with [HTML](h
5151
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------- |
5252
| "order-by-desc" | Order by descending on first click. (default is aescending) |
5353
| "data-sort" | Sort by [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes), e.g &lt;td data-sort="42"&gt; |
54+
| "onload-sort" | Sort column on loading of the page. Simulates a click from the user. (can only sort onload for one column) |
5455
| "file-size-sort" | Sort file sizes(B->TiB) uses the binary prefix. (e.g KiB) |
56+
| "disable-sort" | Disallow sorting the table by this specific column. |
5557
| "alpha-sort" | Sort alphabetically (z11,z2); default is [natural sort](https://en.wikipedia.org/wiki/Natural_sort_order) (z2,z11). |
5658
| "punct-sort" | Sort punctuation; default ignores punctuation. |
57-
| "disable-sort" | Disallow sorting the table by this specific column. |
5859

5960
#### Development:
6061

npm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "table-sort-js",
3-
"version": "1.9.2",
3+
"version": "1.10.2",
44
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.",
55
"license": "MIT",
66
"repository": "LeeWannacott/table-sort-js",

npm/table-sort.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
3535
}
3636
}
3737

38-
function makeTableSortable(sortableTable) {
38+
function createMissingTableHead(sortableTable) {
3939
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) {
4150
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);
4952
if (sortableTable.querySelectorAll("tbody").length > 1) {
50-
tableBody = sortableTable.querySelectorAll("tbody")[1];
53+
return sortableTable.querySelectorAll("tbody")[1];
5154
} else {
52-
tableBody = sortableTable.querySelector("tbody");
55+
return sortableTable.querySelector("tbody");
5356
}
5457
} else {
55-
tableBody = sortableTable.querySelector("tbody");
58+
return sortableTable.querySelector("tbody");
5659
}
60+
}
5761

62+
function makeTableSortable(sortableTable) {
63+
const tableBody = getTableBody(sortableTable);
5864
const tableHead = sortableTable.querySelector("thead");
5965
const tableHeadHeaders = tableHead.querySelectorAll("th");
6066

@@ -320,6 +326,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
320326
getTableData(tableProperties);
321327
updateTable(tableProperties);
322328
});
329+
let isOnloadSort = th.classList.contains("onload-sort");
330+
if (isOnloadSort) {
331+
th.click();
332+
}
323333
}
324334
}
325335

public/table-sort.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
3737

3838
function createMissingTableHead(sortableTable) {
3939
let createTableHead;
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);
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);
4747
}
4848

4949
function getTableBody(sortableTable) {
5050
if (sortableTable.getElementsByTagName("thead").length === 0) {
51-
createMissingTableHead(sortableTable)
51+
createMissingTableHead(sortableTable);
5252
if (sortableTable.querySelectorAll("tbody").length > 1) {
5353
return sortableTable.querySelectorAll("tbody")[1];
5454
} else {
@@ -326,7 +326,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
326326
getTableData(tableProperties);
327327
updateTable(tableProperties);
328328
});
329-
let isOnloadSort = th.classList.contains("onload-sort");
329+
let isOnloadSort = th.classList.contains("onload-sort");
330330
if (isOnloadSort) {
331331
th.click();
332332
}

test/order-by-desc.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,11 @@ test("visible-tr-sort: example sort only visible trs", () => {
176176
).toStrictEqual({ col0: ["row5", "row4", "row2"] });
177177
});
178178

179-
180179
test("onload-sort: testing that it sorts without a click from user", () => {
181180
expect(
182-
createTestTable({ col0: [5, 3, 4, 1, 2] }, { classTags: "order-by-desc onload-sort" })
181+
createTestTable(
182+
{ col0: [5, 3, 4, 1, 2] },
183+
{ classTags: "order-by-desc onload-sort" }
184+
)
183185
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
184186
});

0 commit comments

Comments
 (0)