From c53b82bab791bbc0c77c4c4a920899b20646eef6 Mon Sep 17 00:00:00 2001 From: dtbuild Date: Wed, 8 Jan 2025 17:55:23 +0000 Subject: [PATCH] 02bdbb893b6b0dc34983ebaf6796b06b47dd18ec Tests: Wasn't properly doing a destroy action when there were multiple tables! c23b0484f8dcdd99930365dacd061940ab98f182 Fix: The automatic resize handling wouldn't allow the content to get smaller. The fix took a lot longer than what the title suggests, but it basically boils down to the fact that when you call `.observe()` it will immediately trigger a callback (if the element is visible!) which I very much did not expect. Accounting for that allows things to work as expected. Sync to source repo @c23b0484f8dcdd99930365dacd061940ab98f182 --- datatables.json | 2 +- js/dataTables.js | 68 +++++++++++++++++++++++++------------------ js/dataTables.min.js | 2 +- js/dataTables.min.mjs | 2 +- js/dataTables.mjs | 68 +++++++++++++++++++++++++------------------ 5 files changed, 81 insertions(+), 61 deletions(-) diff --git a/datatables.json b/datatables.json index b377068..7b1d03a 100644 --- a/datatables.json +++ b/datatables.json @@ -11,5 +11,5 @@ ], "src-repo": "http://github.com/DataTables/DataTablesSrc", "last-tag": "2.2.0", - "last-sync": "4652edd0ac3e010443a6ec392c94a62d6e424cb5" + "last-sync": "c23b0484f8dcdd99930365dacd061940ab98f182" } \ No newline at end of file diff --git a/js/dataTables.js b/js/dataTables.js index dc7e218..b43dfb7 100644 --- a/js/dataTables.js +++ b/js/dataTables.js @@ -5350,6 +5350,14 @@ i, column, columnIdx; var styleWidth = table.style.width; + var containerWidth = _fnWrapperWidth(settings); + + // Don't re-run for the same width as the last time + if (containerWidth === settings.containerWidth) { + return false; + } + + settings.containerWidth = containerWidth; // If there is no width applied as a CSS style or as an attribute, we assume that // the width is intended to be 100%, which is usually is in CSS, but it is very @@ -5518,46 +5526,33 @@ } if ( (tableWidthAttr || scrollX) && ! settings._reszEvt ) { - var wrapperWidth = function () { - return $(settings.nTableWrapper).is(':visible') - ? $(settings.nTableWrapper).width() - : 0; - } - - settings.containerWidth = wrapperWidth(); - var resize = DataTable.util.throttle( function () { - var newWidth = wrapperWidth(); + var newWidth = _fnWrapperWidth(settings); - // Don't do it if destroying, is the same size as last time, or the container - // width is 0 - if ( - ! settings.bDestroying && - settings.containerWidth !== newWidth && - newWidth !== 0 - ) { - // Do a resize + // Don't do it if destroying or the container width is 0 + if (! settings.bDestroying && newWidth !== 0) { _fnAdjustColumnSizing( settings ); - settings.containerWidth = newWidth; } } ); // For browsers that support it (~2020 onwards for wide support) we can watch for the // container changing width. if (window.ResizeObserver) { - settings.resizeObserver = new ResizeObserver(function (e) { - var box = e[0].contentBoxSize; - var size = Array.isArray(box) - ? box[0].inlineSize // Spec - : box.inlineSize; // Old Firefox + // This is a tricky beast - if the element is visible when `.observe()` is called, + // then the callback is immediately run. Which we don't want. If the element isn't + // visible, then it isn't run, but we want it to run when it is then made visible. + // This flag allows the above to be satisfied. + var first = $(settings.nTableWrapper).is(':visible'); - // Under this condition a resize will trigger its own resize, causing an error. - if (size < settings.containerWidth) { - return; + settings.resizeObserver = new ResizeObserver(function (e) { + if (first) { + first = false; + } + else { + resize(); } - - resize(); }); + settings.resizeObserver.observe(settings.nTableWrapper); } else { @@ -5569,6 +5564,17 @@ } } + /** + * Get the width of the DataTables wrapper element + * + * @param {*} settings DataTables settings object + * @returns Width + */ + function _fnWrapperWidth(settings) { + return $(settings.nTableWrapper).is(':visible') + ? $(settings.nTableWrapper).width() + : 0; + } /** * Get the maximum strlen for each data column @@ -8931,6 +8937,10 @@ _api_register( 'columns.adjust()', function () { return this.iterator( 'table', function ( settings ) { + // Force a column sizing to happen with a manual call - otherwise it can skip + // if the size hasn't changed + settings.containerWidth = -1; + _fnAdjustColumnSizing( settings ); }, 1 ); } ); @@ -12093,7 +12103,7 @@ resizeObserver: null, /** Keep a record of the last size of the container, so we can skip duplicates */ - containerWidth: 0 + containerWidth: -1 }; /** diff --git a/js/dataTables.min.js b/js/dataTables.min.js index 874527d..b1d89fb 100644 --- a/js/dataTables.min.js +++ b/js/dataTables.min.js @@ -1,4 +1,4 @@ /*! DataTables 2.2.0 * © SpryMedia Ltd - datatables.net/license */ -!function(n){"use strict";var r;"function"==typeof define&&define.amd?define(["jquery"],function(e){return n(e,window,document)}):"object"==typeof exports?(r=require("jquery"),"undefined"==typeof window?module.exports=function(e,t){return e=e||window,t=t||r(e),n(t,e,e.document)}:module.exports=n(r,window,window.document)):window.DataTable=n(jQuery,window,document)}(function(H,W,L){"use strict";function f(e){var t=parseInt(e,10);return!isNaN(t)&&isFinite(e)?t:null}function s(e,t,n,r){var a=typeof e,o="string"==a;return"number"==a||"bigint"==a||!(!r||!T(e))||(t&&o&&(e=E(e,t)),n&&o&&(e=e.replace(P,"")),!isNaN(parseFloat(e))&&isFinite(e))}function c(e,t,n,r){var a;return!(!r||!T(e))||("string"!=typeof e||!e.match(/<(input|select)/i))&&(T(a=e)||"string"==typeof a)&&!!s(A(e),t,n,r)||null}function m(e,t,n,r){var a=[],o=0,i=t.length;if(void 0!==r)for(;o").prependTo(this),fastData:function(e,t,n){return q(c,e,t,n)}}),n=(c.nTable=this,c.oInit=e,o.push(c),c.api=new X(c),c.oInstance=1===E.length?E:a.dataTable(),Q(e),e.aLengthMenu&&!e.iDisplayLength&&(e.iDisplayLength=Array.isArray(e.aLengthMenu[0])?e.aLengthMenu[0][0]:H.isPlainObject(e.aLengthMenu[0])?e.aLengthMenu[0].value:e.aLengthMenu[0]),e=et(H.extend(!0,{},r),e),$(c.oFeatures,e,["bPaginate","bLengthChange","bFilter","bSort","bSortMulti","bInfo","bProcessing","bAutoWidth","bSortClasses","bServerSide","bDeferRender"]),$(c,e,["ajax","fnFormatNumber","sServerMethod","aaSorting","aaSortingFixed","aLengthMenu","sPaginationType","iStateDuration","bSortCellsTop","iTabIndex","sDom","fnStateLoadCallback","fnStateSaveCallback","renderer","searchDelay","rowId","caption","layout","orderDescReverse","typeDetect",["iCookieDuration","iStateDuration"],["oSearch","oPreviousSearch"],["aoSearchCols","aoPreSearchCols"],["iDisplayLength","_iDisplayLength"]]),$(c.oScroll,e,[["sScrollX","sX"],["sScrollXInner","sXInner"],["sScrollY","sY"],["bScrollCollapse","bCollapse"]]),$(c.oLanguage,e,"fnInfoCallback"),Y(c,"aoDrawCallback",e.fnDrawCallback),Y(c,"aoStateSaveParams",e.fnStateSaveParams),Y(c,"aoStateLoadParams",e.fnStateLoadParams),Y(c,"aoStateLoaded",e.fnStateLoaded),Y(c,"aoRowCallback",e.fnRowCallback),Y(c,"aoRowCreatedCallback",e.fnCreatedRow),Y(c,"aoHeaderCallback",e.fnHeaderCallback),Y(c,"aoFooterCallback",e.fnFooterCallback),Y(c,"aoInitComplete",e.fnInitComplete),Y(c,"aoPreDrawCallback",e.fnPreDrawCallback),c.rowIdFn=U(e.rowId),c),d=(B.__browser||(f={},B.__browser=f,p=H("
").css({position:"fixed",top:0,left:-1*W.pageXOffset,height:1,width:1,overflow:"hidden"}).append(H("
").css({position:"absolute",top:1,left:1,width:100,overflow:"scroll"}).append(H("
").css({width:"100%",height:10}))).appendTo("body"),d=p.children(),u=d.children(),f.barWidth=d[0].offsetWidth-d[0].clientWidth,f.bScrollbarLeft=1!==Math.round(u.offset().left),p.remove()),H.extend(n.oBrowser,B.__browser),n.oScroll.iBarWidth=B.__browser.barWidth,c.oClasses),f=(H.extend(d,B.ext.classes,e.oClasses),a.addClass(d.table),c.oFeatures.bPaginate||(e.iDisplayStart=0),void 0===c.iInitDisplayStart&&(c.iInitDisplayStart=e.iDisplayStart,c._iDisplayStart=e.iDisplayStart),e.iDeferLoading),h=(null!==f&&(c.deferLoading=!0,u=Array.isArray(f),c._iRecordsDisplay=u?f[0]:f,c._iRecordsTotal=u?f[1]:f),[]),p=this.getElementsByTagName("thead"),n=Ae(c,p[0]);if(e.aoColumns)h=e.aoColumns;else if(n.length)for(j=n[t=0].length;t").appendTo(a):n).html(c.caption),n.length&&(n[0]._captionSide=n.css("caption-side"),c.captionNode=n[0]),0===p.length&&(p=H("").appendTo(a)),c.nTHead=p[0],a.children("tbody")),n=(0===n.length&&(n=H("").insertAfter(p)),c.nTBody=n[0],a.children("tfoot")),R=(0===n.length&&(n=H("").appendTo(a)),c.nTFoot=n[0],c.aiDisplay=c.aiDisplayMaster.slice(),c.bInitialised=!0,c.oLanguage);H.extend(!0,R,e.oLanguage),R.sUrl?H.ajax({dataType:"json",url:R.sUrl,success:function(e){V(r.oLanguage,e),H.extend(!0,R,e,c.oInit.oLanguage),G(c,null,"i18n",[c],!0),Me(c)},error:function(){z(c,0,"i18n file loading error",21),Me(c)}}):(G(c,null,"i18n",[c],!0),Me(c))}}),E=null,this)},g=(B.ext=I={buttons:{},classes:{},builder:"-source-",errMode:"alert",feature:[],features:{},search:[],selector:{cell:[],column:[],row:[]},legacy:{ajax:null},pager:{},renderer:{pageButton:{},header:{}},order:{},type:{className:{},detect:[],render:{},search:{},order:{}},_unique:0,fnVersionCheck:B.fnVersionCheck,iApiIndex:0,sVersion:B.version},H.extend(I,{afnFiltering:I.search,aTypes:I.type.detect,ofnSearch:I.type.search,oSort:I.type.order,afnSortData:I.order,aoFeatures:I.feature,oStdClasses:I.classes,oPagination:I.pager}),H.extend(B.ext.classes,{container:"dt-container",empty:{row:"dt-empty"},info:{container:"dt-info"},layout:{row:"dt-layout-row",cell:"dt-layout-cell",tableRow:"dt-layout-table",tableCell:"",start:"dt-layout-start",end:"dt-layout-end",full:"dt-layout-full"},length:{container:"dt-length",select:"dt-input"},order:{canAsc:"dt-orderable-asc",canDesc:"dt-orderable-desc",isAsc:"dt-ordering-asc",isDesc:"dt-ordering-desc",none:"dt-orderable-none",position:"sorting_"},processing:{container:"dt-processing"},scrolling:{body:"dt-scroll-body",container:"dt-scroll",footer:{self:"dt-scroll-foot",inner:"dt-scroll-footInner"},header:{self:"dt-scroll-head",inner:"dt-scroll-headInner"}},search:{container:"dt-search",input:"dt-input"},table:"dataTable",tbody:{cell:"",row:""},thead:{cell:"",row:""},tfoot:{cell:"",row:""},paging:{active:"current",button:"dt-paging-button",container:"dt-paging",disabled:"disabled",nav:""}}),{}),C=/[\r\n\u2028]/g,F=/<([^>]*>)/g,O=Math.pow(2,28),j=/^\d{2,4}[./-]\d{1,2}[./-]\d{1,2}([T ]{1}\d{1,2}[:.]\d{2}([.:]\d{2})?)?$/,R=new RegExp("(\\"+["/",".","*","+","?","|","(",")","[","]","{","}","\\","$","^","-"].join("|\\")+")","g"),P=/['\u00A0,$£€¥%\u2009\u202F\u20BD\u20a9\u20BArfkɃΞ]/gi,T=function(e){return!e||!0===e||"-"===e},E=function(e,t){return g[t]||(g[t]=new RegExp(Pe(t),"g")),"string"==typeof e&&"."!==t?e.replace(/\./g,"").replace(g[t],"."):e},b=function(e,t,n){var r=[],a=0,o=e.length;if(void 0!==n)for(;aO)throw new Error("Exceeded max str len");var t;for(e=e.replace(F,"");(e=(t=e).replace(/