Fix detached controls on LAN HTTP and late-loaded views
This commit is contained in:
1 parent
3643638090
commit
bd8e521711
3 files changed
+74
-59
No files matched your search
+10
-7
@@ -3,7 +3,9 @@
|
||||
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 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;}};
|
||||
@@ -19,7 +21,7 @@
|
||||
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 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;
|
||||
@@ -38,7 +40,7 @@
|
||||
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 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();
|
||||
@@ -53,10 +55,11 @@
|
||||
}
|
||||
}
|
||||
function setup(){
|
||||
if(initialized||!domReady)return;initialized=true;
|
||||
if(!domReady)return;const first=!initialized;initialized=true;
|
||||
for(const [id,label]of Object.entries(labels)){
|
||||
const section=document.getElementById(id);if(!section)continue;
|
||||
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));};
|
||||
@@ -73,7 +76,7 @@
|
||||
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'){
|
||||
if(first&&view&&location.pathname!=='/terminal-window'){
|
||||
const previous=navigate;navigate=function(){previous(view);};navigate(view);
|
||||
document.title='BPQ Console · '+labels[view];
|
||||
}
|
||||
@@ -89,5 +92,5 @@
|
||||
});
|
||||
window.addEventListener('pagehide',release);window.addEventListener('beforeunload',release);
|
||||
window.addEventListener('pageshow',()=>{if(view&&authenticated){released=false;claim();}});
|
||||
setInterval(()=>{claim();refresh();},2000);
|
||||
setInterval(()=>{setup();claim();refresh();},2000);
|
||||
})();
|
||||
Reference in new issue
Block a user