-
Notifications
You must be signed in to change notification settings - Fork 2
feat(cms): sortable table (for TACC project pages redesign) #1167
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1fe2544
feat(cms): add sortable project table module
wesleyboar b981afb
fix(cms): guard sortable table when rows lack cells
wesleyboar cda57a3
refactor(cms): move sortable table styles to is-sortable module
wesleyboar 18511ff
docs: remove excess in filename
wesleyboar 7c853e4
fix: cell layout not matching static table
wesleyboar cad1256
fix: assumptions in css
wesleyboar ad16f95
refactor: nest css
wesleyboar 557adf3
refactor: css
wesleyboar 64988bc
fix(css): bootstrap button support
wesleyboar 1d80a9a
fix(css): align toggle
wesleyboar 3e6c06d
refactor(sortableTable): class mgmt, custom btns
wesleyboar 044a1b5
docs: sortableTable.js
wesleyboar 0bffc89
docs: polished, cosnistent, coupling notice
wesleyboar 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
44 changes: 44 additions & 0 deletions
44
taccsite_cms/static/site_cms/css/modules/sortableTable.css
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,44 @@ | ||
| /** | ||
| * Styles for CMS tables with class `is-sortable` | ||
| * IMPORTANT: Coupled with sortableTable.js | ||
| */ | ||
|
|
||
| table.is-sortable { | ||
| /* Buttons */ | ||
| & .is-sortable__sort { | ||
| width: 100%; /* so entire cell is clickable */ | ||
|
|
||
| /* To improve legibility during visible focus */ | ||
| &:focus-visible { | ||
| outline-offset: 0.25em; | ||
| } | ||
|
|
||
| /* So known button styles have no ill effect */ | ||
| &.btn-link, | ||
| &.c-button--as-link { | ||
| line-height: inherit; | ||
| height: 1lh; | ||
| justify-content: start; | ||
| vertical-align: baseline; | ||
| } | ||
| &.c-button--as-link { | ||
| height: 1lh; | ||
| } | ||
| &.btn-link { | ||
| padding: 0; | ||
| } | ||
| } | ||
|
|
||
| /* Sort indicators */ | ||
| & th[aria-sort="ascending"] .is-sortable__sort::after { content: '▲'; } | ||
| & th[aria-sort="descending"] .is-sortable__sort::after { content: '▼'; } | ||
| & th[aria-sort="ascending"] .is-sortable__sort, | ||
| & th[aria-sort="descending"] .is-sortable__sort { | ||
| &::after { | ||
| font-size: smaller; | ||
| } | ||
| &:not(.c-button--as-link)::after { | ||
| margin-inline-start: 0.35em; | ||
| } | ||
| } | ||
| } |
188 changes: 188 additions & 0 deletions
188
taccsite_cms/static/site_cms/js/modules/sortableTable.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,188 @@ | ||
| /** | ||
| * Client-side sort for CMS tables with class `is-sortable` | ||
| * IMPORTANT: Coupled with `sortableTable.css` | ||
| * | ||
| * TODO: Consider adding to TACC/Core-Components | ||
| * NOTE: Not using tristen/tablesort cuz it forgoes button in header cell (a11y) | ||
| * SEE: https://github.com/tristen/tablesort | ||
| * SEE: https://www.w3.org/WAI/ARIA/apg/patterns/table/examples/sortable-table/ | ||
| * | ||
| * Editor markup: | ||
| * - Table: `o-fixed-header-table is-sortable` | ||
| * - Non-sortable column: `th.is-not-sortable` (e.g. Description) | ||
| */ | ||
|
|
||
| const SORT_TABLE_CLASS = 'is-sortable'; | ||
| const NOT_SORTABLE_CLASS = 'is-not-sortable'; | ||
| const SORT_BUTTON_CLASS = 'is-sortable__sort'; | ||
|
|
||
| const DEFAULT_TABLE_SELECTOR = 'table.' + SORT_TABLE_CLASS; | ||
| const NOT_SORTABLE_SELECTOR = 'th.' + NOT_SORTABLE_CLASS; | ||
|
|
||
| /** | ||
| * @param {HTMLTableCellElement | undefined} cell | ||
| * @param {{ table: HTMLTableElement, warnedMissingCell?: boolean }} [logContext] | ||
| * @returns {string} | ||
| */ | ||
| function getSortValue(cell, logContext) { | ||
| if (!cell) { | ||
| if (logContext && !logContext.warnedMissingCell) { | ||
| logContext.warnedMissingCell = true; | ||
| console.warn( | ||
| '[sortableTable] A row is missing a cell for the sorted column. Use the same number of columns on every row in the CMS table (watch colspan/rowspan).', | ||
| logContext.table | ||
| ); | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| const link = cell.querySelector('a'); | ||
| const text = link ? link.textContent : cell.textContent; | ||
| return (text ?? '').trim(); | ||
| } | ||
|
|
||
| /** | ||
| * @param {HTMLTableElement} table | ||
| * @param {HTMLTableRowElement} headerRow | ||
| */ | ||
| function warnIfIrregularRows(table, headerRow) { | ||
| const tbody = table.tBodies[0]; | ||
| if (!tbody) { | ||
| return; | ||
| } | ||
|
|
||
| const expected = headerRow.cells.length; | ||
|
|
||
| for (let i = 0; i < tbody.rows.length; i++) { | ||
| const row = tbody.rows[i]; | ||
| if (row.cells.length < expected) { | ||
| console.warn( | ||
| `[sortableTable] Row ${i + 1} has ${row.cells.length} cells but the header has ${expected}. Fix table markup in the CMS before publishing.`, | ||
| table | ||
| ); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {HTMLTableElement} table | ||
| * @param {number} columnIndex | ||
| * @param {'ascending' | 'descending'} direction | ||
| */ | ||
| function sortTable(table, columnIndex, direction) { | ||
| const tbody = table.tBodies[0]; | ||
| if (!tbody) { | ||
| return; | ||
| } | ||
|
|
||
| const rows = [ ...tbody.rows ]; | ||
| const multiplier = direction === 'ascending' ? 1 : -1; | ||
| const logContext = { table, warnedMissingCell: false }; | ||
|
|
||
| rows.sort((rowA, rowB) => { | ||
| const a = getSortValue(rowA.cells[columnIndex], logContext); | ||
| const b = getSortValue(rowB.cells[columnIndex], logContext); | ||
| return multiplier * a.localeCompare(b, undefined, { sensitivity: 'base' }); | ||
| }); | ||
|
|
||
| rows.forEach((row) => tbody.appendChild(row)); | ||
| } | ||
|
|
||
| /** | ||
| * @param {HTMLTableCellElement} headerCell | ||
| * @param {'ascending' | 'descending' | 'none'} ariaSort | ||
| */ | ||
| function setHeaderSortState(headerCell, ariaSort) { | ||
| headerCell.setAttribute('aria-sort', ariaSort); | ||
| const button = headerCell.querySelector('button'); | ||
| if (button) { | ||
| button.setAttribute( | ||
| 'aria-label', | ||
| ariaSort === 'none' | ||
| ? headerCell.dataset.sortLabel | ||
| : `${headerCell.dataset.sortLabel}, sorted ${ariaSort}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {HTMLTableElement} table | ||
| * @param {string} notSortableSelector | ||
| * @param {string} buttonClass | ||
| */ | ||
| function initSortableTable(table, notSortableSelector, buttonClass) { | ||
| const headerRow = table.tHead?.rows[0]; | ||
| if (!headerRow) { | ||
| return; | ||
| } | ||
|
|
||
| warnIfIrregularRows(table, headerRow); | ||
|
|
||
| /** @type {HTMLTableCellElement[]} */ | ||
| const sortableHeaders = []; | ||
|
|
||
| [ ...headerRow.cells ].forEach((cell, index) => { | ||
| if (!(cell instanceof HTMLTableCellElement)) { | ||
| return; | ||
| } | ||
| if (cell.matches(notSortableSelector)) { | ||
| cell.classList.add(NOT_SORTABLE_CLASS); | ||
| return; | ||
| } | ||
|
|
||
| const label = cell.textContent?.trim() ?? `Column ${index + 1}`; | ||
| cell.dataset.sortLabel = label; | ||
| cell.innerHTML = ''; | ||
|
|
||
| const button = document.createElement('button'); | ||
| button.type = 'button'; | ||
| button.className = buttonClass + ' ' + SORT_BUTTON_CLASS; | ||
| button.textContent = label; | ||
| cell.append(button); | ||
|
|
||
| button.addEventListener('click', () => { | ||
| const current = cell.getAttribute('aria-sort'); | ||
| const next = | ||
| current === 'ascending' ? 'descending' : 'ascending'; | ||
|
|
||
| sortableHeaders.forEach((other) => { | ||
| if (other !== cell) { | ||
| setHeaderSortState(other, 'none'); | ||
| } | ||
| }); | ||
|
|
||
| setHeaderSortState(cell, next); | ||
| sortTable(table, index, next); | ||
| }); | ||
|
|
||
| sortableHeaders.push(cell); | ||
| }); | ||
|
|
||
| if (sortableHeaders.length) { | ||
| const first = sortableHeaders[0]; | ||
| const firstIndex = [ ...headerRow.cells ].indexOf(first); | ||
| setHeaderSortState(first, 'ascending'); | ||
| sortTable(table, firstIndex, 'ascending'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {object} [options] | ||
| * @param {ParentNode} [options.scopeElement=document] | ||
| * @param {string} [options.tableSelector=table.is-sortable] | ||
| * @param {string} [options.notSortableSelector=th.is-not-sortable] | ||
| * @param {string} [options.buttonClass=''] // e.g. 'c-button c-button--as-link' | ||
| */ | ||
| export default function sortableTable({ | ||
| scopeElement = document, | ||
| tableSelector = DEFAULT_TABLE_SELECTOR, | ||
| notSortableSelector = NOT_SORTABLE_SELECTOR, | ||
| buttonClass = '', | ||
| } = {}) { | ||
| scopeElement.querySelectorAll(tableSelector).forEach((table) => { | ||
| if (table instanceof HTMLTableElement) { | ||
| initSortableTable(table, notSortableSelector, buttonClass); | ||
| } | ||
| }); | ||
| } | ||
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
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.