2 lines
5.0 KiB
JavaScript
2 lines
5.0 KiB
JavaScript
export const profileSelectionSource = " function renderProfileSelectionState(count=0){\n hasTorrentSnapshot = false;\n torrentSummary = {filters:{all:{count:0},downloading:{count:0},queued:{count:0},seeding:{count:0},paused:{count:0},checking:{count:0},error:{count:0},stopped:{count:0}}};\n torrents.clear();\n selected.clear();\n renderCounts();\n const body = $('torrentBody');\n if(body){\n body.innerHTML = `<tr><td colspan=\"${torrentColumnSpan()}\" class=\"empty\"><div class=\"empty-state\"><b>Select an rTorrent profile.</b><span>${esc(count)} profile(s) are configured for this trusted bypass session. Choose which one to open.</span><button id=\"chooseProfileBtn\" class=\"btn btn-sm btn-primary\" type=\"button\"><i class=\"fa-solid fa-server\"></i> Choose profile</button></div></td></tr>`;\n }\n const list = $('mobileList');\n if(list) list.innerHTML = `<div class=\"empty\"><div class=\"empty-state\"><b>Select an rTorrent profile.</b><span>Choose a profile to load torrents.</span></div></div>`;\n if($('detailPane')) $('detailPane').innerHTML = 'Choose an rTorrent profile to load details.';\n }\n\n function formatProfileCachedAt(value){\n if(!value) return '';\n if(typeof value==='number') return formatDateTime(value);\n const text=String(value||'').trim();\n const numeric=Number(text);\n if(Number.isFinite(numeric) && numeric>0) return formatDateTime(numeric);\n const normalized=text.includes('T')?text:text.replace(' ', 'T');\n const dt=new Date(normalized.endsWith('Z')?normalized:`${normalized}Z`);\n if(Number.isNaN(dt.getTime())) return '';\n return dt.toLocaleString();\n }\n\n function profileRuntimeStatsHtml(stats){\n if(!stats) return '';\n const parts=[];\n if(stats.torrent_count!==undefined) parts.push(`${stats.torrent_count} torrents`);\n if(stats.total_size_h) parts.push(`total ${stats.total_size_h}`);\n if(stats.seeding_count!==undefined || stats.downloading_count!==undefined) parts.push(`${stats.seeding_count||0} seeding / ${stats.downloading_count||0} downloading`);\n const cachedAt=formatProfileCachedAt(stats.updated_at);\n if(cachedAt) parts.push(`cached ${cachedAt}`);\n return parts.length?`<div class=\"profile-choice-stats\">${parts.map(x=>`<span>${esc(x)}</span>`).join('')}</div>`:'';\n }\n\n function renderProfilePickerChoices(profiles=[], active=null){\n const list=$('profileChoiceList');\n if(!list) return;\n const activeId=Number(active?.id || window.PYTORRENT?.activeProfile || activeProfileId || 0);\n list.innerHTML=(profiles||[]).map(p=>{\n const id=Number(p.id||0);\n const activeClass=id===activeId?' active':'';\n return `<button class=\"profile-choice-card${activeClass}\" type=\"button\" data-profile-id=\"${esc(id)}\"><div class=\"profile-choice-main\"><span><i class=\"fa-solid fa-server\"></i><b>${esc(p.name||('rTorrent '+id))}</b></span><small>#${esc(id)}${id===activeId?' \u00b7 active':''}</small></div>${profileRuntimeStatsHtml(p.runtime_stats)}</button>`;\n }).join('') || '<div class=\"text-muted small\">No profiles configured.</div>';\n }\n\n async function openProfilePicker(){\n try{\n const j=await (await fetch('/api/profiles',{cache:'no-store'})).json();\n renderProfilePickerChoices(j.profiles||[], j.active||null);\n }catch(e){ renderProfilePickerChoices([], null); }\n new bootstrap.Modal($('profilePickerModal')).show();\n }\n\n // Note: On trusted auth-bypass entry, existing profiles are not auto-selected; the visitor must choose the target profile.\n async function showFirstRunSetup(){\n if(hasActiveProfile || firstRunSetupShown) return;\n firstRunSetupShown = true;\n let profiles=[];\n try{\n const j=await (await fetch('/api/profiles',{cache:'no-store'})).json();\n if(j.active?.id){\n activeProfileId=j.active.id;\n hasActiveProfile=true;\n window.PYTORRENT.activeProfile=Number(j.active.id);\n return;\n }\n profiles=j.profiles||[];\n }catch(e){}\n $('connBadge').className='badge text-bg-warning';\n if(profiles.length){\n $('connBadge').textContent='select profile';\n setInitialLoader('Select rTorrent profile','Choose which configured rTorrent profile to open.');\n renderProfileSelectionState(profiles.length);\n hideInitialLoader();\n setTimeout(()=>openProfilePicker(), 120);\n return;\n }\n $('connBadge').textContent='setup required';\n setInitialLoader('Configure rTorrent','Add the first rTorrent profile to start loading torrents.');\n renderNoProfileState();\n hideInitialLoader();\n setTimeout(()=>{ activateToolTab('rtorrents'); new bootstrap.Modal($('toolsModal')).show(); }, 120);\n }\n\n $('profileChoiceList')?.addEventListener('click',async e=>{\n const btn=e.target.closest('.profile-choice-card');\n if(!btn) return;\n const id=btn.dataset.profileId;\n if(!id) return;\n await activateProfileAndRefresh(id, btn.querySelector(\"b\")?.textContent || \"rTorrent\");\n bootstrap.Modal.getInstance($('profilePickerModal'))?.hide();\n });\n";
|