@@ -5,6 +5,7 @@ Sage.table = (function() {
55
66 // const SELECTOR_TABLE = "[data-js-table]";
77 const SELECTOR_TABLE = ".sage-table" ;
8+ const MOBILE_TABLE_MAX_WIDTH = "767" ; // SM_MAX
89
910 // ==================================================
1011 // Functions
@@ -58,40 +59,29 @@ Sage.table = (function() {
5859 } )
5960 }
6061
61- // https://adrianroselli.com/2018/02/tables-css-display-properties-and-aria.html
62- // https://adrianroselli.com/2018/05/functions-to-add-aria-to-tables-and-lists.html
63- function AddTableARIA ( ) {
64- try {
65- var allTables = document . querySelectorAll ( 'table' ) ;
66- for ( var i = 0 ; i < allTables . length ; i ++ ) {
67- allTables [ i ] . setAttribute ( 'role' , 'table' ) ;
68- }
69- var allRowGroups = document . querySelectorAll ( 'thead, tbody, tfoot' ) ;
70- for ( var i = 0 ; i < allRowGroups . length ; i ++ ) {
71- allRowGroups [ i ] . setAttribute ( 'role' , 'rowgroup' ) ;
72- }
73- var allRows = document . querySelectorAll ( 'tr' ) ;
74- for ( var i = 0 ; i < allRows . length ; i ++ ) {
75- allRows [ i ] . setAttribute ( 'role' , 'row' ) ;
76- }
77- var allCells = document . querySelectorAll ( 'td' ) ;
78- for ( var i = 0 ; i < allCells . length ; i ++ ) {
79- allCells [ i ] . setAttribute ( 'role' , 'cell' ) ;
80- }
81- var allHeaders = document . querySelectorAll ( 'th' ) ;
82- for ( var i = 0 ; i < allHeaders . length ; i ++ ) {
83- allHeaders [ i ] . setAttribute ( 'role' , 'columnheader' ) ;
84- }
85- // this accounts for scoped row headers
86- var allRowHeaders = document . querySelectorAll ( 'th[scope=row]' ) ;
87- for ( var i = 0 ; i < allRowHeaders . length ; i ++ ) {
88- allRowHeaders [ i ] . setAttribute ( 'role' , 'rowheader' ) ;
89- }
90- // caption role not needed as it is not a real role and
91- // browsers do not dump their own role with display block
92- } catch ( e ) {
93- console . log ( "AddTableARIA(): " + e ) ;
94- }
62+ function applyAriaRoles ( args ) {
63+ // expects an object of the form { items: "<selector(s)>", role: "<role to represent>" }
64+ // based on:
65+ // https://adrianroselli.com/2018/02/tables-css-display-properties-and-aria.html
66+ // https://adrianroselli.com/2018/05/functions-to-add-aria-to-tables-and-lists.html
67+ const group = document . querySelectorAll ( args . items ) ;
68+
69+ group . forEach ( el => {
70+ el . setAttribute ( 'role' , args . role ) ;
71+ } )
72+ }
73+
74+ function addTableAria ( ) {
75+ const tableItems = [
76+ { items : 'table' , role : 'table' } ,
77+ { items : 'thead, tbody, tfoot' , role : 'rowgroup' } ,
78+ { items : 'tr' , role : 'row' } ,
79+ { items : 'td' , role : 'cell' } ,
80+ { items : 'th' , role : 'columnheader' } ,
81+ { items : 'th[scope=row]' , role : 'rowheader' } ,
82+ ] ;
83+
84+ tableItems . map ( ( item ) => applyAriaRoles ( item ) ) ;
9585 }
9686
9787 // reset classes on elements
@@ -106,11 +96,10 @@ Sage.table = (function() {
10696 if ( document . querySelector ( '.sage-table--sortable' ) !== null ) {
10797 sortEvents ( ) ;
10898 }
109- if ( document . querySelector ( '.sage-table--sortable' ) !== null ) {
99+ if ( document . querySelector ( SELECTOR_TABLE ) !== null && window . innerWidth <= MOBILE_TABLE_MAX_WIDTH ) {
100+ addTableAria ( ) ;
110101 ResponsiveCellHeaders ( SELECTOR_TABLE ) ;
111102 }
112-
113- AddTableARIA ( ) ;
114103 }
115104
116105
0 commit comments