97 lines
7.0 KiB
JavaScript
97 lines
7.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 uuid=()=>globalThis.crypto?.randomUUID?.()||'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,c=>{const n=Math.floor(Math.random()*16);return(c==='x'?n:(n&3)|8).toString(16);});
|
|
const mounted=new WeakSet();
|
|
const owner=params.get('window')||uuid(),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:uuid()}));}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=uuid(),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(!domReady)return;const first=!initialized;initialized=true;
|
|
for(const [id,label]of Object.entries(labels)){
|
|
const section=document.getElementById(id);if(!section||mounted.has(section))continue;
|
|
const heading=section.querySelector('.terminal-actions,.monitor-heading>div,.chat-actions,.page-heading');
|
|
if(!heading)continue;mounted.add(section);
|
|
if(view===id&&heading){
|
|
const fullscreen=document.createElement('button');fullscreen.type='button';fullscreen.className='secondary detach-button';
|
|
const update=()=>{const active=!!document.fullscreenElement;fullscreen.textContent=(active?'⊡ ':'⛶ ')+BPQI18n.t(active?'Torna alla finestra':'Schermo intero');fullscreen.setAttribute('aria-pressed',String(active));};
|
|
fullscreen.onclick=async()=>{try{if(document.fullscreenElement)await document.exitFullscreen();else await document.documentElement.requestFullscreen();}catch{if(typeof toast==='function')toast(BPQI18n.t('Schermo intero non disponibile in questo browser. Prova F11.'));}};
|
|
document.addEventListener('fullscreenchange',update);update();heading.append(fullscreen);
|
|
}
|
|
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(first&&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(()=>{setup();claim();refresh();},2000);
|
|
})();
|