Fix detached controls on LAN HTTP and late-loaded views
This commit is contained in:
1 parent
3643638090
commit
bd8e521711
3 files changed
+26
-11
No files matched your search
+15
-3
@@ -3,18 +3,18 @@ import assert from 'node:assert/strict';
|
||||
import vm from 'node:vm';
|
||||
import {readFileSync} from 'node:fs';
|
||||
const source=readFileSync(new URL('./public/detach.js',import.meta.url),'utf8');
|
||||
function harness(search='',shared=new Map()){
|
||||
function harness(search='',shared=new Map(),options={}){
|
||||
const docEvents={},events={},sections={},calls=[];let serial=0,tick,popup;
|
||||
function element(){const children=new Map();return {hidden:false,dataset:{},textContent:'',className:'',innerHTML:'',classes:new Set(),
|
||||
setAttribute(name,value){this[name]=value;},classList:{toggle(name,on){this.owner.classes[on?'add':'delete'](name);}},append(child){this.child=child;},
|
||||
querySelector(selector){if(selector==='.detached-placeholder')return this.child||null;if(!children.has(selector))children.set(selector,element());return children.get(selector);},
|
||||
querySelectorAll(){return [this.querySelector('first'),this.querySelector('second')];}};}
|
||||
for(const id of ['terminal','aprs','monitor','chat']){sections[id]=element();sections[id].classList.owner=sections[id];}
|
||||
const main={hidden:false};const document={body:{append(node){calls.push(['notice',node.className]);}},querySelector:()=>main,readyState:'complete',documentElement:{dataset:{}},getElementById:id=>sections[id],createElement:element,addEventListener:(name,fn)=>docEvents[name]=fn};
|
||||
const main={hidden:false};const document={body:{append(node){calls.push(['notice',node.className]);}},querySelector:()=>main,readyState:'complete',documentElement:{dataset:{}},getElementById:id=>options.late?.includes(id)?null:sections[id],createElement:element,addEventListener:(name,fn)=>docEvents[name]=fn};
|
||||
const window={name:'',addEventListener:(name,fn)=>events[name]=fn,focus:()=>calls.push('focus'),close:()=>calls.push('close'),
|
||||
open:(...args)=>{calls.push(args);popup={closed:false,focus(){},close(){this.closed=true;}};return popup;}};
|
||||
const location={pathname:'/',search,hash:'',assign:url=>calls.push(['assign',url])};
|
||||
const context=vm.createContext({window,document,location,URLSearchParams,crypto:{randomUUID:()=>String(++serial)},
|
||||
const context=vm.createContext({window,document,location,URLSearchParams,crypto:options.insecure?{}:{randomUUID:()=>String(++serial)},
|
||||
localStorage:{getItem:key=>shared.get(key)||null,setItem:(key,value)=>shared.set(key,value),removeItem:key=>shared.delete(key)},
|
||||
Date,Set,Map,BPQI18n:{t:s=>s},toast:text=>calls.push(['toast',text]),navigate:id=>calls.push(['navigate',id]),setInterval:fn=>{tick=fn;}});
|
||||
vm.runInContext(source,context);docEvents.DOMContentLoaded();
|
||||
@@ -50,3 +50,15 @@ test('a dock request stops the detached view even when its browser refuses scrip
|
||||
h.events.storage({key:'bpq-detached-command',newValue:JSON.stringify({id:'monitor',action:'dock',at:Date.now()})});
|
||||
assert.equal(h.shared.has('bpq-detached:monitor'),false);assert.equal(h.api.shouldPoll('monitor'),false);assert.equal(h.main.hidden,true);assert.ok(h.calls.includes('close'));
|
||||
});
|
||||
|
||||
test('LAN HTTP without randomUUID supports all detached services',()=>{
|
||||
const h=harness('',new Map(),{insecure:true});
|
||||
for(const id of ['terminal','aprs','monitor','chat'])h.api.open(id);
|
||||
assert.equal(h.calls.filter(c=>Array.isArray(c)&&typeof c[0]==='string'&&c[0].includes('window=')).length,4);
|
||||
});
|
||||
test('late sections receive controls and courtesy placeholders on the next setup pass',()=>{
|
||||
const options={late:['chat','monitor','aprs']},h=harness('',new Map(),options);
|
||||
assert.equal(h.sections.chat.child,undefined);options.late=[];h.tick();
|
||||
for(const id of ['chat','monitor','aprs'])assert.ok(h.sections[id].child);
|
||||
const placeholder=h.sections.chat.child;h.tick();assert.equal(h.sections.chat.child,placeholder);
|
||||
});
|
||||
+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);
|
||||
})();
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
<!doctype html>
|
||||
<html lang="it"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="theme-color" content="#101916"><title>BPQ Console</title><link rel="stylesheet" href="/i18n.css"><script src="/ui-config.js"></script><script defer src="/detach.js?v=20261019-multi"></script><link rel="stylesheet" href="/detach.css?v=20261019-multi"><script src="/i18n.js?v=20261019-label-audit"></script><link rel="stylesheet" href="/style.css"><link rel="stylesheet" href="/dark.css"><link rel="stylesheet" href="/mission.css"><link rel="stylesheet" href="/busy.css"><script defer src="/busy.js"></script><script defer src="/app.js"></script><script type="module" src="/enhancements.js"></script><script defer src="/winlink.js"></script><script defer src="/mission.js"></script><script defer src="/attachments.js"></script><link rel="stylesheet" href="/compose-attachments.css"><script defer src="/compose-attachments.js"></script><link rel="stylesheet" href="/monitor.css"><script defer src="/monitor.js"></script><link rel="stylesheet" href="/journal.css"><script defer src="/journal.js"></script><link rel="stylesheet" href="/forwarding.css"><script defer src="/forwarding.js?v=20261017-hourly-2"></script><link rel="stylesheet" href="/winlink-radio.css"><script defer src="/winlink-radio.js"></script><link rel="stylesheet" href="/terminal.css?v=20261019-themes"><script defer src="/terminal.js?v=20261019-themes"></script><link rel="stylesheet" href="/radio-control.css"><link rel="stylesheet" href="/radio-ports.css"><script defer src="/radio-ports.js"></script><link rel="stylesheet" href="/aprs.css"><script defer src="/aprs.js"></script><link rel="stylesheet" href="/vendor/leaflet/leaflet.css"><script defer src="/vendor/leaflet/leaflet.js"></script><link rel="stylesheet" href="/aprs-activity.css?v=20261019-packet-text"><script defer src="/aprs-symbols.js"></script><script defer src="/aprs-activity.js?v=20261019-packet-text"></script><link rel="stylesheet" href="/sync-progress.css"><script type="module" src="/sync-progress.js"></script></head>
|
||||
<html lang="it"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="theme-color" content="#101916"><title>BPQ Console</title><link rel="stylesheet" href="/i18n.css"><script src="/ui-config.js"></script><script defer src="/detach.js?v=20261029-lan"></script><link rel="stylesheet" href="/detach.css?v=20261019-multi"><script src="/i18n.js?v=20261019-label-audit"></script><link rel="stylesheet" href="/style.css"><link rel="stylesheet" href="/dark.css"><link rel="stylesheet" href="/mission.css"><link rel="stylesheet" href="/busy.css"><script defer src="/busy.js"></script><script defer src="/app.js"></script><script type="module" src="/enhancements.js"></script><script defer src="/winlink.js"></script><script defer src="/mission.js"></script><script defer src="/attachments.js"></script><link rel="stylesheet" href="/compose-attachments.css"><script defer src="/compose-attachments.js"></script><link rel="stylesheet" href="/monitor.css"><script defer src="/monitor.js"></script><link rel="stylesheet" href="/journal.css"><script defer src="/journal.js"></script><link rel="stylesheet" href="/forwarding.css"><script defer src="/forwarding.js?v=20261017-hourly-2"></script><link rel="stylesheet" href="/winlink-radio.css"><script defer src="/winlink-radio.js"></script><link rel="stylesheet" href="/terminal.css?v=20261019-themes"><script defer src="/terminal.js?v=20261019-themes"></script><link rel="stylesheet" href="/radio-control.css"><link rel="stylesheet" href="/radio-ports.css"><script defer src="/radio-ports.js"></script><link rel="stylesheet" href="/aprs.css"><script defer src="/aprs.js"></script><link rel="stylesheet" href="/vendor/leaflet/leaflet.css"><script defer src="/vendor/leaflet/leaflet.js"></script><link rel="stylesheet" href="/aprs-activity.css?v=20261019-packet-text"><script defer src="/aprs-symbols.js"></script><script defer src="/aprs-activity.js?v=20261019-packet-text"></script><link rel="stylesheet" href="/sync-progress.css"><script type="module" src="/sync-progress.js"></script></head>
|
||||
<body>
|
||||
<section id="login" class="login-screen"><div class="login-art"><h1>BPQ Console</h1><div class="login-byline" data-no-i18n>by IU2EUG</div><p>La tua stazione, in un unico pannello.</p></div><form id="login-form" class="login-card"><div class="login-brand"><span class="brand">BPQ Console</span><div class="login-byline" data-no-i18n>by IU2EUG</div></div><h2>Benvenuto in stazione.</h2><p>Accedi con l’utente e la password Telnet del tuo BPQ.</p><label>Utente<input name="user" autocomplete="username" required placeholder="Il tuo utente BPQ"></label><label>Password<input name="password" type="password" autocomplete="current-password" required></label><button class="primary" type="submit">Entra nel pannello <span>→</span></button><div id="login-error" class="error" role="alert"></div></form></section>
|
||||
<div id="shell" hidden><aside class="sidebar"><a class="brand" href="#home">BPQ Console</a><div class="side-label">LA TUA STAZIONE</div><nav><button data-page="home" class="active"><span>▦</span> Panoramica</button><button data-page="bbs"><span>▤</span> BBS <b id="nav-count">—</b></button><button data-page="network"><span>⌘</span> Rete & nodi</button></nav><div class="side-station">Stazione BPQ</div><div class="profile"><div class="avatar">BP</div><div>Sysop<small>BPQ</small></div><button id="logout" title="Esci" aria-label="Esci dal pannello">↗</button></div></aside>
|
||||
|
||||
Reference in new issue
Block a user