@toolbox-web/grid 1.16.0 → 1.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/umd/grid.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(_,V){typeof exports=="object"&&typeof module<"u"?V(exports):typeof define=="function"&&define.amd?define(["exports"],V):(_=typeof globalThis<"u"?globalThis:_||self,V(_.TbwGrid={}))})(this,(function(_){"use strict";function V(){return{rowCount:-1,colCount:-1,ariaLabel:void 0,ariaDescribedBy:void 0}}function pt(t,e,i,o,n){if(o===t.rowCount&&n===t.colCount)return!1;const r=t.rowCount;return t.rowCount=o,t.colCount=n,e&&(e.setAttribute("aria-rowcount",String(o)),e.setAttribute("aria-colcount",String(n))),o!==r&&i&&(o>0?i.setAttribute("role","rowgroup"):i.removeAttribute("role")),!0}function wt(t,e){const i=t?.gridAriaLabel;return i||(t?.shell?.header?.title??e?.lightDomTitle??void 0)}function bt(t,e,i,o){if(!e)return!1;let n=!1;const r=wt(i,o);r!==t.ariaLabel&&(t.ariaLabel=r,r?e.setAttribute("aria-label",r):e.removeAttribute("aria-label"),n=!0);const s=i?.gridAriaDescribedBy;return s!==t.ariaDescribedBy&&(t.ariaDescribedBy=s,s?e.setAttribute("aria-describedby",s):e.removeAttribute("aria-describedby"),n=!0),n}const W={STRETCH:"stretch",FIXED:"fixed"},Re={mode:"reduced-motion",duration:200,easing:"ease-out"},Ae='<svg viewBox="0 0 16 16" width="12" height="12"><path fill="currentColor" d="M6 10.5a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/></svg>',k={expand:"▶",collapse:"▼",sortAsc:"▲",sortDesc:"▼",sortNone:"⇅",submenuArrow:"▶",dragHandle:"⋮⋮",toolPanel:"☰",filter:Ae,filterActive:Ae,print:"🖨️"};function mt(t){return Array.from(t.querySelectorAll("tbw-grid-column")).map(i=>{const o=i.getAttribute("field")||"";if(!o)return null;const n=i.getAttribute("type")||void 0,s=n&&new Set(["number","string","date","boolean","select"]).has(n)?n:void 0,l=i.getAttribute("header")||void 0,a=i.hasAttribute("sortable"),c=i.hasAttribute("editable"),h={field:o,type:s,header:l,sortable:a,editable:c},f=i.getAttribute("width");if(f){const m=parseFloat(f);!isNaN(m)&&/^\d+(\.\d+)?$/.test(f.trim())?h.width=m:h.width=f}const p=i.getAttribute("minWidth")||i.getAttribute("min-width");if(p){const m=parseFloat(p);isNaN(m)||(h.minWidth=m)}i.hasAttribute("resizable")&&(h.resizable=!0),i.hasAttribute("sizable")&&(h.resizable=!0);const g=i.getAttribute("editor"),u=i.getAttribute("renderer");g&&(h.__editorName=g),u&&(h.__rendererName=u);const d=i.getAttribute("options");d&&(h.options=d.split(",").map(m=>{const[L,$]=m.includes(":")?m.split(":"):[m.trim(),m.trim()];return{value:L.trim(),label:$?.trim()||L.trim()}}));const w=i.querySelector("tbw-grid-column-view"),v=i.querySelector("tbw-grid-column-editor"),b=i.querySelector("tbw-grid-column-header");w&&(h.__viewTemplate=w),v&&(h.__editorTemplate=v),b&&(h.__headerTemplate=b);const T=globalThis.DataGridElement?.getAdapters?.()??[],S=w??i,H=T.find(m=>m.canHandle(S));if(H){const m=H.createRenderer(S);m&&(h.viewRenderer=m)}const C=v??i,y=T.find(m=>m.canHandle(C));if(y){const m=y.createEditor(C);m&&(h.editor=m)}return h}).filter(i=>!!i)}function Te(t,e){if((!t||!t.length)&&(!e||!e.length))return[];if(!t||!t.length)return e||[];if(!e||!e.length)return t;const i={};e.forEach(n=>{const r=i[n.field];if(r){n.header&&!r.header&&(r.header=n.header),n.type&&!r.type&&(r.type=n.type),n.sortable&&(r.sortable=!0),n.editable&&(r.editable=!0),n.resizable&&(r.resizable=!0),n.width!=null&&r.width==null&&(r.width=n.width),n.minWidth!=null&&r.minWidth==null&&(r.minWidth=n.minWidth),n.__viewTemplate&&(r.__viewTemplate=n.__viewTemplate),n.__editorTemplate&&(r.__editorTemplate=n.__editorTemplate),n.__headerTemplate&&(r.__headerTemplate=n.__headerTemplate);const s=n.renderer||n.viewRenderer,l=r.renderer||r.viewRenderer;s&&!l&&(r.viewRenderer=s,n.renderer&&(r.renderer=s)),n.editor&&!r.editor&&(r.editor=n.editor)}else i[n.field]={...n}});const o=t.map(n=>{const r=i[n.field];if(!r)return n;const s={...n};r.header&&!s.header&&(s.header=r.header),r.type&&!s.type&&(s.type=r.type),s.sortable=n.sortable||r.sortable,(n.resizable===!0||r.resizable===!0)&&(s.resizable=!0),s.editable=n.editable||r.editable,r.width!=null&&s.width==null&&(s.width=r.width),r.minWidth!=null&&s.minWidth==null&&(s.minWidth=r.minWidth),r.__viewTemplate&&(s.__viewTemplate=r.__viewTemplate),r.__editorTemplate&&(s.__editorTemplate=r.__editorTemplate),r.__headerTemplate&&(s.__headerTemplate=r.__headerTemplate);const l=r.renderer||r.viewRenderer,a=s.renderer||s.viewRenderer;return l&&!a&&(s.viewRenderer=l,r.renderer&&(s.renderer=l)),r.editor&&!s.editor&&(s.editor=r.editor),delete i[n.field],s});return Object.keys(i).forEach(n=>o.push(i[n])),o}function xe(t,e){try{t.part?.add?.(e)}catch{}const i=t.getAttribute("part");i?i.split(/\s+/).includes(e)||t.setAttribute("part",i+" "+e):t.setAttribute("part",e)}function He(t){const e=t.effectiveConfig?.fitMode||t.fitMode||W.STRETCH;if(e!==W.STRETCH&&e!==W.FIXED||t.__didInitialAutoSize||!t.isConnected)return;const i=Array.from(t._headerRowEl?.children||[]);if(!i.length)return;let o=!1;t._visibleColumns.forEach((n,r)=>{if(n.width)return;const s=i[r];let l=s?s.scrollWidth:0;for(const a of t._rowPool){const c=a.children[r];if(c){const h=c.scrollWidth;h>l&&(l=h)}}l>0&&(n.width=l+2,n.__autoSized=!0,o=!0)}),o&&G(t),t.__didInitialAutoSize=!0}const vt=/^(?:\d+(?:\.\d+)?(?:px|%|fr|em|rem|ch|vw|vh|vmin|vmax)|calc\(.+\)|min-content|max-content|minmax\(.+\)|fit-content\(.+\)|auto)$/i;function Le(t,e){return typeof t=="number"?`${t}px`:(vt.test(t)||console.warn(`[tbw-grid] Column '${e??"?"}' has an invalid CSS width value: '${t}'. Expected a number (px) or a valid CSS unit string (e.g. '30%', '2fr', 'calc(...)').`),t)}function G(t){(t.effectiveConfig?.fitMode||t.fitMode||W.STRETCH)===W.STRETCH?t._gridTemplate=t._visibleColumns.map(i=>{if(i.width!=null)return Le(i.width,i.field);const o=i.minWidth;return o!=null?`minmax(${o}px, 1fr)`:"1fr"}).join(" ").trim():t._gridTemplate=t._visibleColumns.map(i=>i.width!=null?Le(i.width,i.field):"max-content").join(" "),t.style.setProperty("--tbw-column-template",t._gridTemplate)}function Ct(t){return t==null?"string":typeof t=="number"?"number":typeof t=="boolean"?"boolean":t instanceof Date||typeof t=="string"&&/\d{4}-\d{2}-\d{2}/.test(t)&&!isNaN(Date.parse(t))?"date":"string"}function yt(t,e){const i=t[0]||{},o=Object.keys(i).map(r=>{const s=i[r],l=Ct(s);return{field:r,header:r.charAt(0).toUpperCase()+r.slice(1),type:l}}),n={};return o.forEach(r=>{n[r.field]=r.type||"string"}),{columns:o,typeMap:n}}const _t=/{{\s*([^}]+)\s*}}/g,D="__DG_EMPTY__",Et=/^[\w$. '?+\-*/%:()!<>=,&|]+$/,St=/__(proto|defineGetter|defineSetter)|constructor|window|globalThis|global|process|Function|import|eval|Reflect|Proxy|Error|arguments|document|location|cookie|localStorage|sessionStorage|indexedDB|fetch|XMLHttpRequest|WebSocket|Worker|SharedWorker|ServiceWorker|opener|parent|top|frames|self|this\b/;function Rt(t){return!t||typeof t!="string"?"":t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;")}const At=new Set(["script","iframe","object","embed","form","input","button","textarea","select","link","meta","base","style","template","slot","portal","frame","frameset","applet","noscript","noembed","plaintext","xmp","listing"]),Pe=/^on\w+$/i,Tt=new Set(["href","src","action","formaction","data","srcdoc","xlink:href","poster","srcset"]),xt=/^\s*(javascript|vbscript|data|blob):/i;function F(t){if(!t||typeof t!="string")return"";if(t.indexOf("<")===-1)return t;const e=document.createElement("template");return e.innerHTML=t,Ht(e.content),e.innerHTML}function Ht(t){const e=[],i=t.querySelectorAll("*");for(const o of i){const n=o.tagName.toLowerCase();if(At.has(n)){e.push(o);continue}if((n==="svg"||o.namespaceURI==="http://www.w3.org/2000/svg")&&Array.from(o.attributes).some(l=>Pe.test(l.name)||l.name==="href"||l.name==="xlink:href")){e.push(o);continue}const r=[];for(const s of o.attributes){const l=s.name.toLowerCase();if(Pe.test(l)){r.push(s.name);continue}if(Tt.has(l)&&xt.test(s.value)){r.push(s.name);continue}if(l==="style"&&/expression\s*\(|javascript:|behavior\s*:/i.test(s.value)){r.push(s.name);continue}}r.forEach(s=>o.removeAttribute(s))}e.forEach(o=>o.remove())}function ze(t,e){if(!t||t.indexOf("{{")===-1)return t;const i=[],o=t.replace(_t,(l,a)=>{const c=Lt(a,e);return i.push({expr:a.trim(),result:c}),c}),n=Pt(o),r=i.length&&i.every(l=>l.result===""||l.result===D);return N.test(t)||r?"":n}function Lt(t,e){if(t=(t||"").trim(),!t||N.test(t))return D;if(t==="value")return e.value==null?D:String(e.value);if(t.startsWith("row.")&&!/[()?]/.test(t)&&!t.includes(":")){const o=t.slice(4),n=e.row?e.row[o]:void 0;return n==null?D:String(n)}if(t.length>80||!Et.test(t)||St.test(t))return D;const i=t.match(/\./g);if(i&&i.length>1)return D;try{const n=new Function("value","row",`return (${t});`)(e.value,e.row),r=n==null?"":String(n);return N.test(r)?D:r||D}catch{return D}}const N=/Reflect|Proxy|ownKeys/;function Pt(t){return t&&t.replace(new RegExp(D,"g"),"").replace(/Reflect\.[^<>{}\s]+|\bProxy\b|ownKeys\([^)]*\)/g,"")}function zt(t){if(N.test(t.textContent||"")){for(const e of t.childNodes)e.nodeType===Node.TEXT_NODE&&N.test(e.textContent||"")&&(e.textContent="");N.test(t.textContent||"")&&(t.textContent="")}}function De(t){const e=N.test(t),i=(o=>e?"":ze(t,o));return i.__blocked=e,i}const Dt=100;class Ot{#l;#n;#u;#d;#h;#a={};#o={};#f=!0;#w=[];#v;#s;#C;#r;#g;constructor(e){this.#r=e}get original(){return this.#a}get effective(){return this.#o}get columns(){return this.#o.columns??[]}set columns(e){this.#o.columns=e}get lightDomColumnsCache(){return this.#d}set lightDomColumnsCache(e){this.#d=e}get originalColumnNodes(){return this.#h}set originalColumnNodes(e){this.#h=e}get lightDomTitle(){return this.#g}set lightDomTitle(e){this.#g=e}get initialColumnState(){return this.#C}set initialColumnState(e){this.#C=e}get sourcesChanged(){return this.#f}markSourcesChanged(){this.#f=!0}setGridConfig(e){this.#l=e,this.#f=!0,this.#d=void 0}getGridConfig(){return this.#l}setColumns(e){this.#n=e,this.#f=!0}getColumns(){return this.#n}setFitMode(e){this.#u=e,this.#f=!0}getFitMode(){return this.#u}merge(){const e=(this.#o.columns?.length??0)>0;if(!this.#f&&e)return;const i=this.#I();this.#f=!1,this.#a=i,Object.freeze(this.#a),this.#a.columns&&Object.freeze(this.#a.columns),this.#o=this.#x(this.#a),this.#m()}#x(e){const i={...e};return e.columns&&(i.columns=e.columns.map(o=>({...o}))),e.shell&&(i.shell={...e.shell,header:e.shell.header?{...e.shell.header}:void 0,toolPanel:e.shell.toolPanel?{...e.shell.toolPanel}:void 0,toolPanels:e.shell.toolPanels?.map(o=>({...o})),headerContents:e.shell.headerContents?.map(o=>({...o}))}),i}#m(){const e=this.#o;this.#z(),typeof e.rowHeight=="number"&&e.rowHeight>0&&this.#r.setRowHeight(e.rowHeight),e.fitMode==="fixed"&&this.columns.forEach(o=>{o.width==null&&(o.width=80)}),this.#r.applyAnimationConfig(e)}#z(){const e=this.#o.typeDefaults;if(!e)return;const i=this.columns;for(const o of i){if(!o.type)continue;const n=e[o.type];n&&(!o.renderer&&!o.viewRenderer&&n.renderer&&(o.renderer=n.renderer),!o.format&&n.format&&(o.format=n.format),!o.editor&&n.editor&&(o.editor=n.editor),!o.editorParams&&n.editorParams&&(o.editorParams=n.editorParams))}}#I(){const e=this.#l?{...this.#l}:{},i=Array.isArray(e.columns)?[...e.columns]:[],o=(this.#d??[]).map(s=>({...s}));let n=Te(i,o);this.#n&&this.#n.length&&(n=Te(this.#n,o));const r=this.#r.getRows();return n.length===0&&r.length&&(n=yt(r).columns),n.length&&(n.forEach(s=>{s.sortable===void 0&&(s.sortable=!0),s.resizable===void 0&&(s.resizable=!0),s.__originalWidth===void 0&&typeof s.width=="number"&&(s.__originalWidth=s.width)}),n.forEach(s=>{s.__viewTemplate&&!s.__compiledView&&(s.__compiledView=De(s.__viewTemplate.innerHTML)),s.__editorTemplate&&!s.__compiledEditor&&(s.__compiledEditor=De(s.__editorTemplate.innerHTML))}),e.columns=n),this.#u&&(e.fitMode=this.#u),e.fitMode||(e.fitMode="stretch"),this.#b(e),e.columnState&&!this.#C&&(this.#C=e.columnState),e}#b(e){e.shell=e.shell?{...e.shell}:{},e.shell.header=e.shell.header?{...e.shell.header}:{};const i=this.#r.getShellLightDomTitle();i&&(this.#g=i),this.#g&&!e.shell.header.title&&(e.shell.header.title=this.#g);const o=this.#r.getShellLightDomHeaderContent();o?.length>0&&(e.shell.header.lightDomContent=o),this.#r.getShellHasToolButtonsContainer()&&(e.shell.header.hasToolButtonsContainer=!0);const n=this.#r.getShellToolPanels();if(n.size>0){const f=Array.from(n.values());f.sort((p,g)=>(p.order??100)-(g.order??100)),e.shell.toolPanels=f}const r=this.#r.getShellHeaderContents();if(r.size>0){const f=Array.from(r.values());f.sort((p,g)=>(p.order??100)-(g.order??100)),e.shell.headerContents=f}const s=this.#r.getShellToolbarContents(),l=Array.from(s.values()),a=this.#l?.shell?.header?.toolbarContents??[],c=new Set(a.map(f=>f.id)),h=[...a];for(const f of l)c.has(f.id)||h.push(f);h.sort((f,p)=>(f.order??0)-(p.order??0)),e.shell.header.toolbarContents=h}collectState(e){const i=this.columns,o=this.#y();return{columns:i.map((n,r)=>{const s={field:n.field,order:r,visible:!n.hidden},l=n;l.__renderedWidth!==void 0?s.width=l.__renderedWidth:n.width!==void 0&&(s.width=typeof n.width=="string"?parseFloat(n.width):n.width);const a=o.get(n.field);a&&(s.sort=a);for(const c of e)if(c.getColumnState){const h=c.getColumnState(n.field);h&&Object.assign(s,h)}return s})}}applyState(e,i){if(!e.columns||e.columns.length===0)return;const o=this.columns,n=new Map(e.columns.map(l=>[l.field,l])),r=o.map(l=>{const a=n.get(l.field);if(!a)return l;const c={...l};return a.width!==void 0&&(c.width=a.width,c.__renderedWidth=a.width),a.visible!==void 0&&(c.hidden=!a.visible),c});r.sort((l,a)=>{const c=n.get(l.field)?.order??1/0,h=n.get(a.field)?.order??1/0;return c-h}),this.columns=r;const s=e.columns.filter(l=>l.sort!==void 0).sort((l,a)=>(l.sort?.priority??0)-(a.sort?.priority??0));if(s.length>0){const l=s[0];l.sort&&this.#r.setSortState({field:l.field,direction:l.sort.direction==="asc"?1:-1})}else this.#r.setSortState(null);for(const l of i)if(l.applyColumnState)for(const a of e.columns)l.applyColumnState(a.field,a)}resetState(e){this.#C=void 0,this.#r.setSortState(null),this.#o=this.#x(this.#a),this.#m();for(const i of e)if(i.applyColumnState)for(const o of this.columns)i.applyColumnState(o.field,{field:o.field,order:0,visible:!0});this.requestStateChange(e)}#y(){const e=new Map,i=this.#r.getSortState();return i&&e.set(i.field,{direction:i.direction===1?"asc":"desc",priority:0}),e}requestStateChange(e){this.#s&&clearTimeout(this.#s),this.#s=setTimeout(()=>{this.#s=void 0;const i=this.collectState(e);this.#r.emit("column-state-change",i)},Dt)}setColumnVisible(e,i){const o=this.columns,n=o.find(s=>s.field===e);return!n||!i&&n.lockVisible||!i&&o.filter(l=>!l.hidden&&l.field!==e).length===0||!!n.hidden===!i?!1:(n.hidden=!i,this.#r.emit("column-visibility",{field:e,visible:i,visibleColumns:o.filter(s=>!s.hidden).map(s=>s.field)}),this.#r.clearRowPool(),this.#r.setup(),!0)}toggleColumnVisibility(e){const i=this.columns.find(o=>o.field===e);return i?this.setColumnVisible(e,!!i.hidden):!1}isColumnVisible(e){const i=this.columns.find(o=>o.field===e);return i?!i.hidden:!1}showAllColumns(){const e=this.columns;e.some(i=>i.hidden)&&(e.forEach(i=>i.hidden=!1),this.#r.emit("column-visibility",{visibleColumns:e.map(i=>i.field)}),this.#r.clearRowPool(),this.#r.setup())}getAllColumns(){return this.columns.map(e=>({field:e.field,header:e.header||e.field,visible:!e.hidden,lockVisible:e.lockVisible,utility:e.meta?.utility===!0}))}getColumnOrder(){return this.columns.map(e=>e.field)}setColumnOrder(e){if(!e.length)return;const i=new Map(this.columns.map(n=>[n.field,n])),o=[];for(const n of e){const r=i.get(n);r&&(o.push(r),i.delete(n))}for(const n of i.values())o.push(n);this.columns=o,this.#r.renderHeader(),this.#r.updateTemplate(),this.#r.refreshVirtualWindow()}parseLightDomColumns(e){this.#d||(this.#h=Array.from(e.querySelectorAll("tbw-grid-column")),this.#d=this.#h.length?mt(e):[])}clearLightDomCache(){this.#d=void 0}#p=new Map;registerLightDomHandler(e,i){this.#p.set(e.toLowerCase(),i)}unregisterLightDomHandler(e){this.#p.delete(e.toLowerCase())}observeLightDOM(e){this.#v&&this.#v.disconnect();const i=new Set;let o=null;const n=()=>{o=null;for(const r of i)this.#p.get(r)?.();i.clear()};this.#v=new MutationObserver(r=>{for(const s of r){for(const l of s.addedNodes){if(l.nodeType!==Node.ELEMENT_NODE)continue;const c=l.tagName.toLowerCase();this.#p.has(c)&&i.add(c)}if(s.type==="attributes"&&s.target.nodeType===Node.ELEMENT_NODE){const a=s.target.tagName.toLowerCase();this.#p.has(a)&&i.add(a)}}i.size>0&&!o&&(o=setTimeout(n,0))}),this.#v.observe(e,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["title","field","header","width","hidden","id","icon","tooltip","order"]})}onChange(e){this.#w.push(e)}notifyChange(){for(const e of this.#w)e()}dispose(){this.#v?.disconnect(),this.#w=[],this.#s&&clearTimeout(this.#s)}}function le(){if(typeof window<"u"&&window.location){const t=window.location.hostname;if(t==="localhost"||t==="127.0.0.1"||t==="::1")return!0}return typeof process<"u"&&process.env?.NODE_ENV!=="production"}function Oe(t){return`<span role="checkbox" aria-checked="${t}" aria-label="${t}">${t?"&#x1F5F9;":"&#9744;"}</span>`}function Me(t){if(t==null||t==="")return"";if(t instanceof Date)return isNaN(t.getTime())?"":t.toLocaleDateString();if(typeof t=="number"||typeof t=="string"){const e=new Date(t);return isNaN(e.getTime())?"":e.toLocaleDateString()}return""}function ke(t){if(!t)return-1;const e=t.getAttribute("data-row");if(e)return parseInt(e,10);const i=t.closest(".data-grid-row");if(!i)return-1;const o=i.parentElement;if(!o)return-1;const n=o.querySelectorAll(":scope > .data-grid-row");for(let r=0;r<n.length;r++)if(n[r]===i)return r;return-1}function Mt(t){if(!t)return-1;const e=t.getAttribute("data-col");return e?parseInt(e,10):-1}function ae(t){t&&t.querySelectorAll(".cell-focus").forEach(e=>e.classList.remove("cell-focus"))}function kt(t){try{if(getComputedStyle(t).direction==="rtl")return"rtl"}catch{}try{if(t.closest?.("[dir]")?.getAttribute("dir")==="rtl")return"rtl"}catch{}return"ltr"}function Ie(t){return kt(t)==="rtl"}function Ne(t,e){const i=e.renderer||e.viewRenderer;if(i)return i;if(!e.type)return;const o=t.__frameworkAdapter;if(o?.getTypeDefault){const n=o.getTypeDefault(e.type);if(n?.renderer)return n.renderer}}function qe(t,e){if(e.format)return e.format;if(!e.type)return;const i=t.__frameworkAdapter;if(i?.getTypeDefault){const o=i.getTypeDefault(e.type);if(o?.format)return o.format}}const $e='input,select,textarea,[contenteditable="true"],[contenteditable=""],[tabindex]:not([tabindex="-1"])';function ce(t){return(t.__editingCellCount??0)>0}function de(t){t.__editingCellCount=0,t.removeAttribute("data-has-editing"),t.querySelectorAll(".cell.editing").forEach(i=>i.classList.remove("editing"))}const We=document.createElement("template");We.innerHTML='<div class="cell" role="gridcell" part="cell"></div>';const Fe=document.createElement("template");Fe.innerHTML='<div class="data-grid-row" role="row" part="row"></div>';function It(){return We.content.firstElementChild.cloneNode(!0)}function Nt(){return Fe.content.firstElementChild.cloneNode(!0)}function Q(t){t.__cellDisplayCache=void 0,t.__cellCacheEpoch=void 0,t.__hasSpecialColumns=void 0}function qt(t,e,i,o,n){const r=Math.max(0,i-e),s=t._bodyEl,l=t._visibleColumns,a=l.length;let c=t.__cachedHeaderRowCount;for(c===void 0&&(c=t.querySelector(".header-group-row")?2:1,t.__cachedHeaderRowCount=c);t._rowPool.length<r;){const p=Nt();t._rowPool.push(p)}if(t._rowPool.length>r){for(let p=r;p<t._rowPool.length;p++){const g=t._rowPool[p];g.parentNode===s&&g.remove()}t._rowPool.length=r}const h=n&&t.__hasRenderRowPlugins!==!1,f=t._hasAfterRowRenderHook?.()??!1;for(let p=0;p<r;p++){const g=e+p,u=t._rows[g],d=t._rowPool[p];if(d.setAttribute("aria-rowindex",String(g+c+1)),h&&n(u,d,g)){d.__epoch=o,d.__rowDataRef=u,d.parentNode!==s&&s.appendChild(d);continue}const w=d.__epoch,v=d.__rowDataRef;let b=d.children.length;b>a&&d.lastElementChild?.classList.contains("tbw-row-loading-overlay")&&b--;const T=w===o&&b===a,S=v!==u,H=!!t._isGridEditMode;let C=!1;if(T&&S){for(let R=0;R<a;R++)if(l[R].externalView&&!d.querySelector(`.cell[data-col="${R}"] [data-external-view]`)){C=!0;break}}if(!T||C){const R=ce(d),z=H||t._activeEditRows===g;R&&!z?(d.__isCustomRow&&(d.className="data-grid-row",d.setAttribute("role","row"),d.__isCustomRow=!1),de(d),B(t,d,u,g),d.__epoch=o,d.__rowDataRef=u):R&&z?(he(t,d,u,g),d.__rowDataRef=u):(d.__isCustomRow&&(d.className="data-grid-row",d.setAttribute("role","row"),d.__isCustomRow=!1),B(t,d,u,g),d.__epoch=o,d.__rowDataRef=u)}else if(S){const R=ce(d),z=H||t._activeEditRows===g;R&&!z?(de(d),B(t,d,u,g),d.__epoch=o,d.__rowDataRef=u):(he(t,d,u,g),d.__rowDataRef=u)}else{const R=ce(d),z=H||t._activeEditRows===g;R&&!z?(de(d),B(t,d,u,g),d.__epoch=o,d.__rowDataRef=u):he(t,d,u,g)}let y=!1;const m=t.changedRowIds;if(m&&m.length>0)try{const R=t.getRowId?.(u);R&&(y=m.includes(R))}catch{}const L=d.classList.contains("changed");y!==L&&d.classList.toggle("changed",y);const $=t.effectiveConfig?.rowClass;if($){const R=d.getAttribute("data-dynamic-classes");R&&R.split(" ").forEach(z=>z&&d.classList.remove(z));try{const z=$(u);if(z&&z.length>0){const Se=z.filter(se=>se&&typeof se=="string");Se.forEach(se=>d.classList.add(se)),d.setAttribute("data-dynamic-classes",Se.join(" "))}else d.removeAttribute("data-dynamic-classes")}catch(z){console.warn("[tbw-grid] rowClass callback error:",z),d.removeAttribute("data-dynamic-classes")}}f&&t._afterRowRender?.({row:u,rowIndex:g,rowElement:d}),d.parentNode!==s&&s.appendChild(d)}}function he(t,e,i,o){const n=e.children,r=t._visibleColumns,s=r.length,l=n.length,a=s<l?s:l,c=t._focusRow,h=t._focusCol,f=t._hasAfterCellRenderHook?.()??!1;let p=t.__hasSpecialColumns;if(p===void 0){p=!1;const u=t.__frameworkAdapter;for(let d=0;d<s;d++){const w=r[d];if(w.__viewTemplate||w.__compiledView||w.renderer||w.viewRenderer||w.externalView||w.format||w.type==="date"||w.type==="boolean"||w.type&&u?.getTypeDefault?.(w.type)?.renderer||w.type&&u?.getTypeDefault?.(w.type)?.format){p=!0;break}}t.__hasSpecialColumns=p}const g=String(o);if(!p){for(let u=0;u<a;u++){const d=n[u];if(d.classList.contains("editing"))continue;d.firstElementChild&&t.__frameworkAdapter?.releaseCell?.(d);const w=r[u],v=i[w.field];d.textContent=v==null?"":String(v),d.getAttribute("data-row")!==g&&d.setAttribute("data-row",g);const b=c===o&&h===u,E=d.classList.contains("cell-focus");b!==E&&(d.classList.toggle("cell-focus",b),d.setAttribute("aria-selected",String(b))),f&&t._afterCellRender?.({row:i,rowIndex:o,column:w,colIndex:u,value:v,cellElement:d,rowElement:e})}return}for(let u=0;u<a;u++)if(r[u].externalView&&!n[u].querySelector("[data-external-view]")){B(t,e,i,o);return}for(let u=0;u<a;u++){const d=r[u],w=n[u];w.getAttribute("data-row")!==g&&w.setAttribute("data-row",g);const v=c===o&&h===u,b=w.classList.contains("cell-focus");v!==b&&(w.classList.toggle("cell-focus",v),w.setAttribute("aria-selected",String(v)));const E=d.cellClass;if(E){const y=w.getAttribute("data-dynamic-classes");y&&y.split(" ").forEach(m=>m&&w.classList.remove(m));try{const m=i[d.field],L=E(m,i,d);if(L&&L.length>0){const $=L.filter(R=>R&&typeof R=="string");$.forEach(R=>w.classList.add(R)),w.setAttribute("data-dynamic-classes",$.join(" "))}else w.removeAttribute("data-dynamic-classes")}catch(m){console.warn(`[tbw-grid] cellClass callback error for column '${d.field}':`,m),w.removeAttribute("data-dynamic-classes")}}if(w.classList.contains("editing"))continue;const T=Ne(t,d);if(T){const y=i[d.field],m=T({row:i,value:y,field:d.field,column:d,cellEl:w});typeof m=="string"?(t.__frameworkAdapter?.releaseCell?.(w),w.innerHTML=F(m)):m instanceof Node?m.parentElement!==w&&(t.__frameworkAdapter?.releaseCell?.(w),w.innerHTML="",w.appendChild(m)):m==null&&(t.__frameworkAdapter?.releaseCell?.(w),w.textContent=y==null?"":String(y)),f&&t._afterCellRender?.({row:i,rowIndex:o,column:d,colIndex:u,value:y,cellElement:w,rowElement:e});continue}if(d.__viewTemplate||d.__compiledView||d.externalView)continue;const S=i[d.field];let H;const C=qe(t,d);if(C){try{const y=C(S,i);H=y==null?"":String(y)}catch(y){console.warn(`[tbw-grid] Format error in column '${d.field}':`,y),H=S==null?"":String(S)}w.textContent=H}else d.type==="date"?(H=Me(S),w.textContent=H):d.type==="boolean"?w.innerHTML=Oe(!!S):(H=S==null?"":String(S),w.textContent=H);f&&t._afterCellRender?.({row:i,rowIndex:o,column:d,colIndex:u,value:S,cellElement:w,rowElement:e})}}function B(t,e,i,o){e.classList.remove("tbw-row-loading"),e.removeAttribute("aria-busy");const n=t.__frameworkAdapter;if(n?.releaseCell){const p=e.children;for(let g=p.length-1;g>=0;g--)n.releaseCell(p[g])}e.innerHTML="";const r=t._visibleColumns,s=r.length,l=t._focusRow,a=t._focusCol,c=t,h=t._hasAfterCellRenderHook?.()??!1,f=document.createDocumentFragment();for(let p=0;p<s;p++){const g=r[p],u=It();u.setAttribute("aria-colindex",String(p+1)),u.setAttribute("data-col",String(p)),u.setAttribute("data-row",String(o)),u.setAttribute("data-field",g.field),u.setAttribute("data-header",g.header??g.field),g.type&&u.setAttribute("data-type",g.type);let d=i[g.field];const w=qe(t,g);if(w)try{d=w(d,i)}catch(C){console.warn(`[tbw-grid] Format error in column '${g.field}':`,C)}const v=g.__compiledView,b=g.__viewTemplate,E=Ne(t,g),T=g.externalView;let S=!1;if(E){const C=E({row:i,value:d,field:g.field,column:g,cellEl:u});typeof C=="string"?(u.innerHTML=F(C),S=!0):C instanceof Node?C.parentElement!==u&&(u.textContent="",u.appendChild(C)):C==null&&(u.textContent=d==null?"":String(d))}else if(T){const C=T,y=document.createElement("div");y.setAttribute("data-external-view",""),y.setAttribute("data-field",g.field),u.appendChild(y);const m={row:i,value:d,field:g.field,column:g};if(C.mount)try{C.mount({placeholder:y,context:m,spec:C})}catch(L){console.warn(`[tbw-grid] External view mount error for column '${g.field}':`,L)}else queueMicrotask(()=>{try{c.dispatchEvent(new CustomEvent("mount-external-view",{bubbles:!0,composed:!0,detail:{placeholder:y,spec:C,context:m}}))}catch(L){console.warn(`[tbw-grid] External view event dispatch error for column '${g.field}':`,L)}});y.setAttribute("data-mounted","")}else if(v){const C=v({row:i,value:d,field:g.field,column:g}),y=v.__blocked;u.innerHTML=y?"":F(C),S=!0,y&&(u.textContent="",u.setAttribute("data-blocked-template",""))}else if(b){const C=b.innerHTML;/Reflect\.|\bProxy\b|ownKeys\(/.test(C)?(u.textContent="",u.setAttribute("data-blocked-template","")):(u.innerHTML=F(ze(C,{row:i,value:d})),S=!0)}else w?u.textContent=d==null?"":String(d):g.type==="date"?u.textContent=Me(d):g.type==="boolean"?u.innerHTML=Oe(!!d):u.textContent=d==null?"":String(d);if(S){zt(u);const C=u.textContent||"";/Proxy|Reflect\.ownKeys/.test(C)&&(u.textContent=C.replace(/Proxy|Reflect\.ownKeys/g,"").trim(),/Proxy|Reflect\.ownKeys/.test(u.textContent||"")&&(u.textContent=""))}u.hasAttribute("data-blocked-template")&&(u.textContent||"").trim().length&&(u.textContent=""),g.editable?u.tabIndex=0:g.type==="boolean"&&(u.hasAttribute("tabindex")||(u.tabIndex=0)),l===o&&a===p?(u.classList.add("cell-focus"),u.setAttribute("aria-selected","true")):u.setAttribute("aria-selected","false");const H=g.cellClass;if(H)try{const C=i[g.field],y=H(C,i,g);if(y&&y.length>0){const m=y.filter(L=>L&&typeof L=="string");m.forEach(L=>u.classList.add(L)),u.setAttribute("data-dynamic-classes",m.join(" "))}}catch(C){console.warn(`[tbw-grid] cellClass callback error for column '${g.field}':`,C)}h&&t._afterCellRender?.({row:i,rowIndex:o,column:g,colIndex:p,value:d,cellElement:u,rowElement:e}),f.appendChild(u)}e.appendChild(f)}function Ue(t,e,i){if(e.target?.closest(".resize-handle"))return;const o=i.querySelector(".cell[data-row]"),n=ke(o);if(n<0)return;const r=t._rows[n];if(!r||t._dispatchRowClick?.(e,n,r,i))return;const s=e.target?.closest(".cell[data-col]");if(s){const l=Number(s.getAttribute("data-col"));if(!isNaN(l)){if(t._dispatchCellClick?.(e,n,l,s))return;const a=t._focusRow!==n||t._focusCol!==l;if(t._focusRow=n,t._focusCol=l,s.classList.contains("editing")){a&&(ae(t._bodyEl??t),s.classList.add("cell-focus"));const c=s.querySelector($e);try{c?.focus({preventScroll:!0})}catch{}return}U(t)}}}function $t(t,e){if(t._dispatchKeyDown?.(e))return;const i=t._rows.length-1,o=t._visibleColumns.length-1,n=t._activeEditRows!==void 0&&t._activeEditRows!==-1,s=t._visibleColumns[t._focusCol]?.type,l=e.composedPath?.()??[],a=l.length?l[0]:e.target,c=h=>{if(!h)return!1;const f=h.tagName;return!!(f==="INPUT"||f==="SELECT"||f==="TEXTAREA"||h.isContentEditable)};if(!(c(a)&&(e.key==="Home"||e.key==="End"))&&!(c(a)&&(e.key==="ArrowUp"||e.key==="ArrowDown")&&a.tagName==="INPUT"&&a.type==="number")&&!(c(a)&&(e.key==="ArrowLeft"||e.key==="ArrowRight"))&&!(c(a)&&(e.key==="Enter"||e.key==="Escape"))&&!(n&&s==="select"&&(e.key==="ArrowDown"||e.key==="ArrowUp"))){switch(e.key){case"Tab":{e.preventDefault(),!e.shiftKey?t._focusCol<o?t._focusCol+=1:(typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow<i&&(t._focusRow+=1,t._focusCol=0)):t._focusCol>0?t._focusCol-=1:t._focusRow>0&&(typeof t.commitActiveRowEdit=="function"&&t._activeEditRows===t._focusRow&&t.commitActiveRowEdit(),t._focusRow-=1,t._focusCol=o),U(t);return}case"ArrowDown":n&&typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow=Math.min(i,t._focusRow+1),e.preventDefault();break;case"ArrowUp":n&&typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow=Math.max(0,t._focusRow-1),e.preventDefault();break;case"ArrowRight":{Ie(t)?t._focusCol=Math.max(0,t._focusCol-1):t._focusCol=Math.min(o,t._focusCol+1),e.preventDefault();break}case"ArrowLeft":{Ie(t)?t._focusCol=Math.min(o,t._focusCol+1):t._focusCol=Math.max(0,t._focusCol-1),e.preventDefault();break}case"Home":(e.ctrlKey||e.metaKey)&&(n&&typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow=0),t._focusCol=0,e.preventDefault(),U(t,{forceScrollLeft:!0});return;case"End":(e.ctrlKey||e.metaKey)&&(n&&typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow=i),t._focusCol=o,e.preventDefault(),U(t,{forceScrollRight:!0});return;case"PageDown":t._focusRow=Math.min(i,t._focusRow+20),e.preventDefault();break;case"PageUp":t._focusRow=Math.max(0,t._focusRow-20),e.preventDefault();break;case"Enter":{const h=t._focusRow,f=t._focusCol,p=t._visibleColumns[f],g=t._rows[h],u=p?.field??"",d=u&&g?g[u]:void 0,w=t.querySelector(`[data-row="${h}"][data-col="${f}"]`),v={rowIndex:h,colIndex:f,field:u,value:d,row:g,cellEl:w,trigger:"keyboard",originalEvent:e},b=new CustomEvent("cell-activate",{cancelable:!0,detail:v});t.dispatchEvent(b);const E=new CustomEvent("activate-cell",{cancelable:!0,detail:{row:h,col:f}});if(t.dispatchEvent(E),b.defaultPrevented||E.defaultPrevented){e.preventDefault();return}break}default:return}U(t)}}function U(t,e){if(t._virtualization?.enabled){const{rowHeight:s,container:l,viewportEl:a}=t._virtualization,c=l,h=a?.clientHeight??c?.clientHeight??0;if(c&&h>0){const f=t._focusRow*s;f<c.scrollTop?c.scrollTop=f:f+s>c.scrollTop+h&&(c.scrollTop=f-h+s)}}const i=t._activeEditRows!==void 0&&t._activeEditRows!==-1;i||t.refreshVirtualWindow(!1),ae(t._bodyEl),Array.from(t._bodyEl.querySelectorAll('[aria-selected="true"]')).forEach(s=>{s.setAttribute("aria-selected","false")});const o=t._focusRow,n=t._virtualization.start??0,r=t._virtualization.end??t._rows.length;if(o>=n&&o<r){const s=t._bodyEl.querySelectorAll(".data-grid-row")[o-n];let l=s?.children[t._focusCol];if((!l||!l.classList?.contains("cell"))&&(l=s?.querySelector(`.cell[data-col="${t._focusCol}"]`)??s?.querySelector(".cell[data-col]")),l){l.classList.add("cell-focus"),l.setAttribute("aria-selected","true");const a=t.querySelector(".tbw-scroll-area");if(a&&l&&(!i||e?.forceHorizontalScroll))if(e?.forceScrollLeft)a.scrollLeft=0;else if(e?.forceScrollRight)a.scrollLeft=a.scrollWidth-a.clientWidth;else{const c=t._getHorizontalScrollOffsets?.(s??void 0,l)??{left:0,right:0};if(!c.skipScroll){const h=l.getBoundingClientRect(),f=a.getBoundingClientRect(),p=h.left-f.left+a.scrollLeft,g=p+h.width,u=a.scrollLeft+c.left,d=a.scrollLeft+a.clientWidth-c.right;p<u?a.scrollLeft=p-c.left:g>d&&(a.scrollLeft=g-a.clientWidth+c.right)}}if(t._activeEditRows!==void 0&&t._activeEditRows!==-1&&l.classList.contains("editing")){const c=l.querySelector($e);if(c&&document.activeElement!==c)try{c.focus({preventScroll:!0})}catch{}}else if(!l.contains(document.activeElement)){l.hasAttribute("tabindex")||l.setAttribute("tabindex","-1");try{l.focus({preventScroll:!0})}catch{}}}}}const Z=new WeakMap;function Wt(t,e){const i=ke(e),o=Mt(e);i<0||o<0||(t._focusRow=i,t._focusCol=o,ae(t._bodyEl),e.classList.add("cell-focus"),e.setAttribute("aria-selected","true"))}function ue(t,e,i,o){let n=null;const r=i.composedPath?.();if(r&&r.length>0?n=r[0]:n=i.target,n&&!e.contains(n)){const d=document.elementFromPoint(i.clientX,i.clientY);d&&(n=d)}const s=n?.closest?.("[data-col]"),l=n?.closest?.(".data-grid-row"),a=n?.closest?.(".header-row");let c,h,f,p,g,u;return s&&(c=parseInt(s.getAttribute("data-row")??"-1",10),h=parseInt(s.getAttribute("data-col")??"-1",10),c>=0&&h>=0&&(f=t._rows[c],u=t._columns[h],p=u?.field,g=f&&p?f[p]:void 0)),{type:o,row:f,rowIndex:c!==void 0&&c>=0?c:void 0,colIndex:h!==void 0&&h>=0?h:void 0,field:p,value:g,column:u,originalEvent:i,cellElement:s??void 0,rowElement:l??void 0,isHeader:!!a,cell:c!==void 0&&h!==void 0&&c>=0&&h>=0?{row:c,col:h}:void 0}}function Ft(t,e,i){const o=ue(t,e,i,"mousedown");(t._dispatchCellMouseDown?.(o)??!1)&&Z.set(t,!0)}function Ut(t,e,i){if(!Z.get(t))return;const o=ue(t,e,i,"mousemove");t._dispatchCellMouseMove?.(o)}function Vt(t,e,i){if(!Z.get(t))return;const o=ue(t,e,i,"mouseup");t._dispatchCellMouseUp?.(o),Z.set(t,!1)}function Gt(t,e,i){e.addEventListener("mousedown",o=>{const n=o.target.closest(".cell[data-col]");n&&(n.classList.contains("editing")||Wt(t,n))},{signal:i}),e.addEventListener("click",o=>{const n=o.target.closest(".data-grid-row");n&&Ue(t,o,n)},{signal:i}),e.addEventListener("dblclick",o=>{const n=o.target.closest(".data-grid-row");n&&Ue(t,o,n)},{signal:i})}function Bt(t,e,i,o){e.addEventListener("keydown",n=>$t(t,n),{signal:o}),i.addEventListener("mousedown",n=>Ft(t,i,n),{signal:o}),document.addEventListener("mousemove",n=>Ut(t,i,n),{signal:o}),document.addEventListener("mouseup",n=>Vt(t,i,n),{signal:o})}function Ve(t,e){return t==null&&e==null?0:t==null?-1:e==null||t>e?1:t<e?-1:0}function Ge(t,e,i){const n=i.find(l=>l.field===e.field)?.sortComparator??Ve,{field:r,direction:s}=e;return[...t].sort((l,a)=>n(l[r],a[r],l,a)*s)}function Be(t,e,i,o){t._rows=e,t.__rowRenderEpoch++,t._rowPool.forEach(n=>n.__epoch=-1),oe(t),t.refreshVirtualWindow(!0),t.dispatchEvent(new CustomEvent("sort-change",{detail:{field:i.field,direction:o}})),t.requestStateChange?.()}function Xe(t,e){!t._sortState||t._sortState.field!==e.field?(t._sortState||(t.__originalOrder=t._rows.slice()),Ye(t,e,1)):t._sortState.direction===1?Ye(t,e,-1):(t._sortState=null,t.__rowRenderEpoch++,t._rowPool.forEach(o=>o.__epoch=-1),t._rows=t.__originalOrder.slice(),oe(t),t._headerRowEl?.querySelectorAll('[role="columnheader"].sortable')?.forEach(o=>{o.getAttribute("aria-sort")?(o.getAttribute("aria-sort")==="ascending"||o.getAttribute("aria-sort")==="descending")&&(t._sortState||o.setAttribute("aria-sort","none")):o.setAttribute("aria-sort","none")}),t.refreshVirtualWindow(!0),t.dispatchEvent(new CustomEvent("sort-change",{detail:{field:e.field,direction:0}})),t.requestStateChange?.())}function Ye(t,e,i){t._sortState={field:e.field,direction:i};const o={field:e.field,direction:i},n=t._columns,s=(t.effectiveConfig?.sortHandler??Ge)(t._rows,o,n);s&&typeof s.then=="function"?s.then(l=>{Be(t,l,e,i)}):Be(t,s,e,i)}function X(t,e){return t.effectiveConfig?.sortable!==!1&&e.sortable===!0}function J(t,e){return t.effectiveConfig?.resizable!==!1&&e.resizable!==!1}function Xt(t,e){typeof e=="string"?t.textContent=e:e instanceof HTMLElement&&(t.innerHTML="",t.appendChild(e.cloneNode(!0)))}function ee(t,e){const i=document.createElement("span");xe(i,"sort-indicator");const o=t._sortState?.field===e.field?t._sortState.direction:0,n={...k,...t.icons},r=o===1?n.sortAsc:o===-1?n.sortDesc:n.sortNone;return Xt(i,r),i}function te(t,e,i){const o=document.createElement("div");return o.className="resize-handle",o.setAttribute("aria-hidden","true"),o.addEventListener("mousedown",n=>{n.stopPropagation(),n.preventDefault(),t._resizeController.start(n,e,i)}),o.addEventListener("dblclick",n=>{n.stopPropagation(),n.preventDefault(),t._resizeController.resetColumn(e)}),o}function ie(t,e,i,o){o.classList.add("sortable"),o.tabIndex=0;const n=t._sortState?.field===e.field?t._sortState.direction:0;o.setAttribute("aria-sort",n===0?"none":n===1?"ascending":"descending"),o.addEventListener("click",r=>{t._resizeController?.isResizing||t._dispatchHeaderClick?.(r,i,o)||Xe(t,e)}),o.addEventListener("keydown",r=>{if(r.key==="Enter"||r.key===" "){if(r.preventDefault(),t._dispatchHeaderClick?.(r,i,o))return;Xe(t,e)}})}function Yt(t,e){if(e!=null)if(typeof e=="string"){const i=document.createElement("span");for(i.innerHTML=F(e);i.firstChild;)t.appendChild(i.firstChild)}else e instanceof Node&&t.appendChild(e)}function oe(t){t._headerRowEl=t.findHeaderRow();const e=t._headerRowEl;e&&(e.innerHTML="",t._visibleColumns.forEach((i,o)=>{const n=document.createElement("div");n.className="cell",xe(n,"header-cell"),n.setAttribute("role","columnheader"),n.setAttribute("aria-colindex",String(o+1)),n.setAttribute("data-field",i.field),n.setAttribute("data-col",String(o));const r=i.header??i.field,s=t._sortState?.field===i.field?t._sortState.direction:0,l=s===1?"asc":s===-1?"desc":null;if(i.headerRenderer){const a={column:i,value:r,sortState:l,filterActive:!1,cellEl:n,renderSortIcon:()=>X(t,i)?ee(t,i):null,renderFilterButton:()=>null},c=i.headerRenderer(a);Yt(n,c),X(t,i)&&ie(t,i,o,n),J(t,i)&&(n.classList.add("resizable"),n.appendChild(te(t,o,n)))}else if(i.headerLabelRenderer){const a={column:i,value:r},c=i.headerLabelRenderer(a),h=document.createElement("span");c==null?h.textContent=r:typeof c=="string"?h.innerHTML=F(c):c instanceof Node&&h.appendChild(c),n.appendChild(h),X(t,i)&&(ie(t,i,o,n),n.appendChild(ee(t,i))),J(t,i)&&(n.classList.add("resizable"),n.appendChild(te(t,o,n)))}else if(i.__headerTemplate)Array.from(i.__headerTemplate.childNodes).forEach(a=>n.appendChild(a.cloneNode(!0))),X(t,i)&&(ie(t,i,o,n),n.appendChild(ee(t,i))),J(t,i)&&(n.classList.add("resizable"),n.appendChild(te(t,o,n)));else{const a=document.createElement("span");a.textContent=r,n.appendChild(a),X(t,i)&&(ie(t,i,o,n),n.appendChild(ee(t,i))),J(t,i)&&(n.classList.add("resizable"),n.appendChild(te(t,o,n)))}e.appendChild(n)}),e.querySelectorAll(".cell.sortable").forEach(i=>{i.getAttribute("aria-sort")||i.setAttribute("aria-sort","none")}),e.children.length>0?(e.setAttribute("role","row"),e.setAttribute("aria-rowindex","1")):(e.removeAttribute("role"),e.removeAttribute("aria-rowindex")))}const je=typeof requestIdleCallback=="function";function jt(t,e){return je?requestIdleCallback(t,e):window.setTimeout(()=>{const i=Date.now();t({didTimeout:!1,timeRemaining:()=>Math.max(0,50-(Date.now()-i))})},1)}function Ke(t){je?cancelIdleCallback(t):clearTimeout(t)}function Kt(t){const e=document.createElement("div");return e.className=`tbw-spinner tbw-spinner--${t}`,e.setAttribute("role","progressbar"),e.setAttribute("aria-label","Loading"),e}function Qt(t,e){if(e){const o=e({size:t});if(typeof o=="string"){const n=document.createElement("div");return n.innerHTML=o,n}return o}return Kt(t)}function Zt(t){const e=document.createElement("div");return e.className="tbw-loading-overlay",e.setAttribute("role","status"),e.setAttribute("aria-live","polite"),e.appendChild(Qt("large",t)),e}function Jt(t,e){t.appendChild(e)}function ei(t){t?.remove()}function ti(t,e){if(e){if(t.classList.add("tbw-row-loading"),t.setAttribute("aria-busy","true"),!t.querySelector(".tbw-row-loading-overlay")){const i=document.createElement("div");i.className="tbw-row-loading-overlay",i.setAttribute("aria-hidden","true");const o=document.createElement("div");o.className="tbw-row-loading-spinner",i.appendChild(o),t.appendChild(i)}}else t.classList.remove("tbw-row-loading"),t.removeAttribute("aria-busy"),t.querySelector(".tbw-row-loading-overlay")?.remove()}function ii(t,e){e?(t.classList.add("tbw-cell-loading"),t.setAttribute("aria-busy","true")):(t.classList.remove("tbw-cell-loading"),t.removeAttribute("aria-busy"))}var A=(t=>(t[t.STYLE=1]="STYLE",t[t.VIRTUALIZATION=2]="VIRTUALIZATION",t[t.HEADER=3]="HEADER",t[t.ROWS=4]="ROWS",t[t.COLUMNS=5]="COLUMNS",t[t.FULL=6]="FULL",t))(A||{});class oi{#l;#n=0;#u=0;#d=null;#h=null;#a=null;#o=!1;constructor(e){this.#l=e}requestPhase(e,i){e>this.#n&&(this.#n=e),this.#u===0&&(this.#f(),this.#u=requestAnimationFrame(()=>this.#w()))}whenReady(){return this.#d?this.#d:Promise.resolve()}setInitialReadyResolver(e){this.#a=e}cancel(){this.#u!==0&&(cancelAnimationFrame(this.#u),this.#u=0),this.#n=0,this.#h&&(this.#h(),this.#h=null,this.#d=null)}get isPending(){return this.#n!==0}get pendingPhase(){return this.#n}#f(){this.#d||(this.#d=new Promise(e=>{this.#h=e}))}#w(){if(this.#u=0,!this.#l.isConnected()){this.#n=0,this.#h&&(this.#h(),this.#h=null,this.#d=null);return}const e=this.#n;this.#n=0,e>=5&&this.#l.mergeConfig(),e>=4&&this.#l.processRows(),e>=5&&(this.#l.processColumns(),this.#l.updateTemplate()),e>=3&&this.#l.renderHeader(),e>=2&&this.#l.renderVirtualWindow(),e>=1&&this.#l.afterRender(),!this.#o&&this.#a&&(this.#o=!0,this.#a()),this.#h&&(this.#h(),this.#h=null,this.#d=null)}}function Qe(t){let e=null,i=null,o=null,n=null;const r=a=>{if(!e)return;const c=a.clientX-e.startX,h=Math.max(40,e.startWidth+c),f=t._visibleColumns[e.colIndex];f.width=h,f.__userResized=!0,f.__renderedWidth=h,i==null&&(i=requestAnimationFrame(()=>{i=null,t.updateTemplate?.()})),t.dispatchEvent(new CustomEvent("column-resize",{detail:{field:f.field,width:h}}))};let s=!1;const l=()=>{const a=e!==null;a&&(s=!0,requestAnimationFrame(()=>{s=!1})),window.removeEventListener("mousemove",r),window.removeEventListener("mouseup",l),o!==null&&(document.documentElement.style.cursor=o,o=null),n!==null&&(document.body.style.userSelect=n,n=null),e=null,a&&t.requestStateChange&&t.requestStateChange()};return{get isResizing(){return e!==null||s},start(a,c,h){a.preventDefault();const f=t._visibleColumns[c],p=typeof f?.width=="number"?f.width:void 0,g=f?.__renderedWidth??p??h.getBoundingClientRect().width;e={startX:a.clientX,colIndex:c,startWidth:g},window.addEventListener("mousemove",r),window.addEventListener("mouseup",l),o===null&&(o=document.documentElement.style.cursor),document.documentElement.style.cursor="e-resize",n===null&&(n=document.body.style.userSelect),document.body.style.userSelect="none"},resetColumn(a){const c=t._visibleColumns[a];c&&(c.__userResized=!1,c.__renderedWidth=void 0,c.width=c.__originalWidth,t.updateTemplate?.(),t.requestStateChange?.(),t.dispatchEvent(new CustomEvent("column-resize-reset",{detail:{field:c.field,width:c.width}})))},dispose(){l()}}}const fe="data-animating",ni={change:"--tbw-row-change-duration",insert:"--tbw-row-insert-duration",remove:"--tbw-row-remove-duration"},ri={change:500,insert:300,remove:200};function si(t){const e=t.trim().toLowerCase();return e.endsWith("ms")?parseFloat(e):e.endsWith("s")?parseFloat(e)*1e3:parseFloat(e)}function li(t,e){const i=ni[e],o=getComputedStyle(t).getPropertyValue(i);if(o){const n=si(o);if(!isNaN(n)&&n>0)return n}return ri[e]}function ai(t,e,i){t.removeAttribute(fe),t.offsetWidth,t.setAttribute(fe,e);const o=li(t,e);setTimeout(()=>{e!=="remove"&&t.removeAttribute(fe)},o)}function ge(t,e,i){if(e<0)return!1;const o=t.findRenderedRowElement?.(e);return o?(ai(o,i),!0):!1}function ci(t,e,i){let o=0;for(const n of e)ge(t,n,i)&&o++;return o}function di(t,e,i){const o=t._rows??[],n=t.getRowId;if(!n)return!1;const r=o.findIndex(s=>{if(s==null)return!1;try{return n(s)===e}catch{return!1}});return r<0?!1:ge(t,r,i)}function ne(t,e,i){const o=document.createElement(t);if(e)for(const n in e){const r=e[n];r!=null&&o.setAttribute(n,r)}return o}function P(t,e){const i=document.createElement("div");if(t&&(i.className=t),e)for(const o in e){const n=e[o];n!=null&&i.setAttribute(o,n)}return i}function Ze(t,e,i){const o=document.createElement("button");if(t&&(o.className=t),e)for(const n in e){const r=e[n];r!=null&&o.setAttribute(n,r)}return o}const Je=document.createElement("template");Je.innerHTML=`
1
+ (function(_,V){typeof exports=="object"&&typeof module<"u"?V(exports):typeof define=="function"&&define.amd?define(["exports"],V):(_=typeof globalThis<"u"?globalThis:_||self,V(_.TbwGrid={}))})(this,(function(_){"use strict";function V(){return{rowCount:-1,colCount:-1,ariaLabel:void 0,ariaDescribedBy:void 0}}function pt(t,e,i,o,n){if(o===t.rowCount&&n===t.colCount)return!1;const r=t.rowCount;return t.rowCount=o,t.colCount=n,e&&(e.setAttribute("aria-rowcount",String(o)),e.setAttribute("aria-colcount",String(n))),o!==r&&i&&(o>0?i.setAttribute("role","rowgroup"):i.removeAttribute("role")),!0}function wt(t,e){const i=t?.gridAriaLabel;return i||(t?.shell?.header?.title??e?.lightDomTitle??void 0)}function bt(t,e,i,o){if(!e)return!1;let n=!1;const r=wt(i,o);r!==t.ariaLabel&&(t.ariaLabel=r,r?e.setAttribute("aria-label",r):e.removeAttribute("aria-label"),n=!0);const s=i?.gridAriaDescribedBy;return s!==t.ariaDescribedBy&&(t.ariaDescribedBy=s,s?e.setAttribute("aria-describedby",s):e.removeAttribute("aria-describedby"),n=!0),n}const W={STRETCH:"stretch",FIXED:"fixed"},Re={mode:"reduced-motion",duration:200,easing:"ease-out"},Ae='<svg viewBox="0 0 16 16" width="12" height="12"><path fill="currentColor" d="M6 10.5a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/></svg>',k={expand:"▶",collapse:"▼",sortAsc:"▲",sortDesc:"▼",sortNone:"⇅",submenuArrow:"▶",dragHandle:"⋮⋮",toolPanel:"☰",filter:Ae,filterActive:Ae,print:"🖨️"};function mt(t){return Array.from(t.querySelectorAll("tbw-grid-column")).map(i=>{const o=i.getAttribute("field")||"";if(!o)return null;const n=i.getAttribute("type")||void 0,s=n&&new Set(["number","string","date","boolean","select"]).has(n)?n:void 0,l=i.getAttribute("header")||void 0,a=i.hasAttribute("sortable"),c=i.hasAttribute("editable"),h={field:o,type:s,header:l,sortable:a,editable:c},f=i.getAttribute("width");if(f){const m=parseFloat(f);!isNaN(m)&&/^\d+(\.\d+)?$/.test(f.trim())?h.width=m:h.width=f}const p=i.getAttribute("minWidth")||i.getAttribute("min-width");if(p){const m=parseFloat(p);isNaN(m)||(h.minWidth=m)}i.hasAttribute("resizable")&&(h.resizable=!0),i.hasAttribute("sizable")&&(h.resizable=!0);const g=i.getAttribute("editor"),u=i.getAttribute("renderer");g&&(h.__editorName=g),u&&(h.__rendererName=u);const d=i.getAttribute("options");d&&(h.options=d.split(",").map(m=>{const[L,$]=m.includes(":")?m.split(":"):[m.trim(),m.trim()];return{value:L.trim(),label:$?.trim()||L.trim()}}));const w=i.querySelector("tbw-grid-column-view"),v=i.querySelector("tbw-grid-column-editor"),b=i.querySelector("tbw-grid-column-header");w&&(h.__viewTemplate=w),v&&(h.__editorTemplate=v),b&&(h.__headerTemplate=b);const T=globalThis.DataGridElement?.getAdapters?.()??[],S=w??i,H=T.find(m=>m.canHandle(S));if(H){const m=H.createRenderer(S);m&&(h.viewRenderer=m)}const C=v??i,y=T.find(m=>m.canHandle(C));if(y){const m=y.createEditor(C);m&&(h.editor=m)}return h}).filter(i=>!!i)}function Te(t,e){if((!t||!t.length)&&(!e||!e.length))return[];if(!t||!t.length)return e||[];if(!e||!e.length)return t;const i={};e.forEach(n=>{const r=i[n.field];if(r){n.header&&!r.header&&(r.header=n.header),n.type&&!r.type&&(r.type=n.type),n.sortable&&(r.sortable=!0),n.editable&&(r.editable=!0),n.resizable&&(r.resizable=!0),n.width!=null&&r.width==null&&(r.width=n.width),n.minWidth!=null&&r.minWidth==null&&(r.minWidth=n.minWidth),n.__viewTemplate&&(r.__viewTemplate=n.__viewTemplate),n.__editorTemplate&&(r.__editorTemplate=n.__editorTemplate),n.__headerTemplate&&(r.__headerTemplate=n.__headerTemplate);const s=n.renderer||n.viewRenderer,l=r.renderer||r.viewRenderer;s&&!l&&(r.viewRenderer=s,n.renderer&&(r.renderer=s)),n.editor&&!r.editor&&(r.editor=n.editor)}else i[n.field]={...n}});const o=t.map(n=>{const r=i[n.field];if(!r)return n;const s={...n};r.header&&!s.header&&(s.header=r.header),r.type&&!s.type&&(s.type=r.type),s.sortable=n.sortable||r.sortable,(n.resizable===!0||r.resizable===!0)&&(s.resizable=!0),s.editable=n.editable||r.editable,r.width!=null&&s.width==null&&(s.width=r.width),r.minWidth!=null&&s.minWidth==null&&(s.minWidth=r.minWidth),r.__viewTemplate&&(s.__viewTemplate=r.__viewTemplate),r.__editorTemplate&&(s.__editorTemplate=r.__editorTemplate),r.__headerTemplate&&(s.__headerTemplate=r.__headerTemplate);const l=r.renderer||r.viewRenderer,a=s.renderer||s.viewRenderer;return l&&!a&&(s.viewRenderer=l,r.renderer&&(s.renderer=l)),r.editor&&!s.editor&&(s.editor=r.editor),delete i[n.field],s});return Object.keys(i).forEach(n=>o.push(i[n])),o}function xe(t,e){try{t.part?.add?.(e)}catch{}const i=t.getAttribute("part");i?i.split(/\s+/).includes(e)||t.setAttribute("part",i+" "+e):t.setAttribute("part",e)}function He(t){const e=t.effectiveConfig?.fitMode||t.fitMode||W.STRETCH;if(e!==W.STRETCH&&e!==W.FIXED||t.__didInitialAutoSize||!t.isConnected)return;const i=Array.from(t._headerRowEl?.children||[]);if(!i.length)return;let o=!1;t._visibleColumns.forEach((n,r)=>{if(n.width)return;const s=i[r];let l=s?s.scrollWidth:0;for(const a of t._rowPool){const c=a.children[r];if(c){const h=c.scrollWidth;h>l&&(l=h)}}l>0&&(n.width=l+2,n.__autoSized=!0,o=!0)}),o&&G(t),t.__didInitialAutoSize=!0}const vt=/^(?:\d+(?:\.\d+)?(?:px|%|fr|em|rem|ch|vw|vh|vmin|vmax)|calc\(.+\)|min-content|max-content|minmax\(.+\)|fit-content\(.+\)|auto)$/i;function Le(t,e){return typeof t=="number"?`${t}px`:(vt.test(t)||console.warn(`[tbw-grid] Column '${e??"?"}' has an invalid CSS width value: '${t}'. Expected a number (px) or a valid CSS unit string (e.g. '30%', '2fr', 'calc(...)').`),t)}function G(t){(t.effectiveConfig?.fitMode||t.fitMode||W.STRETCH)===W.STRETCH?t._gridTemplate=t._visibleColumns.map(i=>{if(i.width!=null)return Le(i.width,i.field);const o=i.minWidth;return o!=null?`minmax(${o}px, 1fr)`:"1fr"}).join(" ").trim():t._gridTemplate=t._visibleColumns.map(i=>i.width!=null?Le(i.width,i.field):"max-content").join(" "),t.style.setProperty("--tbw-column-template",t._gridTemplate)}function Ct(t){return t==null?"string":typeof t=="number"?"number":typeof t=="boolean"?"boolean":t instanceof Date||typeof t=="string"&&/\d{4}-\d{2}-\d{2}/.test(t)&&!isNaN(Date.parse(t))?"date":"string"}function yt(t,e){const i=t[0]||{},o=Object.keys(i).map(r=>{const s=i[r],l=Ct(s);return{field:r,header:r.charAt(0).toUpperCase()+r.slice(1),type:l}}),n={};return o.forEach(r=>{n[r.field]=r.type||"string"}),{columns:o,typeMap:n}}const _t=/{{\s*([^}]+)\s*}}/g,D="__DG_EMPTY__",Et=/^[\w$. '?+\-*/%:()!<>=,&|]+$/,St=/__(proto|defineGetter|defineSetter)|constructor|window|globalThis|global|process|Function|import|eval|Reflect|Proxy|Error|arguments|document|location|cookie|localStorage|sessionStorage|indexedDB|fetch|XMLHttpRequest|WebSocket|Worker|SharedWorker|ServiceWorker|opener|parent|top|frames|self|this\b/;function Rt(t){return!t||typeof t!="string"?"":t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;")}const At=new Set(["script","iframe","object","embed","form","input","button","textarea","select","link","meta","base","style","template","slot","portal","frame","frameset","applet","noscript","noembed","plaintext","xmp","listing"]),Pe=/^on\w+$/i,Tt=new Set(["href","src","action","formaction","data","srcdoc","xlink:href","poster","srcset"]),xt=/^\s*(javascript|vbscript|data|blob):/i;function F(t){if(!t||typeof t!="string")return"";if(t.indexOf("<")===-1)return t;const e=document.createElement("template");return e.innerHTML=t,Ht(e.content),e.innerHTML}function Ht(t){const e=[],i=t.querySelectorAll("*");for(const o of i){const n=o.tagName.toLowerCase();if(At.has(n)){e.push(o);continue}if((n==="svg"||o.namespaceURI==="http://www.w3.org/2000/svg")&&Array.from(o.attributes).some(l=>Pe.test(l.name)||l.name==="href"||l.name==="xlink:href")){e.push(o);continue}const r=[];for(const s of o.attributes){const l=s.name.toLowerCase();if(Pe.test(l)){r.push(s.name);continue}if(Tt.has(l)&&xt.test(s.value)){r.push(s.name);continue}if(l==="style"&&/expression\s*\(|javascript:|behavior\s*:/i.test(s.value)){r.push(s.name);continue}}r.forEach(s=>o.removeAttribute(s))}e.forEach(o=>o.remove())}function ze(t,e){if(!t||t.indexOf("{{")===-1)return t;const i=[],o=t.replace(_t,(l,a)=>{const c=Lt(a,e);return i.push({expr:a.trim(),result:c}),c}),n=Pt(o),r=i.length&&i.every(l=>l.result===""||l.result===D);return N.test(t)||r?"":n}function Lt(t,e){if(t=(t||"").trim(),!t||N.test(t))return D;if(t==="value")return e.value==null?D:String(e.value);if(t.startsWith("row.")&&!/[()?]/.test(t)&&!t.includes(":")){const o=t.slice(4),n=e.row?e.row[o]:void 0;return n==null?D:String(n)}if(t.length>80||!Et.test(t)||St.test(t))return D;const i=t.match(/\./g);if(i&&i.length>1)return D;try{const n=new Function("value","row",`return (${t});`)(e.value,e.row),r=n==null?"":String(n);return N.test(r)?D:r||D}catch{return D}}const N=/Reflect|Proxy|ownKeys/;function Pt(t){return t&&t.replace(new RegExp(D,"g"),"").replace(/Reflect\.[^<>{}\s]+|\bProxy\b|ownKeys\([^)]*\)/g,"")}function zt(t){if(N.test(t.textContent||"")){for(const e of t.childNodes)e.nodeType===Node.TEXT_NODE&&N.test(e.textContent||"")&&(e.textContent="");N.test(t.textContent||"")&&(t.textContent="")}}function De(t){const e=N.test(t),i=(o=>e?"":ze(t,o));return i.__blocked=e,i}const Dt=100;class Ot{#l;#n;#u;#d;#h;#a={};#o={};#f=!0;#w=[];#v;#s;#C;#r;#g;constructor(e){this.#r=e}get original(){return this.#a}get effective(){return this.#o}get columns(){return this.#o.columns??[]}set columns(e){this.#o.columns=e}get lightDomColumnsCache(){return this.#d}set lightDomColumnsCache(e){this.#d=e}get originalColumnNodes(){return this.#h}set originalColumnNodes(e){this.#h=e}get lightDomTitle(){return this.#g}set lightDomTitle(e){this.#g=e}get initialColumnState(){return this.#C}set initialColumnState(e){this.#C=e}get sourcesChanged(){return this.#f}markSourcesChanged(){this.#f=!0}setGridConfig(e){this.#l=e,this.#f=!0,this.#d=void 0}getGridConfig(){return this.#l}setColumns(e){this.#n=e,this.#f=!0}getColumns(){return this.#n}setFitMode(e){this.#u=e,this.#f=!0}getFitMode(){return this.#u}merge(){const e=(this.#o.columns?.length??0)>0;if(!this.#f&&e)return;const i=this.#I();this.#f=!1,this.#a=i,Object.freeze(this.#a),this.#a.columns&&Object.freeze(this.#a.columns),this.#o=this.#x(this.#a),this.#m()}#x(e){const i={...e};return e.columns&&(i.columns=e.columns.map(o=>({...o}))),e.shell&&(i.shell={...e.shell,header:e.shell.header?{...e.shell.header}:void 0,toolPanel:e.shell.toolPanel?{...e.shell.toolPanel}:void 0,toolPanels:e.shell.toolPanels?.map(o=>({...o})),headerContents:e.shell.headerContents?.map(o=>({...o}))}),i}#m(){const e=this.#o;this.#z(),typeof e.rowHeight=="number"&&e.rowHeight>0&&this.#r.setRowHeight(e.rowHeight),e.fitMode==="fixed"&&this.columns.forEach(o=>{o.width==null&&(o.width=80)}),this.#r.applyAnimationConfig(e)}#z(){const e=this.#o.typeDefaults;if(!e)return;const i=this.columns;for(const o of i){if(!o.type)continue;const n=e[o.type];n&&(!o.renderer&&!o.viewRenderer&&n.renderer&&(o.renderer=n.renderer),!o.format&&n.format&&(o.format=n.format),!o.editor&&n.editor&&(o.editor=n.editor),!o.editorParams&&n.editorParams&&(o.editorParams=n.editorParams))}}#I(){const e=this.#l?{...this.#l}:{},i=Array.isArray(e.columns)?[...e.columns]:[],o=(this.#d??[]).map(s=>({...s}));let n=Te(i,o);this.#n&&this.#n.length&&(n=Te(this.#n,o));const r=this.#r.getRows();return n.length===0&&r.length&&(n=yt(r).columns),n.length&&(n.forEach(s=>{s.sortable===void 0&&(s.sortable=!0),s.resizable===void 0&&(s.resizable=!0),s.__originalWidth===void 0&&typeof s.width=="number"&&(s.__originalWidth=s.width)}),n.forEach(s=>{s.__viewTemplate&&!s.__compiledView&&(s.__compiledView=De(s.__viewTemplate.innerHTML)),s.__editorTemplate&&!s.__compiledEditor&&(s.__compiledEditor=De(s.__editorTemplate.innerHTML))}),e.columns=n),this.#u&&(e.fitMode=this.#u),e.fitMode||(e.fitMode="stretch"),this.#b(e),e.columnState&&!this.#C&&(this.#C=e.columnState),e}#b(e){e.shell=e.shell?{...e.shell}:{},e.shell.header=e.shell.header?{...e.shell.header}:{};const i=this.#r.getShellLightDomTitle();i&&(this.#g=i),this.#g&&!e.shell.header.title&&(e.shell.header.title=this.#g);const o=this.#r.getShellLightDomHeaderContent();o?.length>0&&(e.shell.header.lightDomContent=o),this.#r.getShellHasToolButtonsContainer()&&(e.shell.header.hasToolButtonsContainer=!0);const n=this.#r.getShellToolPanels();if(n.size>0){const f=Array.from(n.values());f.sort((p,g)=>(p.order??100)-(g.order??100)),e.shell.toolPanels=f}const r=this.#r.getShellHeaderContents();if(r.size>0){const f=Array.from(r.values());f.sort((p,g)=>(p.order??100)-(g.order??100)),e.shell.headerContents=f}const s=this.#r.getShellToolbarContents(),l=Array.from(s.values()),a=this.#l?.shell?.header?.toolbarContents??[],c=new Set(a.map(f=>f.id)),h=[...a];for(const f of l)c.has(f.id)||h.push(f);h.sort((f,p)=>(f.order??0)-(p.order??0)),e.shell.header.toolbarContents=h}collectState(e){const i=this.columns,o=this.#y();return{columns:i.map((n,r)=>{const s={field:n.field,order:r,visible:!n.hidden},l=n;l.__renderedWidth!==void 0?s.width=l.__renderedWidth:n.width!==void 0&&(s.width=typeof n.width=="string"?parseFloat(n.width):n.width);const a=o.get(n.field);a&&(s.sort=a);for(const c of e)if(c.getColumnState){const h=c.getColumnState(n.field);h&&Object.assign(s,h)}return s})}}applyState(e,i){if(!e.columns||e.columns.length===0)return;const o=this.columns,n=new Map(e.columns.map(l=>[l.field,l])),r=o.map(l=>{const a=n.get(l.field);if(!a)return l;const c={...l};return a.width!==void 0&&(c.width=a.width,c.__renderedWidth=a.width),a.visible!==void 0&&(c.hidden=!a.visible),c});r.sort((l,a)=>{const c=n.get(l.field)?.order??1/0,h=n.get(a.field)?.order??1/0;return c-h}),this.columns=r;const s=e.columns.filter(l=>l.sort!==void 0).sort((l,a)=>(l.sort?.priority??0)-(a.sort?.priority??0));if(s.length>0){const l=s[0];l.sort&&this.#r.setSortState({field:l.field,direction:l.sort.direction==="asc"?1:-1})}else this.#r.setSortState(null);for(const l of i)if(l.applyColumnState)for(const a of e.columns)l.applyColumnState(a.field,a)}resetState(e){this.#C=void 0,this.#r.setSortState(null),this.#o=this.#x(this.#a),this.#m();for(const i of e)if(i.applyColumnState)for(const o of this.columns)i.applyColumnState(o.field,{field:o.field,order:0,visible:!0});this.requestStateChange(e)}#y(){const e=new Map,i=this.#r.getSortState();return i&&e.set(i.field,{direction:i.direction===1?"asc":"desc",priority:0}),e}requestStateChange(e){this.#s&&clearTimeout(this.#s),this.#s=setTimeout(()=>{this.#s=void 0;const i=this.collectState(e);this.#r.emit("column-state-change",i)},Dt)}setColumnVisible(e,i){const o=this.columns,n=o.find(s=>s.field===e);return!n||!i&&n.lockVisible||!i&&o.filter(l=>!l.hidden&&l.field!==e).length===0||!!n.hidden===!i?!1:(n.hidden=!i,this.#r.emit("column-visibility",{field:e,visible:i,visibleColumns:o.filter(s=>!s.hidden).map(s=>s.field)}),this.#r.clearRowPool(),this.#r.setup(),!0)}toggleColumnVisibility(e){const i=this.columns.find(o=>o.field===e);return i?this.setColumnVisible(e,!!i.hidden):!1}isColumnVisible(e){const i=this.columns.find(o=>o.field===e);return i?!i.hidden:!1}showAllColumns(){const e=this.columns;e.some(i=>i.hidden)&&(e.forEach(i=>i.hidden=!1),this.#r.emit("column-visibility",{visibleColumns:e.map(i=>i.field)}),this.#r.clearRowPool(),this.#r.setup())}getAllColumns(){return this.columns.map(e=>({field:e.field,header:e.header||e.field,visible:!e.hidden,lockVisible:e.lockVisible,utility:e.meta?.utility===!0}))}getColumnOrder(){return this.columns.map(e=>e.field)}setColumnOrder(e){if(!e.length)return;const i=new Map(this.columns.map(n=>[n.field,n])),o=[];for(const n of e){const r=i.get(n);r&&(o.push(r),i.delete(n))}for(const n of i.values())o.push(n);this.columns=o,this.#r.renderHeader(),this.#r.updateTemplate(),this.#r.refreshVirtualWindow()}parseLightDomColumns(e){this.#d||(this.#h=Array.from(e.querySelectorAll("tbw-grid-column")),this.#d=this.#h.length?mt(e):[])}clearLightDomCache(){this.#d=void 0}#p=new Map;registerLightDomHandler(e,i){this.#p.set(e.toLowerCase(),i)}unregisterLightDomHandler(e){this.#p.delete(e.toLowerCase())}observeLightDOM(e){this.#v&&this.#v.disconnect();const i=new Set;let o=null;const n=()=>{o=null;for(const r of i)this.#p.get(r)?.();i.clear()};this.#v=new MutationObserver(r=>{for(const s of r){for(const l of s.addedNodes){if(l.nodeType!==Node.ELEMENT_NODE)continue;const c=l.tagName.toLowerCase();this.#p.has(c)&&i.add(c)}if(s.type==="attributes"&&s.target.nodeType===Node.ELEMENT_NODE){const a=s.target.tagName.toLowerCase();this.#p.has(a)&&i.add(a)}}i.size>0&&!o&&(o=setTimeout(n,0))}),this.#v.observe(e,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["title","field","header","width","hidden","id","icon","tooltip","order"]})}onChange(e){this.#w.push(e)}notifyChange(){for(const e of this.#w)e()}dispose(){this.#v?.disconnect(),this.#w=[],this.#s&&clearTimeout(this.#s)}}function le(){if(typeof window<"u"&&window.location){const t=window.location.hostname;if(t==="localhost"||t==="127.0.0.1"||t==="::1")return!0}return typeof process<"u"&&process.env?.NODE_ENV!=="production"}function Oe(t){return`<span role="checkbox" aria-checked="${t}" aria-label="${t}">${t?"&#x1F5F9;":"&#9744;"}</span>`}function Me(t){if(t==null||t==="")return"";if(t instanceof Date)return isNaN(t.getTime())?"":t.toLocaleDateString();if(typeof t=="number"||typeof t=="string"){const e=new Date(t);return isNaN(e.getTime())?"":e.toLocaleDateString()}return""}function ke(t){if(!t)return-1;const e=t.getAttribute("data-row");if(e)return parseInt(e,10);const i=t.closest(".data-grid-row");if(!i)return-1;const o=i.parentElement;if(!o)return-1;const n=o.querySelectorAll(":scope > .data-grid-row");for(let r=0;r<n.length;r++)if(n[r]===i)return r;return-1}function Mt(t){if(!t)return-1;const e=t.getAttribute("data-col");return e?parseInt(e,10):-1}function ae(t){t&&t.querySelectorAll(".cell-focus").forEach(e=>e.classList.remove("cell-focus"))}function kt(t){try{if(getComputedStyle(t).direction==="rtl")return"rtl"}catch{}try{if(t.closest?.("[dir]")?.getAttribute("dir")==="rtl")return"rtl"}catch{}return"ltr"}function Ie(t){return kt(t)==="rtl"}function Ne(t,e){const i=e.renderer||e.viewRenderer;if(i)return i;if(!e.type)return;const o=t.__frameworkAdapter;if(o?.getTypeDefault){const n=o.getTypeDefault(e.type);if(n?.renderer)return n.renderer}}function qe(t,e){if(e.format)return e.format;if(!e.type)return;const i=t.__frameworkAdapter;if(i?.getTypeDefault){const o=i.getTypeDefault(e.type);if(o?.format)return o.format}}const $e='input,select,textarea,[contenteditable="true"],[contenteditable=""],[tabindex]:not([tabindex="-1"])';function ce(t){return(t.__editingCellCount??0)>0}function de(t){t.__editingCellCount=0,t.removeAttribute("data-has-editing"),t.querySelectorAll(".cell.editing").forEach(i=>i.classList.remove("editing"))}const We=document.createElement("template");We.innerHTML='<div class="cell" role="gridcell" part="cell"></div>';const Fe=document.createElement("template");Fe.innerHTML='<div class="data-grid-row" role="row" part="row"></div>';function It(){return We.content.firstElementChild.cloneNode(!0)}function Nt(){return Fe.content.firstElementChild.cloneNode(!0)}function Q(t){t.__cellDisplayCache=void 0,t.__cellCacheEpoch=void 0,t.__hasSpecialColumns=void 0}function qt(t,e,i,o,n){const r=Math.max(0,i-e),s=t._bodyEl,l=t._visibleColumns,a=l.length;let c=t.__cachedHeaderRowCount;for(c===void 0&&(c=t.querySelector(".header-group-row")?2:1,t.__cachedHeaderRowCount=c);t._rowPool.length<r;){const p=Nt();t._rowPool.push(p)}if(t._rowPool.length>r){for(let p=r;p<t._rowPool.length;p++){const g=t._rowPool[p];g.parentNode===s&&g.remove()}t._rowPool.length=r}const h=n&&t.__hasRenderRowPlugins!==!1,f=t._hasAfterRowRenderHook?.()??!1;for(let p=0;p<r;p++){const g=e+p,u=t._rows[g],d=t._rowPool[p];if(d.setAttribute("aria-rowindex",String(g+c+1)),h&&n(u,d,g)){d.__epoch=o,d.__rowDataRef=u,d.parentNode!==s&&s.appendChild(d);continue}const w=d.__epoch,v=d.__rowDataRef;let b=d.children.length;b>a&&d.lastElementChild?.classList.contains("tbw-row-loading-overlay")&&b--;const T=w===o&&b===a,S=v!==u,H=!!t._isGridEditMode;let C=!1;if(T&&S){for(let R=0;R<a;R++)if(l[R].externalView&&!d.querySelector(`.cell[data-col="${R}"] [data-external-view]`)){C=!0;break}}if(!T||C){const R=ce(d),z=H||t._activeEditRows===g;R&&!z?(d.__isCustomRow&&(d.className="data-grid-row",d.setAttribute("role","row"),d.__isCustomRow=!1),de(d),B(t,d,u,g),d.__epoch=o,d.__rowDataRef=u):R&&z?(he(t,d,u,g),d.__rowDataRef=u):(d.__isCustomRow&&(d.className="data-grid-row",d.setAttribute("role","row"),d.__isCustomRow=!1),B(t,d,u,g),d.__epoch=o,d.__rowDataRef=u)}else if(S){const R=ce(d),z=H||t._activeEditRows===g;R&&!z?(de(d),B(t,d,u,g),d.__epoch=o,d.__rowDataRef=u):(he(t,d,u,g),d.__rowDataRef=u)}else{const R=ce(d),z=H||t._activeEditRows===g;R&&!z?(de(d),B(t,d,u,g),d.__epoch=o,d.__rowDataRef=u):he(t,d,u,g)}let y=!1;const m=t.changedRowIds;if(m&&m.length>0)try{const R=t.getRowId?.(u);R&&(y=m.includes(R))}catch{}const L=d.classList.contains("changed");y!==L&&d.classList.toggle("changed",y);const $=t.effectiveConfig?.rowClass;if($){const R=d.getAttribute("data-dynamic-classes");R&&R.split(" ").forEach(z=>z&&d.classList.remove(z));try{const z=$(u);if(z&&z.length>0){const Se=z.filter(se=>se&&typeof se=="string");Se.forEach(se=>d.classList.add(se)),d.setAttribute("data-dynamic-classes",Se.join(" "))}else d.removeAttribute("data-dynamic-classes")}catch(z){console.warn("[tbw-grid] rowClass callback error:",z),d.removeAttribute("data-dynamic-classes")}}f&&t._afterRowRender?.({row:u,rowIndex:g,rowElement:d}),d.parentNode!==s&&s.appendChild(d)}}function he(t,e,i,o){const n=e.children,r=t._visibleColumns,s=r.length,l=n.length,a=s<l?s:l,c=t._focusRow,h=t._focusCol,f=t._hasAfterCellRenderHook?.()??!1;let p=t.__hasSpecialColumns;if(p===void 0){p=!1;const u=t.__frameworkAdapter;for(let d=0;d<s;d++){const w=r[d];if(w.__viewTemplate||w.__compiledView||w.renderer||w.viewRenderer||w.externalView||w.format||w.type==="date"||w.type==="boolean"||w.type&&u?.getTypeDefault?.(w.type)?.renderer||w.type&&u?.getTypeDefault?.(w.type)?.format){p=!0;break}}t.__hasSpecialColumns=p}const g=String(o);if(!p){for(let u=0;u<a;u++){const d=n[u];if(d.classList.contains("editing"))continue;d.firstElementChild&&t.__frameworkAdapter?.releaseCell?.(d);const w=r[u],v=i[w.field];d.textContent=v==null?"":String(v),d.getAttribute("data-row")!==g&&d.setAttribute("data-row",g);const b=c===o&&h===u,E=d.classList.contains("cell-focus");b!==E&&(d.classList.toggle("cell-focus",b),d.setAttribute("aria-selected",String(b))),f&&t._afterCellRender?.({row:i,rowIndex:o,column:w,colIndex:u,value:v,cellElement:d,rowElement:e})}return}for(let u=0;u<a;u++)if(r[u].externalView&&!n[u].querySelector("[data-external-view]")){B(t,e,i,o);return}for(let u=0;u<a;u++){const d=r[u],w=n[u];w.getAttribute("data-row")!==g&&w.setAttribute("data-row",g);const v=c===o&&h===u,b=w.classList.contains("cell-focus");v!==b&&(w.classList.toggle("cell-focus",v),w.setAttribute("aria-selected",String(v)));const E=d.cellClass;if(E){const y=w.getAttribute("data-dynamic-classes");y&&y.split(" ").forEach(m=>m&&w.classList.remove(m));try{const m=i[d.field],L=E(m,i,d);if(L&&L.length>0){const $=L.filter(R=>R&&typeof R=="string");$.forEach(R=>w.classList.add(R)),w.setAttribute("data-dynamic-classes",$.join(" "))}else w.removeAttribute("data-dynamic-classes")}catch(m){console.warn(`[tbw-grid] cellClass callback error for column '${d.field}':`,m),w.removeAttribute("data-dynamic-classes")}}if(w.classList.contains("editing"))continue;const T=Ne(t,d);if(T){const y=i[d.field],m=T({row:i,value:y,field:d.field,column:d,cellEl:w});typeof m=="string"?(t.__frameworkAdapter?.releaseCell?.(w),w.innerHTML=F(m)):m instanceof Node?m.parentElement!==w&&(t.__frameworkAdapter?.releaseCell?.(w),w.innerHTML="",w.appendChild(m)):m==null&&(t.__frameworkAdapter?.releaseCell?.(w),w.textContent=y==null?"":String(y)),f&&t._afterCellRender?.({row:i,rowIndex:o,column:d,colIndex:u,value:y,cellElement:w,rowElement:e});continue}if(d.__viewTemplate||d.__compiledView||d.externalView)continue;const S=i[d.field];let H;const C=qe(t,d);if(C){try{const y=C(S,i);H=y==null?"":String(y)}catch(y){console.warn(`[tbw-grid] Format error in column '${d.field}':`,y),H=S==null?"":String(S)}w.textContent=H}else d.type==="date"?(H=Me(S),w.textContent=H):d.type==="boolean"?w.innerHTML=Oe(!!S):(H=S==null?"":String(S),w.textContent=H);f&&t._afterCellRender?.({row:i,rowIndex:o,column:d,colIndex:u,value:S,cellElement:w,rowElement:e})}}function B(t,e,i,o){e.classList.remove("tbw-row-loading"),e.removeAttribute("aria-busy");const n=t.__frameworkAdapter;if(n?.releaseCell){const p=e.children;for(let g=p.length-1;g>=0;g--)n.releaseCell(p[g])}e.innerHTML="";const r=t._visibleColumns,s=r.length,l=t._focusRow,a=t._focusCol,c=t,h=t._hasAfterCellRenderHook?.()??!1,f=document.createDocumentFragment();for(let p=0;p<s;p++){const g=r[p],u=It();u.setAttribute("aria-colindex",String(p+1)),u.setAttribute("data-col",String(p)),u.setAttribute("data-row",String(o)),u.setAttribute("data-field",g.field),u.setAttribute("data-header",g.header??g.field),g.type&&u.setAttribute("data-type",g.type);let d=i[g.field];const w=qe(t,g);if(w)try{d=w(d,i)}catch(C){console.warn(`[tbw-grid] Format error in column '${g.field}':`,C)}const v=g.__compiledView,b=g.__viewTemplate,E=Ne(t,g),T=g.externalView;let S=!1;if(E){const C=E({row:i,value:d,field:g.field,column:g,cellEl:u});typeof C=="string"?(u.innerHTML=F(C),S=!0):C instanceof Node?C.parentElement!==u&&(u.textContent="",u.appendChild(C)):C==null&&(u.textContent=d==null?"":String(d))}else if(T){const C=T,y=document.createElement("div");y.setAttribute("data-external-view",""),y.setAttribute("data-field",g.field),u.appendChild(y);const m={row:i,value:d,field:g.field,column:g};if(C.mount)try{C.mount({placeholder:y,context:m,spec:C})}catch(L){console.warn(`[tbw-grid] External view mount error for column '${g.field}':`,L)}else queueMicrotask(()=>{try{c.dispatchEvent(new CustomEvent("mount-external-view",{bubbles:!0,composed:!0,detail:{placeholder:y,spec:C,context:m}}))}catch(L){console.warn(`[tbw-grid] External view event dispatch error for column '${g.field}':`,L)}});y.setAttribute("data-mounted","")}else if(v){const C=v({row:i,value:d,field:g.field,column:g}),y=v.__blocked;u.innerHTML=y?"":F(C),S=!0,y&&(u.textContent="",u.setAttribute("data-blocked-template",""))}else if(b){const C=b.innerHTML;/Reflect\.|\bProxy\b|ownKeys\(/.test(C)?(u.textContent="",u.setAttribute("data-blocked-template","")):(u.innerHTML=F(ze(C,{row:i,value:d})),S=!0)}else w?u.textContent=d==null?"":String(d):g.type==="date"?u.textContent=Me(d):g.type==="boolean"?u.innerHTML=Oe(!!d):u.textContent=d==null?"":String(d);if(S){zt(u);const C=u.textContent||"";/Proxy|Reflect\.ownKeys/.test(C)&&(u.textContent=C.replace(/Proxy|Reflect\.ownKeys/g,"").trim(),/Proxy|Reflect\.ownKeys/.test(u.textContent||"")&&(u.textContent=""))}u.hasAttribute("data-blocked-template")&&(u.textContent||"").trim().length&&(u.textContent=""),g.editable?u.tabIndex=0:g.type==="boolean"&&(u.hasAttribute("tabindex")||(u.tabIndex=0)),l===o&&a===p?(u.classList.add("cell-focus"),u.setAttribute("aria-selected","true")):u.setAttribute("aria-selected","false");const H=g.cellClass;if(H)try{const C=i[g.field],y=H(C,i,g);if(y&&y.length>0){const m=y.filter(L=>L&&typeof L=="string");m.forEach(L=>u.classList.add(L)),u.setAttribute("data-dynamic-classes",m.join(" "))}}catch(C){console.warn(`[tbw-grid] cellClass callback error for column '${g.field}':`,C)}h&&t._afterCellRender?.({row:i,rowIndex:o,column:g,colIndex:p,value:d,cellElement:u,rowElement:e}),f.appendChild(u)}e.appendChild(f)}function Ue(t,e,i){if(e.target?.closest(".resize-handle"))return;const o=i.querySelector(".cell[data-row]"),n=ke(o);if(n<0)return;const r=t._rows[n];if(!r||t._dispatchRowClick?.(e,n,r,i))return;const s=e.target?.closest(".cell[data-col]");if(s){const l=Number(s.getAttribute("data-col"));if(!isNaN(l)){if(t._dispatchCellClick?.(e,n,l,s))return;const a=t._focusRow!==n||t._focusCol!==l;if(t._focusRow=n,t._focusCol=l,s.classList.contains("editing")){a&&(ae(t._bodyEl??t),s.classList.add("cell-focus"));const c=s.querySelector($e);try{c?.focus({preventScroll:!0})}catch{}return}U(t)}}}function $t(t,e){if(t._dispatchKeyDown?.(e))return;const i=t._rows.length-1,o=t._visibleColumns.length-1,n=t._activeEditRows!==void 0&&t._activeEditRows!==-1,s=t._visibleColumns[t._focusCol]?.type,l=e.composedPath?.()??[],a=l.length?l[0]:e.target,c=h=>{if(!h)return!1;const f=h.tagName;return!!(f==="INPUT"||f==="SELECT"||f==="TEXTAREA"||h.isContentEditable)};if(!(c(a)&&(e.key==="Home"||e.key==="End"))&&!(c(a)&&(e.key==="ArrowUp"||e.key==="ArrowDown")&&a.tagName==="INPUT"&&a.type==="number")&&!(c(a)&&(e.key==="ArrowLeft"||e.key==="ArrowRight"))&&!(c(a)&&(e.key==="Enter"||e.key==="Escape"))&&!(n&&s==="select"&&(e.key==="ArrowDown"||e.key==="ArrowUp"))){switch(e.key){case"Tab":{e.preventDefault(),!e.shiftKey?t._focusCol<o?t._focusCol+=1:(typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow<i&&(t._focusRow+=1,t._focusCol=0)):t._focusCol>0?t._focusCol-=1:t._focusRow>0&&(typeof t.commitActiveRowEdit=="function"&&t._activeEditRows===t._focusRow&&t.commitActiveRowEdit(),t._focusRow-=1,t._focusCol=o),U(t);return}case"ArrowDown":n&&typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow=Math.min(i,t._focusRow+1),e.preventDefault();break;case"ArrowUp":n&&typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow=Math.max(0,t._focusRow-1),e.preventDefault();break;case"ArrowRight":{Ie(t)?t._focusCol=Math.max(0,t._focusCol-1):t._focusCol=Math.min(o,t._focusCol+1),e.preventDefault();break}case"ArrowLeft":{Ie(t)?t._focusCol=Math.min(o,t._focusCol+1):t._focusCol=Math.max(0,t._focusCol-1),e.preventDefault();break}case"Home":(e.ctrlKey||e.metaKey)&&(n&&typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow=0),t._focusCol=0,e.preventDefault(),U(t,{forceScrollLeft:!0});return;case"End":(e.ctrlKey||e.metaKey)&&(n&&typeof t.commitActiveRowEdit=="function"&&t.commitActiveRowEdit(),t._focusRow=i),t._focusCol=o,e.preventDefault(),U(t,{forceScrollRight:!0});return;case"PageDown":t._focusRow=Math.min(i,t._focusRow+20),e.preventDefault();break;case"PageUp":t._focusRow=Math.max(0,t._focusRow-20),e.preventDefault();break;case"Enter":{const h=t._focusRow,f=t._focusCol,p=t._visibleColumns[f],g=t._rows[h],u=p?.field??"",d=u&&g?g[u]:void 0,w=t.querySelector(`[data-row="${h}"][data-col="${f}"]`),v={rowIndex:h,colIndex:f,field:u,value:d,row:g,cellEl:w,trigger:"keyboard",originalEvent:e},b=new CustomEvent("cell-activate",{cancelable:!0,detail:v});t.dispatchEvent(b);const E=new CustomEvent("activate-cell",{cancelable:!0,detail:{row:h,col:f}});if(t.dispatchEvent(E),b.defaultPrevented||E.defaultPrevented){e.preventDefault();return}break}default:return}U(t)}}function U(t,e){if(t._virtualization?.enabled){const{rowHeight:s,container:l,viewportEl:a}=t._virtualization,c=l,h=a?.clientHeight??c?.clientHeight??0;if(c&&h>0){const f=t._focusRow*s;f<c.scrollTop?c.scrollTop=f:f+s>c.scrollTop+h&&(c.scrollTop=f-h+s)}}const i=t._activeEditRows!==void 0&&t._activeEditRows!==-1;i||t.refreshVirtualWindow(!1),ae(t._bodyEl),Array.from(t._bodyEl.querySelectorAll('[aria-selected="true"]')).forEach(s=>{s.setAttribute("aria-selected","false")});const o=t._focusRow,n=t._virtualization.start??0,r=t._virtualization.end??t._rows.length;if(o>=n&&o<r){const s=t._bodyEl.querySelectorAll(".data-grid-row")[o-n];let l=s?.children[t._focusCol];if((!l||!l.classList?.contains("cell"))&&(l=s?.querySelector(`.cell[data-col="${t._focusCol}"]`)??s?.querySelector(".cell[data-col]")),l){l.classList.add("cell-focus"),l.setAttribute("aria-selected","true");const a=t.querySelector(".tbw-scroll-area");if(a&&l&&(!i||e?.forceHorizontalScroll))if(e?.forceScrollLeft)a.scrollLeft=0;else if(e?.forceScrollRight)a.scrollLeft=a.scrollWidth-a.clientWidth;else{const c=t._getHorizontalScrollOffsets?.(s??void 0,l)??{left:0,right:0};if(!c.skipScroll){const h=l.getBoundingClientRect(),f=a.getBoundingClientRect(),p=h.left-f.left+a.scrollLeft,g=p+h.width,u=a.scrollLeft+c.left,d=a.scrollLeft+a.clientWidth-c.right;p<u?a.scrollLeft=p-c.left:g>d&&(a.scrollLeft=g-a.clientWidth+c.right)}}if(t._activeEditRows!==void 0&&t._activeEditRows!==-1&&l.classList.contains("editing")){const c=l.querySelector($e);if(c&&document.activeElement!==c)try{c.focus({preventScroll:!0})}catch{}}else if(!l.contains(document.activeElement)){l.hasAttribute("tabindex")||l.setAttribute("tabindex","-1");try{l.focus({preventScroll:!0})}catch{}}}}}const Z=new WeakMap;function Wt(t,e){const i=ke(e),o=Mt(e);if(i<0||o<0)return;t._focusRow=i,t._focusCol=o,ae(t._bodyEl),e.classList.add("cell-focus"),e.setAttribute("aria-selected","true");const n=e.closest("tbw-grid");n&&document.activeElement!==n&&n.focus({preventScroll:!0})}function ue(t,e,i,o){let n=null;const r=i.composedPath?.();if(r&&r.length>0?n=r[0]:n=i.target,n&&!e.contains(n)){const d=document.elementFromPoint(i.clientX,i.clientY);d&&(n=d)}const s=n?.closest?.("[data-col]"),l=n?.closest?.(".data-grid-row"),a=n?.closest?.(".header-row");let c,h,f,p,g,u;return s&&(c=parseInt(s.getAttribute("data-row")??"-1",10),h=parseInt(s.getAttribute("data-col")??"-1",10),c>=0&&h>=0&&(f=t._rows[c],u=t._columns[h],p=u?.field,g=f&&p?f[p]:void 0)),{type:o,row:f,rowIndex:c!==void 0&&c>=0?c:void 0,colIndex:h!==void 0&&h>=0?h:void 0,field:p,value:g,column:u,originalEvent:i,cellElement:s??void 0,rowElement:l??void 0,isHeader:!!a,cell:c!==void 0&&h!==void 0&&c>=0&&h>=0?{row:c,col:h}:void 0}}function Ft(t,e,i){const o=ue(t,e,i,"mousedown");(t._dispatchCellMouseDown?.(o)??!1)&&Z.set(t,!0)}function Ut(t,e,i){if(!Z.get(t))return;const o=ue(t,e,i,"mousemove");t._dispatchCellMouseMove?.(o)}function Vt(t,e,i){if(!Z.get(t))return;const o=ue(t,e,i,"mouseup");t._dispatchCellMouseUp?.(o),Z.set(t,!1)}function Gt(t,e,i){e.addEventListener("mousedown",o=>{const n=o.target.closest(".cell[data-col]");n&&(n.classList.contains("editing")||(o.preventDefault(),Wt(t,n)))},{signal:i}),e.addEventListener("click",o=>{const n=o.target.closest(".data-grid-row");if(n&&Ue(t,o,n),!document.activeElement?.closest(".cell.editing")){const r=o.target.closest("tbw-grid");r&&r.focus({preventScroll:!0})}},{signal:i}),e.addEventListener("dblclick",o=>{const n=o.target.closest(".data-grid-row");n&&Ue(t,o,n)},{signal:i})}function Bt(t,e,i,o){e.addEventListener("keydown",n=>$t(t,n),{signal:o}),i.addEventListener("mousedown",n=>Ft(t,i,n),{signal:o}),document.addEventListener("mousemove",n=>Ut(t,i,n),{signal:o}),document.addEventListener("mouseup",n=>Vt(t,i,n),{signal:o})}function Ve(t,e){return t==null&&e==null?0:t==null?-1:e==null||t>e?1:t<e?-1:0}function Ge(t,e,i){const n=i.find(l=>l.field===e.field)?.sortComparator??Ve,{field:r,direction:s}=e;return[...t].sort((l,a)=>n(l[r],a[r],l,a)*s)}function Be(t,e,i,o){t._rows=e,t.__rowRenderEpoch++,t._rowPool.forEach(n=>n.__epoch=-1),oe(t),t.refreshVirtualWindow(!0),t.dispatchEvent(new CustomEvent("sort-change",{detail:{field:i.field,direction:o}})),t.requestStateChange?.()}function Xe(t,e){!t._sortState||t._sortState.field!==e.field?(t._sortState||(t.__originalOrder=t._rows.slice()),Ye(t,e,1)):t._sortState.direction===1?Ye(t,e,-1):(t._sortState=null,t.__rowRenderEpoch++,t._rowPool.forEach(o=>o.__epoch=-1),t._rows=t.__originalOrder.slice(),oe(t),t._headerRowEl?.querySelectorAll('[role="columnheader"].sortable')?.forEach(o=>{o.getAttribute("aria-sort")?(o.getAttribute("aria-sort")==="ascending"||o.getAttribute("aria-sort")==="descending")&&(t._sortState||o.setAttribute("aria-sort","none")):o.setAttribute("aria-sort","none")}),t.refreshVirtualWindow(!0),t.dispatchEvent(new CustomEvent("sort-change",{detail:{field:e.field,direction:0}})),t.requestStateChange?.())}function Ye(t,e,i){t._sortState={field:e.field,direction:i};const o={field:e.field,direction:i},n=t._columns,s=(t.effectiveConfig?.sortHandler??Ge)(t._rows,o,n);s&&typeof s.then=="function"?s.then(l=>{Be(t,l,e,i)}):Be(t,s,e,i)}function X(t,e){return t.effectiveConfig?.sortable!==!1&&e.sortable===!0}function J(t,e){return t.effectiveConfig?.resizable!==!1&&e.resizable!==!1}function Xt(t,e){typeof e=="string"?t.textContent=e:e instanceof HTMLElement&&(t.innerHTML="",t.appendChild(e.cloneNode(!0)))}function ee(t,e){const i=document.createElement("span");xe(i,"sort-indicator");const o=t._sortState?.field===e.field?t._sortState.direction:0,n={...k,...t.icons},r=o===1?n.sortAsc:o===-1?n.sortDesc:n.sortNone;return Xt(i,r),i}function te(t,e,i){const o=document.createElement("div");return o.className="resize-handle",o.setAttribute("aria-hidden","true"),o.addEventListener("mousedown",n=>{n.stopPropagation(),n.preventDefault(),t._resizeController.start(n,e,i)}),o.addEventListener("dblclick",n=>{n.stopPropagation(),n.preventDefault(),t._resizeController.resetColumn(e)}),o}function ie(t,e,i,o){o.classList.add("sortable"),o.tabIndex=0;const n=t._sortState?.field===e.field?t._sortState.direction:0;o.setAttribute("aria-sort",n===0?"none":n===1?"ascending":"descending"),o.addEventListener("click",r=>{t._resizeController?.isResizing||t._dispatchHeaderClick?.(r,i,o)||Xe(t,e)}),o.addEventListener("keydown",r=>{if(r.key==="Enter"||r.key===" "){if(r.preventDefault(),t._dispatchHeaderClick?.(r,i,o))return;Xe(t,e)}})}function Yt(t,e){if(e!=null)if(typeof e=="string"){const i=document.createElement("span");for(i.innerHTML=F(e);i.firstChild;)t.appendChild(i.firstChild)}else e instanceof Node&&t.appendChild(e)}function oe(t){t._headerRowEl=t.findHeaderRow();const e=t._headerRowEl;e&&(e.innerHTML="",t._visibleColumns.forEach((i,o)=>{const n=document.createElement("div");n.className="cell",xe(n,"header-cell"),n.setAttribute("role","columnheader"),n.setAttribute("aria-colindex",String(o+1)),n.setAttribute("data-field",i.field),n.setAttribute("data-col",String(o));const r=i.header??i.field,s=t._sortState?.field===i.field?t._sortState.direction:0,l=s===1?"asc":s===-1?"desc":null;if(i.headerRenderer){const a={column:i,value:r,sortState:l,filterActive:!1,cellEl:n,renderSortIcon:()=>X(t,i)?ee(t,i):null,renderFilterButton:()=>null},c=i.headerRenderer(a);Yt(n,c),X(t,i)&&ie(t,i,o,n),J(t,i)&&(n.classList.add("resizable"),n.appendChild(te(t,o,n)))}else if(i.headerLabelRenderer){const a={column:i,value:r},c=i.headerLabelRenderer(a),h=document.createElement("span");c==null?h.textContent=r:typeof c=="string"?h.innerHTML=F(c):c instanceof Node&&h.appendChild(c),n.appendChild(h),X(t,i)&&(ie(t,i,o,n),n.appendChild(ee(t,i))),J(t,i)&&(n.classList.add("resizable"),n.appendChild(te(t,o,n)))}else if(i.__headerTemplate)Array.from(i.__headerTemplate.childNodes).forEach(a=>n.appendChild(a.cloneNode(!0))),X(t,i)&&(ie(t,i,o,n),n.appendChild(ee(t,i))),J(t,i)&&(n.classList.add("resizable"),n.appendChild(te(t,o,n)));else{const a=document.createElement("span");a.textContent=r,n.appendChild(a),X(t,i)&&(ie(t,i,o,n),n.appendChild(ee(t,i))),J(t,i)&&(n.classList.add("resizable"),n.appendChild(te(t,o,n)))}e.appendChild(n)}),e.querySelectorAll(".cell.sortable").forEach(i=>{i.getAttribute("aria-sort")||i.setAttribute("aria-sort","none")}),e.children.length>0?(e.setAttribute("role","row"),e.setAttribute("aria-rowindex","1")):(e.removeAttribute("role"),e.removeAttribute("aria-rowindex")))}const je=typeof requestIdleCallback=="function";function jt(t,e){return je?requestIdleCallback(t,e):window.setTimeout(()=>{const i=Date.now();t({didTimeout:!1,timeRemaining:()=>Math.max(0,50-(Date.now()-i))})},1)}function Ke(t){je?cancelIdleCallback(t):clearTimeout(t)}function Kt(t){const e=document.createElement("div");return e.className=`tbw-spinner tbw-spinner--${t}`,e.setAttribute("role","progressbar"),e.setAttribute("aria-label","Loading"),e}function Qt(t,e){if(e){const o=e({size:t});if(typeof o=="string"){const n=document.createElement("div");return n.innerHTML=o,n}return o}return Kt(t)}function Zt(t){const e=document.createElement("div");return e.className="tbw-loading-overlay",e.setAttribute("role","status"),e.setAttribute("aria-live","polite"),e.appendChild(Qt("large",t)),e}function Jt(t,e){t.appendChild(e)}function ei(t){t?.remove()}function ti(t,e){if(e){if(t.classList.add("tbw-row-loading"),t.setAttribute("aria-busy","true"),!t.querySelector(".tbw-row-loading-overlay")){const i=document.createElement("div");i.className="tbw-row-loading-overlay",i.setAttribute("aria-hidden","true");const o=document.createElement("div");o.className="tbw-row-loading-spinner",i.appendChild(o),t.appendChild(i)}}else t.classList.remove("tbw-row-loading"),t.removeAttribute("aria-busy"),t.querySelector(".tbw-row-loading-overlay")?.remove()}function ii(t,e){e?(t.classList.add("tbw-cell-loading"),t.setAttribute("aria-busy","true")):(t.classList.remove("tbw-cell-loading"),t.removeAttribute("aria-busy"))}var A=(t=>(t[t.STYLE=1]="STYLE",t[t.VIRTUALIZATION=2]="VIRTUALIZATION",t[t.HEADER=3]="HEADER",t[t.ROWS=4]="ROWS",t[t.COLUMNS=5]="COLUMNS",t[t.FULL=6]="FULL",t))(A||{});class oi{#l;#n=0;#u=0;#d=null;#h=null;#a=null;#o=!1;constructor(e){this.#l=e}requestPhase(e,i){e>this.#n&&(this.#n=e),this.#u===0&&(this.#f(),this.#u=requestAnimationFrame(()=>this.#w()))}whenReady(){return this.#d?this.#d:Promise.resolve()}setInitialReadyResolver(e){this.#a=e}cancel(){this.#u!==0&&(cancelAnimationFrame(this.#u),this.#u=0),this.#n=0,this.#h&&(this.#h(),this.#h=null,this.#d=null)}get isPending(){return this.#n!==0}get pendingPhase(){return this.#n}#f(){this.#d||(this.#d=new Promise(e=>{this.#h=e}))}#w(){if(this.#u=0,!this.#l.isConnected()){this.#n=0,this.#h&&(this.#h(),this.#h=null,this.#d=null);return}const e=this.#n;this.#n=0,e>=5&&this.#l.mergeConfig(),e>=4&&this.#l.processRows(),e>=5&&(this.#l.processColumns(),this.#l.updateTemplate()),e>=3&&this.#l.renderHeader(),e>=2&&this.#l.renderVirtualWindow(),e>=1&&this.#l.afterRender(),!this.#o&&this.#a&&(this.#o=!0,this.#a()),this.#h&&(this.#h(),this.#h=null,this.#d=null)}}function Qe(t){let e=null,i=null,o=null,n=null;const r=a=>{if(!e)return;const c=a.clientX-e.startX,h=Math.max(40,e.startWidth+c),f=t._visibleColumns[e.colIndex];f.width=h,f.__userResized=!0,f.__renderedWidth=h,i==null&&(i=requestAnimationFrame(()=>{i=null,t.updateTemplate?.()})),t.dispatchEvent(new CustomEvent("column-resize",{detail:{field:f.field,width:h}}))};let s=!1;const l=()=>{const a=e!==null;a&&(s=!0,requestAnimationFrame(()=>{s=!1})),window.removeEventListener("mousemove",r),window.removeEventListener("mouseup",l),o!==null&&(document.documentElement.style.cursor=o,o=null),n!==null&&(document.body.style.userSelect=n,n=null),e=null,a&&t.requestStateChange&&t.requestStateChange()};return{get isResizing(){return e!==null||s},start(a,c,h){a.preventDefault();const f=t._visibleColumns[c],p=typeof f?.width=="number"?f.width:void 0,g=f?.__renderedWidth??p??h.getBoundingClientRect().width;e={startX:a.clientX,colIndex:c,startWidth:g},window.addEventListener("mousemove",r),window.addEventListener("mouseup",l),o===null&&(o=document.documentElement.style.cursor),document.documentElement.style.cursor="e-resize",n===null&&(n=document.body.style.userSelect),document.body.style.userSelect="none"},resetColumn(a){const c=t._visibleColumns[a];c&&(c.__userResized=!1,c.__renderedWidth=void 0,c.width=c.__originalWidth,t.updateTemplate?.(),t.requestStateChange?.(),t.dispatchEvent(new CustomEvent("column-resize-reset",{detail:{field:c.field,width:c.width}})))},dispose(){l()}}}const fe="data-animating",ni={change:"--tbw-row-change-duration",insert:"--tbw-row-insert-duration",remove:"--tbw-row-remove-duration"},ri={change:500,insert:300,remove:200};function si(t){const e=t.trim().toLowerCase();return e.endsWith("ms")?parseFloat(e):e.endsWith("s")?parseFloat(e)*1e3:parseFloat(e)}function li(t,e){const i=ni[e],o=getComputedStyle(t).getPropertyValue(i);if(o){const n=si(o);if(!isNaN(n)&&n>0)return n}return ri[e]}function ai(t,e,i){t.removeAttribute(fe),t.offsetWidth,t.setAttribute(fe,e);const o=li(t,e);setTimeout(()=>{e!=="remove"&&t.removeAttribute(fe)},o)}function ge(t,e,i){if(e<0)return!1;const o=t.findRenderedRowElement?.(e);return o?(ai(o,i),!0):!1}function ci(t,e,i){let o=0;for(const n of e)ge(t,n,i)&&o++;return o}function di(t,e,i){const o=t._rows??[],n=t.getRowId;if(!n)return!1;const r=o.findIndex(s=>{if(s==null)return!1;try{return n(s)===e}catch{return!1}});return r<0?!1:ge(t,r,i)}function ne(t,e,i){const o=document.createElement(t);if(e)for(const n in e){const r=e[n];r!=null&&o.setAttribute(n,r)}return o}function P(t,e){const i=document.createElement("div");if(t&&(i.className=t),e)for(const o in e){const n=e[o];n!=null&&i.setAttribute(o,n)}return i}function Ze(t,e,i){const o=document.createElement("button");if(t&&(o.className=t),e)for(const n in e){const r=e[n];r!=null&&o.setAttribute(n,r)}return o}const Je=document.createElement("template");Je.innerHTML=`
2
2
  <div class="tbw-scroll-area">
3
3
  <div class="rows-body-wrapper">
4
4
  <div class="rows-body" role="grid">