xq-treegrid
Version:
一个基于Bootstrap5和html5sortable的可拖动排序的树状表格列表。
7 lines • 34.4 kB
JavaScript
/*!
* xq-treegrid v1.0.7 (https://xqkeji.cn/demo/xq-treegrid/)
* Author xqkeji.cn
* LICENSE SSPL-1.0
* Copyright 2023 xqkeji.cn
*/
"use strict";(()=>{var j={expanderTemplate:'<span class="xq-treegrid-expander"></span>',indentTemplate:'<span class="xq-treegrid-indent"></span>',indentClass:".xq-treegrid-indent",expanderClass:".xq-treegrid-expander",expanderExpandedClass:"xq-treegrid-expander-expanded",expanderCollapsedClass:"xq-treegrid-expander-collapsed",treeColumn:2,tableClass:".xq-treegrid",topClass:".xq-treegrid-top",dragClass:".xq-drag",checkAllClass:".xq-treegrid .xq-check-all",addBtnClass:".xq-treegrid .xq-add",editBtnClass:".xq-edit",deleteBtnClass:".xq-delete",copyBtnClass:".xq-copy",dragOffset:-30,"xq-url":null};var r={};var Y=(e={})=>{r=Object.assign({},j);if(e){for(const t in e){if(Object.hasOwn(r,t)){r[t]=e[t]}}}};var g=e=>{if(e in r){return r[e]}const t=r["tableClass"];const n=document.querySelector(t);if(n.hasAttribute(e)){return String(n.getAttribute(e))}return""};var n;var v=()=>{if(n){return n}else{const e=g("tableClass");const t=document.querySelector(e);n=t;return n}};var P=()=>{const e=g("checkAllClass");const t=document.querySelector(e);if(t){t.addEventListener("click",e=>{const t=e.currentTarget;const{checked:n}=t;const r=v();const o=r?.querySelectorAll("tr > td:first-child > input[type=checkbox]");for(const s of o){if(s===t){continue}if(n){s.checked=true}else{s.checked=false}}})}};var W=t=>{if(t){let e=t.querySelector("tbody");if(e&&e.querySelectorAll("tr").length>1){const n=e.querySelector("tbody>tr:first-child");const r=n.getAttribute("id");const o=n.querySelector("td:first-child");o?.classList.add("xq-move");e.setAttribute("id","tbody_"+r);const s=e.querySelectorAll("tbody>tr:not(:first-child)");for(const a of s){const i=a.querySelector("td:first-child");i?.classList.add("xq-move");const l=document.createElement("tbody");l.append(a);const c=a.getAttribute("id");l.setAttribute("id","tbody_"+c);e.after(l);e=l}}}};var z=()=>{const e=v();const t=g("topClass");const n=e.querySelectorAll("tbody>"+t);if(n){return n}return null};var F=()=>{const e=v();const t=g("topClass");const n=e.querySelector("tbody>"+t);if(n){return n.getAttribute("pid")}return null};var y=e=>{const t=e.getAttribute("id");if(t){return t}return""};var b=e=>{const t=v();if(!e.startsWith("xq_")){e="xq_"+e}return t.querySelector("#"+e)};var x=e=>{if(e.startsWith("xq_")){e=e.replace("xq_","")}const t=v();const n=t.querySelectorAll('tr[pid="'+e+'"]');return n};var m=e=>{const t=x(e);return t.length>0};var R=e=>{const t=e.getAttribute("pid");if(t){return b(t)}return null};var l=e=>{const t=e.getAttribute("pid");if(t){return t}return null};var E=e=>{const t=e.getAttribute("depth");if(t){return Number.parseInt(t,10)}return 0};var q=e=>{const t=e.getAttribute("is_leaf");return Number.parseInt(t,10)!==0};var e=e=>{if(document.readyState==="loading"){document.addEventListener("DOMContentLoaded",e)}else{e()}};var X=(e,t)=>{const n=[];let r=e.parentNode;while(r&&r.nodeType===Node.ELEMENT_NODE&&r.nodeType!==3){if(r.matches(t)){n.push(r)}r=r.parentNode}return n};var _=(e,t)=>{const n=document.createRange().createContextualFragment(t);e.append(n)};var $=(e,t)=>{const n=document.createRange().createContextualFragment(t);e.prepend(n)};var J=e=>{const t=g("editBtnClass");const n=e.querySelector(t);if(n){n.addEventListener("click",e=>{const t=e.currentTarget;const n=X(t,"tr")[0];const r=y(n);let o="";let s="";o=r.replace("xq_","");if(o){let e=t.getAttribute("xq-url");if(!e){e=window.location.href;if(e.includes(".html")||e.endsWith("/")){e=e.slice(0,Math.max(0,e.lastIndexOf("/")));e=e+"/edit.html?id="+o}else{e=e.slice(0,Math.max(0,e.lastIndexOf("/")));e=e+"/edit/"+o}}const a=n.querySelector("#name_"+o);s=a.getAttribute("value")+"设置";if(window!==window.parent){const i="treegrid-edit-"+o;const l={callback:"xqAddTab",params:[i,s,e]};window.parent.postMessage(l,"*")}else{const c=document.querySelector("#xq-not-tab-url");const d=document.querySelector("#xq-not-tab-url-id");if(c&&d){c.setAttribute("action",e);d.setAttribute("value",o);c.submit()}else{const f='<form id="xq-not-tab-url" target="_blank" action="'+e+'" method="get"><input type="hidden" id="xq-not-tab-url-id" name="id" value="'+o+'"/></form>';_(document.body,f);const u=document.querySelector("#xq-not-tab-url");u.submit()}}}})}};var t='<div id="xq-bs-modal" class="modal" tabindex="-1" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 class="modal-title"><i></i><span>title</span></h5><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body"><p>Modal content.</p></div><div class="modal-footer"></div></div></div></div>';var U={id:"xq-bs-modal",type:"alert",size:"modal-sm",position:"modal-dialog-centered",template:t,title:"提示信息",content:"您确认要进行操作吗?",icon:"info",confirmButton:"确认",cancelButton:"取消",confirmButtonClass:"btn-primary",cancelButtonClass:"btn-secondary",confirm:()=>{},cancel:()=>{},backgroundDismiss:true,autoClose:false};var o={};var V=(e={})=>{o=Object.assign({},U);if(e){for(const t in e){if(Object.prototype.hasOwnProperty.call(o,t)){o[t]=e[t]}}}};var M=e=>{if(e in o){return o[e]}const t=o["id"];const n=document.querySelector("#"+t);if(n.hasAttribute(e)){return String(n.getAttribute(e))}return""};var G={info:"bi bi-info-circle-fill link-primary",warn:"bi bi-info-circle-fill link-warning",error:"bi bi-info-circle-fill link-danger"};var K=e=>{if(Object.prototype.hasOwnProperty.call(G,e)){return G[e]}else{return e}};var Q=(e={})=>{V(e);const t=M("template");_(document.body,t);const n=M("id");const o=document.querySelector("#"+n);if(o){const r=o.querySelector(".modal-dialog");const s=M("size");const a=M("position");r.classList.add(s);r.classList.add(a);const i=o.querySelector(".modal-title>i");const l=o.querySelector(".modal-title>span");const c=o.querySelector(".modal-body>p");if(i){const d=M("icon");const f=K(d);i.className=f}if(l){const u=M("title");l.innerHTML=u}if(c){const p=M("content");c.innerHTML=p}if(typeof bootstrap!==void 0&&typeof bootstrap.Modal!==void 0){const m=new bootstrap.Modal(o);const h=document.querySelector(".modal-footer");if(h){const v=M("type");if(v!=="alert"){const E=M("cancelButtonClass");const q=M("cancelButton");_(h,'<button id="xq-bs-modal-cancel" type="button" class="btn '+E+'" data-bs-dismiss="modal">'+q+"</button>");const w=h.querySelector("#xq-bs-modal-cancel");w?.addEventListener("click",e=>{e.preventDefault();m.hide();const t=M("cancel");if(t!==null){t()}})}const y=M("confirmButtonClass");const b=M("confirmButton");_(h,'<button id="xq-bs-modal-confirm" type="button" class="btn '+y+'">'+b+"</button>");const x=h.querySelector("#xq-bs-modal-confirm");x?.addEventListener("click",e=>{e.preventDefault();m.hide();const t=M("confirm");if(t!==null){t()}})}o.addEventListener("hidden.bs.modal",()=>{m.dispose();o.remove()});const g=M("autoClose");if(g){const C=g;if(C.indexOf("|")){const S=C.split("|");const L=S[0];const A=Number.parseInt(S[1],10);let t;let n;let r=Math.ceil(A/1e3);const T='<span class="countdown"> ('+r+")</span>";if(L==="confirm"){n=h.querySelector("#xq-bs-modal-confirm");_(n,T)}else{n=h.querySelector("#xq-bs-modal-cancel");_(n,T)}o.addEventListener("show.bs.modal",()=>{t=setInterval(function(){const e=n.querySelector(".countdown");r=r-1;e.innerHTML=" ("+r+") ";if(r<=0){clearInterval(t);n.click()}},1e3)})}}m.show()}else{console.log("error","the bootstrap not loaded!")}}};var a=(e={})=>{Q(e)};window.xqConfirm=a;var Z=s=>{const e=g("deleteBtnClass");const t=s.querySelector(e);if(t){t.addEventListener("click",()=>{let t="";if(q(s)){const e=g("xq-url");const n=y(s);const r=n.replace("xq_","");const o={id:r};if(!e){let e=window.location.href;e=e.slice(0,Math.max(0,e.lastIndexOf("/")));e+="/delete";t=e}else{t=e+"/delete"}a({content:"确定要删除吗?",confirm(){fetch(t,{body:JSON.stringify(o),headers:{"content-type":"application/json"},method:"POST"}).then(async e=>{return e.json()}).then(e=>{a({content:e.message,confirm(){if(e.code===200){ke(s)}}})})}})}else{a({content:"存在子栏目无法进行删除操作!"})}})}};var ee=e=>{const l=v();let c=e.getAttribute("id");c=c?.replace("xq_","");const t=e.querySelectorAll("input,select");for(const n of t){const r=n.getAttribute("id");if(r!=="id"&&r!=="check_all"){n.addEventListener("change",e=>{const t=e.currentTarget;let n=null;n=t.type==="checkbox"?t.checked:t.value;const r=t.getAttribute("id");const o=r?.slice(0,Math.max(0,r.indexOf("_")));const s=l?.getAttribute("pid");let a=g("xq-url");const i={id:c,field:o,value:n};if(!a){let e=window.location.href;if(s){e=e.replace("/"+s,"")}e=e.slice(0,Math.max(0,e.lastIndexOf("/")));e+="/change";a=e}else{a+="/change"}fetch(a,{body:JSON.stringify(i),headers:{"content-type":"application/json"},method:"POST"}).then(async e=>{return e.json()}).then(e=>{console.log(e)})})}}};var te=t=>{const e=g("copyBtnClass");const n=t.querySelector(e);if(n){n.addEventListener("click",()=>{let e=t.getAttribute("id");e=e.replace("xq_","");if(e){ne(e);a({content:"已经将id复制到粘贴板"})}})}};var ne=e=>{const t=document.createElement("input");t.setAttribute("value",e);document.body.append(t);t.select();document.execCommand("copy");t.remove()};function c(e,t,n){if(n===void 0){return e&&e.h5s&&e.h5s.data&&e.h5s.data[t]}else{e.h5s=e.h5s||{};e.h5s.data=e.h5s.data||{};e.h5s.data[t]=n}}function re(e){if(e.h5s){delete e.h5s.data}}var w=(e,t)=>{if(!(e instanceof NodeList||e instanceof HTMLCollection||e instanceof Array)){throw new Error("You must provide a nodeList/HTMLCollection/Array of elements to be filtered.")}if(typeof t!=="string"){return Array.from(e)}return Array.from(e).filter(e=>e.nodeType===1&&e.matches(t))};var C=new Map;var oe=class{constructor(){this._config=new Map;this._placeholder=void 0;this._data=new Map}set config(e){if(typeof e!=="object"){throw new Error("You must provide a valid configuration object to the config setter.")}const t=Object.assign({},e);this._config=new Map(Object.entries(t))}get config(){const n={};this._config.forEach((e,t)=>{n[t]=e});return n}setConfig(e,t){if(!this._config.has(e)){throw new Error(`Trying to set invalid configuration item: ${e}`)}this._config.set(e,t)}getConfig(e){if(!this._config.has(e)){throw new Error(`Invalid configuration item requested: ${e}`)}return this._config.get(e)}get placeholder(){return this._placeholder}set placeholder(e){if(!(e instanceof HTMLElement)&&e!==null){throw new Error("A placeholder must be an html element or null.")}this._placeholder=e}setData(e,t){if(typeof e!=="string"){throw new Error("The key must be a string.")}this._data.set(e,t)}getData(e){if(typeof e!=="string"){throw new Error("The key must be a string.")}return this._data.get(e)}deleteData(e){if(typeof e!=="string"){throw new Error("The key must be a string.")}return this._data.delete(e)}};var S=e=>{if(!(e instanceof HTMLElement)){throw new Error("Please provide a sortable to the store function.")}if(!C.has(e)){C.set(e,new oe)}return C.get(e)};function i(t,n,r){if(t instanceof Array){for(let e=0;e<t.length;++e){i(t[e],n,r)}return}t.addEventListener(n,r);S(t).setData(`event${n}`,r)}function s(t,n){if(t instanceof Array){for(let e=0;e<t.length;++e){s(t[e],n)}return}t.removeEventListener(n,S(t).getData(`event${n}`));S(t).deleteData(`event${n}`)}function d(t,n,r){if(t instanceof Array){for(let e=0;e<t.length;++e){d(t[e],n,r)}return}t.setAttribute(n,r)}function f(t,n){if(t instanceof Array){for(let e=0;e<t.length;++e){f(t[e],n)}return}t.removeAttribute(n)}var L=e=>{if(!e.parentElement||e.getClientRects().length===0){throw new Error("target element must be part of the dom")}const t=e.getClientRects()[0];return{left:t.left+window.pageXOffset,right:t.right+window.pageXOffset,top:t.top+window.pageYOffset,bottom:t.bottom+window.pageYOffset}};var se=(t,n=0)=>{let r;return(...e)=>{clearTimeout(r);r=setTimeout(()=>{t(...e)},n)}};var A=(e,t)=>{if(!(e instanceof HTMLElement)||!(t instanceof NodeList||t instanceof HTMLCollection||t instanceof Array)){throw new Error("You must provide an element and a list of elements.")}return Array.from(t).indexOf(e)};var ae=e=>{if(!(e instanceof HTMLElement)){throw new Error("Element is not a node element.")}return e.parentNode!==null};var ie=(e,t,n)=>{if(!(e instanceof HTMLElement)||!(e.parentElement instanceof HTMLElement)){throw new Error("target and element must be a node")}e.parentElement.insertBefore(t,n==="before"?e:e.nextElementSibling)};var le=(e,t)=>ie(e,t,"before");var ce=(e,t)=>ie(e,t,"after");var de=(t,n=(e,t)=>e,e=e=>e)=>{if(!(t instanceof HTMLElement)||!t.isSortable===true){throw new Error("You need to provide a sortableContainer to be serialized.")}if(typeof n!=="function"||typeof e!=="function"){throw new Error("You need to provide a valid serializer for items and the container.")}const r=c(t,"opts");const o=r.items;const s=w(t.children,o);const a=s.map(e=>{return{parent:t,node:e,html:e.outerHTML,index:A(e,s)}});const i={node:t,itemCount:a.length};return{container:e(i),items:a.map(e=>n(e,t))}};var fe=(e,t,n="sortable-placeholder")=>{if(!(e instanceof HTMLElement)){throw new Error("You must provide a valid element as a sortable.")}if(!(t instanceof HTMLElement)&&t!==void 0){throw new Error("You must provide a valid element as a placeholder or set ot to undefined.")}if(t===void 0){if(["UL","OL"].includes(e.tagName)){t=document.createElement("li")}else if(["TABLE","TBODY"].includes(e.tagName)){t=document.createElement("tr");t.innerHTML='<td colspan="100"></td>'}else{t=document.createElement("div")}}if(typeof n==="string"){t.classList.add(...n.split(" "))}return t};var ue=e=>{if(!(e instanceof HTMLElement)){throw new Error("You must provide a valid dom element")}const n=window.getComputedStyle(e);if(n.getPropertyValue("box-sizing")==="border-box"){return parseInt(n.getPropertyValue("height"),10)}return["height","padding-top","padding-bottom"].map(e=>{const t=parseInt(n.getPropertyValue(e),10);return isNaN(t)?0:t}).reduce((e,t)=>e+t)};var pe=e=>{if(!(e instanceof HTMLElement)){throw new Error("You must provide a valid dom element")}const n=window.getComputedStyle(e);return["width","padding-left","padding-right"].map(e=>{const t=parseInt(n.getPropertyValue(e),10);return isNaN(t)?0:t}).reduce((e,t)=>e+t)};var me=(e,t)=>{if(!(e instanceof Array)){throw new Error("You must provide a Array of HTMLElements to be filtered.")}if(typeof t!=="string"){return e}return e.filter(e=>{return e.querySelector(t)instanceof HTMLElement||e.shadowRoot&&e.shadowRoot.querySelector(t)instanceof HTMLElement}).map(e=>{return e.querySelector(t)||e.shadowRoot&&e.shadowRoot.querySelector(t)})};var he=e=>{return e.composedPath&&e.composedPath()[0]||e.target};var ge=(e,t,n)=>{return{element:e,posX:n.pageX-t.left,posY:n.pageY-t.top}};var ve=(e,t,n)=>{if(!(e instanceof Event)){throw new Error("setDragImage requires a DragEvent as the first argument.")}if(!(t instanceof HTMLElement)){throw new Error("setDragImage requires the dragged element as the second argument.")}if(!n){n=ge}if(e.dataTransfer&&e.dataTransfer.setDragImage){const r=L(t);const o=n(t,r,e);if(!(o.element instanceof HTMLElement)||typeof o.posX!=="number"||typeof o.posY!=="number"){throw new Error("The customDragImage function you provided must return and object with the properties element[string], posX[integer], posY[integer].")}e.dataTransfer.effectAllowed="copyMove";e.dataTransfer.setData("text/plain",he(e).id);e.dataTransfer.setDragImage(o.element,o.posX,o.posY)}};var ye=(e,t)=>{if(e.isSortable===true){const n=S(e).getConfig("acceptFrom");if(n!==null&&n!==false&&typeof n!=="string"){throw new Error('HTML5Sortable: Wrong argument, "acceptFrom" must be "null", "false", or a valid selector string.')}if(n!==null){return n!==false&&n.split(",").filter(function(e){return e.length>0&&t.matches(e)}).length>0}if(e===t){return true}if(S(e).getConfig("connectWith")!==void 0&&S(e).getConfig("connectWith")!==null){return S(e).getConfig("connectWith")===S(t).getConfig("connectWith")}}return false};var be={items:null,connectWith:null,disableIEFix:null,acceptFrom:null,copy:false,placeholder:null,placeholderClass:"sortable-placeholder",draggingClass:"sortable-dragging",hoverClass:false,dropTargetContainerClass:false,debounce:0,throttleTime:100,maxItems:0,itemSerializer:void 0,containerSerializer:void 0,customDragImage:null,orientation:"vertical"};function xe(n,r=250){if(typeof n!=="function"){throw new Error("You must provide a function as the first argument for throttle.")}if(typeof r!=="number"){throw new Error("You must provide a number as the second argument for throttle.")}let o=null;return(...e)=>{const t=Date.now();if(o===null||t-o>=r){o=t;n.apply(this,e)}}}var Ee=(e,t)=>{if(typeof S(e).getConfig("hoverClass")==="string"){const n=S(e).getConfig("hoverClass").split(" ");if(t===true){i(e,"mousemove",xe(t=>{if(t.buttons===0){w(e.children,S(e).getConfig("items")).forEach(e=>{if(e===t.target||e.contains(t.target)){e.classList.add(...n)}else{e.classList.remove(...n)}})}},S(e).getConfig("throttleTime")));i(e,"mouseleave",()=>{w(e.children,S(e).getConfig("items")).forEach(e=>{e.classList.remove(...n)})})}else{s(e,"mousemove");s(e,"mouseleave")}}};var T;var O;var I;var u;var p;var D;var qe;var H;var we;var Ce=function(e){s(e,"dragstart");s(e,"dragend");s(e,"dragover");s(e,"dragenter");s(e,"drop");s(e,"mouseenter");s(e,"mouseleave")};var Se=function(e,t){if(e){s(e,"dragleave")}if(t&&t!==e){s(t,"dragleave")}};var Le=function(e,t){let n=e;if(S(t).getConfig("copy")===true){n=e.cloneNode(true);d(n,"aria-copied","true");e.parentElement.appendChild(n);n.style.display="none";n.oldDisplay=e.style.display}return n};var Ae=function(e){re(e);f(e,"aria-dropeffect")};var Te=function(e){f(e,"aria-grabbed");f(e,"aria-copied");f(e,"draggable");f(e,"role")};function _e(e,t){if(t.composedPath){return t.composedPath().find(e=>e.isSortable)}while(e.isSortable!==true){e=e.parentElement}return e}function Me(e,t){const n=c(e,"opts");const r=w(e.children,n.items);const o=r.filter(function(e){return e.contains(t)||e.shadowRoot&&e.shadowRoot.contains(t)});return o.length>0?o[0]:t}var Oe=function(e){const t=c(e,"opts")||{};const n=w(e.children,t.items);const r=me(n,t.handle);Ee(e,false);s(e,"dragover");s(e,"dragenter");s(e,"dragstart");s(e,"dragend");s(e,"drop");Ae(e);s(r,"mousedown");Ce(n);Te(n);Se(u,H);e.isSortable=false};var Ie=function(e){const t=c(e,"opts");const n=w(e.children,t.items);const r=me(n,t.handle);d(e,"aria-dropeffect","move");c(e,"_disabled","false");d(r,"draggable","true");Ee(e,true);if(t.disableIEFix===false){const o=(document||window.document).createElement("span");if(typeof o.dragDrop==="function"){i(r,"mousedown",function(){if(n.indexOf(this)!==-1){this.dragDrop()}else{let e=this.parentElement;while(n.indexOf(e)===-1){e=e.parentElement}e.dragDrop()}})}}};var De=function(e){const t=c(e,"opts");const n=w(e.children,t.items);const r=me(n,t.handle);d(e,"aria-dropeffect","none");c(e,"_disabled","true");d(r,"draggable","false");s(r,"mousedown");Ee(e,false)};var He=function(e){const t=c(e,"opts");const n=w(e.children,t.items);const r=me(n,t.handle);c(e,"_disabled","false");Ce(n);Se(u,H);s(r,"mousedown");s(e,"dragover");s(e,"dragenter");s(e,"drop")};function k(e,h){const s=String(h);h=h||{};if(typeof e==="string"){e=document.querySelectorAll(e)}if(e instanceof HTMLElement){e=[e]}e=Array.prototype.slice.call(e);if(/serialize/.test(s)){return e.map(e=>{const t=c(e,"opts");return de(e,t.itemSerializer,t.containerSerializer)})}e.forEach(function(l){if(/enable|disable|destroy/.test(s)){return k[s](l)}["connectWith","disableIEFix"].forEach(e=>{if(Object.prototype.hasOwnProperty.call(h,e)&&h[e]!==null){console.warn(`HTML5Sortable: You are using the deprecated configuration "${e}". This will be removed in an upcoming version, make sure to migrate to the new options when updating.`)}});h=Object.assign({},be,S(l).config,h);S(l).config=h;c(l,"opts",h);l.isSortable=true;He(l);const e=w(l.children,h.items);let t;if(h.placeholder!==null&&h.placeholder!==void 0){const r=document.createElement(l.tagName);if(h.placeholder instanceof HTMLElement){r.appendChild(h.placeholder)}else{r.innerHTML=h.placeholder}t=r.children[0]}S(l).placeholder=fe(l,t,h.placeholderClass);c(l,"items",h.items);if(h.acceptFrom){c(l,"acceptFrom",h.acceptFrom)}else if(h.connectWith){c(l,"connectWith",h.connectWith)}Ie(l);d(e,"role","option");d(e,"aria-grabbed","false");i(l,"dragstart",function(e){const t=he(e);if(t.isSortable===true){return}e.stopImmediatePropagation();if(h.handle&&!t.matches(h.handle)||t.getAttribute("draggable")==="false"){return}const n=_e(t,e);const r=Me(n,t);qe=w(n.children,h.items);p=qe.indexOf(r);D=A(r,n.children);u=n;ve(e,r,h.customDragImage);O=ue(r);I=pe(r);r.classList.add(h.draggingClass);T=Le(r,n);d(T,"aria-grabbed","true");n.dispatchEvent(new CustomEvent("sortstart",{detail:{origin:{elementIndex:D,index:p,container:u},item:T,originalTarget:t,dragEvent:e}}))});i(l,"dragenter",e=>{const n=he(e);const r=_e(n,e);if(r&&r!==H){we=w(r.children,c(r,"items")).filter(e=>e!==S(l).placeholder);if(h.dropTargetContainerClass){r.classList.add(h.dropTargetContainerClass)}r.dispatchEvent(new CustomEvent("sortenter",{detail:{origin:{elementIndex:D,index:p,container:u},destination:{container:r,itemsBeforeUpdate:we},item:T,originalTarget:n}}));i(r,"dragleave",function(e){const t=e.relatedTarget||e.fromElement;if(!e.currentTarget.contains(t)){if(h.dropTargetContainerClass){r.classList.remove(h.dropTargetContainerClass)}r.dispatchEvent(new CustomEvent("sortleave",{detail:{origin:{elementIndex:D,index:p,container:r},item:T,originalTarget:n}}))}})}H=r});i(l,"dragend",function(e){if(!T){return}T.classList.remove(h.draggingClass);d(T,"aria-grabbed","false");if(T.getAttribute("aria-copied")==="true"&&c(T,"dropped")!=="true"){T.remove()}if(T.oldDisplay!==void 0){T.style.display=T.oldDisplay;delete T.oldDisplay}const t=Array.from(C.values()).map(e=>e.placeholder).filter(e=>e instanceof HTMLElement).filter(ae)[0];if(t){t.remove()}l.dispatchEvent(new CustomEvent("sortstop",{detail:{origin:{elementIndex:D,index:p,container:u},item:T,dragEvent:e}}));H=null;T=null;O=null;I=null});i(l,"drop",function(e){if(!ye(l,T.parentElement)){return}e.preventDefault();e.stopPropagation();c(T,"dropped","true");const t=Array.from(C.values()).map(e=>{return e.placeholder}).filter(e=>e instanceof HTMLElement).filter(ae)[0];if(t){t.replaceWith(T);if(T.oldDisplay!==void 0){T.style.display=T.oldDisplay;delete T.oldDisplay}}else{c(T,"dropped","false");return}const n=S(l).placeholder;const r=w(u.children,h.items).filter(e=>e!==n);const o=this.isSortable===true?this:this.parentElement;const s=w(o.children,c(o,"items")).filter(e=>e!==n);const a=A(T,Array.from(T.parentElement.children).filter(e=>e!==n));const i=A(T,s);if(h.dropTargetContainerClass){o.classList.remove(h.dropTargetContainerClass)}l.dispatchEvent(new CustomEvent("sortstop",{detail:{origin:{elementIndex:D,index:p,container:u},item:T,destination:{index:i,elementIndex:a,container:o,itemsBeforeUpdate:we,items:s},dragEvent:e}}));if(D!==a||u!==o){l.dispatchEvent(new CustomEvent("sortupdate",{detail:{origin:{elementIndex:D,index:p,container:u,itemsBeforeUpdate:qe,items:r},destination:{index:i,elementIndex:a,container:o,itemsBeforeUpdate:we,items:s},item:T,dragEvent:e}}))}});const o=se((n,e,r,o)=>{if(!T){return}if(h.forcePlaceholderSize){S(n).placeholder.style.height=O+"px";S(n).placeholder.style.width=I+"px"}if(Array.from(n.children).indexOf(e)>-1){const s=ue(e);const a=pe(e);const i=A(S(n).placeholder,e.parentElement.children);const l=A(e,e.parentElement.children);if(s>O||a>I){const c=s-O;const d=a-I;const f=L(e).top;const u=L(e).left;if(i<l&&(h.orientation==="vertical"&&o<f||h.orientation==="horizontal"&&r<u)){return}if(i>l&&(h.orientation==="vertical"&&o>f+s-c||h.orientation==="horizontal"&&r>u+a-d)){return}}if(T.oldDisplay===void 0){T.oldDisplay=T.style.display}if(T.style.display!=="none"){T.style.display="none"}let t=false;try{const p=L(e).top+e.offsetHeight/2;const m=L(e).left+e.offsetWidth/2;t=h.orientation==="vertical"&&o>=p||h.orientation==="horizontal"&&r>=m}catch(e){t=i<l}if(t){ce(e,S(n).placeholder)}else{le(e,S(n).placeholder)}Array.from(C.values()).filter(e=>e.placeholder!==void 0).forEach(e=>{if(e.placeholder!==S(n).placeholder){e.placeholder.remove()}})}else{const t=Array.from(C.values()).filter(e=>e.placeholder!==void 0).map(e=>{return e.placeholder});if(t.indexOf(e)===-1&&n===e&&!w(e.children,h.items).length){t.forEach(e=>e.remove());e.appendChild(S(n).placeholder)}}},h.debounce);const n=function(e){let t=e.target;const n=t.isSortable===true?t:_e(t,e);t=Me(n,t);if(!T||!ye(n,T.parentElement)||c(n,"_disabled")==="true"){return}const r=c(n,"opts");if(parseInt(r.maxItems)&&w(n.children,c(n,"items")).length>=parseInt(r.maxItems)&&T.parentElement!==n){return}e.preventDefault();e.stopPropagation();e.dataTransfer.dropEffect=S(n).getConfig("copy")===true?"copy":"move";o(n,t,e.pageX,e.pageY)};i(e.concat(l),"dragover",n);i(e.concat(l),"dragenter",n)});return e}k.destroy=function(e){Oe(e)};k.enable=function(e){Ie(e)};k.disable=function(e){De(e)};k.__testing={data:c,removeItemEvents:Ce,removeItemData:Te,removeSortableData:Ae,removeContainerEvents:Se};var ke=e=>{const t=g("treeColumn");const n=l(e);e.parentElement?.remove();if(n){const r=b(n);if(r&&!m(n)){r.setAttribute("is_leaf","1");const o=r.querySelector("td:nth-child("+t.toString()+")");const s=o.querySelectorAll("span");for(const a of s){a.remove()}B(r)}}};var Ne=e=>{const t=g("treeColumn");const n=g("expanderClass");const r=g("expanderCollapsedClass");const o=e.querySelector("td:nth-child("+t.toString()+")");if(o&&!q(e)){const s=o.querySelector(n);const a=e.getAttribute("id");if(s.classList.contains(r)){return false}else{return true}}else{return true}};var Be=(e,n=()=>{})=>{const t=g("treeColumn");const r=g("expanderClass");const o=g("expanderCollapsedClass");const s=g("expanderExpandedClass");const a=e.querySelector("td:nth-child("+t.toString()+")");if(a&&!q(e)){const i=a.querySelector(r);const l=e.getAttribute("id");if(i.classList.contains(o)){i.classList.remove(o);i.classList.add(s);if(!m(l)){let t=g("xq-url");if(!t){let e=window.location.href;e=e.slice(0,Math.max(0,e.lastIndexOf("/")));const c=l.replace("xq_","");e=e+"/subnode/"+c;t=e}else{const c=l.replace("xq_","");t=t+"/subnode/"+c}if(t.includes(":3000")){t=t.replace(":3000",":9000")}je(e,t,n);return}else{const d=x(l);for(const f of d){Ye(f)}}}else if(i.classList.contains(s)){i.classList.remove(s);i.classList.add(o);const d=x(l);for(const f of d){Pe(f)}}}n()};var je=(e,t,n=()=>{})=>{let r=e;const o=E(e);fetch(t).then(async e=>{return e.json()}).then(e=>{for(const t of e){t.depth=o+1;r=N(r,t)}n()})};var N=(e,t)=>{const n=v();const r=t.id;const o=t.pid;const s=t.is_leaf;const a=t.depth;const i=t.content;const l=document.createElement("tr");const c="xq_"+r;l.setAttribute("id","xq_"+r);l.setAttribute("pid",o);l.setAttribute("is_leaf",s);l.setAttribute("depth",a);l.innerHTML=i;B(l);ee(l);Z(l);J(l);te(l);const d=l.querySelector("td:first-child");d?.classList.add("xq-move");const f=document.createElement("tbody");f.setAttribute("id","tbody_"+c);f.append(l);if(e.tagName==="TABLE"){const p=n.querySelector("thead");p?.after(f)}else{e.parentElement?.after(f)}const u=g("dragClass");if(n.classList.contains(u.substring(1))){k(n,{})}return l};var Ye=e=>{if(e.classList.contains("d-none")){e.classList.remove("d-none")}};var Pe=e=>{const t=g("treeColumn");const n=g("expanderClass");const r=g("expanderCollapsedClass");const o=g("expanderExpandedClass");e.classList.add("d-none");const s=e.querySelector("td:first-child .form-check-input");if(s){s.checked=false}if(!q(e)){const a=x(y(e));const i=e.querySelector("td:nth-child("+t.toString()+")");if(i){const l=i.querySelector(n);l.classList.remove(o);l.classList.add(r)}for(const c of a){Pe(c)}}};var We=e=>{const t=R(e);const n=l(e).replace("xq_","");const r=E(t)+1;e.setAttribute("pid",n);e.setAttribute("depth",r.toString());B(e);if(!q(e)){const o=y(e);const s=x(o);if(s){ze(s)}}};var ze=e=>{for(const t of e){We(t)}};var Fe=e=>{const t=F().replace("xq_","");e.setAttribute("pid",t);e.setAttribute("depth","1");const n=g("topClass").slice(1);if(!e.classList.contains(n)){e.classList.add(n)}h(e)};var Re=(e,t)=>{let n=null;const r=E(e);if(r>1){n=R(e)}const o=y(t).replace("xq_","");const s=E(t)+1;e.setAttribute("pid",o);e.setAttribute("depth",s.toString());const a=g("topClass").slice(1);if(e.classList.contains(a)){e.classList.remove(a)}if(q(t)){t.setAttribute("is_leaf","0")}h(t);const i=g("treeColumn");const l=g("expanderClass");const c=g("expanderCollapsedClass");const d=g("expanderExpandedClass");const f=t.querySelector("td:nth-child("+i.toString()+")");if(f&&!q(t)){const u=f.querySelector(l);if(u.classList.contains(c)){u.classList.remove(c);u.classList.add(d)}}h(e);if(n){const p=y(n);if(!m(p)){n.setAttribute("is_leaf","1")}h(n,true)}};var Xe=(e,t)=>{let n=null;const r=E(e);if(r>1){n=R(e)}const o=l(t).replace("xq_","");const s=E(t);e.setAttribute("pid",o);e.setAttribute("depth",s.toString());const a=g("topClass").slice(1);if(e.classList.contains(a)){e.classList.remove(a)}h(e);if(!q(t)){h(t)}if(n){const i=y(n);if(!m(i)){n.setAttribute("is_leaf","1")}h(n)}};var $e=e=>{const t=g("treeColumn");const n=g("expanderTemplate");const r=g("expanderClass");const o=g("expanderCollapsedClass");const s=g("expanderExpandedClass");const a=e.querySelector("td:nth-child("+t.toString()+")");let i=o;if(a){const l=a.querySelector(r);if(l){if(l.classList.contains(s)){i=s}}const c=a.querySelectorAll("span");for(const d of c){d.remove()}$(a,n);if(!q(e)){const f=a.querySelector(r);if(f){f.classList.add(i);f.addEventListener("click",()=>{Be(e)})}}}};var Je=e=>{const t=g("treeColumn");const n=g("indentTemplate");const r=g("expanderClass");const o=e.querySelector(r);if(o){const s=e.querySelector("td:nth-child("+t.toString()+")");if(s){const a=E(e);for(let e=1;e<a;e++){$(s,n)}}}};var B=e=>{$e(e);Je(e)};var h=(t,n=false)=>{B(t);if(!q(t)){const r=y(t);const o=x(r);const s=t.parentElement;let e=s;for(const a of o){const i=a.parentElement;if(e!==i&&!n){e.after(i)}e=i;We(a);if(!q(a)){h(a)}}}};var Ue=()=>{const u=g("treeColumn");const p=g("expanderClass");const m=g("expanderCollapsedClass");const h=g("expanderExpandedClass");const e=g("addBtnClass");const t=document.querySelector(e);if(t){t.addEventListener("click",()=>{const r=v();const e=r.querySelectorAll("tbody > tr > td:first-child > input[type=checkbox]");const t=[];for(const o of e){if(o.checked){const s=X(o,"tr")[0];const a=y(s);t.push(a)}}let n=g("xq-url");if(!n){let e=window.location.href;e=e.slice(0,Math.max(0,e.lastIndexOf("/")));e+="/add";n=e}else{n+="/add"}if(t.length>0){for(const l of t){const i=b(l);const c=E(i);const d=l.replace("xq_","");const f={pid:d,depth:c};fetch(n,{body:JSON.stringify(f),headers:{"content-type":"application/json"},method:"POST"}).then(async e=>{return e.json()}).then(n=>{const e=b(l);if(e){if(q(e)){const t=e.querySelector("td:nth-child("+u.toString()+")");const r=t.querySelectorAll("span");for(const s of r){s.remove()}e.setAttribute("is_leaf","0");B(e);const o=t.querySelector(p);if(o){o.classList.remove(m);o.classList.add(h)}N(e,n)}else{const t=e.querySelector("td:nth-child("+u.toString()+")");const o=t.querySelector(p);if(o.classList.contains(m)){Be(e,()=>{const e=x(l);const t=e[e.length-1];N(t,n)})}else{const a=x(l);const i=a[a.length-1];N(i,n)}}}})}}else{const f={pid:""};fetch(n,{body:JSON.stringify(f),headers:{"content-type":"application/json"},method:"POST"}).then(async e=>{return e.json()}).then(e=>{const t=r.querySelector("tbody:last-of-type>tr");if(t===null){const n=r.querySelector("tbody:last-of-type");if(n!==null){n.remove()}N(r,e)}else{N(t,e)}})}})}};var Ve=()=>{const e=g("addBtnClass");const t=document.querySelector(e);if(t){if(typeof bootstrap!==void 0&&bootstrap.Tooltip!==void 0){const n=new bootstrap.Tooltip(t)}}};var Ge=e=>{const t=e.previousElementSibling;const n=t.querySelector("tr");if(n){if(n.classList.contains("d-none")){return Ge(t)}return t}return null};var Ke=(e,t)=>{const n=Number.parseInt(e.getAttribute("depth"),10);const r=e.parentElement;let o=r.nextElementSibling;let s=o.firstElementChild;let a=Number.parseInt(s.getAttribute("depth"),10);if(o.tagName=="TFOOT"){return null}while(o){if(a>n){return null}if(s.matches(t)){return s}o=o.nextElementSibling;s=o.firstElementChild;a=Number.parseInt(s.getAttribute("depth"),10);if(o.tagName=="TFOOT"){return null}}return null};var Qe=e=>{const{item:t}=e.detail;const n=t.querySelector("tr");if(n&&!q(n)){const r=t.querySelector("tr");const o=x(y(r));for(const s of o){t.append(s)}}};var Ze=e=>{let{item:t}=e.detail;const n=t.querySelectorAll("tbody>tr:not(:first-child)");if(n){const r=v();for(const o of n){const s=o.getAttribute("id");const a="#tbody_"+s;const i=r.querySelector(a);if(i){t.after(i);t=i;i.append(o)}}}};var et=e=>{const t=v();const{offsetLeft:n}=t;const r=e.detail;const o=r.item;const s=o.querySelector("td:first-child");const a=o.querySelector("tr");const{offsetWidth:i}=s;const l=r.dragEvent.pageX-n-i-10;const c=Ge(o);const d=c.querySelector("tr");if(c.tagName==="THEAD"){Fe(a);const f=tt(a);nt(f)}else if(l>0){if(Ne(d)){Re(a,d);const f=tt(a);nt(f)}else{Be(d,()=>{Re(a,d);const e=tt(a);nt(e)})}}else{Xe(a,d);const f=tt(a);nt(f)}};var tt=e=>{const t={id:"",pid:"",nextid:""};t.id=y(e).replace("xq_","");t.pid=l(e);const n=Ke(e,'tr[pid="'+t.pid+'"]');if(n){t.nextid=y(n).replace("xq_","")}else{t.nextid=""}return t};var nt=e=>{let t;const n=g("xq-url");if(!n){let e=window.location.href;e=e.slice(0,Math.max(0,e.lastIndexOf("/")));e+="/move";t=e}else{t=n+"/move"}fetch(t,{body:JSON.stringify(e),headers:{"content-type":"application/json"},method:"POST"}).then(async e=>{return e.json()}).then(e=>{console.log(e)})};var rt=()=>{const e=g("tableClass");const t=g("dragClass");const n=document.querySelector(e+t);if(n){k(n,{items:"tbody",handle:".xq-move",placeholder:'<tbody><tr><td colspan="99"> </td></tr><tbody>'});n.addEventListener("sortstart",e=>{Qe(e)});n.addEventListener("sortstop",e=>{const t=e.detail.dragEvent;if(t.type==="dragend"){Ze(e);et(e)}})}};var ot=rt;var st=(e={})=>{Y(e);const t=v();if(t){W(t);const n=z();if(n){for(const r of n){B(r);ee(r);Z(r);J(r);te(r)}}Ue();Ve();P();ot()}};e(()=>{st()});window.xqTreegrid=st;var at=st})();