-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(templates): Notes tables are now responsive with accordion in sm…
…aller screens
- Loading branch information
Showing
3 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,115 @@ | ||
/** | ||
* Adds header labels as `::before` pseudo-elements to table cells for mobile views. | ||
* | ||
* @param {string} elmID The ID of the table to apply the headers to. | ||
*/ | ||
function ResponsiveCellHeaders(elmID) { | ||
try { | ||
var THarray = []; | ||
var table = document.getElementById(elmID); | ||
var ths = table.getElementsByTagName('th'); | ||
for (var i = 0; i < ths.length; i++) { | ||
var headingText = ths[i].innerHTML; | ||
THarray.push(headingText); | ||
} | ||
var styleElm = document.createElement('style'), | ||
styleSheet; | ||
document.head.appendChild(styleElm); | ||
styleSheet = styleElm.sheet; | ||
for (var i = 0; i < THarray.length; i++) { | ||
styleSheet.insertRule( | ||
'#' + elmID + ' td:nth-child(' + (i + 1) + ')::before {content:"' + THarray[i] + ': ";}', | ||
styleSheet.cssRules.length | ||
); | ||
} | ||
} catch (e) { | ||
console.log('ResponsiveCellHeaders(): ' + e); | ||
} | ||
} | ||
|
||
// https://adrianroselli.com/2018/02/tables-css-display-properties-and-aria.html | ||
// https://adrianroselli.com/2018/05/functions-to-add-aria-to-tables-and-lists.html | ||
function AddTableARIA() { | ||
try { | ||
var allTables = document.querySelectorAll('table'); | ||
for (var i = 0; i < allTables.length; i++) { | ||
allTables[i].setAttribute('role', 'table'); | ||
} | ||
var allRowGroups = document.querySelectorAll('thead, tbody, tfoot'); | ||
for (var i = 0; i < allRowGroups.length; i++) { | ||
allRowGroups[i].setAttribute('role', 'rowgroup'); | ||
} | ||
var allRows = document.querySelectorAll('tr'); | ||
for (var i = 0; i < allRows.length; i++) { | ||
allRows[i].setAttribute('role', 'row'); | ||
} | ||
var allCells = document.querySelectorAll('td'); | ||
for (var i = 0; i < allCells.length; i++) { | ||
allCells[i].setAttribute('role', 'cell'); | ||
} | ||
var allHeaders = document.querySelectorAll('th'); | ||
for (var i = 0; i < allHeaders.length; i++) { | ||
allHeaders[i].setAttribute('role', 'columnheader'); | ||
} | ||
// this accounts for scoped row headers | ||
var allRowHeaders = document.querySelectorAll('th[scope=row]'); | ||
for (var i = 0; i < allRowHeaders.length; i++) { | ||
allRowHeaders[i].setAttribute('role', 'rowheader'); | ||
} | ||
// caption role not needed as it is not a real role and | ||
// browsers do not dump their own role with display block | ||
} catch (e) { | ||
console.log('AddTableARIA(): ' + e); | ||
} | ||
} | ||
document.addEventListener('DOMContentLoaded', function () { | ||
ResponsiveCellHeaders('dockets'); | ||
AddTableARIA(); | ||
}); | ||
|
||
function toggle() { | ||
const row = window.event.target.closest('tr'); | ||
row.classList.toggle('row-active'); | ||
|
||
const isActive = row.classList.contains('row-active'); | ||
|
||
if (isActive) { | ||
const activeColumns = row.querySelectorAll('td:not(:first-child)'); | ||
activeColumns.forEach(function (col) { | ||
col.setAttribute('aria-hidden', 'false'); | ||
}); | ||
} else { | ||
const activeColumns = row.querySelectorAll('td[aria-hidden="false"]'); | ||
activeColumns.forEach(function (col) { | ||
col.setAttribute('aria-hidden', 'true'); | ||
}); | ||
} | ||
} | ||
|
||
document.querySelectorAll('td').forEach(function (td) { | ||
td.addEventListener('click', toggle); | ||
}); | ||
|
||
function handleResize() { | ||
const isMobileMode = window.matchMedia('screen and (max-width: 880px)'); | ||
const inactiveColumns = document.querySelectorAll('tbody > tr > td:not(:first-child)'); | ||
|
||
inactiveColumns.forEach(function (col) { | ||
col.setAttribute('aria-hidden', isMobileMode.matches.toString()); | ||
}); | ||
} | ||
|
||
window.addEventListener('resize', handleResize); | ||
|
||
handleResize(); | ||
|
||
document.querySelectorAll('a[data-toggle="tab"]').forEach(function (tab) { | ||
tab.addEventListener('click', function (event) { | ||
// Extract the tab's href attribute (which is the tab pane's ID) | ||
var tabId = event.target.getAttribute('href').substring(1); // Remove the `#` from href | ||
console.log('Tab clicked:', tabId); // Debugging log to check the correct tab ID | ||
|
||
// Run ResponsiveCellHeaders to update the table for the selected tab | ||
ResponsiveCellHeaders(tabId); // This assumes each tab corresponds to a table with the same ID | ||
}); | ||
}); |
This file contains 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