|
1
|
|
-(()=>{"use strict";const e={title:"asc-icon",src:"<svg fill='#323131' viewBox='0 0 14 14' role='img' focusable='false' aria-hidden='true' xmlns='http://www.w3.org/2000/svg' stroke='#323131'><g id='SVGRepo_bgCarrier' stroke-width='0'></g><g id='SVGRepo_tracerCarrier' stroke-linecap='round' stroke-linejoin='round'></g><g id='SVGRepo_iconCarrier'><path d='m 11.363635,5.363637 c 0,0.1477267 -0.05398,0.2755673 -0.16193,0.383522 -0.107953,0.1079547 -0.235793,0.161932 -0.38352,0.161932 l -7.63637,0 c -0.1477267,0 -0.2755667,-0.053977 -0.38352,-0.161932 -0.1079533,-0.1079547 -0.16193,-0.2357953 -0.16193,-0.383522 0,-0.147728 0.053977,-0.2755693 0.16193,-0.383524 L 6.616475,1.161932 C 6.724435,1.0539773 6.8522783,1 7.000005,1 c 0.1477267,0 0.2755667,0.053977 0.38352,0.161932 l 3.81818,3.818181 c 0.107953,0.1079547 0.16193,0.235796 0.16193,0.383524 z'></path></g></svg>"},t={title:"desc-icon",src:"<svg fill='#323131' viewBox='0 0 14 14' role='img' focusable='false' aria-hidden='true' xmlns='http://www.w3.org/2000/svg' stroke='#323131'><g id='SVGRepo_bgCarrier' stroke-width='0'></g><g id='SVGRepo_tracerCarrier' stroke-linecap='round' stroke-linejoin='round'></g><g id='SVGRepo_iconCarrier'><path d='m 11.363635,8.636363 c 0,0.147728 -0.05398,0.275569 -0.16193,0.383523 l -3.81818,3.818182 C 7.2755717,12.946023 7.1477317,13 7.000005,13 6.8522783,13 6.724435,12.946023 6.616475,12.838068 L 2.798295,9.019886 C 2.6903417,8.911932 2.636365,8.784091 2.636365,8.636363 c 0,-0.1477267 0.053977,-0.2755673 0.16193,-0.383522 0.1079533,-0.1079547 0.2357933,-0.161932 0.38352,-0.161932 l 7.63637,0 c 0.147727,0 0.275567,0.053977 0.38352,0.161932 0.107953,0.1079547 0.16193,0.2357953 0.16193,0.383522 z'></path></g></svg>"},r={title:"default-sort-icon",src:"<svg xmlns='http://www.w3.org/2000/svg' fill='#323131' stroke='#323131' aria-hidden='true' viewBox='0 0 14 14'><path d='M11.364 8.636q0 .222-.162.384l-3.818 3.818Q7.222 13 7 13t-.384-.162L2.798 9.02q-.162-.162-.162-.384 0-.221.162-.383t.384-.162h7.636q.222 0 .384.162t.162.383zm0-3.272q0 .221-.162.383t-.384.162H3.182q-.222 0-.384-.162t-.162-.383q0-.222.162-.384l3.818-3.818Q6.778 1 7 1t.384.162l3.818 3.818q.162.162.162.384z'/></svg>"},o=(e,t)=>{e.forEach((e=>{e.classList.remove("sorted-asc","sorted-desc");const r=e.querySelector(".sorting");r&&(r.innerHTML="",r.appendChild(i(t.src)))}))},s=(e,t,r)=>{const o=e.querySelector("tbody");if(!o)return;const s=Array.from(o.querySelectorAll("tr"));0!==s.length&&(s.sort(((e,o)=>l(e,o,t,r))),o.innerHTML="",s.forEach((e=>o.appendChild(e))))},l=(e,t,r,o)=>{const s=e.cells[r]?.textContent.trim(),l=t.cells[r]?.textContent.trim();return void 0===s||void 0===l?0:isNaN(s)||isNaN(l)?o?s.localeCompare(l):l.localeCompare(s):o?s-l:l-s},i=e=>{if(!e)return null;const t=document.createElement("div");t.innerHTML=e.trim();const r=t.firstChild;return r&&"svg"===r.tagName&&(r.setAttribute("width","16"),r.setAttribute("height","16")),r},n=e=>{if(!e||"string"!=typeof e)return null;const t=e.trim();if(t.includes(",")){const e=t.split(",");return e.length>1?e[1].trim():null}return t};class a{constructor(e,t={}){if(this.table="string"==typeof e?document.querySelector(e):e,!this.table)throw new Error("Table element not found");this.config={fixedHeader:!1,fixedColumns:!1,leftFreeze:{from:null,to:null},rightFreeze:{from:null,to:null},minColumnWidth:120,...t},this.cellPositions=new Map,this.isInitialized=!1,this.observer=null,this.resizeObserver=null,this.init()}init(){this.isInitialized&&this.cleanup(),this.ensureTableWidth(),this.trackCellPositions(),this.applyStyles(),this.observeDOMChanges(),this.observeTableResize(),this.isInitialized=!0}observeDOMChanges(){const e=this.table.querySelector("tbody");e&&(this.observer&&this.observer.disconnect(),this.observer=new MutationObserver((e=>{let t=!1;for(const r of e)if("childList"===r.type&&(r.addedNodes.length||r.removedNodes.length)){t=!0;break}t&&(clearTimeout(this.debounceTimer),this.debounceTimer=setTimeout((()=>this.refreshPositions()),0))})),this.observer.observe(e,{childList:!0,subtree:!1}))}observeTableResize(){this.resizeObserver&&this.resizeObserver.disconnect();const e=this.table.closest(".table-freezer-container")||this.setupTableContainer();this.resizeObserver=new ResizeObserver((()=>{this.refreshPositions()})),this.resizeObserver.observe(e)}ensureTableWidth(){const e=this.table.querySelectorAll("tr");if(0===e.length)return;const t=e[0].querySelectorAll("th, td");t.forEach((e=>{const t=this.config.minColumnWidth;e.offsetWidth<t&&(e.style.minWidth=`${t}px`,e.style.width=`${t}px`)})),this.table.style.minWidth=t.length*this.config.minColumnWidth+"px"}trackCellPositions(){const e=this.table.querySelectorAll("tr");this.cellPositions.clear(),e.forEach(((e,t)=>{const r=e.querySelectorAll("th, td");let o=0;r.forEach(((e,r)=>{const s=`${t}-${r}`,l=e.getBoundingClientRect().width;this.cellPositions.set(s,{left:o,width:l,rowIndex:t,cellIndex:r}),e.setAttribute("data-row-index",t),e.setAttribute("data-cell-index",r),e.setAttribute("data-left-position",o),e.setAttribute("data-cell-width",l),o+=l}))}))}applyStyles(){this.setupTableContainer(),this.config.fixedColumns&&(null!==this.config.leftFreeze.from&&null!==this.config.leftFreeze.to&&this.applyLeftFreeze(),null!==this.config.rightFreeze.from&&null!==this.config.rightFreeze.to&&this.applyRightFreeze()),this.config.fixedHeader&&this.applyFixedHeader()}setupTableContainer(){let e=this.table.closest(".table-freezer-container");if(!e){const t=document.createElement("div");t.className="table-freezer-container",t.style.cssText="\n overflow: auto;\n position: relative;\n max-width: 100%;\n max-height: 500px;\n border: 1px solid #dee2e6;\n ",this.table.parentNode.insertBefore(t,this.table),t.appendChild(this.table),e=t}return this.table.style.cssText+="\n border-collapse: separate;\n border-spacing: 0;\n position: relative;\n ",e}applyFixedHeader(){const e=this.table.querySelector("thead");e&&(this.config.headerBgColor&&(e.style.backgroundColor=n(this.config.headerBgColor)),this.config.headerColor&&(e.style.color=n(this.config.headerColor)),e.style.cssText+="\n position: sticky;\n top: 0;\n z-index: 20;\n ",e.setAttribute("data-frozen","header"))}clearFrozenColumns(e="left"){this.table.querySelectorAll(`[data-frozen="${e}"]`).forEach((e=>{e.removeAttribute("data-frozen"),e.removeAttribute("data-freeze-position"),e.style.position="",e.style.left="",e.style.right="",e.style.zIndex="",e.hasAttribute("data-conditional-formatting")||(e.style.backgroundColor="",e.style.color="")}))}applyLeftFreeze(){let{from:e,to:t}=this.config.leftFreeze;if(null===e||null===t)return;t<e&&(t=e),this.clearFrozenColumns("left");let r=0;for(let o=e-1;o<t;o++){const e=this.table.querySelectorAll(`[data-cell-index="${o}"]`);if(!e.length)continue;e.forEach((e=>{const t=0===parseInt(e.getAttribute("data-row-index"))&&this.config.fixedHeader?20:15;e.style.position="sticky",e.style.left=`${Math.round(r)}px`,e.style.zIndex=t,e.hasAttribute("data-conditional-formatting")||(this.config.leftColumnBgColor&&(e.style.backgroundColor=n(this.config.leftColumnBgColor)),this.config.leftColumnColor&&(e.style.color=n(this.config.leftColumnColor))),e.setAttribute("data-frozen","left"),e.setAttribute("data-freeze-position",Math.round(r))}));const t=this.table.querySelector(`[data-cell-index="${o}"]`);t&&(r+=t.getBoundingClientRect().width)}}applyRightFreeze(){let{from:e,to:t}=this.config.rightFreeze;if(null===e||null===t)return;t<e&&(t=e),this.clearFrozenColumns("right");let r=0;for(let o=t-1;o>=e-1;o--){const e=this.table.querySelectorAll(`[data-cell-index="${o}"]`);if(!e.length)continue;e.forEach((e=>{const t=0===parseInt(e.getAttribute("data-row-index"))&&this.config.fixedHeader?20:15;e.style.position="sticky",e.style.right=`${Math.round(r)}px`,e.style.zIndex=t,e.hasAttribute("data-conditional-formatting")||(this.config.rightColumnBgColor&&(e.style.backgroundColor=n(this.config.rightColumnBgColor)),this.config.rightColumnColor&&(e.style.color=n(this.config.rightColumnColor))),e.setAttribute("data-frozen","right"),e.setAttribute("data-freeze-position",Math.round(r))}));const t=this.table.querySelector(`[data-cell-index="${o}"]`);t&&(r+=t.getBoundingClientRect().width)}}updateConfig(e){this.config={...this.config,...e},this.refreshPositions()}cleanup(){["left","right"].forEach((e=>this.clearFrozenColumns(e))),this.table.querySelectorAll("thead, tr, th, td").forEach((e=>{["data-frozen","data-freeze-position","data-row-index","data-cell-index","data-left-position","data-cell-width"].forEach((t=>e.removeAttribute(t))),"THEAD"===e.tagName||"TH"===e.tagName?(e.style.backgroundColor="",e.style.color=""):e.hasAttribute("data-conditional-formatting")||(e.style.position="",e.style.left="",e.style.right="",e.style.top="",e.style.zIndex="")})),this.table.style.position="",this.cellPositions.clear()}destroy(){this.cleanup(),this.observer&&this.observer.disconnect(),this.resizeObserver&&this.resizeObserver.disconnect();const e=this.table.closest(".table-freezer-container");e&&e.parentNode&&(e.parentNode.insertBefore(this.table,e),e.remove()),this.isInitialized=!1}getCellPosition(e,t){return this.cellPositions.get(`${e}-${t}`)}refreshPositions(){this.trackCellPositions(),this.applyStyles()}}const c=e=>{if(!e||"string"!=typeof e)return null;const t=e.trim();return t.includes(",")?t.split(",")[1]?.trim():t},d={equal:(e,t)=>e===t,notEqual:(e,t)=>e!==t,contains:(e,t)=>e.includes(t),notContains:(e,t)=>!e.includes(t),greaterThan:(e,t)=>isNaN(e)||isNaN(t)?e>t:parseFloat(e)>parseFloat(t),lessThan:(e,t)=>isNaN(e)||isNaN(t)?e<t:parseFloat(e)<parseFloat(t),greaterThanOrEqual:(e,t)=>isNaN(e)||isNaN(t)?e>=t:parseFloat(e)>=parseFloat(t),lessThanOrEqual:(e,t)=>isNaN(e)||isNaN(t)?e<=t:parseFloat(e)<=parseFloat(t)},h=e=>{if(!e?.element||!e?.data)return;const t=document.querySelector('iframe[name="editor-canvas"]')||document.querySelector("iframe"),r=(t?.contentDocument||t?.contentWindow?.document||document).querySelector(`.${e.element}`);if(!r)return;const o=Array.from(r.querySelectorAll("table")),s=[...new Set([...o])];s.length&&s.forEach((t=>{(e=>{e.querySelectorAll("[data-conditional-formatting='true']").forEach((e=>{["color","backgroundColor","background","backgroundSize"].forEach((t=>{e.style[t]=""}));const t=e.getAttribute("data-previous-class");t&&(e.classList.remove(t),e.removeAttribute("data-previous-class")),e.removeAttribute("data-conditional-formatting")}))})(t);const r=(e=>{const t=new Map,r=e.querySelector("tr:first-child");return r&&r.querySelectorAll("th, td").forEach(((e,r)=>{const o=e.textContent.trim().toLowerCase().replace(/\s+/g,"");t.set(o,r)})),t})(t);e.data.forEach((e=>{((e,t,r)=>{const{columnKey:o,comparisonType:s="equal",comparisonValue:l="",formatAction:i,formatTextColor:n,formatBackground:a,formatCssClass:h}=t;if(!o||!i)return;const u=r.get(o);if(void 0===u)return;const g=Array.from(e.querySelectorAll("tr")).slice(1),f=l.toString().trim().toLowerCase(),p=i.startsWith("column"),m=i.startsWith("row");g.forEach((e=>{const t=e.querySelectorAll("td, th"),r=t[u];if(!r)return;const o=r.textContent.trim().toLowerCase(),l=d[s]?.(o,f);let g=[];p?g=[r]:l&&(g=m?Array.from(t):[r]),g.forEach((e=>{((e,t)=>{const{formatAction:r,formatTextColor:o,formatBackground:s,formatCssClass:l}=t;if(r.includes("TextColor")&&o){const t=c(o);t&&(e.style.color=t)}if(r.includes("BackgroundColor")&&s){const t=(e=>{if(!e)return{background:"",size:""};let t="";return"classic"===e.backgroundType&&e.backgroundColor?t=c(e.backgroundColor)||"":"gradient"===e.backgroundType&&e.gradient&&(t=(e=>{if(!e)return"";if("linear"===e.type){const t=e.angle||0,r=e.stops?.map((e=>`${e.color} ${e.position}%`)).join(", ");return`linear-gradient(${t}deg, ${r})`}if("radial"===e.type){const t=e.stops?.map((e=>`${e.color} ${e.position}%`)).join(", ");return`radial-gradient(circle, ${t})`}return""})(e.gradient)),{background:t,size:""}})(s);t.background&&(e.style.background=t.background),t.size&&(e.style.backgroundSize=t.size)}if(r.includes("CssClass")&&l){const t="string"!=typeof(i=l)?"":i.toLowerCase().trim().replace(/[^\w-]+/g,"-").replace(/^-+|-+$/g,"").replace(/-+/g,"-").replace(/script/g,"");if(t){const r=e.getAttribute("data-previous-class");r&&e.classList.remove(r),e.classList.add(t),e.setAttribute("data-previous-class",t)}}var i;e.setAttribute("data-conditional-formatting","true")})(e,{formatAction:i,formatTextColor:n,formatBackground:a,formatCssClass:h})}))}))})(t,e,r)}))}))};class u{constructor(e,t,r,o,s=()=>{},l=()=>{}){this.getModalTable=e,this.getExternalTable=t,this.cells=r||{},this.setAttributes=o,this.onMergeModeChange=s,this.onMergeWarning=l,this.selectedCell=null,this.mergeMode="merge",this.isSelecting=!1,this.startCoords=null,this.invalidSelection=!1}getKey(e,t){return`${e}-${t}`}static assignDataAttributes(e){e&&e.querySelectorAll("tr").forEach(((e,t)=>{Array.from(e.querySelectorAll("td, th")).forEach(((e,r)=>{e.setAttribute("data-row",t),e.setAttribute("data-col",r)}))}))}getSectionType(e){if(!e?.parentElement?.parentElement)return null;const t=e.parentElement.parentElement;return"THEAD"===t.tagName?"thead":"TFOOT"===t.tagName?"tfoot":"tbody"}restoreTableState(){const e=this.getModalTable(),t=this.getExternalTable();e&&t&&(u.assignDataAttributes(e),u.assignDataAttributes(t),Object.keys(this.cells).forEach((r=>{const o=this.cells[r];if(!o)return;const[s,l]=r.split("-").map(Number);[e.querySelector(`[data-row="${s}"][data-col="${l}"]`),t.querySelector(`[data-row="${s}"][data-col="${l}"]`)].forEach((e=>{e&&(e.removeAttribute("rowspan"),e.removeAttribute("colspan"),e.style.display="",o.hidden||"none"===o.display?e.style.display="none":(o.rowSpan>1&&(e.rowSpan=o.rowSpan),o.colSpan>1&&(e.colSpan=o.colSpan)))}))})))}clearSelection(){[this.getModalTable(),this.getExternalTable()].forEach((e=>{e&&e.querySelectorAll(".selected").forEach((e=>e.classList.remove("selected")))})),this.selectedCell=null}handleCellSelection(e){if(!e)return;const t=parseInt(e.dataset.row),r=parseInt(e.dataset.col),o=this.getKey(t,r),s=this.cells[o];this.clearSelection(),e.classList.add("selected");const l=this.getExternalTable()?.querySelector(`[data-row="${t}"][data-col="${r}"]`);l&&l.classList.add("selected"),this.selectedCell={row:t,col:r,element:e};const i=s&&(s.rowSpan>1||s.colSpan>1)||e.rowSpan>1||e.colSpan>1;this.mergeMode=i?"unmerge":"merge",this.onMergeModeChange(this.mergeMode)}updateSelectionRange(e){if(!this.startCoords||!e)return;const t=this.getModalTable(),r=this.getExternalTable();if(!t||!r)return;const o=t.querySelector(`[data-row="${this.startCoords.row}"][data-col="${this.startCoords.col}"]`);if(!o)return;const s=this.getSectionType(o),l=this.getSectionType(e);this.invalidSelection=s!==l;const i=this.startCoords.row,n=this.startCoords.col,a=parseInt(e.dataset.row),c=parseInt(e.dataset.col),d=Math.min(i,a),h=Math.max(i,a),u=Math.min(n,c),g=Math.max(n,c);this.clearSelection();let f=!1;for(let e=d;e<=h;e++)for(let o=u;o<=g;o++){const l=t.querySelector(`[data-row="${e}"][data-col="${o}"]`),i=r.querySelector(`[data-row="${e}"][data-col="${o}"]`);l&&this.getSectionType(l)===s&&(l.classList.add("selected"),"none"===l.style.display&&(f=!0),i&&i.classList.add("selected"))}this.mergeMode=f?"unmerge":"merge",this.onMergeModeChange(this.mergeMode)}handleMerge(){const e=this.getModalTable(),t=this.getExternalTable();if(!e||!t)return!1;const r=e.querySelectorAll(".selected");return!(r.length<1)&&(1===r.length&&"unmerge"===this.mergeMode?this.unmergeCell():r.length>1&&this.mergeCells(r))}mergeCells(e){if(e.length<2)return!1;const t=this.getSectionType(e[0]);if(!t)return!1;for(let r of e)if(this.getSectionType(r)!==t)return console.warn("Cannot merge cells from different table sections."),!1;const r=Array.from(e).map((e=>({row:parseInt(e.dataset.row),col:parseInt(e.dataset.col),element:e}))),o=r.map((e=>e.row)),s=r.map((e=>e.col)),l=Math.min(...o),i=Math.max(...o),n=Math.min(...s),a=Math.max(...s),c=i-l+1,d=a-n+1,h=r.find((e=>e.row===l&&e.col===n));if(!h)return!1;this.getModalTable();const u=this.getExternalTable(),g={...this.cells};h.element.rowSpan=c,h.element.colSpan=d;const f=u.querySelector(`[data-row="${l}"][data-col="${n}"]`);return f&&(f.rowSpan=c,f.colSpan=d),g[this.getKey(l,n)]={rowSpan:c,colSpan:d,hidden:!1,display:"","data-row":l,"data-col":n},r.forEach((e=>{if(e.element!==h.element){const t=this.getKey(e.row,e.col);e.element.style.display="none";const r=u.querySelector(`[data-row="${e.row}"][data-col="${e.col}"]`);r&&(r.style.display="none"),g[t]={rowSpan:1,colSpan:1,hidden:!0,display:"none","data-row":e.row,"data-col":e.col}}})),this.setAttributes(g),this.clearSelection(),!0}unmergeCell(){if(!this.selectedCell)return!1;const{row:e,col:t,element:r}=this.selectedCell,o=this.getKey(e,t),s=this.cells[o];if(!s)return!1;const l=this.getModalTable(),i=this.getExternalTable(),n={...this.cells},a=s.rowSpan||1,c=s.colSpan||1;r.rowSpan=1,r.colSpan=1;const d=i.querySelector(`[data-row="${e}"][data-col="${t}"]`);d&&(d.rowSpan=1,d.colSpan=1);for(let r=e;r<e+a;r++)for(let e=t;e<t+c;e++){const t=this.getKey(r,e),o=l.querySelector(`[data-row="${r}"][data-col="${e}"]`);o&&(o.style.display="");const s=i.querySelector(`[data-row="${r}"][data-col="${e}"]`);s&&(s.style.display=""),n[t]={rowSpan:1,colSpan:1,hidden:!1,display:"","data-row":r,"data-col":e}}return this.setAttributes(n),this.clearSelection(),!0}handleMouseDown(e){const t=e.target.closest("td, th");t&&(this.isSelecting=!0,this.invalidSelection=!1,this.startCoords={row:parseInt(t.dataset.row),col:parseInt(t.dataset.col)},this.handleCellSelection(t))}handleMouseOver(e){if(!this.isSelecting)return;const t=e.target.closest("td, th");t&&this.updateSelectionRange(t)}handleMouseUp(){this.isSelecting=!1,this.invalidSelection&&(this.clearSelection(),this.mergeMode="merge",this.onMergeModeChange(this.mergeMode),this.onMergeWarning&&requestAnimationFrame((()=>{this.onMergeWarning("Cannot select cells across different table sections (thead, tbody, tfoot).")}))),this.invalidSelection=!1}getMergeMode(){return this.mergeMode}getSelectedCellsCount(){const e=this.getModalTable();return e?e.querySelectorAll(".selected").length:0}}window.addEventListener("load",(()=>{document.querySelectorAll(".wp-block-tablebuilder-table-builder").forEach((l=>{const n=l.querySelector("table");if(!n)return;const{enableSorting:c,tableConditionalFormat:d,mergeTableCell:g,freezeSettings:f,blockClass:p,enableFreeze:m}=JSON.parse(l.dataset?.settings);l.removeAttribute("data-settings"),c&&((l,{ascIcon:n=e,descIcon:a=t,defaultSortIcon:c=r,enableSorting:d=!0})=>{if(!l)return;const h=l.querySelectorAll("th");0!==h.length&&h.forEach(((e,t)=>{if(!d){console.log("Sorting disabled for header:",e),e.classList.remove("sortable");const t=e.querySelector(".sorting");t&&t.remove();const r=e.cloneNode(!0);return e.parentNode.replaceChild(r,e),void(r.dataset.sortableApplied="false")}if("true"===e.dataset.sortableApplied)return;e.classList.add("sortable");let r=e.querySelector(".sorting");r&&r.remove(),r=document.createElement("span"),r.classList.add("sorting");const u=i(c?.src);u&&r.appendChild(u),e.appendChild(r),e.addEventListener("click",(()=>((e,t,r,l,n,a,c)=>{const d=e.classList.contains("sorted-asc");o(r,c),e.classList.add(d?"sorted-desc":"sorted-asc");const h=e.querySelector(".sorting");h&&(h.innerHTML="",h.appendChild(i(d?a.src:n.src))),s(l,t,!d)})(e,t,h,l,n,a,c))),e.dataset.sortableApplied="true"}))})(n,{enableSorting:c}),d.length&&p&&h({data:d,element:p}),m&&f&&new a(n,f),g&&(u.assignDataAttributes(n),function(e,t){if(e&&t)for(const r in t){const o=t[r],{rowSpan:s,colSpan:l,hidden:i,display:n,"data-row":a,"data-col":c}=o,d=`[data-row="${a}"][data-col="${c}"]`,h=e.querySelector(d);h?(void 0!==s&&(h.rowSpan=s),void 0!==l&&(h.colSpan=l),h.style.display=i?"none":null!=n?n:""):console.warn(`Cell not found at row ${a}, col ${c}`)}}(n,g))}))}))})(); |
|
1
|
+(()=>{"use strict";const e={title:"asc-icon",src:"<svg fill='#323131' viewBox='0 0 14 14' role='img' focusable='false' aria-hidden='true' xmlns='http://www.w3.org/2000/svg' stroke='#323131'><g id='SVGRepo_bgCarrier' stroke-width='0'></g><g id='SVGRepo_tracerCarrier' stroke-linecap='round' stroke-linejoin='round'></g><g id='SVGRepo_iconCarrier'><path d='m 11.363635,5.363637 c 0,0.1477267 -0.05398,0.2755673 -0.16193,0.383522 -0.107953,0.1079547 -0.235793,0.161932 -0.38352,0.161932 l -7.63637,0 c -0.1477267,0 -0.2755667,-0.053977 -0.38352,-0.161932 -0.1079533,-0.1079547 -0.16193,-0.2357953 -0.16193,-0.383522 0,-0.147728 0.053977,-0.2755693 0.16193,-0.383524 L 6.616475,1.161932 C 6.724435,1.0539773 6.8522783,1 7.000005,1 c 0.1477267,0 0.2755667,0.053977 0.38352,0.161932 l 3.81818,3.818181 c 0.107953,0.1079547 0.16193,0.235796 0.16193,0.383524 z'></path></g></svg>"},t={title:"desc-icon",src:"<svg fill='#323131' viewBox='0 0 14 14' role='img' focusable='false' aria-hidden='true' xmlns='http://www.w3.org/2000/svg' stroke='#323131'><g id='SVGRepo_bgCarrier' stroke-width='0'></g><g id='SVGRepo_tracerCarrier' stroke-linecap='round' stroke-linejoin='round'></g><g id='SVGRepo_iconCarrier'><path d='m 11.363635,8.636363 c 0,0.147728 -0.05398,0.275569 -0.16193,0.383523 l -3.81818,3.818182 C 7.2755717,12.946023 7.1477317,13 7.000005,13 6.8522783,13 6.724435,12.946023 6.616475,12.838068 L 2.798295,9.019886 C 2.6903417,8.911932 2.636365,8.784091 2.636365,8.636363 c 0,-0.1477267 0.053977,-0.2755673 0.16193,-0.383522 0.1079533,-0.1079547 0.2357933,-0.161932 0.38352,-0.161932 l 7.63637,0 c 0.147727,0 0.275567,0.053977 0.38352,0.161932 0.107953,0.1079547 0.16193,0.2357953 0.16193,0.383522 z'></path></g></svg>"},s={title:"default-sort-icon",src:"<svg xmlns='http://www.w3.org/2000/svg' fill='#323131' stroke='#323131' aria-hidden='true' viewBox='0 0 14 14'><path d='M11.364 8.636q0 .222-.162.384l-3.818 3.818Q7.222 13 7 13t-.384-.162L2.798 9.02q-.162-.162-.162-.384 0-.221.162-.383t.384-.162h7.636q.222 0 .384.162t.162.383zm0-3.272q0 .221-.162.383t-.384.162H3.182q-.222 0-.384-.162t-.162-.383q0-.222.162-.384l3.818-3.818Q6.778 1 7 1t.384.162l3.818 3.818q.162.162.162.384z'/></svg>"},r=(e,t)=>{e.forEach(e=>{e.classList.remove("sorted-asc","sorted-desc");const s=e.querySelector(".sorting");s&&(s.innerHTML="",s.appendChild(l(t.src)))})},i=(e,t,s)=>{const r=e.querySelector("tbody");if(!r)return;const i=Array.from(r.querySelectorAll("tr"));0!==i.length&&(i.sort((e,r)=>o(e,r,t,s)),r.innerHTML="",i.forEach(e=>r.appendChild(e)))},o=(e,t,s,r)=>{const i=e.cells[s]?.textContent.trim(),o=t.cells[s]?.textContent.trim();return void 0===i||void 0===o?0:isNaN(i)||isNaN(o)?r?i.localeCompare(o):o.localeCompare(i):r?i-o:o-i},l=e=>{if(!e)return null;const t=document.createElement("div");t.innerHTML=e.trim();const s=t.firstChild;return s&&"svg"===s.tagName&&(s.setAttribute("width","16"),s.setAttribute("height","16")),s},n=window.wp.i18n,a=e=>{if(!e||"string"!=typeof e)return null;const t=e.trim();if(t.includes(",")){const e=t.split(",");return e.length>1?e[1].trim():null}return t},c=e=>{if(!e)return null;const t=parseInt(e.from,10);let s=parseInt(e.to,10);return!Number.isFinite(t)||t<1?null:((!Number.isFinite(s)||s<t)&&(s=t),{from:t-1,to:s-1})},h="500px",d="rgba(15, 23, 42, 0.18)",u=e=>!e||"transparent"===e||/rgba?\([^)]*,\s*0(\.0+)?\s*\)$/.test(e),f="rgba(128, 128, 128, 0.45)",p="tablekit:columns-resized",g="tablekit:conditional-formatting-applied";class m{constructor(e,t={}){if(this.table="string"==typeof e?document.querySelector(e):e,!this.table)throw new Error("Table element not found");this.doc=this.table.ownerDocument||document,this.win=this.doc.defaultView||window,this.id=`tk-fz-${Math.random().toString(36).slice(2,10)}`,this.config={fixedHeader:!1,fixedColumns:!1,leftFreeze:{from:null,to:null},rightFreeze:{from:null,to:null},minColumnWidth:120,mobileBreakpoint:768,mobileFreeze:"all",edgeShadow:!0,...t},this.container=null,this.columns=[],this.cellPositions=new Map,this.styleEl=null,this.frozenCells=new Map,this.widthCells=new Set,this.ownsTablePosition=!1,this.ownsTableMinWidth=!1,this.ownsHeader=!1,this.ownsContainer=!1,this.scrollState="",this.columnsSuspended=!1,this.borders=null,this.headerHeight=0,this.ownsHeaderBg=!1,this.ownsA11y=!1,this.isInitialized=!1,this.observer=null,this.resizeObserver=null,this.mediaQuery=null,this.rafId=null,this.scrollRafId=null,this.scheduleRefresh=this.scheduleRefresh.bind(this),this.reapplyColors=this.reapplyColors.bind(this),this.onScroll=this.onScroll.bind(this),this.onMediaChange=this.onMediaChange.bind(this),this.init()}init(){this.isInitialized&&this.cleanup(),this.table.setAttribute("data-tk-freeze-id",this.id),this.setupTableContainer(),this.ensureTableWidth(),this.refreshPositions(),this.observeDOMChanges(),this.observeTableResize(),this.observeMedia(),this.table.addEventListener(p,this.scheduleRefresh),this.table.addEventListener(g,this.reapplyColors),this.isInitialized=!0}isMobile(){return"function"==typeof this.win.matchMedia&&this.win.matchMedia(`(max-width: ${this.config.mobileBreakpoint}px)`).matches}getActiveFreeze(){const e=this.getRequestedFreeze();return this.columnsSuspended?{...e,left:null,right:null}:e}getRequestedFreeze(){const e=this.isMobile()?this.config.mobileFreeze:"all";if("none"===e)return{header:!1,left:null,right:null};const t=this.config.fixedColumns?c(this.config.leftFreeze):null,s=this.config.fixedColumns?c(this.config.rightFreeze):null;return"first"===e?{header:!!this.config.fixedHeader,left:t?{from:0,to:0}:null,right:null}:{header:!!this.config.fixedHeader,left:t,right:s}}hasFrozenColumns(){const{left:e,right:t}=this.getActiveFreeze();return!(!e&&!t)}observeDOMChanges(){const e=this.table.tBodies[0];e&&"undefined"!=typeof MutationObserver&&(this.observer&&this.observer.disconnect(),this.observer=new MutationObserver(e=>{e.some(e=>"childList"===e.type&&(e.addedNodes.length||e.removedNodes.length))&&this.scheduleRefresh()}),this.observer.observe(e,{childList:!0,subtree:!1}))}observeTableResize(){this.resizeObserver&&this.resizeObserver.disconnect(),"undefined"!=typeof ResizeObserver&&(this.resizeObserver=new ResizeObserver(this.scheduleRefresh),this.resizeObserver.observe(this.setupTableContainer()),this.resizeObserver.observe(this.table))}observeMedia(){"function"==typeof this.win.matchMedia&&(this.mediaQuery=this.win.matchMedia(`(max-width: ${this.config.mobileBreakpoint}px)`),this.mediaQuery.addEventListener?.("change",this.onMediaChange))}onMediaChange(){this.syncTableWidth(),this.scheduleRefresh()}scheduleRefresh(){this.rafId||(this.rafId=this.win.requestAnimationFrame(()=>{this.rafId=null,this.table.isConnected&&this.refreshPositions()}))}onScroll(){this.scrollRafId||(this.scrollRafId=this.win.requestAnimationFrame(()=>{this.scrollRafId=null,this.updateScrollState()}))}updateScrollState(){const e=this.container;if(!e)return;const t=e.scrollWidth-e.clientWidth,s=Math.abs(e.scrollLeft),r=[];e.scrollTop>0&&r.push("y"),s>0&&r.push("start"),t-s>1&&r.push("end");const i=r.join(" ");i!==this.scrollState&&(this.scrollState=i,e.setAttribute("data-tk-freeze-scrolled",i))}ensureTableWidth(){if(this.isMobile()||!this.hasFrozenColumns())return;const e=this.table.rows[0];if(!e)return;const t=this.config.minColumnWidth,s=Array.from(e.cells);s.filter(e=>!e.style.width&&e.offsetWidth<t).forEach(e=>{e.style.minWidth=`${t}px`,e.style.width=`${t}px`,this.widthCells.add(e)}),this.table.style.minWidth=s.length*t+"px",this.ownsTableMinWidth=!0}syncTableWidth(){this.releaseTableWidth(),this.ensureTableWidth()}releaseTableWidth(){const e=`${this.config.minColumnWidth}px`;this.widthCells.forEach(t=>{t.style.width===e&&(t.style.width=""),t.style.minWidth===e&&(t.style.minWidth="")}),this.widthCells.clear(),this.ownsTableMinWidth&&(this.table.style.minWidth="",this.ownsTableMinWidth=!1)}measureColumns(){const e=Array.from(this.table.rows),t=e.reduce((e,t)=>Math.max(e,t.cells.length),0),s=new Array(t).fill(null);let r=t;for(const t of e){if(!r)break;Array.from(t.cells).forEach((e,t)=>{null===s[t]&&(e.colSpan>1||"none"===e.style.display||(s[t]=e.getBoundingClientRect().width,r--))})}return s.map(e=>null!=e?e:0)}trackCellPositions(){const e=this.measureColumns();let t=0;this.columns=e.map(e=>{const s={width:e,left:t};return t+=e,s}),this.cellPositions.clear(),this.columns.forEach((e,t)=>{this.cellPositions.set(t,{...e,rowIndex:0,cellIndex:t})})}applyStyles(){this.setupTableContainer(),this.writeFreezeCss(),this.applyHeader(),this.applyColors(),this.updateScrollState(),this.updateAccessibility()}setupTableContainer(){let e=this.table.closest(".table-freezer-container");if(!e){const t=this.doc.createElement("div");t.className="table-freezer-container",t.style.cssText="\n overflow: auto;\n position: relative;\n max-width: 100%;\n isolation: isolate;\n ",this.table.parentNode.insertBefore(t,this.table),t.appendChild(this.table),e=t,this.ownsContainer=!0}e!==this.container&&(this.container?.removeEventListener("scroll",this.onScroll),e.addEventListener("scroll",this.onScroll,{passive:!0}),this.container=e,this.markScrollHosts()),e.setAttribute("data-tk-freeze-wrap",this.id);const{header:t}=this.getActiveFreeze();return e.style.maxHeight=t?(e=>{if(!e)return h;if("string"==typeof e)return/^\d+(\.\d+)?(px|vh)$/.test(e.trim())?e.trim():h;const t=parseFloat(e.size),s="vh"===e.unit?"vh":"px";return!Number.isFinite(t)||t<=0?h:`${t}${s}`})(this.config.scrollHeight):"",this.table.style.position||(this.table.style.position="relative",this.ownsTablePosition=!0),e}markScrollHosts(){this.unmarkScrollHosts();const e=this.table.closest('[class*="wp-block-tablebuilder"]');let t=this.container?.parentElement;for(;t&&t!==this.doc.body;){const s=this.win.getComputedStyle(t);if(/(auto|scroll)/.test(s.overflowX)&&t.setAttribute("data-tk-freeze-host",this.id),t===e)break;t=t.parentElement}}unmarkScrollHosts(){this.doc.querySelectorAll(`[data-tk-freeze-host="${this.id}"]`).forEach(e=>e.removeAttribute("data-tk-freeze-host"))}writeFreezeCss(){const{left:e,right:t,header:s}=this.getActiveFreeze(),r=this.columns.length,i=`table[data-tk-freeze-id="${this.id}"]`,o=`${i} > :is(thead, tbody, tfoot) > tr`,l=`.table-freezer-container[data-tk-freeze-wrap="${this.id}"]`,n=[];n.push(`[data-tk-freeze-host="${this.id}"]{overflow-y:hidden}`),n.push(`${l}{scrollbar-width:thin;scrollbar-color:${f} transparent;overscroll-behavior-x:contain;-webkit-overflow-scrolling:touch}`,`${l}::-webkit-scrollbar{width:8px;height:8px}`,`${l}::-webkit-scrollbar-track,${l}::-webkit-scrollbar-corner{background:transparent}`,`${l}::-webkit-scrollbar-thumb{background-color:${f};border-radius:8px;border:2px solid transparent;background-clip:padding-box}`,`${l}::-webkit-scrollbar-thumb:hover{background-color:rgba(128, 128, 128, 0.7)}`);const a=(e,t,s)=>`${o} > :nth-child(${e+1}){position:sticky!important;${t}:${Math.round(s)}px!important;z-index:2!important}`;let c=-1,h=-1;if(e){let t=0;for(let s=e.from;s<=e.to&&s<r;s++)n.push(a(s,"left",t)),t+=this.columns[s].width,c=s}if(t){let e=0;for(let s=Math.min(t.to,r-1);s>=t.from;s--)n.push(a(s,"right",e)),e+=this.columns[s].width,h=s}const u={left:{},right:{},head:[]},p=(e,t,s)=>{if(!e)return null;const[r,...i]=e.split(" "),o=parseFloat(r);return`inset ${t*o}px ${s*o}px 0 ${i.join(" ")}`},g=this.borders;if(g?.left&&e)for(let t=e.from;t<=e.to&&t<r;t++)u.left[t]=[p(g.left.right,-1,0),p(g.left.bottom,0,-1),0===t?p(g.left.left,1,0):null].filter(Boolean);if(g?.right&&t)for(let e=t.from;e<=t.to&&e<r;e++)u.right[e]=[p(g.right.left,1,0),p(g.right.bottom,0,-1),e===r-1?p(g.right.right,-1,0):null].filter(Boolean);g?.head&&s&&(u.head=[p(g.head.bottom,0,-1)].filter(Boolean));const m=e=>e.length?`box-shadow:${e.join(",")}`:"",b=!1!==this.config.edgeShadow,y=e=>`${l}[data-tk-freeze-scrolled~="${e}"]`;if(Object.entries(u.left).forEach(([e,t])=>{t.length&&n.push(`${o} > :nth-child(${+e+1}){${m(t)}}`)}),Object.entries(u.right).forEach(([e,t])=>{t.length&&n.push(`${o} > :nth-child(${+e+1}){${m(t)}}`)}),u.head.length&&(n.push(`${i} > thead > tr > *{${m(u.head)}}`),[...Object.entries(u.left),...Object.entries(u.right)].forEach(([e,t])=>{n.push(`${i} > thead > tr > :nth-child(${+e+1}){${m([...t,...u.head])}}`)})),b){if(c>=0){const e=[...u.left[c]||[],`6px 0 8px -6px ${d}`];n.push(`${y("start")} ${o} > :nth-child(${c+1}){${m(e)}}`)}if(h>=0){const e=[...u.right[h]||[],`-6px 0 8px -6px ${d}`];n.push(`${y("end")} ${o} > :nth-child(${h+1}){${m(e)}}`)}s&&n.push(`${y("y")} > ${i} > thead > tr > *{${m([...u.head,`0 6px 8px -6px ${d}`])}}`)}const w=c>=0?this.columns.slice(e.from,c+1).reduce((e,t)=>e+t.width,0):0,C=h>=0&&t?this.columns.slice(h,Math.min(t.to,r-1)+1).reduce((e,t)=>e+t.width,0):0;n.push(`${l}{scroll-padding:${Math.round(this.headerHeight)}px ${Math.round(C)}px 0 ${Math.round(w)}px}`),n.push(`${l}:focus-visible{outline:2px solid currentColor;outline-offset:2px}`),n.push(`@media print{${l}{max-height:none!important;overflow:visible!important}${i} > thead,${o} > :nth-child(n){position:static!important;box-shadow:none!important}[data-tk-freeze-host="${this.id}"]{overflow:visible!important}}`);const S=n.join("\n");this.styleEl&&this.styleEl.isConnected||(this.styleEl=this.doc.createElement("style"),this.styleEl.id=`${this.id}-css`,(this.doc.head||this.doc.documentElement).appendChild(this.styleEl)),this.styleEl.textContent!==S&&(this.styleEl.textContent=S)}applyHeader(){const e=this.table.tHead;this.getActiveFreeze().header?e&&(this.config.headerBgColor&&(e.style.backgroundColor=a(this.config.headerBgColor)),this.config.headerColor&&(e.style.color=a(this.config.headerColor)),e.style.position="sticky",e.style.top="0",e.style.zIndex=String(3),e.setAttribute("data-frozen","header"),this.ownsHeader=!0):this.ownsHeader&&this.clearHeader()}clearHeader(){const e=this.table.tHead;e&&(e.style.position="",e.style.top="",e.style.zIndex="",e.style.backgroundColor="",e.style.color="",e.removeAttribute("data-frozen")),this.ownsHeader=!1,this.ownsHeaderBg=!1}applyColors(){const{left:e,right:t}=this.getActiveFreeze(),s=new Map;(e||t)&&Array.from(this.table.rows).forEach(r=>{Array.from(r.cells).forEach((r,i)=>{e&&i>=e.from&&i<=e.to?s.set(r,"left"):t&&i>=t.from&&i<=t.to&&s.set(r,"right")})}),this.frozenCells.forEach((e,t)=>{s.has(t)||this.clearFrozenCell(t)}),s.forEach((e,t)=>{if(t.getAttribute("data-frozen")!==e&&t.setAttribute("data-frozen",e),t.hasAttribute("data-conditional-formatting"))return;const s="left"===e?this.config.leftColumnBgColor:this.config.rightColumnBgColor,r="left"===e?this.config.leftColumnColor:this.config.rightColumnColor;s&&(t.style.backgroundColor=a(s)),r&&(t.style.color=a(r))}),this.frozenCells=s,this.fillTransparentBackgrounds()}fillTransparentBackgrounds(){const e=new Map,t=t=>{if(e.has(t))return e.get(t);let s=t,r="#fff";for(;s&&1===s.nodeType;){const e=this.win.getComputedStyle(s).backgroundColor;if(!u(e)){r=e;break}s=s.parentElement}return e.set(t,r),r},s=[];this.frozenCells.forEach((e,r)=>{r.hasAttribute("data-conditional-formatting")||u(this.win.getComputedStyle(r).backgroundColor)&&s.push([r,t(r.parentElement)])});const r=this.table.tHead;let i=null;if(r&&this.ownsHeader&&u(this.win.getComputedStyle(r).backgroundColor)){const e=r.rows[0]?.cells[0];e&&!u(this.win.getComputedStyle(e).backgroundColor)||(i=t(r.parentElement))}s.forEach(([e,t])=>{e.style.backgroundColor=t}),i&&(r.style.backgroundColor=i,this.ownsHeaderBg=!0)}updateAccessibility(){const e=this.container;if(!e)return;const t=e.scrollWidth-e.clientWidth>1||e.scrollHeight-e.clientHeight>1;if(!t||this.ownsA11y||e.hasAttribute("tabindex"))!t&&this.ownsA11y&&this.clearAccessibility();else{const t=this.table.closest("figure")?.querySelector("figcaption")?.textContent?.trim()||this.table.caption?.textContent?.trim();e.setAttribute("tabindex","0"),e.setAttribute("role","region"),e.setAttribute("aria-label",t?t.slice(0,80):(0,n.__)("Scrollable table","table-builder-block")),this.ownsA11y=!0}}clearAccessibility(){this.ownsA11y&&this.container&&(["tabindex","role","aria-label"].forEach(e=>this.container.removeAttribute(e)),this.ownsA11y=!1)}reapplyColors(){this.frozenCells.size&&this.applyColors()}clearFrozenCell(e){e.removeAttribute("data-frozen"),e.hasAttribute("data-conditional-formatting")||(e.style.backgroundColor="",e.style.color="")}clearFrozenColumns(e="left"){this.frozenCells.forEach((t,s)=>{t===e&&(this.clearFrozenCell(s),this.frozenCells.delete(s))})}updateConfig(e){this.config={...this.config,...e},this.syncTableWidth(),this.refreshPositions()}cleanup(){this.rafId&&(this.win.cancelAnimationFrame(this.rafId),this.rafId=null),this.scrollRafId&&(this.win.cancelAnimationFrame(this.scrollRafId),this.scrollRafId=null),this.styleEl?.remove(),this.styleEl=null,this.frozenCells.forEach((e,t)=>this.clearFrozenCell(t)),this.frozenCells.clear(),this.ownsHeader&&this.clearHeader(),this.releaseTableWidth(),this.ownsTablePosition&&(this.table.style.position="",this.ownsTablePosition=!1),this.unmarkScrollHosts(),this.clearAccessibility(),this.container&&(this.container.removeEventListener("scroll",this.onScroll),this.container.removeAttribute("data-tk-freeze-scrolled"),this.container.removeAttribute("data-tk-freeze-wrap"),this.container.removeAttribute("data-tk-freeze-columns-paused"),this.container=null),this.scrollState="",this.columnsSuspended=!1,this.borders=null,this.table.removeAttribute("data-tk-freeze-id"),this.cellPositions.clear(),this.columns=[]}destroy(){this.cleanup(),this.observer&&this.observer.disconnect(),this.resizeObserver&&this.resizeObserver.disconnect(),this.mediaQuery?.removeEventListener?.("change",this.onMediaChange),this.mediaQuery=null,this.table.removeEventListener(p,this.scheduleRefresh),this.table.removeEventListener(g,this.reapplyColors);const e=this.table.closest(".table-freezer-container");e&&this.ownsContainer&&e.parentNode?(e.parentNode.insertBefore(this.table,e),e.remove()):e&&(e.style.maxHeight=""),this.ownsContainer=!1,this.isInitialized=!1}getCellPosition(e,t){return this.cellPositions.get(t)}refreshPositions(){this.table.hasAttribute("data-tk-freeze-id")||this.table.setAttribute("data-tk-freeze-id",this.id),this.setupTableContainer(),this.trackCellPositions(),this.updateColumnSuspension()&&(this.syncTableWidth(),this.trackCellPositions()),this.readLayout(),this.applyStyles()}updateColumnSuspension(){const{left:e,right:t}=this.getRequestedFreeze(),s=this.container?.clientWidth||0;if(!s||!e&&!t)return this.setColumnsSuspended(!1);const r=new Set;if(e)for(let t=e.from;t<=e.to;t++)r.add(t);if(t)for(let e=t.from;e<=t.to;e++)r.add(e);const i=this.isMobile()?0:this.config.minColumnWidth;let o=0;r.forEach(e=>{this.columns[e]&&(o+=Math.max(this.columns[e].width,i))});const l=o/s;return this.columnsSuspended?this.setColumnsSuspended(l>.5):this.setColumnsSuspended(l>.6)}setColumnsSuspended(e){return e!==this.columnsSuspended&&(this.columnsSuspended=e,e?this.container?.setAttribute("data-tk-freeze-columns-paused",""):this.container?.removeAttribute("data-tk-freeze-columns-paused"),!0)}readLayout(){const{left:e,right:t,header:s}=this.getActiveFreeze();if(this.headerHeight=s&&this.table.tHead?this.table.tHead.getBoundingClientRect().height:0,this.borders=null,"collapse"!==this.win.getComputedStyle(this.table).borderCollapse||!e&&!t&&!s)return;const r=this.table.tBodies[0]?.rows[0]||this.table.rows[this.table.rows.length-1],i=this.table.tHead?.rows[0],o=(e,t)=>{const s=parseFloat(e);return s>0&&!u(t)?`${s}px ${t}`:null},l=(e,t)=>{const s=e?.cells[t];if(!s)return null;const r=this.win.getComputedStyle(s);return{left:o(r.borderLeftWidth,r.borderLeftColor),right:o(r.borderRightWidth,r.borderRightColor),bottom:o(r.borderBottomWidth,r.borderBottomColor)}};this.borders={left:e?l(r,e.from):null,right:t?l(r,t.to):null,head:s&&i?l(i,0):null}}}const b=e=>{if(!e||"string"!=typeof e)return null;const t=e.trim();return t.includes(",")?t.split(",")[1]?.trim():t},y={equal:(e,t)=>e===t,notEqual:(e,t)=>e!==t,contains:(e,t)=>e.includes(t),notContains:(e,t)=>!e.includes(t),greaterThan:(e,t)=>isNaN(e)||isNaN(t)?e>t:parseFloat(e)>parseFloat(t),lessThan:(e,t)=>isNaN(e)||isNaN(t)?e<t:parseFloat(e)<parseFloat(t),greaterThanOrEqual:(e,t)=>isNaN(e)||isNaN(t)?e>=t:parseFloat(e)>=parseFloat(t),lessThanOrEqual:(e,t)=>isNaN(e)||isNaN(t)?e<=t:parseFloat(e)<=parseFloat(t)},w=e=>{if(!e?.element||!e?.data)return;const t=(()=>{const e=document.querySelector('iframe[name="editor-canvas"]')||document.querySelector(".block-editor-iframe__container iframe")||document.querySelector("iframe");try{if(e?.contentDocument)return e.contentDocument;if(e?.contentWindow?.document)return e.contentWindow.document}catch(e){console.warn("[table-builder][conditional-formatting.apply] Blocked cross-origin frame access. Using current document.")}return document})().querySelector(`.${e.element}`)||document.querySelector(`.${e.element}`);if(!t)return;const s=Array.from(t.querySelectorAll("table")),r=[...new Set([...s])];r.length&&r.forEach(t=>{(e=>{e.querySelectorAll("[data-conditional-formatting='true']").forEach(e=>{["color","backgroundColor","background","backgroundSize"].forEach(t=>{e.style[t]=""});const t=e.getAttribute("data-previous-class");t&&(e.classList.remove(t),e.removeAttribute("data-previous-class")),e.removeAttribute("data-conditional-formatting")})})(t);const s=(e=>{const t=new Map,s=e.querySelector("tr:first-child");return s&&s.querySelectorAll("th, td").forEach((e,s)=>{const r=e.textContent.trim().toLowerCase().replace(/\s+/g,"");t.set(r,s)}),t})(t);e.data.forEach(e=>{((e,t,s)=>{const{columnKey:r,comparisonType:i="equal",comparisonValue:o="",formatAction:l,formatTextColor:n,formatBackground:a,formatCssClass:c}=t;if(!r||!l)return;const h=s.get(r);if(void 0===h)return;const d=Array.from(e.querySelectorAll("tr")).slice(1),u=o.toString().trim().toLowerCase(),f=l.startsWith("column"),p=l.startsWith("row");d.forEach(e=>{const t=e.querySelectorAll("td, th"),s=t[h];if(!s)return;const r=s.textContent.trim().toLowerCase(),o=y[i]?.(r,u);let d=[];f?d=[s]:o&&(d=p?Array.from(t):[s]),d.forEach(e=>{((e,t)=>{const{formatAction:s,formatTextColor:r,formatBackground:i,formatCssClass:o}=t;if(s.includes("TextColor")&&r){const t=b(r);t&&(e.style.color=t)}if(s.includes("BackgroundColor")&&i){const t=(e=>{if(!e)return{background:"",size:""};let t="";return"classic"===e.backgroundType&&e.backgroundColor?t=b(e.backgroundColor)||"":"gradient"===e.backgroundType&&e.gradient&&(t=(e=>{if(!e)return"";if("linear"===e.type){const t=e.angle||0,s=e.stops?.map(e=>`${e.color} ${e.position}%`).join(", ");return`linear-gradient(${t}deg, ${s})`}if("radial"===e.type){const t=e.stops?.map(e=>`${e.color} ${e.position}%`).join(", ");return`radial-gradient(circle, ${t})`}return""})(e.gradient)),{background:t,size:""}})(i);t.background&&(e.style.background=t.background),t.size&&(e.style.backgroundSize=t.size)}if(s.includes("CssClass")&&o){const t="string"!=typeof(l=o)?"":l.toLowerCase().trim().replace(/[^\w-]+/g,"-").replace(/^-+|-+$/g,"").replace(/-+/g,"-").replace(/script/g,"");if(t){const s=e.getAttribute("data-previous-class");s&&e.classList.remove(s),e.classList.add(t),e.setAttribute("data-previous-class",t)}}var l;e.setAttribute("data-conditional-formatting","true")})(e,{formatAction:l,formatTextColor:n,formatBackground:a,formatCssClass:c})})})})(t,e,s)});const r=t.ownerDocument?.defaultView?.CustomEvent||CustomEvent;t.dispatchEvent(new r("tablekit:conditional-formatting-applied"))})};function C(e,t,s=768){if(!e||!t?.length)return;if("undefined"!=typeof window&&window.matchMedia(`(max-width: ${s}px)`).matches)return;const r=t.reduce((e,t)=>e+(t||0),0);0!==r&&(e.style.tableLayout="fixed",e.style.width="100%",e.querySelectorAll("thead tr, tbody tr, tfoot tr").forEach(e=>{e.querySelectorAll("td, th").forEach((e,s)=>{t[s]&&"none"!==e.style.display&&(e.style.width=(e=>`${((t[e]||0)/r*100).toFixed(4)}%`)(s),e.style.minWidth="",e.style.maxWidth="")})}))}window.wp.element;class S{constructor(e,t,s,r,i=()=>{},o=()=>{}){this.getModalTable=e,this.getExternalTable=t,this.cells=s||{},this.setAttributes=r,this.onMergeModeChange=i,this.onMergeWarning=o,this.selectedCell=null,this.mergeMode="merge",this.isSelecting=!1,this.startCoords=null,this.invalidSelection=!1,this.focusedCell=null,this.isShiftSelecting=!1}getKey(e,t){return`${e}-${t}`}static assignDataAttributes(e){e&&e.querySelectorAll("tr").forEach((e,t)=>{Array.from(e.querySelectorAll("td, th")).forEach((e,s)=>{e.setAttribute("data-row",t),e.setAttribute("data-col",s)})})}getSectionType(e){if(!e?.parentElement?.parentElement)return null;const t=e.parentElement.parentElement;return"THEAD"===t.tagName?"thead":"TFOOT"===t.tagName?"tfoot":"tbody"}restoreTableState(){const e=this.getModalTable(),t=this.getExternalTable();e&&t&&(S.assignDataAttributes(e),S.assignDataAttributes(t),Object.keys(this.cells).forEach(s=>{const r=this.cells[s];if(!r)return;const[i,o]=s.split("-").map(Number);[e.querySelector(`[data-row="${i}"][data-col="${o}"]`),t.querySelector(`[data-row="${i}"][data-col="${o}"]`)].forEach(e=>{e&&(e.removeAttribute("rowspan"),e.removeAttribute("colspan"),e.style.display="",r.hidden||"none"===r.display?e.style.display="none":(r.rowSpan>1&&(e.rowSpan=r.rowSpan),r.colSpan>1&&(e.colSpan=r.colSpan)))})}))}clearSelection(){[this.getModalTable(),this.getExternalTable()].forEach(e=>{e&&e.querySelectorAll(".selected").forEach(e=>e.classList.remove("selected"))}),this.selectedCell=null}handleCellSelection(e){if(!e)return;const t=parseInt(e.dataset.row),s=parseInt(e.dataset.col),r=this.getKey(t,s),i=this.cells[r];this.clearSelection(),e.classList.add("selected");const o=this.getExternalTable()?.querySelector(`[data-row="${t}"][data-col="${s}"]`);o&&o.classList.add("selected"),this.selectedCell={row:t,col:s,element:e},this.focusedCell={row:t,col:s};const l=i&&(i.rowSpan>1||i.colSpan>1)||e.rowSpan>1||e.colSpan>1;this.mergeMode=l?"unmerge":"merge",this.onMergeModeChange(this.mergeMode)}updateSelectionRange(e){if(!this.startCoords||!e)return;const t=this.getModalTable(),s=this.getExternalTable();if(!t||!s)return;const r=t.querySelector(`[data-row="${this.startCoords.row}"][data-col="${this.startCoords.col}"]`);if(!r)return;const i=this.getSectionType(r),o=this.getSectionType(e);this.invalidSelection=i!==o;const l=this.startCoords.row,n=this.startCoords.col,a=parseInt(e.dataset.row),c=parseInt(e.dataset.col),h=Math.min(l,a),d=Math.max(l,a),u=Math.min(n,c),f=Math.max(n,c);this.clearSelection();let p=!1;for(let e=h;e<=d;e++)for(let r=u;r<=f;r++){const o=t.querySelector(`[data-row="${e}"][data-col="${r}"]`),l=s.querySelector(`[data-row="${e}"][data-col="${r}"]`);o&&this.getSectionType(o)===i&&(o.classList.add("selected"),"none"===o.style.display&&(p=!0),l&&l.classList.add("selected"))}this.mergeMode=p?"unmerge":"merge",this.onMergeModeChange(this.mergeMode)}handleMerge(){const e=this.getModalTable(),t=this.getExternalTable();if(!e||!t)return!1;const s=e.querySelectorAll(".selected");return!(s.length<1)&&(1===s.length&&"unmerge"===this.mergeMode?this.unmergeCell():s.length>1&&this.mergeCells(s))}mergeCells(e){if(e.length<2)return!1;const t=this.getSectionType(e[0]);if(!t)return!1;for(let s of e)if(this.getSectionType(s)!==t)return console.warn("Cannot merge cells from different table sections."),!1;const s=Array.from(e).map(e=>({row:parseInt(e.dataset.row),col:parseInt(e.dataset.col),element:e})),r=s.map(e=>e.row),i=s.map(e=>e.col),o=Math.min(...r),l=Math.max(...r),n=Math.min(...i),a=Math.max(...i),c=l-o+1,h=a-n+1,d=s.find(e=>e.row===o&&e.col===n);if(!d)return!1;this.getModalTable();const u=this.getExternalTable(),f={...this.cells};d.element.rowSpan=c,d.element.colSpan=h;const p=u.querySelector(`[data-row="${o}"][data-col="${n}"]`);return p&&(p.rowSpan=c,p.colSpan=h),f[this.getKey(o,n)]={rowSpan:c,colSpan:h,hidden:!1,display:"","data-row":o,"data-col":n},s.forEach(e=>{if(e.element!==d.element){const t=this.getKey(e.row,e.col);e.element.style.display="none";const s=u.querySelector(`[data-row="${e.row}"][data-col="${e.col}"]`);s&&(s.style.display="none"),f[t]={rowSpan:1,colSpan:1,hidden:!0,display:"none","data-row":e.row,"data-col":e.col}}}),this.setAttributes(f),this.clearSelection(),!0}unmergeCell(){if(!this.selectedCell)return!1;const{row:e,col:t,element:s}=this.selectedCell,r=this.getKey(e,t),i=this.cells[r];if(!i)return!1;const o=this.getModalTable(),l=this.getExternalTable(),n={...this.cells},a=i.rowSpan||1,c=i.colSpan||1;s.rowSpan=1,s.colSpan=1;const h=l.querySelector(`[data-row="${e}"][data-col="${t}"]`);h&&(h.rowSpan=1,h.colSpan=1);for(let s=e;s<e+a;s++)for(let e=t;e<t+c;e++){const t=this.getKey(s,e),r=o.querySelector(`[data-row="${s}"][data-col="${e}"]`);r&&(r.style.display="");const i=l.querySelector(`[data-row="${s}"][data-col="${e}"]`);i&&(i.style.display=""),n[t]={rowSpan:1,colSpan:1,hidden:!1,display:"","data-row":s,"data-col":e}}return this.setAttributes(n),this.clearSelection(),!0}handleMouseDown(e){const t=e.target.closest("td, th");t&&(this.isShiftSelecting=e.shiftKey,this.isShiftSelecting&&this.startCoords?this.updateSelectionRange(t):(this.isSelecting=!0,this.invalidSelection=!1,this.startCoords={row:parseInt(t.dataset.row),col:parseInt(t.dataset.col)},this.handleCellSelection(t)))}handleMouseOver(e){if(!this.isSelecting||this.isShiftSelecting)return;const t=e.target.closest("td, th");t&&this.updateSelectionRange(t)}handleMouseUp(){this.isSelecting=!1,this.isShiftSelecting=!1,this.invalidSelection&&(this.clearSelection(),this.mergeMode="merge",this.onMergeModeChange(this.mergeMode),this.onMergeWarning&&requestAnimationFrame(()=>{this.onMergeWarning("Cannot select cells across different table sections (thead, tbody, tfoot).")})),this.invalidSelection=!1}getMergeMode(){return this.mergeMode}getSelectedCellsCount(){const e=this.getModalTable();return e?e.querySelectorAll(".selected").length:0}moveFocus(e){if(!this.focusedCell){const e=this.getModalTable();if(!e)return;const t=e.querySelector("td, th");return void(t&&(this.focusedCell={row:parseInt(t.dataset.row),col:parseInt(t.dataset.col)},this.handleCellSelection(t)))}const t=this.getModalTable();if(!t)return;let s=this.focusedCell.row,r=this.focusedCell.col;switch(e){case"up":s=Math.max(0,s-1);break;case"down":s+=1;break;case"left":r=Math.max(0,r-1);break;case"right":r+=1}const i=t.querySelector(`[data-row="${s}"][data-col="${r}"]`);i&&"none"!==i.style.display&&(this.handleCellSelection(i),i.scrollIntoView({block:"nearest",inline:"nearest"}))}expandSelection(e){if(!this.startCoords){if(!this.focusedCell)return;this.startCoords={...this.focusedCell}}if(!this.focusedCell)return;const t=this.getModalTable();if(!t)return;let s=this.focusedCell.row,r=this.focusedCell.col;switch(e){case"up":s=Math.max(0,s-1);break;case"down":s+=1;break;case"left":r=Math.max(0,r-1);break;case"right":r+=1}const i=t.querySelector(`[data-row="${s}"][data-col="${r}"]`);i&&"none"!==i.style.display&&(this.focusedCell={row:parseInt(i.dataset.row),col:parseInt(i.dataset.col)},this.updateSelectionRange(i),i.scrollIntoView({block:"nearest",inline:"nearest"}))}clearAllMerges(){const e=this.getModalTable(),t=this.getExternalTable();if(!e||!t)return!1;const s={};return e.querySelectorAll("td, th").forEach(e=>{const r=parseInt(e.dataset.row),i=parseInt(e.dataset.col),o=this.getKey(r,i);e.rowSpan=1,e.colSpan=1,e.style.display="";const l=t.querySelector(`[data-row="${r}"][data-col="${i}"]`);l&&(l.rowSpan=1,l.colSpan=1,l.style.display=""),s[o]={rowSpan:1,colSpan:1,hidden:!1,display:"","data-row":r,"data-col":i}}),this.setAttributes(s),this.clearSelection(),!0}}const v=()=>{const e=window?.tableBuilderPro?.isProActive;return void 0!==window?.tableBuilderPro&&(!0===e||"1"===e||1===e)};window.addEventListener("load",()=>{document.querySelectorAll(".wp-block-tablebuilder-table-builder").forEach(o=>{const n=o.querySelector("table");if(!n)return;const{enableSorting:a,tableConditionalFormat:c,mergeTableCell:h,freezeSettings:d,blockClass:u,enableFreeze:f,columnWidths:p,rowHeights:g,headerHeight:b,resizerEnabled:y,mobileBreakpoint:$}=JSON.parse(o.dataset?.settings||"{}");o.removeAttribute("data-settings"),v()&&!1!==y&&p&&Array.isArray(p)&&p.length>0&&(C(n,p,$),"undefined"!=typeof window)&&window.matchMedia(`(max-width: ${$||768}px)`).addEventListener("change",()=>C(n,p,$)),v()&&!1!==y&&function(e,t,s){if(e&&(t?.length&&Array.from(e.querySelectorAll("tbody tr")).forEach((e,s)=>{t[s]&&(e.style.height=`${t[s]}px`)}),s)){const t=e.querySelector("thead tr");t&&(t.style.height=`${s}px`)}}(n,g,b),v()&&a&&((o,{ascIcon:n=e,descIcon:a=t,defaultSortIcon:c=s,enableSorting:h=!0})=>{if(!o)return;const d=o.querySelectorAll("th");0!==d.length&&d.forEach((e,t)=>{if(!h){e.classList.remove("sortable");const t=e.querySelector(".sorting");t&&t.remove();const s=e.cloneNode(!0);return e.parentNode.replaceChild(s,e),void(s.dataset.sortableApplied="false")}if("true"===e.dataset.sortableApplied&&e.querySelector(".sorting"))return;e.classList.add("sortable");let s=e.querySelector(".sorting");s&&s.remove(),s=document.createElement("span"),s.classList.add("sorting");const u=l(c?.src);u&&s.appendChild(u),e.appendChild(s),e.addEventListener("click",()=>((e,t,s,o,n,a,c)=>{const h=e.classList.contains("sorted-asc");r(s,c),e.classList.add(h?"sorted-desc":"sorted-asc");const d=e.querySelector(".sorting");d&&(d.innerHTML="",d.appendChild(l(h?a.src:n.src))),i(o,t,!h)})(e,t,d,o,n,a,c)),e.dataset.sortableApplied="true"})})(n,{enableSorting:a}),v()&&c&&c.length&&u&&w({data:c,element:u}),v()&&f&&d&&new m(n,{...d,mobileBreakpoint:$}),v()&&h&&(S.assignDataAttributes(n),function(e,t){if(e&&t)for(const s in t){const r=t[s],{rowSpan:i,colSpan:o,hidden:l,display:n,"data-row":a,"data-col":c}=r,h=`[data-row="${a}"][data-col="${c}"]`,d=e.querySelector(h);d?(void 0!==i&&(d.rowSpan=i),void 0!==o&&(d.colSpan=o),d.style.display=l?"none":null!=n?n:""):console.warn(`Cell not found at row ${a}, col ${c}`)}}(n,h))})})})(); |