/* Window ownership is UI metadata only; authentication stays in the HttpOnly cookie. */ (()=>{ const labels={terminal:'Terminale',aprs:'APRS',monitor:'Monitor',chat:'Chat'},params=new URLSearchParams(location.search); const requested=location.pathname==='/terminal-window'?'terminal':params.get('detached'); const view=Object.hasOwn(labels,requested)?requested:null; const owner=params.get('window')||crypto.randomUUID(),refs=new Map(),ready=new Set(),ttl=120000; const key=id=>'bpq-detached:'+id,commandKey='bpq-detached-command'; const target=id=>'bpq-console-'+id; const read=id=>{try{const value=JSON.parse(localStorage.getItem(key(id)));return value&&typeof value.owner==='string'&&Number.isFinite(value.at)?value:null;}catch{return null;}}; const write=(id,value)=>{try{value?localStorage.setItem(key(id),JSON.stringify(value)):localStorage.removeItem(key(id));}catch{}}; let initialized=false,authenticated=false,released=false,domReady=document.readyState==='complete'; if(view){document.documentElement.dataset.detachedView=view;window.name=target(view);} function isAway(id){ if(view===id)return false; const ref=refs.get(id); if(ref){if(ref.closed){refs.delete(id);ready.delete(id);write(id,null);return false;}if(ready.has(id))return true;} const record=read(id);return !!record&&Date.now()-record.atdiv,.chat-actions,.page-heading'); if(id!=='terminal'&&heading){ const button=document.createElement('button');button.type='button';button.className='secondary detach-button';button.dataset.detach=id; button.textContent=view===id?'↙ Torna al pannello':'↗ Stacca';button.onclick=()=>view===id?dock(id):open(id);heading.append(button); } if(!view){ const placeholder=document.createElement('div');placeholder.className='detached-placeholder';placeholder.hidden=true; placeholder.innerHTML='

Questa sezione è aperta in una finestra separata.

'; placeholder.querySelector('h2').textContent=label;const buttons=placeholder.querySelectorAll('button');buttons[0].onclick=()=>focus(id);buttons[1].onclick=()=>dock(id);section.append(placeholder); } } if(view&&location.pathname!=='/terminal-window'){ const previous=navigate;navigate=function(){previous(view);};navigate(view); document.title='BPQ Console · '+labels[view]; } refresh(); } window.BPQDetached={view,open,dock,isAway,shouldPoll}; document.addEventListener('DOMContentLoaded',()=>{domReady=true;setup();}); document.addEventListener('station-ready',()=>{authenticated=true;released=false;setup();claim();refresh();if(view){document.title='BPQ Console · '+labels[view];if(location.pathname!=='/terminal-window')navigate(view);}}); document.addEventListener('bpq-detach-auth-lost',()=>{authenticated=false;release();}); window.addEventListener('storage',event=>{ if(event.key===commandKey&&view){try{const cmd=JSON.parse(event.newValue);if(cmd.id===view&&Date.now()-cmd.at<5000){if(cmd.action==='focus')window.focus();else if(cmd.action==='dock')closeView();}}catch{}} if(event.key?.startsWith('bpq-detached:'))refresh(); }); window.addEventListener('pagehide',release);window.addEventListener('beforeunload',release); window.addEventListener('pageshow',()=>{if(view&&authenticated){released=false;claim();}}); setInterval(()=>{claim();refresh();},2000); })();