Files
bpq_gui/public/detach.js
T
alberto c98c67e18b Add detached service windows and selectable terminal themes
Support standalone APRS, Monitor, Chat and Terminal views with courtesy screens, window focus and return controls, shared sessions and suspended embedded polling.

Add five persistent phosphor palettes, remove Console terminal idle disconnections, and display decoded Mic-E movement and altitude with expandable original packets.

Document the new behavior and add regression coverage. Validation: 182 tests passed; the known hanging PowerShell startup readiness test was excluded.
2026-10-19 22:15:15 +02:00

88 lines
6.0 KiB
JavaScript

/* 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.at<ttl;
}
function shouldPoll(id){return (!view||!released&&view===id)&&!isAway(id);}
function release(){if(!view)return;released=true;const record=read(view);if(record?.owner===owner)write(view,null);}
function claim(){if(!view||!authenticated||released)return;write(view,{owner,at:Date.now()});}
function tell(id,action){try{localStorage.setItem(commandKey,JSON.stringify({id,action,at:Date.now(),nonce:crypto.randomUUID()}));}catch{}}
function focus(id){const ref=refs.get(id);if(ref&&!ref.closed){ref.focus();return;}tell(id,'focus');const popup=window.open('',target(id),'popup=yes,width=1180,height=820,resizable=yes,scrollbars=yes');if(popup)popup.focus();}
function closeView(){
release();const main=document.querySelector('main');if(main)main.hidden=true;
const note=document.createElement('section');note.className='detached-returned-note';
note.innerHTML='<span class="detached-placeholder-icon" aria-hidden="true">↙</span><h2>Sezione riportata nel pannello</h2><p>Puoi chiudere questa finestra.</p>';
document.body.append(note);window.close();
}
function dock(id){
if(view===id){
try{if(window.opener&&!window.opener.closed){window.opener.location.hash='#'+id;window.opener.focus();}}catch{}
closeView();return;
}
tell(id,'dock');const ref=refs.get(id);if(ref&&!ref.closed)ref.close();refs.delete(id);ready.delete(id);write(id,null);refresh();
if(typeof navigate==='function')navigate(id);
}
function open(id){
if(!Object.hasOwn(labels,id))return;
if(isAway(id)){focus(id);return;}
const nonce=crypto.randomUUID(),url=id==='terminal'?'/terminal-window?window='+nonce:'/?detached='+id+'&window='+nonce+'#'+id;
const popup=window.open(url,target(id),'popup=yes,width=1180,height=820,resizable=yes,scrollbars=yes');
if(!popup){if(typeof toast==='function')toast(BPQI18n.t('Consenti le finestre popup per aprire la sezione separata.'));return;}
refs.set(id,popup);popup.focus();
}
function refresh(){
if(!initialized)return;
for(const id of Object.keys(labels)){
const section=document.getElementById(id);if(!section)continue;
const away=isAway(id);section.classList.toggle('section-detached-away',away);
const placeholder=section.querySelector('.detached-placeholder');if(placeholder)placeholder.hidden=!away;
if(read(id)&&refs.has(id))ready.add(id);
}
}
function setup(){
if(initialized||!domReady)return;initialized=true;
for(const [id,label]of Object.entries(labels)){
const section=document.getElementById(id);if(!section)continue;
const heading=section.querySelector('.terminal-actions,.monitor-heading>div,.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='<span class="detached-placeholder-icon" aria-hidden="true">↗</span><h2></h2><p>Questa sezione è aperta in una finestra separata.</p><div><button type="button" class="primary">Mostra finestra</button><button type="button" class="secondary">Riporta qui</button></div>';
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);
})();