Upload files to "JS"

This commit is contained in:
2026-11-25 20:22:18 -06:00
parent 30c4b8aed8
commit 1677455dd2
5 changed files with 894 additions and 0 deletions
+156
View File
@@ -0,0 +1,156 @@
(() => {
'use strict';
const $=id=>document.getElementById(id), wrap=$('borgWindowWrap'), frame=$('borgChatFrame'), ai=$('balrocWindow');
const labels={room:'Room',chat:'Chat',media:'Media',ai:'AI Command',more:'More',libraries:'Libraries',movies:'Movies',shows:'TV Shows',music:'Music',iptv:'IPTV',youtube:'Tube',twitch:'Twitch',settings:'Manage Libraries'};
const libraryKind={movies:'movies',shows:'tv',music:'music',iptv:'iptv',youtube:'tube',twitch:'twitch'};
let permittedLibraries={}, currentSection='libraries', navigationBusy=false, loaded=false, navigateTrail=[], activeTab='room';
let frameReady=false;
function roomCommand(action){frame.contentWindow?.postMessage({type:'KRONIC_ROOM_UI',action},location.origin);}
function chatMode(open){try{const k=frame.contentWindow.Kronick;if(open)k?.openChatWindow?.();else k?.closeChatWindow?.();}catch(e){console.warn('Room interface unavailable',e);}}
function tab(name){
if(name==='ai' && $('balrocOpen').hidden)return;
if(name==='media' && $('mobileMediaTab').hidden)return;
activeTab=name; document.body.dataset.mobileTab=name; $('mobileTitle').textContent=labels[name];
for(const b of document.querySelectorAll('#mobileNav [data-tab]')){if(b.dataset.tab===name)b.setAttribute('aria-current','page');else b.removeAttribute('aria-current');}
wrap.hidden=name!=='media'; $('mobileMore').hidden=name!=='more'; ai.hidden=name!=='ai';
if(name==='chat')chatMode(true);else chatMode(false);
if(name!=='room')roomCommand('close-audio');
}
async function refreshLibraryPermissions(){
try{const c=await api('/api/auth/media-context');permittedLibraries=c.allowedLibraries||{};
$('mobileManage').hidden=!c.canManageMedia;$('mobileAdmin').hidden=!c.isPlatformAdmin;
}catch(e){permittedLibraries={};$('mobileManage').hidden=true;$('mobileAdmin').hidden=true;}
const any=Object.values(libraryKind).some(k=>permittedLibraries[k]===true);
$('mobileMediaTab').hidden=!any;
for(const b of document.querySelectorAll('[data-borg]'))b.hidden=b.dataset.borg==='libraries'?!any:permittedLibraries[libraryKind[b.dataset.borg]]!==true;
if(activeTab==='media'&&(!any||(libraryKind[currentSection]&&permittedLibraries[libraryKind[currentSection]]!==true)))tab('room');
}
window.addEventListener('focus',()=>void refreshLibraryPermissions());
window.addEventListener('kronic:permissions-changed',()=>void refreshLibraryPermissions());
async function showCategory(type) {
const sources = await api('/api/sources');
const matches = sources.filter(s => s.library_type === type && s.enabled !== false && s.enabled !== 0);
if (matches.length === 1) return showLibrary(matches[0].id,matches[0].name,matches[0].library_type);
// Multiple libraries: use the existing library homepage and select a category
// using its existing card click handlers, rather than inventing a separate renderer.
await showLibraries();
const content = document.getElementById('content');
const section = document.createElement('div');
section.className = 'borg-category-picks';
const heading = document.createElement('h2');
heading.textContent = type === 'movies' ? 'Movie Libraries' : 'TV Show Libraries';
section.appendChild(heading);
if (!matches.length) {
const empty = document.createElement('p');
empty.textContent = 'No enabled libraries in this category.';
section.appendChild(empty);
} else {
for (const source of matches) {
const button=document.createElement('button');
button.type='button'; button.className='borg-category-choice';
button.textContent=source.name;
button.addEventListener('click',()=>showLibrary(source.id,source.name,source.library_type));
section.appendChild(button);
}
}
content.replaceChildren(section);
}
async function navigate(kind){
if(navigationBusy)throw Error('Media navigation is busy; retry shortly');
navigationBusy=true;
try{
await refreshLibraryPermissions();
if(kind==='settings'?!canManageMedia():kind==='libraries'?$('mobileMediaTab').hidden:permittedLibraries[libraryKind[kind]]!==true)throw Error('Library access denied');
if(kind==='settings'&&$('mobileMediaTab').hidden)throw Error('No media library access');
tab('media'); $('borgWindowTitle').textContent=labels[kind]||'Media';
for(const b of document.querySelectorAll('[data-borg]'))b.classList.toggle('borg-selected',b.dataset.borg===kind);
if(kind==='libraries')await showLibraries();else if(kind==='movies'||kind==='shows')await showCategory(kind);else if(kind==='music')await showMusicLibrary();else if(kind==='iptv')await showIptvLibrary('library');else if(kind==='youtube')await showYouTubeLibrary();else if(kind==='twitch')await showTwitchLibrary();else if(kind==='settings')await showManageLibraries();else throw Error('Unknown media category');
currentSection=kind;loaded=true;wrap.scrollTop=0;
}finally{navigationBusy=false;}
}
function report(e){$('content').textContent='Could not open media: '+e.message;}
window.addEventListener('kronic:ai-media-navigate',async e=>{try{await navigate(e.detail.kind);e.detail.finish?.();}catch(err){e.detail.finish?.(err);}});
window.addEventListener('kronic:ai-open-library', async event => {
const d=event.detail||{};
try {
const sources=await api('/api/sources');
const source=sources.find(x=>Number(x.id)===Number(d.source_id) && String(x.library_type)===String(d.kind) && x.enabled!==false && x.enabled!==0);
if(!source)throw Error('Requested media library unavailable');
await refreshLibraryPermissions();
const key=d.kind==='shows'?'tv':'movies';
if(permittedLibraries?.[key]!==true)throw Error('Library access denied');
tab('media');currentSection=d.kind;loaded=true;
await showLibrary(source.id,source.name,source.library_type);
if(d.show_name){
// Only navigate into an actual series displayed by this library.
const media=await api('/api/media?source_id='+encodeURIComponent(source.id));
const names=[...new Set(media.map(i=>getShowName(i)))];
const show=names.find(x=>x.toLowerCase()===String(d.show_name).toLowerCase());
if(show){
await openShow(show);
if(d.season!==undefined && d.season!==null){
const exists=media.some(i=>getShowName(i)===show && Number(getSeasonNumber(i))===Number(d.season));
if(exists)await openSeason(show,'Season '+Number(d.season));
}
}
}
d.finish?.();
} catch(error){d.finish?.(error);}
});
for(const b of document.querySelectorAll('[data-borg]'))b.addEventListener('click',()=>navigate(b.dataset.borg).catch(report));
for(const b of document.querySelectorAll('#mobileNav [data-tab]'))b.addEventListener('click',()=>{const name=b.dataset.tab;if(name==='media'&&!loaded){navigate('libraries').catch(report);return;}tab(name);});
const back=document.createElement('button');back.type='button';back.textContent='← Back';back.id='borgWindowBack';$('borgWindowTitle').before(back);
back.addEventListener('click',async()=>{try{if(navigationBusy)return;
const bc=document.getElementById('breadcrumb')?.textContent?.trim() || '';
// Native library navigation handles nested movie/show/episode breadcrumbs.
// Every category owns its history: never fall through to the global Libraries screen.
// We use the existing library functions so filters, sorting and selection remain intact.
const crumbs=bc.split('›').map(part=>part.trim()).filter(Boolean);
if(currentSection==='movies' || currentSection==='shows' ||
(currentSection==='libraries' && typeof currentSourceId!=='undefined' && currentSourceId)) {
if(crumbs.length>=4 && typeof openShow==='function') {
await openShow(currentShowName || crumbs[2]); return;
}
if(crumbs.length>=3 && typeof showLibrary==='function') {
await showLibrary(currentSourceId,currentSourceName,currentSourceType); return;
}
// At the selected library's root, remain in its category's selection view.
if(crumbs.length>=2 && typeof showCategory==='function') {
await showCategory(currentSourceType==='shows'?'shows':'movies'); return;
}
return;
}
// Music, IPTV, Twitch and Tube are their own roots, not shortcuts to Libraries.
if(currentSection==='music'||currentSection==='iptv'||currentSection==='youtube'||currentSection==='twitch') return;
if(currentSection==='libraries' && navigateTrail.length) {
await navigate(navigateTrail.pop(),true); return;
}
if(currentSection==='libraries') return;
await navigate('libraries');
}catch(e){report(e);}});
$('balrocClose').addEventListener('click',()=>tab('room'));
$('borgRoomMute').addEventListener('click',()=>roomCommand('mute'));
$('borgRoomLeave').addEventListener('click',()=>roomCommand('leave'));
$('mobileAudio').addEventListener('click',()=>tab('more'));
for(const b of document.querySelectorAll('[data-mobile-audio]'))b.addEventListener('click',()=>{tab('room');roomCommand('open-audio-'+b.dataset.mobileAudio);});
$('mobileManage').addEventListener('click',()=>navigate('settings').catch(report));
const interfaceSelect = $('mobileInterfacePreference');
if (interfaceSelect) {
interfaceSelect.value = getInterfacePreference();
interfaceSelect.addEventListener('change', () => {
if (!setInterfacePreference(interfaceSelect.value)) interfaceSelect.value = getInterfacePreference();
});
}
$('mobileLogout').addEventListener('click',async()=>{try{await api('/api/auth/logout',{method:'POST'});location.replace('/login');}catch(e){alert(e.message);}});
window.addEventListener('message',e=>{if(e.origin===location.origin&&e.source===frame.contentWindow&&e.data?.type==='KRONIC_ROOM_UI_STATUS')$('borgRoomVoiceState').textContent=e.data.label||'Not connected';});
function styleRoom(){try{const d=frame.contentDocument;if(!d?.head||!d.getElementById('participant-container'))return;d.documentElement.classList.add('mobile-room-embedded');if(!d.getElementById('mobileRoomStyles')){const l=d.createElement('link');l.id='mobileRoomStyles';l.rel='stylesheet';l.href='/CSS/mobile-room.css?v=1';d.head.append(l);}if(activeTab==='chat')chatMode(true);}catch(e){console.warn(e);}}
frame.addEventListener('load',styleRoom);styleRoom();
// Move lazy styles behind the compatibility layer, without loading the desktop shell.
const css=document.querySelector('link[href^="/CSS/mobile.css"]');
new MutationObserver(records=>{if(records.some(r=>[...r.addedNodes].some(n=>n!==css&&n.tagName==='LINK'&&n.rel==='stylesheet')))document.head.append(css);}).observe(document.head,{childList:true});
window.addEventListener('kronic:preferred-start-view',e=>{if(e.detail?.section==='libraries'&&activeTab==='room')navigate('libraries').catch(report);});
void refreshLibraryPermissions();
})();