From 0d772b5eba99c79727384a1bc9292e6083278055 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Sun, 11 Dec 2011 08:27:41 +0000 Subject: [PATCH] Always add an ID to a table if it doesn't already have one Add a class to the table to identify it as a DataTable Update setAttribute to use id - works just as well and slightly smaller --- media/js/jquery.dataTables.js | 88 ++++++++++++++---------------- media/src/core/core.constructor.js | 12 +++- media/src/core/core.draw.js | 16 ++---- media/src/core/core.filter.js | 24 ++++---- media/src/core/core.info.js | 7 +-- media/src/core/core.length.js | 13 ++--- media/src/core/core.processing.js | 4 +- media/src/core/core.support.js | 4 +- media/src/ext/ext.classes.js | 4 ++ media/src/ext/ext.paging.js | 4 +- 10 files changed, 84 insertions(+), 92 deletions(-) diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index bb8efbc2..0572e7fc 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -991,7 +991,7 @@ /* Special parameters can be given by the data source to be used on the row */ if ( typeof oData._aData.DT_RowId != 'undefined' ) { - oData.nTr.setAttribute( 'id', oData._aData.DT_RowId ); + oData.nTr.id = oData._aData.DT_RowId; } if ( typeof oData._aData.DT_RowClass != 'undefined' ) @@ -1073,10 +1073,7 @@ nTh.setAttribute('role', 'columnheader'); nTh.setAttribute('aria-label', 'Activate to sort column'); - if ( oSettings.sTableId ) - { - nTh.setAttribute('aria-controls', oSettings.sTableId); - } + nTh.setAttribute('aria-controls', oSettings.sTableId); if ( oSettings.aoColumns[i].sClass !== null ) { @@ -1727,11 +1724,8 @@ */ oSettings.nTableWrapper = document.createElement( 'div' ); oSettings.nTableWrapper.className = oSettings.oClasses.sWrapper; + oSettings.nTableWrapper.id = oSettings.sTableId+'_wrapper'; oSettings.nTableWrapper.setAttribute('role', 'grid'); - if ( oSettings.sTableId !== '' ) - { - oSettings.nTableWrapper.setAttribute( 'id', oSettings.sTableId+'_wrapper' ); - } oSettings.nTableReinsertBefore = oSettings.nTable.nextSibling; @@ -1779,12 +1773,12 @@ if ( sAttr.indexOf('.') != -1 ) { var aSplit = sAttr.split('.'); - nNewNode.setAttribute('id', aSplit[0].substr(1, aSplit[0].length-1) ); + nNewNode.id = aSplit[0].substr(1, aSplit[0].length-1); nNewNode.className = aSplit[1]; } else if ( sAttr.charAt(0) == "#" ) { - nNewNode.setAttribute('id', sAttr.substr(1, sAttr.length-1) ); + nNewNode.id = sAttr.substr(1, sAttr.length-1); } else { @@ -1998,9 +1992,9 @@ var nFilter = document.createElement( 'div' ); nFilter.className = oSettings.oClasses.sFilter; nFilter.innerHTML = ''; - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.f == "undefined" ) + if ( typeof oSettings.aanFeatures.f == "undefined" ) { - nFilter.setAttribute( 'id', oSettings.sTableId+'_filter' ); + nFilter.id = oSettings.sTableId+'_filter'; } var jqFilter = $("input", nFilter); @@ -2027,18 +2021,16 @@ } } ); - if ( oSettings.sTableId ) - { - jqFilter.attr('aria-controls', oSettings.sTableId); - } - - jqFilter.bind( 'keypress.DT', function(e) { - /* Prevent default */ - if ( e.keyCode == 13 ) - { - return false; + jqFilter + .attr('aria-controls', oSettings.sTableId) + .bind( 'keypress.DT', function(e) { + /* Prevent form submission */ + if ( e.keyCode == 13 ) + { + return false; + } } - } ); + ); return nFilter; } @@ -2382,12 +2374,9 @@ } ); /* Add id */ - if ( oSettings.sTableId !== '' ) - { - nInfo.setAttribute( 'id', oSettings.sTableId+'_info' ); - oSettings.nTable.setAttribute( 'aria-describedby', oSettings.sTableId+'_info' ); - } + nInfo.id = oSettings.sTableId+'_info'; } + oSettings.nTable.setAttribute( 'aria-describedby', oSettings.sTableId+'_info' ); return nInfo; } @@ -2642,7 +2631,7 @@ } /* This can be overruled by not using the _MENU_ var/macro in the language variable */ - var sName = (oSettings.sTableId === "") ? "" : 'name="'+oSettings.sTableId+'_length"'; + var sName = 'name="'+oSettings.sTableId+'_length"'; var sStdMenu = ''; var nLength = document.createElement( 'div' ); - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.l == "undefined" ) + if ( typeof oSettings.aanFeatures.l == "undefined" ) { - nLength.setAttribute( 'id', oSettings.sTableId+'_length' ); + nLength.id = oSettings.sTableId+'_length'; } nLength.className = oSettings.oClasses.sLength; nLength.innerHTML = ''; @@ -2677,7 +2666,7 @@ * Set the length to the current display length - thanks to Andrea Pavlovic for this fix, * and Stefan Skopnik for fixing the fix! */ - $('select option[value="'+oSettings._iDisplayLength+'"]',nLength).attr("selected",true); + $('select option[value="'+oSettings._iDisplayLength+'"]', nLength).attr("selected", true); $('select', nLength).bind( 'change.DT', function(e) { var iVal = $(this).val(); @@ -2715,10 +2704,7 @@ } ); - if ( oSettings.sTableId ) - { - $('select', nLength).attr('aria-controls', oSettings.sTableId); - } + $('select', nLength).attr('aria-controls', oSettings.sTableId); return nLength; } @@ -2874,9 +2860,9 @@ { var nProcessing = document.createElement( 'div' ); - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.r == "undefined" ) + if ( typeof oSettings.aanFeatures.r == "undefined" ) { - nProcessing.setAttribute( 'id', oSettings.sTableId+'_processing' ); + nProcessing.id = oSettings.sTableId+'_processing'; } nProcessing.innerHTML = oSettings.oLanguage.sProcessing; nProcessing.className = oSettings.oClasses.sProcessing; @@ -4622,9 +4608,7 @@ */ function _fnLog( oSettings, iLevel, sMesg ) { - var sAlert = oSettings.sTableId === "" ? - "DataTables warning: " +sMesg : - "DataTables warning (table id = '"+oSettings.sTableId+"'): " +sMesg; + var sAlert = "DataTables warning (table id = '"+oSettings.sTableId+"'): "+sMesg; if ( iLevel === 0 ) { @@ -5999,13 +5983,20 @@ * instance by simply deleting it. This is under the assumption that the table has been * destroyed by other methods. Anyone using non-id selectors will need to do this manually */ - if ( DataTable.settings[i].sTableId !== "" && DataTable.settings[i].sTableId == this.getAttribute('id') ) + if ( DataTable.settings[i].sTableId == this.id ) { DataTable.settings.splice( i, 1 ); break; } } + /* Ensure the table has an ID - required for accessibility */ + if ( sId === null ) + { + sId = "DataTables_Table_"+(DataTable.ext._oExternConfig.iNextUnique++); + this.id = sId; + } + /* Create the settings object for this table and set some of the default parameters */ var oSettings = $.extend( true, {}, DataTable.models.oSettings, { "nTable": this, @@ -6013,7 +6004,7 @@ "oInit": oInit, "oInstance": (_that.length===1) ? _that : $(this).dataTable(), "sDestroyWidth": $(this).width(), - "sInstance": (sId!==null) ? sId : DataTable.ext._oExternConfig.iNextUnique++, + "sInstance": sId, "sTableId": sId } ); DataTable.settings.push( oSettings ); @@ -6147,6 +6138,7 @@ { oSettings.oClasses = DataTable.ext.oStdClasses; } + $(this).addClass( oSettings.oClasses.sTable ); /* Calculate the scroll bar width and cache it for use later on */ if ( oSettings.oScroll.sX !== "" || oSettings.oScroll.sY !== "" ) @@ -9795,6 +9787,8 @@ DataTable.ext = $.extend( true, {}, DataTable.models.ext ); $.extend( DataTable.ext.oStdClasses, { + "sTable": "dataTable", + /* Two buttons buttons */ "sPagePrevEnabled": "paginate_enabled_previous", "sPagePrevDisabled": "paginate_disabled_previous", @@ -9857,6 +9851,8 @@ $.extend( DataTable.ext.oJUIClasses, { + "sTable": "dataTable", + /* Two buttons buttons */ "sPagePrevEnabled": "fg-button ui-button ui-state-default ui-corner-left", "sPagePrevDisabled": "fg-button ui-button ui-state-default ui-corner-left ui-state-disabled", @@ -9978,7 +9974,7 @@ .bind( 'selectstart.DT', function () { return false; } ); /* ID the first elements only */ - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.p == "undefined" ) + if ( typeof oSettings.aanFeatures.p == "undefined" ) { nPaging.id = oSettings.sTableId+'_paginate'; nPrevious.id = oSettings.sTableId+'_previous'; @@ -10078,7 +10074,7 @@ .bind( 'selectstart.DT', function () { return false; } ); /* ID the first elements only */ - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.p == "undefined" ) + if ( typeof oSettings.aanFeatures.p == "undefined" ) { nPaging.id = oSettings.sTableId+'_paginate'; nFirst.id =oSettings.sTableId+'_first'; diff --git a/media/src/core/core.constructor.js b/media/src/core/core.constructor.js index daae1af2..f5262894 100644 --- a/media/src/core/core.constructor.js +++ b/media/src/core/core.constructor.js @@ -45,13 +45,20 @@ for ( i=0, iLen=DataTable.settings.length ; i'; - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.f == "undefined" ) + if ( typeof oSettings.aanFeatures.f == "undefined" ) { - nFilter.setAttribute( 'id', oSettings.sTableId+'_filter' ); + nFilter.id = oSettings.sTableId+'_filter'; } var jqFilter = $("input", nFilter); @@ -45,18 +45,16 @@ function _fnFeatureHtmlFilter ( oSettings ) } } ); - if ( oSettings.sTableId ) - { - jqFilter.attr('aria-controls', oSettings.sTableId); - } - - jqFilter.bind( 'keypress.DT', function(e) { - /* Prevent default */ - if ( e.keyCode == 13 ) - { - return false; + jqFilter + .attr('aria-controls', oSettings.sTableId) + .bind( 'keypress.DT', function(e) { + /* Prevent form submission */ + if ( e.keyCode == 13 ) + { + return false; + } } - } ); + ); return nFilter; } diff --git a/media/src/core/core.info.js b/media/src/core/core.info.js index cbe67af8..a160b4d9 100644 --- a/media/src/core/core.info.js +++ b/media/src/core/core.info.js @@ -21,12 +21,9 @@ function _fnFeatureHtmlInfo ( oSettings ) } ); /* Add id */ - if ( oSettings.sTableId !== '' ) - { - nInfo.setAttribute( 'id', oSettings.sTableId+'_info' ); - oSettings.nTable.setAttribute( 'aria-describedby', oSettings.sTableId+'_info' ); - } + nInfo.id = oSettings.sTableId+'_info'; } + oSettings.nTable.setAttribute( 'aria-describedby', oSettings.sTableId+'_info' ); return nInfo; } diff --git a/media/src/core/core.length.js b/media/src/core/core.length.js index b2d17d06..fee5ad52 100644 --- a/media/src/core/core.length.js +++ b/media/src/core/core.length.js @@ -14,7 +14,7 @@ function _fnFeatureHtmlLength ( oSettings ) } /* This can be overruled by not using the _MENU_ var/macro in the language variable */ - var sName = (oSettings.sTableId === "") ? "" : 'name="'+oSettings.sTableId+'_length"'; + var sName = 'name="'+oSettings.sTableId+'_length"'; var sStdMenu = ''; var nLength = document.createElement( 'div' ); - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.l == "undefined" ) + if ( typeof oSettings.aanFeatures.l == "undefined" ) { - nLength.setAttribute( 'id', oSettings.sTableId+'_length' ); + nLength.id = oSettings.sTableId+'_length'; } nLength.className = oSettings.oClasses.sLength; nLength.innerHTML = ''; @@ -49,7 +49,7 @@ function _fnFeatureHtmlLength ( oSettings ) * Set the length to the current display length - thanks to Andrea Pavlovic for this fix, * and Stefan Skopnik for fixing the fix! */ - $('select option[value="'+oSettings._iDisplayLength+'"]',nLength).attr("selected",true); + $('select option[value="'+oSettings._iDisplayLength+'"]', nLength).attr("selected", true); $('select', nLength).bind( 'change.DT', function(e) { var iVal = $(this).val(); @@ -87,10 +87,7 @@ function _fnFeatureHtmlLength ( oSettings ) } ); - if ( oSettings.sTableId ) - { - $('select', nLength).attr('aria-controls', oSettings.sTableId); - } + $('select', nLength).attr('aria-controls', oSettings.sTableId); return nLength; } diff --git a/media/src/core/core.processing.js b/media/src/core/core.processing.js index bdfd0989..593ec25e 100644 --- a/media/src/core/core.processing.js +++ b/media/src/core/core.processing.js @@ -10,9 +10,9 @@ function _fnFeatureHtmlProcessing ( oSettings ) { var nProcessing = document.createElement( 'div' ); - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.r == "undefined" ) + if ( typeof oSettings.aanFeatures.r == "undefined" ) { - nProcessing.setAttribute( 'id', oSettings.sTableId+'_processing' ); + nProcessing.id = oSettings.sTableId+'_processing'; } nProcessing.innerHTML = oSettings.oLanguage.sProcessing; nProcessing.className = oSettings.oClasses.sProcessing; diff --git a/media/src/core/core.support.js b/media/src/core/core.support.js index 2c1b6f18..2208dd39 100644 --- a/media/src/core/core.support.js +++ b/media/src/core/core.support.js @@ -133,9 +133,7 @@ function _fnGetTdNodes ( oSettings, iIndividualRow ) */ function _fnLog( oSettings, iLevel, sMesg ) { - var sAlert = oSettings.sTableId === "" ? - "DataTables warning: " +sMesg : - "DataTables warning (table id = '"+oSettings.sTableId+"'): " +sMesg; + var sAlert = "DataTables warning (table id = '"+oSettings.sTableId+"'): "+sMesg; if ( iLevel === 0 ) { diff --git a/media/src/ext/ext.classes.js b/media/src/ext/ext.classes.js index 1e88d106..17d9b37e 100644 --- a/media/src/ext/ext.classes.js +++ b/media/src/ext/ext.classes.js @@ -1,5 +1,7 @@ $.extend( DataTable.ext.oStdClasses, { + "sTable": "dataTable", + /* Two buttons buttons */ "sPagePrevEnabled": "paginate_enabled_previous", "sPagePrevDisabled": "paginate_disabled_previous", @@ -62,6 +64,8 @@ $.extend( DataTable.ext.oStdClasses, { $.extend( DataTable.ext.oJUIClasses, { + "sTable": "dataTable", + /* Two buttons buttons */ "sPagePrevEnabled": "fg-button ui-button ui-state-default ui-corner-left", "sPagePrevDisabled": "fg-button ui-button ui-state-default ui-corner-left ui-state-disabled", diff --git a/media/src/ext/ext.paging.js b/media/src/ext/ext.paging.js index b371ef86..48285ff8 100644 --- a/media/src/ext/ext.paging.js +++ b/media/src/ext/ext.paging.js @@ -58,7 +58,7 @@ $.extend( DataTable.ext.oPagination, { .bind( 'selectstart.DT', function () { return false; } ); /* ID the first elements only */ - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.p == "undefined" ) + if ( typeof oSettings.aanFeatures.p == "undefined" ) { nPaging.id = oSettings.sTableId+'_paginate'; nPrevious.id = oSettings.sTableId+'_previous'; @@ -158,7 +158,7 @@ $.extend( DataTable.ext.oPagination, { .bind( 'selectstart.DT', function () { return false; } ); /* ID the first elements only */ - if ( oSettings.sTableId !== '' && typeof oSettings.aanFeatures.p == "undefined" ) + if ( typeof oSettings.aanFeatures.p == "undefined" ) { nPaging.id = oSettings.sTableId+'_paginate'; nFirst.id =oSettings.sTableId+'_first';