-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
* class="dir-d|u"->aria-sort="ascending|descending" resolves #62
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,65 @@ | ||
/** | ||
* This is a "plugin" for the sortable package: | ||
* https://www.npmjs.com/package/sortable-tablesort | ||
* https://github.com/tofsjonas/sortable | ||
* | ||
* Enhances the accessibility of class="sortable" tables by adding ARIA attributes and keyboard event listeners. | ||
* @param tables - A list of HTML table elements to enhance. | ||
*/ | ||
var enhanceSortableAccessibility = function (tables) { | ||
function updateAriaLabel(header_element) { | ||
/** | ||
* Generates an aria-label attribute for a table header cell based on its content and current sort direction. | ||
* @param element - The table header cell to update. | ||
* @param default_direction - The default sort direction for the table. | ||
*/ | ||
function updateAriaLabel(element, default_direction) { | ||
var _a; | ||
if (default_direction === void 0) { default_direction = ""; } | ||
// Generate aria-label based on header content | ||
var header_text = header_element.textContent || ''; | ||
var direction = header_element.classList.contains('dir-d') ? 'ascending' : 'descending'; | ||
var aria_label = "Click to sort table by ".concat(header_text, " in ").concat(direction, " order"); | ||
header_element.setAttribute('aria-label', aria_label); | ||
// header_element.setAttribute('title', aria_label) | ||
var header_text = element.textContent || 'element'; | ||
var current_direction = (_a = element.getAttribute('aria-sort')) !== null && _a !== void 0 ? _a : ''; | ||
var new_direction = 'descending'; | ||
if (current_direction === 'descending' || | ||
default_direction && current_direction !== 'ascending') { | ||
new_direction = 'ascending'; | ||
} | ||
var aria_label = "Click to sort table by ".concat(header_text, " in ").concat(new_direction, " order"); | ||
element.setAttribute('aria-label', aria_label); | ||
// element.setAttribute('title', aria_label) | ||
} | ||
/** | ||
* Handles keyboard events on table header cells and triggers a click event when the Enter key is pressed. | ||
* @param event - The keyboard event to handle. | ||
*/ | ||
function handleKeyDown(event) { | ||
if (event.key === 'Enter') { | ||
var element = event.target; | ||
element.click(); | ||
} | ||
} | ||
// Iterate over each table in the input list | ||
tables.forEach(function (table) { | ||
var default_direction = table.classList.contains('asc') ? 'ascending' : ''; | ||
var headers = table.querySelectorAll('th'); | ||
// Iterate over each header cell in the table | ||
headers.forEach(function (header) { | ||
var header_element = header; | ||
header_element.setAttribute('tabindex', '0'); | ||
updateAriaLabel(header_element); | ||
header_element.addEventListener('click', function () { | ||
var element = header; | ||
// Add tabindex attribute and generate initial aria-label attribute | ||
element.setAttribute('tabindex', '0'); | ||
updateAriaLabel(element, default_direction); | ||
// Attach click event listener to update aria-label attribute | ||
element.addEventListener('click', function () { | ||
// Add a delay to allow the new sort order to be applied | ||
setTimeout(function () { | ||
updateAriaLabel(header_element); | ||
updateAriaLabel(element, default_direction); | ||
}, 50); | ||
}); | ||
// Attach event handlers | ||
header_element.addEventListener('keydown', handleKeyDown); | ||
// Attach keyboard event listener to trigger click event | ||
element.addEventListener('keydown', handleKeyDown); | ||
}); | ||
}); | ||
}; | ||
// Attach function to DOMContentLoaded event to execute when page is loaded | ||
document.addEventListener('DOMContentLoaded', function () { | ||
enhanceSortableAccessibility(document.querySelectorAll('.sortable')); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.