backpack-viewer 0.2.13 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- (function(){const n=document.createElement("link").relList;if(n&&n.supports&&n.supports("modulepreload"))return;for(const r of document.querySelectorAll('link[rel="modulepreload"]'))t(r);new MutationObserver(r=>{for(const e of r)if(e.type==="childList")for(const l of e.addedNodes)l.tagName==="LINK"&&l.rel==="modulepreload"&&t(l)}).observe(document,{childList:!0,subtree:!0});function o(r){const e={};return r.integrity&&(e.integrity=r.integrity),r.referrerPolicy&&(e.referrerPolicy=r.referrerPolicy),r.crossOrigin==="use-credentials"?e.credentials="include":r.crossOrigin==="anonymous"?e.credentials="omit":e.credentials="same-origin",e}function t(r){if(r.ep)return;r.ep=!0;const e=o(r);fetch(r.href,e)}})();async function de(){const c=await fetch("/api/ontologies");return c.ok?c.json():[]}async function re(c){const n=await fetch(`/api/ontologies/${encodeURIComponent(c)}`);if(!n.ok)throw new Error(`Failed to load ontology: ${c}`);return n.json()}async function Te(c,n){if(!(await fetch(`/api/ontologies/${encodeURIComponent(c)}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)})).ok)throw new Error(`Failed to save ontology: ${c}`)}async function Ae(c,n){if(!(await fetch(`/api/ontologies/${encodeURIComponent(c)}/rename`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:n})})).ok)throw new Error(`Failed to rename ontology: ${c}`)}function ke(c,n){const o=typeof n=="function"?{onSelect:n}:n,t=document.createElement("h2");t.textContent="Backpack Viewer";const r=document.createElement("input");r.type="text",r.placeholder="Filter...",r.id="filter";const e=document.createElement("ul");e.id="ontology-list";const l=document.createElement("div");l.className="sidebar-footer",l.innerHTML='<a href="mailto:support@backpackontology.com">support@backpackontology.com</a><span>Feedback & support</span>',c.appendChild(t),c.appendChild(r),c.appendChild(e),c.appendChild(l);let x=[],f="";return r.addEventListener("input",()=>{const u=r.value.toLowerCase();for(const h of x){const i=h.dataset.name??"";h.style.display=i.includes(u)?"":"none"}}),{setSummaries(u){e.innerHTML="",x=u.map(h=>{const i=document.createElement("li");i.className="ontology-item",i.dataset.name=h.name;const p=document.createElement("span");p.className="name",p.textContent=h.name;const L=document.createElement("span");if(L.className="stats",L.textContent=`${h.nodeCount} nodes, ${h.edgeCount} edges`,i.appendChild(p),i.appendChild(L),o.onRename){const A=document.createElement("button");A.className="sidebar-edit-btn",A.textContent="✎",A.title="Rename";const Q=o.onRename;A.addEventListener("click",C=>{C.stopPropagation();const w=document.createElement("input");w.type="text",w.className="sidebar-rename-input",w.value=h.name,p.textContent="",p.appendChild(w),A.style.display="none",w.focus(),w.select();const d=()=>{const M=w.value.trim();M&&M!==h.name?Q(h.name,M):(p.textContent=h.name,A.style.display="")};w.addEventListener("blur",d),w.addEventListener("keydown",M=>{M.key==="Enter"&&w.blur(),M.key==="Escape"&&(w.value=h.name,w.blur())})}),i.appendChild(A)}return i.addEventListener("click",()=>o.onSelect(h.name)),e.appendChild(i),i}),f&&this.setActive(f)},setActive(u){f=u;for(const h of x)h.classList.toggle("active",h.dataset.name===u)}}}const De=5e3,Pe=.005,be=150,he=.9,ge=.01,ye=30,pe=50;function Oe(c,n){for(const o of Object.values(c))if(typeof o=="string")return o;return n}function Be(c){const n=Math.sqrt(c.nodes.length)*be*.5,o=new Map,t=c.nodes.map((e,l)=>{const x=2*Math.PI*l/c.nodes.length,f={id:e.id,x:Math.cos(x)*n,y:Math.sin(x)*n,vx:0,vy:0,label:Oe(e.properties,e.id),type:e.type};return o.set(e.id,f),f}),r=c.edges.map(e=>({sourceId:e.sourceId,targetId:e.targetId,type:e.type}));return{nodes:t,edges:r,nodeMap:o}}function Re(c,n){const{nodes:o,edges:t,nodeMap:r}=c;for(let e=0;e<o.length;e++)for(let l=e+1;l<o.length;l++){const x=o[e],f=o[l];let u=f.x-x.x,h=f.y-x.y,i=Math.sqrt(u*u+h*h);i<ye&&(i=ye);const p=De*n/(i*i),L=u/i*p,A=h/i*p;x.vx-=L,x.vy-=A,f.vx+=L,f.vy+=A}for(const e of t){const l=r.get(e.sourceId),x=r.get(e.targetId);if(!l||!x)continue;const f=x.x-l.x,u=x.y-l.y,h=Math.sqrt(f*f+u*u);if(h===0)continue;const i=Pe*(h-be)*n,p=f/h*i,L=u/h*i;l.vx+=p,l.vy+=L,x.vx-=p,x.vy-=L}for(const e of o)e.vx-=e.x*ge*n,e.vy-=e.y*ge*n;for(const e of o){e.vx*=he,e.vy*=he;const l=Math.sqrt(e.vx*e.vx+e.vy*e.vy);l>pe&&(e.vx=e.vx/l*pe,e.vy=e.vy/l*pe),e.x+=e.vx,e.y+=e.vy}return n*.995}const xe=["#d4a27f","#c17856","#b07a5e","#d4956b","#a67c5a","#cc9e7c","#c4866a","#cb8e6c","#b8956e","#a88a70","#d9b08c","#c4a882","#e8b898","#b5927a","#a8886e","#d1a990"],ve=new Map;function ne(c){const n=ve.get(c);if(n)return n;let o=0;for(let r=0;r<c.length;r++)o=(o<<5)-o+c.charCodeAt(r)|0;const t=xe[Math.abs(o)%xe.length];return ve.set(c,t),t}function K(c){return getComputedStyle(document.documentElement).getPropertyValue(c).trim()}const ee=20,ze=.001;function Ye(c,n){const o=c.querySelector("canvas"),t=o.getContext("2d"),r=window.devicePixelRatio||1;let e={x:0,y:0,scale:1},l=null,x=1,f=0,u=new Set,h=null,i=null,p=null;const L=300;function A(){o.width=o.clientWidth*r,o.height=o.clientHeight*r,d()}const Q=new ResizeObserver(A);Q.observe(c),A();function C(a,s){return[a/e.scale+e.x,s/e.scale+e.y]}function w(a,s){if(!l)return null;const[g,m]=C(a,s);for(let I=l.nodes.length-1;I>=0;I--){const P=l.nodes[I],v=g-P.x,$=m-P.y;if(v*v+$*$<=ee*ee)return P}return null}function d(){if(!l){t.clearRect(0,0,o.width,o.height);return}const a=K("--canvas-edge"),s=K("--canvas-edge-highlight"),g=K("--canvas-edge-dim"),m=K("--canvas-edge-label"),I=K("--canvas-edge-label-highlight"),P=K("--canvas-edge-label-dim"),v=K("--canvas-arrow"),$=K("--canvas-arrow-highlight"),U=K("--canvas-node-label"),V=K("--canvas-node-label-dim"),oe=K("--canvas-type-badge"),we=K("--canvas-type-badge-dim"),Ne=K("--canvas-selection-border"),Se=K("--canvas-node-border");t.save(),t.setTransform(r,0,0,r,0,0),t.clearRect(0,0,o.clientWidth,o.clientHeight),t.save(),t.translate(-e.x*e.scale,-e.y*e.scale),t.scale(e.scale,e.scale);for(const O of l.edges){const _=l.nodeMap.get(O.sourceId),J=l.nodeMap.get(O.targetId);if(!_||!J)continue;const le=h===null||h.has(O.sourceId),ae=h===null||h.has(O.targetId),se=le&&ae;if(h!==null&&!le&&!ae)continue;const Z=u.size>0&&(u.has(O.sourceId)||u.has(O.targetId))||h!==null&&se,fe=h!==null&&!se;if(O.sourceId===O.targetId){N(_,O.type,Z,a,s,m,I);continue}t.beginPath(),t.moveTo(_.x,_.y),t.lineTo(J.x,J.y),t.strokeStyle=Z?s:fe?g:a,t.lineWidth=Z?2.5:1.5,t.stroke(),M(_.x,_.y,J.x,J.y,Z,v,$);const Me=(_.x+J.x)/2,Ie=(_.y+J.y)/2;t.fillStyle=Z?I:fe?P:m,t.font="9px system-ui, sans-serif",t.textAlign="center",t.textBaseline="bottom",t.fillText(O.type,Me,Ie-4)}for(const O of l.nodes){const _=ne(O.type),J=u.has(O.id),le=u.size>0&&l.edges.some(Z=>u.has(Z.sourceId)&&Z.targetId===O.id||u.has(Z.targetId)&&Z.sourceId===O.id),ae=h!==null&&!h.has(O.id),se=ae||u.size>0&&!J&&!le;J&&(t.save(),t.shadowColor=_,t.shadowBlur=20,t.beginPath(),t.arc(O.x,O.y,ee+3,0,Math.PI*2),t.fillStyle=_,t.globalAlpha=.3,t.fill(),t.restore()),t.beginPath(),t.arc(O.x,O.y,ee,0,Math.PI*2),t.fillStyle=_,t.globalAlpha=ae?.1:se?.3:1,t.fill(),t.strokeStyle=J?Ne:Se,t.lineWidth=J?3:1.5,t.stroke();const me=O.label.length>24?O.label.slice(0,22)+"...":O.label;t.fillStyle=se?V:U,t.font="11px system-ui, sans-serif",t.textAlign="center",t.textBaseline="top",t.fillText(me,O.x,O.y+ee+4),t.fillStyle=se?we:oe,t.font="9px system-ui, sans-serif",t.textBaseline="bottom",t.fillText(O.type,O.x,O.y-ee-3),t.globalAlpha=1}t.restore(),t.restore()}function M(a,s,g,m,I,P,v){const $=Math.atan2(m-s,g-a),U=g-Math.cos($)*ee,V=m-Math.sin($)*ee,oe=8;t.beginPath(),t.moveTo(U,V),t.lineTo(U-oe*Math.cos($-.4),V-oe*Math.sin($-.4)),t.lineTo(U-oe*Math.cos($+.4),V-oe*Math.sin($+.4)),t.closePath(),t.fillStyle=I?v:P,t.fill()}function N(a,s,g,m,I,P,v){const $=a.x+ee+15,U=a.y-ee-15;t.beginPath(),t.arc($,U,15,0,Math.PI*2),t.strokeStyle=g?I:m,t.lineWidth=g?2.5:1.5,t.stroke(),t.fillStyle=g?v:P,t.font="9px system-ui, sans-serif",t.textAlign="center",t.fillText(s,$,U-18)}function E(){if(!i||!p)return;const a=performance.now()-p.time,s=Math.min(a/L,1),g=1-Math.pow(1-s,3);e.x=p.x+(i.x-p.x)*g,e.y=p.y+(i.y-p.y)*g,d(),s<1?requestAnimationFrame(E):(i=null,p=null)}function B(){!l||x<ze||(x=Re(l,x),d(),f=requestAnimationFrame(B))}let X=!1,q=!1,j=0,W=0;o.addEventListener("mousedown",a=>{X=!0,q=!1,j=a.clientX,W=a.clientY}),o.addEventListener("mousemove",a=>{if(!X)return;const s=a.clientX-j,g=a.clientY-W;(Math.abs(s)>2||Math.abs(g)>2)&&(q=!0),e.x-=s/e.scale,e.y-=g/e.scale,j=a.clientX,W=a.clientY,d()}),o.addEventListener("mouseup",a=>{if(X=!1,q)return;const s=o.getBoundingClientRect(),g=a.clientX-s.left,m=a.clientY-s.top,I=w(g,m),P=a.ctrlKey||a.metaKey;if(I){P?u.has(I.id)?u.delete(I.id):u.add(I.id):u.size===1&&u.has(I.id)?u.clear():(u.clear(),u.add(I.id));const v=[...u];n==null||n(v.length>0?v:null)}else u.clear(),n==null||n(null);d()}),o.addEventListener("mouseleave",()=>{X=!1}),o.addEventListener("wheel",a=>{a.preventDefault();const s=o.getBoundingClientRect(),g=a.clientX-s.left,m=a.clientY-s.top,[I,P]=C(g,m),v=a.ctrlKey?1-a.deltaY*.01:a.deltaY>0?.9:1.1;e.scale=Math.max(.05,Math.min(10,e.scale*v)),e.x=I-g/e.scale,e.y=P-m/e.scale,d()},{passive:!1});let H=[],T=0,z=1,F=0,G=0,S=!1;o.addEventListener("touchstart",a=>{a.preventDefault(),H=Array.from(a.touches),H.length===2?(T=k(H[0],H[1]),z=e.scale):H.length===1&&(j=H[0].clientX,W=H[0].clientY,F=H[0].clientX,G=H[0].clientY,S=!1)},{passive:!1}),o.addEventListener("touchmove",a=>{a.preventDefault();const s=Array.from(a.touches);if(s.length===2&&H.length===2){const m=k(s[0],s[1])/T;e.scale=Math.max(.05,Math.min(10,z*m)),d()}else if(s.length===1){const g=s[0].clientX-j,m=s[0].clientY-W;(Math.abs(s[0].clientX-F)>10||Math.abs(s[0].clientY-G)>10)&&(S=!0),e.x-=g/e.scale,e.y-=m/e.scale,j=s[0].clientX,W=s[0].clientY,d()}H=s},{passive:!1}),o.addEventListener("touchend",a=>{if(a.preventDefault(),S||a.changedTouches.length!==1)return;const s=a.changedTouches[0],g=o.getBoundingClientRect(),m=s.clientX-g.left,I=s.clientY-g.top,P=w(m,I);if(P){u.size===1&&u.has(P.id)?u.clear():(u.clear(),u.add(P.id));const v=[...u];n==null||n(v.length>0?v:null)}else u.clear(),n==null||n(null);d()},{passive:!1}),o.addEventListener("gesturestart",a=>a.preventDefault()),o.addEventListener("gesturechange",a=>a.preventDefault());function k(a,s){const g=a.clientX-s.clientX,m=a.clientY-s.clientY;return Math.sqrt(g*g+m*m)}const y=document.createElement("div");y.className="zoom-controls";const R=document.createElement("button");R.className="zoom-btn",R.textContent="+",R.title="Zoom in",R.addEventListener("click",()=>{const a=o.clientWidth/2,s=o.clientHeight/2,[g,m]=C(a,s);e.scale=Math.min(10,e.scale*1.3),e.x=g-a/e.scale,e.y=m-s/e.scale,d()});const D=document.createElement("button");D.className="zoom-btn",D.textContent="−",D.title="Zoom out",D.addEventListener("click",()=>{const a=o.clientWidth/2,s=o.clientHeight/2,[g,m]=C(a,s);e.scale=Math.max(.05,e.scale/1.3),e.x=g-a/e.scale,e.y=m-s/e.scale,d()});const Y=document.createElement("button");return Y.className="zoom-btn",Y.textContent="○",Y.title="Reset zoom",Y.addEventListener("click",()=>{if(l){if(e={x:0,y:0,scale:1},l.nodes.length>0){let a=1/0,s=1/0,g=-1/0,m=-1/0;for(const v of l.nodes)v.x<a&&(a=v.x),v.y<s&&(s=v.y),v.x>g&&(g=v.x),v.y>m&&(m=v.y);const I=(a+g)/2,P=(s+m)/2;e.x=I-o.clientWidth/2,e.y=P-o.clientHeight/2}d()}}),y.appendChild(R),y.appendChild(Y),y.appendChild(D),c.appendChild(y),{loadGraph(a){if(cancelAnimationFrame(f),l=Be(a),x=1,u=new Set,h=null,e={x:0,y:0,scale:1},l.nodes.length>0){let s=1/0,g=1/0,m=-1/0,I=-1/0;for(const V of l.nodes)V.x<s&&(s=V.x),V.y<g&&(g=V.y),V.x>m&&(m=V.x),V.y>I&&(I=V.y);const P=(s+m)/2,v=(g+I)/2,$=o.clientWidth,U=o.clientHeight;e.x=P-$/2,e.y=v-U/2}B()},setFilteredNodeIds(a){h=a,d()},panToNode(a){if(!l)return;const s=l.nodeMap.get(a);if(!s)return;u=new Set([a]),n==null||n([a]);const g=o.clientWidth,m=o.clientHeight;p={x:e.x,y:e.y,time:performance.now()},i={x:s.x-g/(2*e.scale),y:s.y-m/(2*e.scale)},E()},destroy(){cancelAnimationFrame(f),Q.disconnect()}}}function ce(c){for(const n of Object.values(c.properties))if(typeof n=="string")return n;return c.id}const He="✎";function $e(c,n,o){const t=document.createElement("div");t.id="info-panel",t.className="info-panel hidden",c.appendChild(t);let r=!1,e=[],l=-1,x=!1,f=null;function u(){t.classList.add("hidden"),t.classList.remove("info-panel-maximized"),t.innerHTML="",r=!1,e=[],l=-1}function h(C){!f||!o||(l<e.length-1&&(e=e.slice(0,l+1)),e.push(C),l=e.length-1,x=!0,o(C),x=!1)}function i(){l<=0||!f||!o||(l--,x=!0,o(e[l]),x=!1)}function p(){l>=e.length-1||!f||!o||(l++,x=!0,o(e[l]),x=!1)}function L(){const C=document.createElement("div");C.className="info-panel-toolbar";const w=document.createElement("button");w.className="info-toolbar-btn",w.textContent="←",w.title="Back",w.disabled=l<=0,w.addEventListener("click",i),C.appendChild(w);const d=document.createElement("button");d.className="info-toolbar-btn",d.textContent="→",d.title="Forward",d.disabled=l>=e.length-1,d.addEventListener("click",p),C.appendChild(d);const M=document.createElement("button");M.className="info-toolbar-btn",M.textContent=r?"⎘":"⛶",M.title=r?"Restore":"Maximize",M.addEventListener("click",()=>{r=!r,t.classList.toggle("info-panel-maximized",r),M.textContent=r?"⎘":"⛶",M.title=r?"Restore":"Maximize"}),C.appendChild(M);const N=document.createElement("button");return N.className="info-toolbar-btn info-close-btn",N.textContent="×",N.title="Close",N.addEventListener("click",u),C.appendChild(N),C}function A(C,w){const d=w.nodes.find(S=>S.id===C);if(!d)return;const M=w.edges.filter(S=>S.sourceId===C||S.targetId===C);t.innerHTML="",t.classList.remove("hidden"),r&&t.classList.add("info-panel-maximized"),t.appendChild(L());const N=document.createElement("div");N.className="info-header";const E=document.createElement("span");if(E.className="info-type-badge",E.textContent=d.type,E.style.backgroundColor=ne(d.type),n){E.classList.add("info-editable");const S=document.createElement("button");S.className="info-inline-edit",S.textContent=He,S.addEventListener("click",k=>{k.stopPropagation();const y=document.createElement("input");y.type="text",y.className="info-edit-inline-input",y.value=d.type,E.textContent="",E.appendChild(y),y.focus(),y.select();const R=()=>{const D=y.value.trim();D&&D!==d.type?n.onChangeNodeType(C,D):(E.textContent=d.type,E.appendChild(S))};y.addEventListener("blur",R),y.addEventListener("keydown",D=>{D.key==="Enter"&&y.blur(),D.key==="Escape"&&(y.value=d.type,y.blur())})}),E.appendChild(S)}const B=document.createElement("h3");B.className="info-label",B.textContent=ce(d);const X=document.createElement("span");X.className="info-id",X.textContent=d.id,N.appendChild(E),N.appendChild(B),N.appendChild(X),t.appendChild(N);const q=Object.keys(d.properties),j=ie("Properties");if(q.length>0){const S=document.createElement("dl");S.className="info-props";for(const k of q){const y=document.createElement("dt");y.textContent=k;const R=document.createElement("dd");if(n){const D=ue(d.properties[k]),Y=document.createElement("input");Y.type="text",Y.className="info-edit-input",Y.value=D,Y.addEventListener("keydown",s=>{s.key==="Enter"&&Y.blur()}),Y.addEventListener("blur",()=>{const s=Y.value;s!==D&&n.onUpdateNode(C,{[k]:Fe(s)})}),R.appendChild(Y);const a=document.createElement("button");a.className="info-delete-prop",a.textContent="×",a.title=`Remove ${k}`,a.addEventListener("click",()=>{const s={...d.properties};delete s[k],n.onUpdateNode(C,s)}),R.appendChild(a)}else R.appendChild(Xe(d.properties[k]));S.appendChild(y),S.appendChild(R)}j.appendChild(S)}if(n){const S=document.createElement("button");S.className="info-add-btn",S.textContent="+ Add property",S.addEventListener("click",()=>{const k=document.createElement("div");k.className="info-add-row";const y=document.createElement("input");y.type="text",y.className="info-edit-input",y.placeholder="key";const R=document.createElement("input");R.type="text",R.className="info-edit-input",R.placeholder="value";const D=document.createElement("button");D.className="info-add-save",D.textContent="Add",D.addEventListener("click",()=>{y.value&&n.onAddProperty(C,y.value,R.value)}),k.appendChild(y),k.appendChild(R),k.appendChild(D),j.appendChild(k),y.focus()}),j.appendChild(S)}if(t.appendChild(j),M.length>0){const S=ie(`Connections (${M.length})`),k=document.createElement("ul");k.className="info-connections";for(const y of M){const R=y.sourceId===C,D=R?y.targetId:y.sourceId,Y=w.nodes.find(v=>v.id===D),a=Y?ce(Y):D,s=document.createElement("li");if(s.className="info-connection",o&&Y&&(s.classList.add("info-connection-link"),s.addEventListener("click",v=>{v.target.closest(".info-delete-edge")||h(D)})),Y){const v=document.createElement("span");v.className="info-target-dot",v.style.backgroundColor=ne(Y.type),s.appendChild(v)}const g=document.createElement("span");g.className="info-arrow",g.textContent=R?"→":"←";const m=document.createElement("span");m.className="info-edge-type",m.textContent=y.type;const I=document.createElement("span");I.className="info-target",I.textContent=a,s.appendChild(g),s.appendChild(m),s.appendChild(I);const P=Object.keys(y.properties);if(P.length>0){const v=document.createElement("div");v.className="info-edge-props";for(const $ of P){const U=document.createElement("span");U.className="info-edge-prop",U.textContent=`${$}: ${ue(y.properties[$])}`,v.appendChild(U)}s.appendChild(v)}if(n){const v=document.createElement("button");v.className="info-delete-edge",v.textContent="×",v.title="Remove connection",v.addEventListener("click",$=>{$.stopPropagation(),n.onDeleteEdge(y.id)}),s.appendChild(v)}k.appendChild(s)}S.appendChild(k),t.appendChild(S)}const W=ie("Timestamps"),H=document.createElement("dl");H.className="info-props";const T=document.createElement("dt");T.textContent="created";const z=document.createElement("dd");z.textContent=Ce(d.createdAt);const F=document.createElement("dt");F.textContent="updated";const G=document.createElement("dd");if(G.textContent=Ce(d.updatedAt),H.appendChild(T),H.appendChild(z),H.appendChild(F),H.appendChild(G),W.appendChild(H),t.appendChild(W),n){const S=document.createElement("div");S.className="info-section info-danger";const k=document.createElement("button");k.className="info-delete-node",k.textContent="Delete node",k.addEventListener("click",()=>{n.onDeleteNode(C),u()}),S.appendChild(k),t.appendChild(S)}}function Q(C,w){const d=new Set(C),M=w.nodes.filter(T=>d.has(T.id));if(M.length===0)return;const N=w.edges.filter(T=>d.has(T.sourceId)&&d.has(T.targetId));t.innerHTML="",t.classList.remove("hidden"),r&&t.classList.add("info-panel-maximized"),t.appendChild(L());const E=document.createElement("div");E.className="info-header";const B=document.createElement("h3");B.className="info-label",B.textContent=`${M.length} nodes selected`,E.appendChild(B);const X=document.createElement("div");X.style.cssText="display:flex;flex-wrap:wrap;gap:4px;margin-top:6px";const q=new Map;for(const T of M)q.set(T.type,(q.get(T.type)??0)+1);for(const[T,z]of q){const F=document.createElement("span");F.className="info-type-badge",F.style.backgroundColor=ne(T),F.textContent=z>1?`${T} (${z})`:T,X.appendChild(F)}E.appendChild(X),t.appendChild(E);const j=ie("Selected Nodes"),W=document.createElement("ul");W.className="info-connections";for(const T of M){const z=document.createElement("li");z.className="info-connection",o&&(z.classList.add("info-connection-link"),z.addEventListener("click",()=>{h(T.id)}));const F=document.createElement("span");F.className="info-target-dot",F.style.backgroundColor=ne(T.type);const G=document.createElement("span");G.className="info-target",G.textContent=ce(T);const S=document.createElement("span");S.className="info-edge-type",S.textContent=T.type,z.appendChild(F),z.appendChild(G),z.appendChild(S),W.appendChild(z)}j.appendChild(W),t.appendChild(j);const H=ie(N.length>0?`Connections Between Selected (${N.length})`:"Connections Between Selected");if(N.length===0){const T=document.createElement("p");T.style.cssText="font-size:12px;color:var(--text-dim)",T.textContent="No direct connections between selected nodes",H.appendChild(T)}else{const T=document.createElement("ul");T.className="info-connections";for(const z of N){const F=w.nodes.find(m=>m.id===z.sourceId),G=w.nodes.find(m=>m.id===z.targetId),S=F?ce(F):z.sourceId,k=G?ce(G):z.targetId,y=document.createElement("li");if(y.className="info-connection",F){const m=document.createElement("span");m.className="info-target-dot",m.style.backgroundColor=ne(F.type),y.appendChild(m)}const R=document.createElement("span");R.className="info-target",R.textContent=S;const D=document.createElement("span");D.className="info-arrow",D.textContent="→";const Y=document.createElement("span");Y.className="info-edge-type",Y.textContent=z.type;const a=document.createElement("span");if(a.className="info-arrow",a.textContent="→",y.appendChild(R),y.appendChild(D),y.appendChild(Y),y.appendChild(a),G){const m=document.createElement("span");m.className="info-target-dot",m.style.backgroundColor=ne(G.type),y.appendChild(m)}const s=document.createElement("span");s.className="info-target",s.textContent=k,y.appendChild(s);const g=Object.keys(z.properties);if(g.length>0){const m=document.createElement("div");m.className="info-edge-props";for(const I of g){const P=document.createElement("span");P.className="info-edge-prop",P.textContent=`${I}: ${ue(z.properties[I])}`,m.appendChild(P)}y.appendChild(m)}T.appendChild(y)}H.appendChild(T)}t.appendChild(H)}return{show(C,w){if(f=w,C.length===1&&!x){const d=C[0];e[l]!==d&&(l<e.length-1&&(e=e.slice(0,l+1)),e.push(d),l=e.length-1)}C.length===1?A(C[0],w):C.length>1&&Q(C,w)},hide:u,get visible(){return!t.classList.contains("hidden")}}}function ie(c){const n=document.createElement("div");n.className="info-section";const o=document.createElement("h4");return o.className="info-section-title",o.textContent=c,n.appendChild(o),n}function Xe(c){if(Array.isArray(c)){const o=document.createElement("div");o.className="info-array";for(const t of c){const r=document.createElement("span");r.className="info-tag",r.textContent=String(t),o.appendChild(r)}return o}if(c!==null&&typeof c=="object"){const o=document.createElement("pre");return o.className="info-json",o.textContent=JSON.stringify(c,null,2),o}const n=document.createElement("span");return n.className="info-value",n.textContent=String(c??""),n}function ue(c){return Array.isArray(c)?c.map(String).join(", "):c!==null&&typeof c=="object"?JSON.stringify(c):String(c??"")}function Fe(c){const n=c.trim();if(n==="true")return!0;if(n==="false")return!1;if(n!==""&&!isNaN(Number(n)))return Number(n);if(n.startsWith("[")&&n.endsWith("]")||n.startsWith("{")&&n.endsWith("}"))try{return JSON.parse(n)}catch{return c}return c}function Ce(c){try{return new Date(c).toLocaleString()}catch{return c}}function Le(c){for(const n of Object.values(c.properties))if(typeof n=="string")return n;return c.id}function Ee(c,n){const o=n.toLowerCase();if(Le(c).toLowerCase().includes(o)||c.type.toLowerCase().includes(o))return!0;for(const t of Object.values(c.properties))if(typeof t=="string"&&t.toLowerCase().includes(o))return!0;return!1}function je(c){let n=null,o=null,t=null,r=new Set,e=null;const l=document.createElement("div");l.className="search-overlay hidden";const x=document.createElement("div");x.className="search-input-wrap";const f=document.createElement("input");f.className="search-input",f.type="text",f.placeholder="Search nodes...",f.setAttribute("autocomplete","off"),f.setAttribute("spellcheck","false");const u=document.createElement("kbd");u.className="search-kbd",u.textContent="/";const h=document.createElement("button");h.className="chip-toggle",h.setAttribute("aria-label","Toggle filter chips"),h.innerHTML='<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3"/></svg>';let i=!1;h.addEventListener("click",()=>{i=!i,L.classList.toggle("hidden",!i),h.classList.toggle("active",i)}),x.appendChild(f),x.appendChild(u),x.appendChild(h);const p=document.createElement("ul");p.className="search-results hidden";const L=document.createElement("div");L.className="type-chips hidden",l.appendChild(x),l.appendChild(p),l.appendChild(L),c.appendChild(l);function A(){if(L.innerHTML="",!n)return;const d=new Map;for(const N of n.nodes)d.set(N.type,(d.get(N.type)??0)+1);const M=[...d.keys()].sort();r=new Set;for(const N of M){const E=document.createElement("button");E.className="type-chip",E.dataset.type=N;const B=document.createElement("span");B.className="type-chip-dot",B.style.backgroundColor=ne(N);const X=document.createElement("span");X.textContent=`${N} (${d.get(N)})`,E.appendChild(B),E.appendChild(X),E.addEventListener("click",()=>{r.has(N)?(r.delete(N),E.classList.remove("active")):(r.add(N),E.classList.add("active")),C()}),L.appendChild(E)}}function Q(){if(!n)return null;const d=f.value.trim(),M=r.size===0,N=d.length===0;if(N&&M)return null;const E=new Set;for(const B of n.nodes)!M&&!r.has(B.type)||(N||Ee(B,d))&&E.add(B.id);return E}function C(){const d=Q();o==null||o(d),w()}function w(){p.innerHTML="";const d=f.value.trim();if(!n||d.length===0){p.classList.add("hidden");return}const M=r.size===0,N=[];for(const E of n.nodes)if(!(!M&&!r.has(E.type))&&Ee(E,d)&&(N.push(E),N.length>=8))break;if(N.length===0){p.classList.add("hidden");return}for(const E of N){const B=document.createElement("li");B.className="search-result-item";const X=document.createElement("span");X.className="search-result-dot",X.style.backgroundColor=ne(E.type);const q=document.createElement("span");q.className="search-result-label";const j=Le(E);q.textContent=j.length>36?j.slice(0,34)+"...":j;const W=document.createElement("span");W.className="search-result-type",W.textContent=E.type,B.appendChild(X),B.appendChild(q),B.appendChild(W),B.addEventListener("click",()=>{t==null||t(E.id),f.value="",p.classList.add("hidden"),C()}),p.appendChild(B)}p.classList.remove("hidden")}return f.addEventListener("input",()=>{e&&clearTimeout(e),e=setTimeout(C,150)}),f.addEventListener("keydown",d=>{if(d.key==="Escape")f.value="",f.blur(),p.classList.add("hidden"),C();else if(d.key==="Enter"){const M=p.querySelector(".search-result-item");M==null||M.click()}}),document.addEventListener("click",d=>{l.contains(d.target)||p.classList.add("hidden")}),f.addEventListener("focus",()=>u.classList.add("hidden")),f.addEventListener("blur",()=>{f.value.length===0&&u.classList.remove("hidden")}),{setLearningGraphData(d){n=d,f.value="",p.classList.add("hidden"),n&&n.nodes.length>0?(l.classList.remove("hidden"),A()):l.classList.add("hidden")},onFilterChange(d){o=d},onNodeSelect(d){t=d},clear(){f.value="",p.classList.add("hidden"),r.clear(),i=!1,L.classList.add("hidden"),h.classList.remove("active"),o==null||o(null)},focus(){f.focus()}}}let te="",b=null;async function We(){const c=document.getElementById("canvas-container"),n=window.matchMedia("(prefers-color-scheme: dark)"),t=localStorage.getItem("backpack-theme")??(n.matches?"dark":"light");document.documentElement.setAttribute("data-theme",t);const r=document.createElement("button");r.className="theme-toggle",r.textContent=t==="light"?"☾":"☼",r.title="Toggle light/dark mode",r.addEventListener("click",()=>{const p=document.documentElement.getAttribute("data-theme")==="light"?"dark":"light";document.documentElement.setAttribute("data-theme",p),localStorage.setItem("backpack-theme",p),r.textContent=p==="light"?"☾":"☼"}),c.appendChild(r);async function e(){if(!te||!b)return;b.metadata.updatedAt=new Date().toISOString(),await Te(te,b),l.loadGraph(b),f.setLearningGraphData(b);const i=await de();u.setSummaries(i)}let l;const x=$e(c,{onUpdateNode(i,p){if(!b)return;const L=b.nodes.find(A=>A.id===i);L&&(L.properties={...L.properties,...p},L.updatedAt=new Date().toISOString(),e().then(()=>x.show([i],b)))},onChangeNodeType(i,p){if(!b)return;const L=b.nodes.find(A=>A.id===i);L&&(L.type=p,L.updatedAt=new Date().toISOString(),e().then(()=>x.show([i],b)))},onDeleteNode(i){b&&(b.nodes=b.nodes.filter(p=>p.id!==i),b.edges=b.edges.filter(p=>p.sourceId!==i&&p.targetId!==i),e())},onDeleteEdge(i){var L;if(!b)return;const p=(L=b.edges.find(A=>A.id===i))==null?void 0:L.sourceId;b.edges=b.edges.filter(A=>A.id!==i),e().then(()=>{p&&b&&x.show([p],b)})},onAddProperty(i,p,L){if(!b)return;const A=b.nodes.find(Q=>Q.id===i);A&&(A.properties[p]=L,A.updatedAt=new Date().toISOString(),e().then(()=>x.show([i],b)))}},i=>{l.panToNode(i)});l=Ye(c,i=>{i&&i.length>0&&b?x.show(i,b):x.hide()});const f=je(c);f.onFilterChange(i=>{l.setFilteredNodeIds(i)}),f.onNodeSelect(i=>{l.panToNode(i),b&&x.show([i],b)});const u=ke(document.getElementById("sidebar"),{onSelect:async i=>{te=i,u.setActive(i),x.hide(),f.clear(),b=await re(i),l.loadGraph(b),f.setLearningGraphData(b)},onRename:async(i,p)=>{await Ae(i,p),te===i&&(te=p);const L=await de();u.setSummaries(L),u.setActive(te),te===p&&(b=await re(p),l.loadGraph(b),f.setLearningGraphData(b))}}),h=await de();u.setSummaries(h),h.length>0&&(te=h[0].name,u.setActive(te),b=await re(te),l.loadGraph(b),f.setLearningGraphData(b)),document.addEventListener("keydown",i=>{i.target instanceof HTMLInputElement||i.target instanceof HTMLTextAreaElement||(i.key==="/"||i.key==="k"&&(i.metaKey||i.ctrlKey))&&(i.preventDefault(),f.focus())})}We();