PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.14
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.14
2.2.14 2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
← All changes | build/blocks/table-builder/frontend.js +1 -1 2.2.22.2.14 View file →
@@ -1 +1 @@
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},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};class n{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.debounceTimer=null,this.resizeDebounceTimer=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=>{e.some(e=>"childList"===e.type&&(e.addedNodes.length||e.removedNodes.length))&&(clearTimeout(this.debounceTimer),this.debounceTimer=setTimeout(()=>this.refreshPositions(),100))}),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(()=>{clearTimeout(this.resizeDebounceTimer),this.resizeDebounceTimer=setTimeout(()=>this.refreshPositions(),100)}),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.style.width||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");if(this.cellPositions.clear(),!e.length)return;const t=e[0].querySelectorAll("th, td"),r=[];let o=0;t.forEach(e=>{const t=e.getBoundingClientRect().width;r.push({width:t,left:o}),o+=t}),e.forEach((e,t)=>{e.querySelectorAll("th, td").forEach((e,o)=>{const s=r[o];s&&(this.cellPositions.set(`${t}-${o}`,{left:s.left,width:s.width,rowIndex:t,cellIndex:o}),e.setAttribute("data-row-index",t),e.setAttribute("data-cell-index",o))})})}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.position||(this.table.style.position="relative"),e}applyFixedHeader(){const e=this.table.querySelector("thead");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="20",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="")})}applyFrozenCellStyles(e,{position:t,left:r,right:o,zIndex:s,bgColor:l,color:i}){e.style.position="sticky",e.style.zIndex=s,"left"===t?(e.style.left=`${Math.round(r)}px`,e.setAttribute("data-freeze-position",Math.round(r))):(e.style.right=`${Math.round(o)}px`,e.setAttribute("data-freeze-position",Math.round(o))),e.hasAttribute("data-conditional-formatting")||(l&&(e.style.backgroundColor=a(l)),i&&(e.style.color=a(i))),e.setAttribute("data-frozen",t)}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;this.applyFrozenCellStyles(e,{position:"left",left:r,zIndex:t,bgColor:this.config.leftColumnBgColor,color:this.config.leftColumnColor})});const t=this.cellPositions.get(`0-${o}`);t&&(r+=t.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;this.applyFrozenCellStyles(e,{position:"right",right:r,zIndex:t,bgColor:this.config.rightColumnBgColor,color:this.config.rightColumnColor})});const t=this.cellPositions.get(`0-${o}`);t&&(r+=t.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.table.style.minWidth="",this.cellPositions.clear()}destroy(){this.cleanup(),this.observer&&this.observer.disconnect(),this.resizeObserver&&this.resizeObserver.disconnect(),clearTimeout(this.debounceTimer),clearTimeout(this.resizeDebounceTimer);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=(()=>{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 r=Array.from(t.querySelectorAll("table")),o=[...new Set([...r])];o.length&&o.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:a,formatBackground:n,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:a,formatBackground:n,formatCssClass:h})})})})(t,e,r)})})};window.wp.element;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,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,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},this.focusedCell={row:t,col:r};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,a=this.startCoords.col,n=parseInt(e.dataset.row),c=parseInt(e.dataset.col),d=Math.min(i,n),h=Math.max(i,n),u=Math.min(a,c),g=Math.max(a,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),a=Math.min(...s),n=Math.max(...s),c=i-l+1,d=n-a+1,h=r.find(e=>e.row===l&&e.col===a);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="${a}"]`);return f&&(f.rowSpan=c,f.colSpan=d),g[this.getKey(l,a)]={rowSpan:c,colSpan:d,hidden:!1,display:"","data-row":l,"data-col":a},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(),a={...this.cells},n=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+n;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=""),a[t]={rowSpan:1,colSpan:1,hidden:!1,display:"","data-row":r,"data-col":e}}return this.setAttributes(a),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 r=this.focusedCell.row,o=this.focusedCell.col;switch(e){case"up":r=Math.max(0,r-1);break;case"down":r+=1;break;case"left":o=Math.max(0,o-1);break;case"right":o+=1}const s=t.querySelector(`[data-row="${r}"][data-col="${o}"]`);s&&"none"!==s.style.display&&(this.handleCellSelection(s),s.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 r=this.focusedCell.row,o=this.focusedCell.col;switch(e){case"up":r=Math.max(0,r-1);break;case"down":r+=1;break;case"left":o=Math.max(0,o-1);break;case"right":o+=1}const s=t.querySelector(`[data-row="${r}"][data-col="${o}"]`);s&&"none"!==s.style.display&&(this.focusedCell={row:parseInt(s.dataset.row),col:parseInt(s.dataset.col)},this.updateSelectionRange(s),s.scrollIntoView({block:"nearest",inline:"nearest"}))}clearAllMerges(){const e=this.getModalTable(),t=this.getExternalTable();if(!e||!t)return!1;const r={};return e.querySelectorAll("td, th").forEach(e=>{const o=parseInt(e.dataset.row),s=parseInt(e.dataset.col),l=this.getKey(o,s);e.rowSpan=1,e.colSpan=1,e.style.display="";const i=t.querySelector(`[data-row="${o}"][data-col="${s}"]`);i&&(i.rowSpan=1,i.colSpan=1,i.style.display=""),r[l]={rowSpan:1,colSpan:1,hidden:!1,display:"","data-row":o,"data-col":s}}),this.setAttributes(r),this.clearSelection(),!0}}const g=()=>{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(l=>{const a=l.querySelector("table");if(!a)return;const{enableSorting:c,tableConditionalFormat:d,mergeTableCell:f,freezeSettings:p,blockClass:m,enableFreeze:y,columnWidths:b,rowHeights:S,headerHeight:w,resizerEnabled:C}=JSON.parse(l.dataset?.settings||"{}");l.removeAttribute("data-settings"),g()&&!1!==C&&b&&Array.isArray(b)&&b.length>0&&function(e,t){if(!e||!t?.length)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,o)=>{t[o]&&"none"!==e.style.display&&(e.style.width=(e=>`${((t[e]||0)/r*100).toFixed(4)}%`)(o),e.style.minWidth="",e.style.maxWidth="")})}))}(a,b),g()&&!1!==C&&function(e,t,r){if(e&&(t?.length&&Array.from(e.querySelectorAll("tbody tr")).forEach((e,r)=>{t[r]&&(e.style.height=`${t[r]}px`)}),r)){const t=e.querySelector("thead tr");t&&(t.style.height=`${r}px`)}}(a,S,w),g()&&c&&((l,{ascIcon:a=e,descIcon:n=t,defaultSortIcon:c=r,enableSorting:d=!0})=>{if(!l)return;const h=l.querySelectorAll("th");0!==h.length&&h.forEach((e,t)=>{if(!d){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&&e.querySelector(".sorting"))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,a,n,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?n.src:a.src))),s(l,t,!d)})(e,t,h,l,a,n,c)),e.dataset.sortableApplied="true"})})(a,{enableSorting:c}),g()&&d&&d.length&&m&&h({data:d,element:m}),g()&&y&&p&&new n(a,p),g()&&f&&(u.assignDataAttributes(a),function(e,t){if(e&&t)for(const r in t){const o=t[r],{rowSpan:s,colSpan:l,hidden:i,display:a,"data-row":n,"data-col":c}=o,d=`[data-row="${n}"][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!=a?a:""):console.warn(`Cell not found at row ${n}, col ${c}`)}}(a,f))})})})();
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>"},o={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 o=e.querySelector(".sorting");o&&(o.innerHTML="",o.appendChild(i(t.src)))})},s=(e,t,o)=>{const r=e.querySelector("tbody");if(!r)return;const s=Array.from(r.querySelectorAll("tr"));0!==s.length&&(s.sort((e,r)=>l(e,r,t,o)),r.innerHTML="",s.forEach(e=>r.appendChild(e)))},l=(e,t,o,r)=>{const s=e.cells[o]?.textContent.trim(),l=t.cells[o]?.textContent.trim();return void 0===s||void 0===l?0:isNaN(s)||isNaN(l)?r?s.localeCompare(l):l.localeCompare(s):r?s-l:l-s},i=e=>{if(!e)return null;const t=document.createElement("div");t.innerHTML=e.trim();const o=t.firstChild;return o&&"svg"===o.tagName&&(o.setAttribute("width","16"),o.setAttribute("height","16")),o};window.wp.i18n;const 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,mobileBreakpoint:768,...t},this.cellPositions=new Map,this.isInitialized=!1,this.observer=null,this.resizeObserver=null,this.debounceTimer=null,this.resizeDebounceTimer=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=>{e.some(e=>"childList"===e.type&&(e.addedNodes.length||e.removedNodes.length))&&(clearTimeout(this.debounceTimer),this.debounceTimer=setTimeout(()=>this.refreshPositions(),100))}),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(()=>{clearTimeout(this.resizeDebounceTimer),this.resizeDebounceTimer=setTimeout(()=>this.refreshPositions(),100)}),this.resizeObserver.observe(e)}ensureTableWidth(){if("undefined"!=typeof window&&window.matchMedia(`(max-width: ${this.config.mobileBreakpoint}px)`).matches)return;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.style.width||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");if(this.cellPositions.clear(),!e.length)return;const t=e[0].querySelectorAll("th, td"),o=[];let r=0;t.forEach(e=>{const t=e.getBoundingClientRect().width;o.push({width:t,left:r}),r+=t}),e.forEach((e,t)=>{e.querySelectorAll("th, td").forEach((e,r)=>{const s=o[r];s&&(this.cellPositions.set(`${t}-${r}`,{left:s.left,width:s.width,rowIndex:t,cellIndex:r}),e.setAttribute("data-row-index",t),e.setAttribute("data-cell-index",r))})})}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.position||(this.table.style.position="relative"),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.position="sticky",e.style.top="0",e.style.zIndex="20",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="")})}applyFrozenCellStyles(e,{position:t,left:o,right:r,zIndex:s,bgColor:l,color:i}){e.style.position="sticky",e.style.zIndex=s,"left"===t?(e.style.left=`${Math.round(o)}px`,e.setAttribute("data-freeze-position",Math.round(o))):(e.style.right=`${Math.round(r)}px`,e.setAttribute("data-freeze-position",Math.round(r))),e.hasAttribute("data-conditional-formatting")||(l&&(e.style.backgroundColor=n(l)),i&&(e.style.color=n(i))),e.setAttribute("data-frozen",t)}applyLeftFreeze(){let{from:e,to:t}=this.config.leftFreeze;if(null===e||null===t)return;t<e&&(t=e),this.clearFrozenColumns("left");let o=0;for(let r=e-1;r<t;r++){const e=this.table.querySelectorAll(`[data-cell-index="${r}"]`);if(!e.length)continue;e.forEach(e=>{const t=0===parseInt(e.getAttribute("data-row-index"))&&this.config.fixedHeader?20:15;this.applyFrozenCellStyles(e,{position:"left",left:o,zIndex:t,bgColor:this.config.leftColumnBgColor,color:this.config.leftColumnColor})});const t=this.cellPositions.get(`0-${r}`);t&&(o+=t.width)}}applyRightFreeze(){let{from:e,to:t}=this.config.rightFreeze;if(null===e||null===t)return;t<e&&(t=e),this.clearFrozenColumns("right");let o=0;for(let r=t-1;r>=e-1;r--){const e=this.table.querySelectorAll(`[data-cell-index="${r}"]`);if(!e.length)continue;e.forEach(e=>{const t=0===parseInt(e.getAttribute("data-row-index"))&&this.config.fixedHeader?20:15;this.applyFrozenCellStyles(e,{position:"right",right:o,zIndex:t,bgColor:this.config.rightColumnBgColor,color:this.config.rightColumnColor})});const t=this.cellPositions.get(`0-${r}`);t&&(o+=t.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.table.style.minWidth="",this.cellPositions.clear()}destroy(){this.cleanup(),this.observer&&this.observer.disconnect(),this.resizeObserver&&this.resizeObserver.disconnect(),clearTimeout(this.debounceTimer),clearTimeout(this.resizeDebounceTimer);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=(()=>{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 o=Array.from(t.querySelectorAll("table")),r=[...new Set([...o])];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 o=(e=>{const t=new Map,o=e.querySelector("tr:first-child");return o&&o.querySelectorAll("th, td").forEach((e,o)=>{const r=e.textContent.trim().toLowerCase().replace(/\s+/g,"");t.set(r,o)}),t})(t);e.data.forEach(e=>{((e,t,o)=>{const{columnKey:r,comparisonType:s="equal",comparisonValue:l="",formatAction:i,formatTextColor:n,formatBackground:a,formatCssClass:h}=t;if(!r||!i)return;const u=o.get(r);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"),o=t[u];if(!o)return;const r=o.textContent.trim().toLowerCase(),l=d[s]?.(r,f);let g=[];p?g=[o]:l&&(g=m?Array.from(t):[o]),g.forEach(e=>{((e,t)=>{const{formatAction:o,formatTextColor:r,formatBackground:s,formatCssClass:l}=t;if(o.includes("TextColor")&&r){const t=c(r);t&&(e.style.color=t)}if(o.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,o=e.stops?.map(e=>`${e.color} ${e.position}%`).join(", ");return`linear-gradient(${t}deg, ${o})`}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(o.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 o=e.getAttribute("data-previous-class");o&&e.classList.remove(o),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,o)})})};function u(e,t,o=768){if(!e||!t?.length)return;if("undefined"!=typeof window&&window.matchMedia(`(max-width: ${o}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,o)=>{t[o]&&"none"!==e.style.display&&(e.style.width=(e=>`${((t[e]||0)/r*100).toFixed(4)}%`)(o),e.style.minWidth="",e.style.maxWidth="")})}))}window.wp.element;class g{constructor(e,t,o,r,s=()=>{},l=()=>{}){this.getModalTable=e,this.getExternalTable=t,this.cells=o||{},this.setAttributes=r,this.onMergeModeChange=s,this.onMergeWarning=l,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,o)=>{e.setAttribute("data-row",t),e.setAttribute("data-col",o)})})}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&&(g.assignDataAttributes(e),g.assignDataAttributes(t),Object.keys(this.cells).forEach(o=>{const r=this.cells[o];if(!r)return;const[s,l]=o.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="",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),o=parseInt(e.dataset.col),r=this.getKey(t,o),s=this.cells[r];this.clearSelection(),e.classList.add("selected");const l=this.getExternalTable()?.querySelector(`[data-row="${t}"][data-col="${o}"]`);l&&l.classList.add("selected"),this.selectedCell={row:t,col:o,element:e},this.focusedCell={row:t,col:o};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(),o=this.getExternalTable();if(!t||!o)return;const r=t.querySelector(`[data-row="${this.startCoords.row}"][data-col="${this.startCoords.col}"]`);if(!r)return;const s=this.getSectionType(r),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 r=u;r<=g;r++){const l=t.querySelector(`[data-row="${e}"][data-col="${r}"]`),i=o.querySelector(`[data-row="${e}"][data-col="${r}"]`);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 o=e.querySelectorAll(".selected");return!(o.length<1)&&(1===o.length&&"unmerge"===this.mergeMode?this.unmergeCell():o.length>1&&this.mergeCells(o))}mergeCells(e){if(e.length<2)return!1;const t=this.getSectionType(e[0]);if(!t)return!1;for(let o of e)if(this.getSectionType(o)!==t)return console.warn("Cannot merge cells from different table sections."),!1;const o=Array.from(e).map(e=>({row:parseInt(e.dataset.row),col:parseInt(e.dataset.col),element:e})),r=o.map(e=>e.row),s=o.map(e=>e.col),l=Math.min(...r),i=Math.max(...r),n=Math.min(...s),a=Math.max(...s),c=i-l+1,d=a-n+1,h=o.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},o.forEach(e=>{if(e.element!==h.element){const t=this.getKey(e.row,e.col);e.element.style.display="none";const o=u.querySelector(`[data-row="${e.row}"][data-col="${e.col}"]`);o&&(o.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:o}=this.selectedCell,r=this.getKey(e,t),s=this.cells[r];if(!s)return!1;const l=this.getModalTable(),i=this.getExternalTable(),n={...this.cells},a=s.rowSpan||1,c=s.colSpan||1;o.rowSpan=1,o.colSpan=1;const d=i.querySelector(`[data-row="${e}"][data-col="${t}"]`);d&&(d.rowSpan=1,d.colSpan=1);for(let o=e;o<e+a;o++)for(let e=t;e<t+c;e++){const t=this.getKey(o,e),r=l.querySelector(`[data-row="${o}"][data-col="${e}"]`);r&&(r.style.display="");const s=i.querySelector(`[data-row="${o}"][data-col="${e}"]`);s&&(s.style.display=""),n[t]={rowSpan:1,colSpan:1,hidden:!1,display:"","data-row":o,"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 o=this.focusedCell.row,r=this.focusedCell.col;switch(e){case"up":o=Math.max(0,o-1);break;case"down":o+=1;break;case"left":r=Math.max(0,r-1);break;case"right":r+=1}const s=t.querySelector(`[data-row="${o}"][data-col="${r}"]`);s&&"none"!==s.style.display&&(this.handleCellSelection(s),s.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 o=this.focusedCell.row,r=this.focusedCell.col;switch(e){case"up":o=Math.max(0,o-1);break;case"down":o+=1;break;case"left":r=Math.max(0,r-1);break;case"right":r+=1}const s=t.querySelector(`[data-row="${o}"][data-col="${r}"]`);s&&"none"!==s.style.display&&(this.focusedCell={row:parseInt(s.dataset.row),col:parseInt(s.dataset.col)},this.updateSelectionRange(s),s.scrollIntoView({block:"nearest",inline:"nearest"}))}clearAllMerges(){const e=this.getModalTable(),t=this.getExternalTable();if(!e||!t)return!1;const o={};return e.querySelectorAll("td, th").forEach(e=>{const r=parseInt(e.dataset.row),s=parseInt(e.dataset.col),l=this.getKey(r,s);e.rowSpan=1,e.colSpan=1,e.style.display="";const i=t.querySelector(`[data-row="${r}"][data-col="${s}"]`);i&&(i.rowSpan=1,i.colSpan=1,i.style.display=""),o[l]={rowSpan:1,colSpan:1,hidden:!1,display:"","data-row":r,"data-col":s}}),this.setAttributes(o),this.clearSelection(),!0}}const f=()=>{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(l=>{const n=l.querySelector("table");if(!n)return;const{enableSorting:c,tableConditionalFormat:d,mergeTableCell:p,freezeSettings:m,blockClass:y,enableFreeze:b,columnWidths:S,rowHeights:w,headerHeight:C,resizerEnabled:v,mobileBreakpoint:A}=JSON.parse(l.dataset?.settings||"{}");l.removeAttribute("data-settings"),f()&&!1!==v&&S&&Array.isArray(S)&&S.length>0&&(u(n,S,A),"undefined"!=typeof window)&&window.matchMedia(`(max-width: ${A||768}px)`).addEventListener("change",()=>u(n,S,A)),f()&&!1!==v&&function(e,t,o){if(e&&(t?.length&&Array.from(e.querySelectorAll("tbody tr")).forEach((e,o)=>{t[o]&&(e.style.height=`${t[o]}px`)}),o)){const t=e.querySelector("thead tr");t&&(t.style.height=`${o}px`)}}(n,w,C),f()&&c&&((l,{ascIcon:n=e,descIcon:a=t,defaultSortIcon:c=o,enableSorting:d=!0})=>{if(!l)return;const h=l.querySelectorAll("th");0!==h.length&&h.forEach((e,t)=>{if(!d){e.classList.remove("sortable");const t=e.querySelector(".sorting");t&&t.remove();const o=e.cloneNode(!0);return e.parentNode.replaceChild(o,e),void(o.dataset.sortableApplied="false")}if("true"===e.dataset.sortableApplied&&e.querySelector(".sorting"))return;e.classList.add("sortable");let o=e.querySelector(".sorting");o&&o.remove(),o=document.createElement("span"),o.classList.add("sorting");const u=i(c?.src);u&&o.appendChild(u),e.appendChild(o),e.addEventListener("click",()=>((e,t,o,l,n,a,c)=>{const d=e.classList.contains("sorted-asc");r(o,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}),f()&&d&&d.length&&y&&h({data:d,element:y}),f()&&b&&m&&new a(n,{...m,mobileBreakpoint:A}),f()&&p&&(g.assignDataAttributes(n),function(e,t){if(e&&t)for(const o in t){const r=t[o],{rowSpan:s,colSpan:l,hidden:i,display:n,"data-row":a,"data-col":c}=r,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,p))})})})();