(() => { const detached=document.body.classList.contains('terminal-detached'); const nav=document.createElement('button');nav.dataset.page='terminal';nav.innerHTML='▸_ Terminale';nav.onclick=()=>navigate('terminal');$('nav').append(nav); const section=document.createElement('section');section.id='terminal';section.className='page';section.hidden=true;section.innerHTML=`

Terminale

BPQ · Telnet

Fosfori
BPQ / REMOTE TERMINALOFFLINE
TELNET · BPQ LOCALE↑ ↓ cronologia · Invio per inviare
`;$('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{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{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'); })();