/* ---------------- seasons (#482) ---------------- */ /// A podcast's seasons, by feed id, as its page last fetched them, so going back to it draws its /// picker at once while the counts are asked for again. const seasonsOf=new Map(); /// Whether a podcast is told in seasons: two or more of them, holding most of its episodes. A few /// numbered among hundreds (The Vintage RPG Podcast's 2 of 366), or years given as seasons on a /// quarter of them (Ken and Robin's), are no seasons worth a picker. function seasonsShown(list: SeasonCount[]|undefined): boolean{ if(!list) return false; const numbered=list.filter(s=>s.season!=null); const inOne=numbered.reduce((n,s)=>n+s.episodes,0), all=list.reduce((n,s)=>n+s.episodes,0); return numbered.length>=2 && inOne*2>=all; } /// What the picker sends for a season: its number, or `none` for the episodes that name none. const seasonKey=(s: SeasonCount)=>s.season==null?'none':String(s.season); const seasonName=(s: number|null|undefined)=>s==null?'Other episodes':`Season ${s}`; /// The picker, under the tabs: every season, one of them, or the episodes naming none. function seasonsHTML(list: SeasonCount[]): string{ const chip=(key: string,label: string,title: string)=> ``; return `
${chip('','All seasons','Every season')}${ list.map(s=>chip(seasonKey(s),s.season==null?'Other':`Season ${s.season}`, `${plural(s.episodes,'episode')}, ${s.unread} unread`)).join('')}
`; } function drawSeasons(f: Feed, list: SeasonCount[]|undefined){ const bar=$('#content .toolbar'); if(!bar||S.feed!==f.id) return; bar.querySelector('.seasonrow')?.remove(); if(!list||!seasonsShown(list)) return; // The download beside the picker, on its row: on a phone, on a row of its own, it cost a line. const picked=list.find(s=>seasonKey(s)===S.season); bar.insertAdjacentHTML('beforeend',`
${seasonsHTML(list)}${picked?``:''}
`); bar.querySelector('.seasondl')?.addEventListener('click',()=>downloadSeason(f)); // The one picked in view: on a phone Season 3 of six is past the edge, and the picker looked as // if every season were still showing. const track=bar.querySelector('.seasons'), on=track?.querySelector('.on'); if(track&&on) track.scrollLeft+=on.getBoundingClientRect().left-track.getBoundingClientRect().left-(track.clientWidth-on.offsetWidth)/2; $$('.seasons button',bar).forEach(b=>b.onclick=()=>{ S.season=b.dataset.season!; S.offset=0; renderFeed(); loadEntries(); }); // The dividers in the list wait on knowing the podcast is told in seasons. if(S.entries.length) renderEntries(); } /// A podcast's seasons onto its page: kept ones at once, then the server's. Told in seasons only /// once the answer came, its list is asked for again in its own order. async function loadSeasons(f: Feed){ const had=seasonsShown(seasonsOf.get(f.id)); drawSeasons(f,seasonsOf.get(f.id)); try{ const got: SeasonCount[]=await api(`/api/feeds/${encodeURIComponent(f.id)}/seasons`); seasonsOf.set(f.id,got); drawSeasons(f,got); // Drawn again, so the headings say the order too. if(!had&&seasonsShown(got)&&!sortPicked&&S.feed===f.id){ S.offset=0; renderFeed(); loadEntries(); } }catch{} } /// Whether a heading was clicked to sort the podcast open: until one is, a podcast told in /// seasons runs in its own order, season 1 episode 1 first, not by date (#482). let sortPicked=false; /// The order a list is asked for in: the page's, or a seasoned podcast's own. function sortFor(feed: string): {col: string, dir: 'asc'|'desc'}{ return feed===S.feed&&!sortPicked&&seasonsShown(seasonsOf.get(feed)) ? {col:'episode',dir:'asc'} : S.sort; } /// The unread of the season picked, for the count under the list; null for every season. function seasonUnread(feed: string): number|null{ if(!S.season) return null; return seasonsOf.get(feed)?.find(s=>seasonKey(s)===S.season)?.unread ?? null; } /// Where a season starts in the list, said above its first episode: only for every season at /// once, newest or oldest first, where the episodes run season by season. function seasonBreaks(): boolean{ const feed=S.feed ?? '', col=sortFor(feed).col; return !S.season && (col==='episode'||col==='published') && seasonsShown(seasonsOf.get(feed)); } /// Every episode of the season picked onto iPX, after asking: a season can be a few gigabytes. function downloadSeason(f: Feed){ const s=seasonsOf.get(f.id)?.find(x=>seasonKey(x)===S.season); if(!s) return; const name=seasonName(s.season); openModal(`

Download ${esc(name)}?

Every episode of ${esc(name)} of ${esc(f.title||f.id)} not on iPX yet, ${ plural(s.episodes,'episode')} in all, is downloaded in order, the first episode first.

`); $('#sgo').onclick=async()=>{ try{ const r=await api(`/api/feeds/${encodeURIComponent(f.id)}/download-latest`,{method:'POST',body:JSON.stringify({season:S.season})}); closeModal(); toast(r.queued?`Downloading ${plural(r.queued,'episode')} of ${name}`:`All of ${name} is on iPX already`); }catch(e){ toast(why(e),true); } }; }