Files
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

33 lines
8.1 KiB
JavaScript

(() => {
const detached=document.body.classList.contains('terminal-detached');
const nav=document.createElement('button');nav.dataset.page='terminal';nav.innerHTML='<span>▸_</span> Terminale';nav.onclick=()=>navigate('terminal');$('nav').append(nav);
const section=document.createElement('section');section.id='terminal';section.className='page';section.hidden=true;section.innerHTML=`<div class="terminal-heading"><div><h1>Terminale</h1><p>BPQ · Telnet</p></div><div class="terminal-actions"><button id="terminal-detach" class="secondary" title="Apri il terminale in una finestra separata">↗ Stacca</button><button id="terminal-dock" class="secondary" hidden>↙ Torna al pannello</button><button id="terminal-clear" class="secondary">Pulisci</button><button id="terminal-export" class="secondary">Salva testo</button><button id="terminal-connect" class="primary">Collega</button><button id="terminal-stop" class="secondary" hidden>Disconnetti</button></div></div><div class="terminal-theme-bar"><span>Fosfori</span><div role="group" aria-label="Stile del terminale"><button type="button" data-terminal-theme="amber" aria-pressed="true">Amber</button><button type="button" data-terminal-theme="green" aria-pressed="false">Green</button><button type="button" data-terminal-theme="ice" aria-pressed="false">Ice</button><button type="button" data-terminal-theme="violet" aria-pressed="false">Violet</button><button type="button" data-terminal-theme="white" aria-pressed="false">White</button></div></div><div class="phosphor-case"><div class="phosphor-top"><span><i id="terminal-led"></i> BPQ / REMOTE TERMINAL</span><span id="terminal-state" role="status">OFFLINE</span></div><div class="phosphor-screen" id="terminal-screen" tabindex="0" aria-label="Terminale Telnet BPQ"><pre id="terminal-output" aria-live="off"></pre><span class="phosphor-cursor" aria-hidden="true">▌</span></div><form id="terminal-form" autocomplete="off"><label for="terminal-input">&gt;</label><input id="terminal-input" aria-label="Comando Telnet" placeholder="Collega il terminale per iniziare" spellcheck="false" autocapitalize="off" maxlength="1000" disabled><button type="submit" id="terminal-send" disabled aria-label="Invia comando">INVIO ↵</button></form><div class="phosphor-bottom"><span id="terminal-endpoint">TELNET · BPQ LOCALE</span><span>↑ ↓ cronologia · Invio per inviare</span><button type="button" id="terminal-follow" hidden>↓ Torna al fondo</button></div></div>`;$('main').append(section);if(detached)section.hidden=false;
const themes=['amber','green','ice','violet','white'];
function applyTheme(value){const theme=themes.includes(value)?value:'amber';section.dataset.theme=theme;section.querySelectorAll('[data-terminal-theme]').forEach(button=>button.setAttribute('aria-pressed',String(button.dataset.terminalTheme===theme)));}
function savedTheme(){try{return localStorage.getItem('bpq-terminal-theme');}catch{return 'amber';}}
applyTheme(savedTheme());
section.querySelector('.terminal-theme-bar').onclick=event=>{const theme=event.target.closest('[data-terminal-theme]')?.dataset.terminalTheme;if(!themes.includes(theme))return;applyTheme(theme);try{localStorage.setItem('bpq-terminal-theme',theme);}catch{}};
window.addEventListener('storage',event=>{if(event.key==='bpq-terminal-theme')applyTheme(event.newValue);});
$('#terminal-detach').hidden=detached;$('#terminal-dock').hidden=!detached;
$('#terminal-detach').onclick=()=>{if(window.BPQDetached){window.BPQDetached.open('terminal');return;}const popup=window.open('/terminal-window','bpq-console-terminal','popup=yes,width=1120,height=780,resizable=yes,scrollbars=yes');if(popup)popup.focus();else toast(BPQI18n.t('Consenti le finestre popup per aprire il terminale separato.'));};
$('#terminal-dock').onclick=()=>{if(window.BPQDetached){window.BPQDetached.dock('terminal');return;}try{if(window.opener&&!window.opener.closed){window.opener.location.hash='#terminal';window.opener.focus();window.close();return;}}catch{}window.location.assign('/#terminal');};
let cursor=0,busy=false,state='offline',buffer='',following=true,history=[],historyIndex=0,draft='',sending=false,generation=0;
const area=$('#terminal-screen'),output=$('#terminal-output'),input=$('#terminal-input');
const previousNavigate=navigate;navigate=function(next){previousNavigate(next);if(next==='terminal'){$('#breadcrumb').textContent='Stazione / Terminale';poll();}};
function bottom(){area.scrollTop=area.scrollHeight;following=true;$('#terminal-follow').hidden=true;}
function append(text){buffer=(buffer+text).slice(-200000);output.textContent=buffer;if(following)bottom();}
function status(data){const wasOnline=state==='online';state=data.state;const online=state==='online',connecting=state==='connecting';$('#terminal-state').textContent=data.error||({online:'ONLINE',connecting:'COLLEGAMENTO…',offline:'OFFLINE',error:'ERRORE'}[state]||state);$('#terminal-led').classList.toggle('online',online);$('#terminal-connect').hidden=online||connecting;$('#terminal-stop').hidden=!online&&!connecting;input.disabled=!online;$('#terminal-send').disabled=!online||sending;input.placeholder=online?'Scrivi un comando…':'Collega il terminale per iniziare';$('#terminal-endpoint').textContent='TELNET · 127.0.0.1:'+data.port;if(online&&!wasOnline&&page==='terminal')input.focus();}
async function poll(){if(!csrf||busy||!station.features.terminal||window.BPQDetached&&!window.BPQDetached.shouldPoll('terminal'))return;busy=true;const current=generation;try{const d=await api('/terminal?after='+cursor,{busy:false});if(current!==generation)return;if(d.latest<cursor){cursor=0;buffer='';}status(d);for(const e of d.events)append(e.text);cursor=d.latest;}catch(e){if(page==='terminal')$('#terminal-state').textContent=e.message;}finally{busy=false;}}
$('#terminal-connect').onclick=async()=>{const button=$('#terminal-connect');button.disabled=true;try{const d=await api('/terminal/connect',{method:'POST',body:'{}'});buffer='';output.textContent='';following=true;cursor=0;status(d);await poll();}catch(e){toast(e.message);}finally{button.disabled=false;}};
$('#terminal-stop').onclick=async()=>{try{status(await api('/terminal/disconnect',{method:'POST',body:'{}'}));await poll();}catch(e){toast(e.message);}};
$('#terminal-form').onsubmit=async e=>{e.preventDefault();if(sending||state!=='online')return;const text=input.value;sending=true;$('#terminal-send').disabled=true;try{await api('/terminal/send',{method:'POST',body:JSON.stringify({text})});if(text&&history.at(-1)!==text)history.push(text);history=history.slice(-100);historyIndex=history.length;draft='';input.value='';await poll();}catch(e){toast(e.message);}finally{sending=false;$('#terminal-send').disabled=state!=='online';if(state==='online')input.focus();}};
input.addEventListener('keydown',e=>{if(e.key==='ArrowUp'){e.preventDefault();if(historyIndex===history.length)draft=input.value;if(historyIndex>0)input.value=history[--historyIndex];}else if(e.key==='ArrowDown'){e.preventDefault();if(historyIndex<history.length)historyIndex++;input.value=historyIndex===history.length?draft:history[historyIndex];}});
area.addEventListener('scroll',()=>{following=area.scrollHeight-area.scrollTop-area.clientHeight<40;$('#terminal-follow').hidden=following;});$('#terminal-follow').onclick=bottom;
$('#terminal-clear').onclick=()=>{buffer='';output.textContent='';bottom();input.focus();};
$('#terminal-export').onclick=()=>{if(!buffer){toast('Il terminale è vuoto');return;}const url=URL.createObjectURL(new Blob([buffer],{type:'text/plain;charset=utf-8'})),a=document.createElement('a');a.href=url;a.download='BPQ-terminal-'+new Date().toISOString().replace(/[:.]/g,'-')+'.txt';a.click();setTimeout(()=>URL.revokeObjectURL(url),1000);};
document.addEventListener('station-ready',()=>{generation++;cursor=0;buffer='';output.textContent='';history=[];historyIndex=0;nav.hidden=!station.features.terminal;if(page==='terminal')poll();});
const logout=$('#logout').onclick;$('#logout').onclick=async()=>{generation++;buffer='';output.textContent='';history=[];input.value='';await logout();};
setInterval(()=>{if(csrf&&page==='terminal')poll();},700);
window.addEventListener('hashchange',()=>{if(location.hash==='#terminal'&&page!=='terminal')navigate('terminal');});if(location.hash==='#terminal')navigate('terminal');
})();