-
-
Notifications
You must be signed in to change notification settings - Fork 402
feat(core/sortable-table): sortable tables #3812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sidvishnoi
wants to merge
5
commits into
main
Choose a base branch
from
sortable-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b54c869
feat(core/sortable-table): sortable tables
sidvishnoi 82d8dcf
Merge branch 'develop' into sortable-table
sidvishnoi 09e7edd
Merge branch 'main' into sortable-table
sidvishnoi c2c23ec
Merge branch 'main' into sortable-table
sidvishnoi d74446d
Merge branch 'main' into sortable-table
caribouW3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// @ts-check | ||
import css from "../styles/sortable-table.css.js"; | ||
import { fetchBase } from "./text-loader.js"; | ||
|
||
export const name = "core/sortable-table"; | ||
|
||
export async function run() { | ||
if (!document.querySelector("table.sortable")) { | ||
return; | ||
} | ||
|
||
const style = document.createElement("style"); | ||
style.textContent = css; | ||
document.head.insertBefore(style, document.querySelector("link")); | ||
|
||
const script = document.createElement("script"); | ||
script.id = "respec-dfn-panel"; | ||
script.textContent = await loadScript(); | ||
document.body.append(script); | ||
} | ||
|
||
async function loadScript() { | ||
try { | ||
return (await import("text!./sortable-table.runtime.js")).default; | ||
} catch { | ||
return fetchBase("./src/core/sortable-table.runtime.js"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
if (document.respec) { | ||
document.respec.ready.then(setupSortableTable); | ||
} else { | ||
setupSortableTable(); | ||
} | ||
|
||
function setupSortableTable() { | ||
/** @type {WeakMap<HTMLTableElement, WeakMap<HTMLTableCellElement, -1 | 1>>} */ | ||
const STATE = new WeakMap(); | ||
|
||
/** | ||
* @param {HTMLTableCellElement} th | ||
* @param {-1 | 0 | 1} dir | ||
*/ | ||
const createOrUpdateButton = (th, dir) => { | ||
const textValue = [" descending", "", " ascending"][dir + 1]; | ||
const iconValue = ["▼", "▲/▼", "▲"][dir + 1]; // TODO: fix these mappings | ||
const label = th.dataset.text || th.textContent; | ||
if (!th.dataset.text) th.dataset.text = label; | ||
|
||
const button = | ||
th.querySelector("button") || document.createElement("button"); | ||
const text = `Sort${textValue} by ${label}`; | ||
button.title = text; | ||
button.setAttribute("aria-label", text); | ||
button.textContent = iconValue; | ||
return button; | ||
}; | ||
|
||
/** @type {NodeListOf<HTMLTableElement>} */ | ||
const tables = document.querySelectorAll("table.sortable"); | ||
for (const table of tables) { | ||
for (const th of table.tHead.querySelectorAll("th")) { | ||
th.append(createOrUpdateButton(th, 0)); | ||
} | ||
} | ||
|
||
/** @type {(el: MouseEvent)=> HTMLTableCellElement | null} */ | ||
const getTrigger = ev => { | ||
if (!(ev.target instanceof HTMLElement)) return null; | ||
const el = ev.target; | ||
if (el.localName === "th") { | ||
return el; | ||
} | ||
if (el.localName === "button" && el.parentElement.localName === "th") { | ||
return el.parentElement; | ||
} | ||
return null; | ||
}; | ||
|
||
document.addEventListener("click", ev => { | ||
const th = getTrigger(ev); | ||
if (!th) return; | ||
|
||
/** @type {HTMLTableElement | null} */ | ||
const table = th.closest("table.sortable"); | ||
if (!table) return; | ||
|
||
ev.preventDefault(); | ||
|
||
const state = | ||
STATE.get(table) || STATE.set(table, new WeakMap()).get(table); | ||
const direction = state.get(th) === 1 ? -1 : 1; // -1: ascending, 1: descending | ||
state.set(th, direction); | ||
|
||
createOrUpdateButton(th, direction); | ||
|
||
const tbody = table.tBodies[0]; | ||
const headers = th.closest("tr").cells; | ||
const colIndex = Array.from(headers).findIndex(node => node === th); | ||
const rows = Array.from(tbody.rows).sort((a, b) => { | ||
const x = a.cells[colIndex].textContent.trimStart(); | ||
const y = b.cells[colIndex].textContent.trimStart(); | ||
return direction * x.localeCompare(y); | ||
}); | ||
|
||
/** @type {typeof tbody} */ | ||
const tbodyCone = tbody.cloneNode(false); | ||
tbodyCone.append(...rows); | ||
tbody.replaceWith(tbodyCone); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const css = String.raw; | ||
|
||
export default css` | ||
.sortable th { | ||
vertical-align: middle; | ||
white-space: nowrap; | ||
} | ||
|
||
.sortable th button { | ||
background: inherit; | ||
color: inherit; | ||
border: none; | ||
font-size: 0.6em; | ||
padding: 0.5em; | ||
} | ||
|
||
.sortable th button:hover { | ||
cursor: pointer; | ||
} | ||
|
||
.sortable th button:focus { | ||
outline: 1px solid; | ||
} | ||
`; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.