import { css, html, LitElement, nothing, type PropertyValues } from "lit"; import { uid, type Building, type PlanTopology } from "../../model.ts"; import { addTopologyOutside, enableTopology, isOutdoorFloor, outsideFloor, outsideSelection, type OutsideOwner } from "../../topology.ts"; import { tokens } from "../../styles.ts"; import { hint } from "./fields.ts"; import type { EditorScope } from "./editor-context.ts"; /** Metadata only. Picking a row never reassigns floors or creates a storey. */ class HierarchyManager extends LitElement { static properties = { doc:{attribute:false}, admin:{type:Boolean}, suggestedName:{attribute:false}, locationId:{state:true}, branchId:{state:true}, buildingId:{state:true} }; declare doc: Building; declare admin: boolean; declare suggestedName: string; private declare locationId: string; private declare branchId: string; private declare buildingId: string; constructor(){super();this.admin=false;this.suggestedName="Gebäude 1";this.locationId="";this.branchId="";this.buildingId="";} protected willUpdate(_changed:PropertyValues){ const t=this.doc?.topology;if(!t)return; if(!t.locations.some((l)=>l.id===this.locationId))this.locationId=t.locations[0]?.id??""; const branches=t.branches.filter((n)=>n.location_id===this.locationId); if(!branches.some((n)=>n.id===this.branchId))this.branchId=branches[0]?.id??""; const buildings=t.buildings.filter((g)=>g.branch_id===this.branchId); if(!buildings.some((g)=>g.id===this.buildingId))this.buildingId=buildings[0]?.id??""; } private edit(change:(b:Building,t:PlanTopology)=>void){ if(!this.admin||!this.doc?.topology)return; const next=structuredClone(this.doc);change(next,next.topology!);this.publish(next); } private publish(next:Building){this.doc=next;this.dispatchEvent(new CustomEvent("hierarchy-change",{detail:{building:next},bubbles:true,composed:true}));} private open(scope:EditorScope){this.dispatchEvent(new CustomEvent("hierarchy-open",{detail:scope,bubbles:true,composed:true}));} private outsideButton(owner:OutsideOwner){ const found=!!outsideFloor(this.doc,outsideSelection(owner)); return html``; } private name(label:string,value:string,save:(name:string)=>void){ return html``; } render(){ const t=this.doc?.topology; if(!t)return html`
${hint("Vorhandene Etagen werden einem ersten Ort, einer Niederlassung und einem Gebäude zugeordnet. Geometrie, Raumverknüpfungen und Benutzerrechte bleiben erhalten.")}
`; const l=t.locations.find((l)=>l.id===this.locationId),n=t.branches.find((n)=>n.id===this.branchId),g=t.buildings.find((g)=>g.id===this.buildingId); return html`
${hint("Hier pflegst du Ort, Niederlassung und Gebäude. Die Listen wechseln nur die Auswahl. Etagen legst du anschließend oben im Editor im gewählten Gebäude an. Räume und ihre Symcon-Bereichskategorie bearbeitest du rechts auf der ausgewählten Etage.")}

Orte

${t.locations.map((s)=>html``)}
${l?html`
${this.name("Name des Ortes",l.name,(name)=>{if(this.locationId===l.id)this.edit((_,p)=>{p.locations.find((s)=>s.id===l.id)!.name=name;});})} ${this.outsideButton({kind:"location",id:l.id})}
`:nothing}

Niederlassungen

${t.branches.filter((s)=>s.location_id===this.locationId).map((s)=>html``)}
${n?html`
${this.name("Name der Niederlassung",n.name,(name)=>{if(this.branchId===n.id)this.edit((_,p)=>{p.branches.find((s)=>s.id===n.id)!.name=name;});})} ${this.outsideButton({kind:"branch",id:n.id})}
`:nothing}

Gebäude

${t.buildings.filter((s)=>s.branch_id===this.branchId).map((s)=>html``)}
${g?html`
${this.name("Name des Gebäudes",g.name,(name)=>{if(this.buildingId===g.id)this.edit((_,p)=>{p.buildings.find((s)=>s.id===g.id)!.name=name;});})} ${this.outsideButton({kind:"building",id:g.id})}
`:nothing}
`; } static styles=[tokens,css` :host{display:block;font:13px/1.4 system-ui;color:var(--nf-text)}button,input,select{box-sizing:border-box;font:inherit;color:inherit;background:var(--nf-fill,#ffffff08);border:1px solid var(--nf-line);border-radius:8px;padding:8px 10px;min-width:0}button{cursor:pointer}button:disabled,input:disabled,select:disabled{opacity:.45;cursor:default}button:focus-visible,input:focus-visible,select:focus-visible{outline:2px solid var(--nf-accent);outline-offset:2px}.columns{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:16px}.help{margin-bottom:8px}section{min-width:0;border:1px solid var(--nf-line);border-radius:12px;padding:12px}h3{font-size:14px;margin:0 0 10px}.items{display:grid;align-content:start;gap:5px;min-height:100px;max-height:240px;overflow:auto;margin-bottom:10px}.pick{text-align:left;display:flex;justify-content:space-between;gap:8px;overflow-wrap:anywhere}.pick[aria-pressed=true]{border-color:var(--nf-accent);background:color-mix(in srgb,var(--nf-accent) 15%,transparent)}small{white-space:nowrap;color:var(--nf-muted)}.details{display:grid;gap:10px;margin-top:18px;padding-top:12px;border-top:1px solid var(--nf-line)}label{display:grid;gap:5px;font-size:12px;color:var(--nf-muted)}input,select{width:100%;color:var(--nf-text)}.delete{color:var(--nf-muted)}.primary{border-color:var(--nf-accent)}.setup{display:flex;gap:10px;align-items:center}@media(max-width:720px){.columns{grid-template-columns:1fr}.items{min-height:0;max-height:140px}.details{margin-top:10px}section{padding:10px}} `]; } if(!customElements.get("nf-hierarchy-manager"))customElements.define("nf-hierarchy-manager",HierarchyManager);