/* ---------------- Up Next ---------------- */ /// Up Next (#473): the episodes lined up to play, kept on the server so the phone and the browser /// share one. What is playing is not in it: an episode leaves Up Next as it starts, as Overcast's /// does, so the first one in it is always the one that follows. let upNext: Entry[]=[]; /// The next one in the list that can play now, the one playing aside. One not downloaded yet is /// passed over, to wait in the list until it is. const nextUp=(list: Entry[], playing: string|null)=>list.find(e=>entryKey(e)!==playing&&e.enclosures.some(isPlayable)); const queued=(e: Entry)=>upNext.some(x=>entryKey(x)===entryKey(e)); function loadUpNext(){ api('/api/queue').then((r: ListPage)=>{ upNext=r.entries; upNextChanged(); }).catch(()=>{}); } /// Saves Up Next in this order and takes the server's answer, which leaves out what was not /// yours to queue. Drawn at once from what was asked, so a drag lands where it was dropped. async function saveUpNext(list: Entry[]){ upNext=list; upNextChanged(); try{ const r: ListPage=await api('/api/queue',{method:'PUT',body:JSON.stringify(list.map(e=>({feed_id:e.feed_id,guid:e.guid})))}); upNext=r.entries; }catch(err){ toast(why(err),true); loadUpNext(); return; } upNextChanged(); } function upNextChanged(){ renderFeeds(); if(S.feed===':upnext') drawUpNext(); drawNow(); // The rows' and the reader's buttons say whether each is in it. for(const b of $$('[data-a=queue]')) paintQueueButton(b); hostNext(); } /// Adds an episode at the end, or first with `next`, or takes it out if it is in already and /// neither was asked. A file still on the feed's server is fetched now, so it is here by its turn. async function toggleQueued(e: Entry, next=false){ const rest=upNext.filter(x=>entryKey(x)!==entryKey(e)); if(queued(e)&&!next){ await saveUpNext(rest); toast('Removed from Up Next'); return; } const enc=e.enclosures.find(isPlayable)||e.enclosures.find(playsKind); if(!enc){ toast('There is nothing to play in this item.',true); return; } await saveUpNext(next?[e,...rest]:[...rest,e]); if(!enc.path){ await api(`/api/enclosures/${enc.id}/download`,{method:'POST'}).catch(()=>{}); enc.state='pending'; } toast((next?'Plays next':'Added to Up Next')+(enc.path?'':'. Downloading it first.')); } function paintQueueButton(b: HTMLButtonElement){ const key=b.dataset.key, on=!!key&&upNext.some(x=>entryKey(x)===key); const label=on?'Remove from Up Next':'Add to Up Next'; b.setAttribute('aria-pressed',String(on)); b.title=label; b.setAttribute('aria-label',label); b.innerHTML=on?ICON.queued:ICON.queue; } /// The toggle on a row or in the reader, drawn with its state. const queueButton=(e: Entry,cls='iconbtn')=>``; /// What plays after this one ends, or the next button is pressed. /// Up Next first; with nothing in it, the next in the playlist the one playing came from. function playNext(){ const e=nextUp(upNext,player.guid?player.feed+'\n'+player.guid:null); if(e){ play(e); return true; } const n=nextInPlaylist(); if(n&&playingFrom){ playFrom(playingFrom,n); return true; } return false; } /// Playing takes an episode out of Up Next: it is under way, and Currently Listening has it now. function startedPlaying(e: Entry){ if(queued(e)) saveUpNext(upNext.filter(x=>entryKey(x)!==entryKey(e))); } /// The phone's player is told what follows (#473), so with the screen locked, when the page is /// frozen and cannot answer the end of an episode, it goes on to the next by itself. function hostNext(){ const next=nextUp(upNext,player.guid?player.feed+'\n'+player.guid:null)??nextInPlaylist(); $('#pnext').hidden=!next; try{ if(!ipxHost||!hostCan.has('next')) return; const e=next, enc=e?.enclosures.find(isPlayable); ipxHost(e&&enc?{t:'next',...hostItem(e,enc,mediaSrc(enc))}:{t:'next'}); }catch{} } /// Up Next's place: the list, a handle on each row to drag it up or down (or its arrow keys), /// and a button to take it out. function drawUpNext(){ const box=$('#listening'); if(!box) return; $('#count').textContent=`Up Next: ${plural(upNext.length,'episode')}`; box.innerHTML=upNext.length?'':'

Nothing lined up. Add an episode with its Up Next button, and it plays when the one before it ends.

'; for(const e of upNext){ const el=listenRow(e,'Remove from Up Next',()=>saveUpNext(upNext.filter(x=>entryKey(x)!==entryKey(e)))); el.insertAdjacentHTML('afterbegin',``); box.appendChild(el); } for(const g of $$('.grip',box)) grip(g,box); } /// One row's handle. A drag moves the row among the others as the pointer goes, and saves where /// it is let go; Up and Down move it one place, for a keyboard. function grip(g: HTMLButtonElement,box: HTMLElement){ const row=g.closest('.childrow')!; const save=()=>{ const list=$$('.childrow',box).map(r=>r.entry!); if(list.some((e,i)=>e!==upNext[i])) saveUpNext(list); }; g.onclick=ev=>ev.stopPropagation(); g.onkeydown=ev=>{ const [up,down]=[row.previousElementSibling,row.nextElementSibling]; if(ev.key==='ArrowUp'&&up) box.insertBefore(row,up); else if(ev.key==='ArrowDown'&&down) box.insertBefore(down,row); else return; ev.preventDefault(); g.focus(); save(); }; g.onpointerdown=ev=>{ ev.preventDefault(); ev.stopPropagation(); g.setPointerCapture(ev.pointerId); row.classList.add('dragging'); g.onpointermove=m=>{ const over=$$('.childrow',box).find(r=>r!==row&&m.clientYover.getBoundingClientRect().top+over.offsetHeight/2) box.insertBefore(row,over.nextElementSibling); else if(over) box.insertBefore(row,over); else box.appendChild(row); }; g.onpointerup=g.onpointercancel=()=>{ g.onpointermove=null; row.classList.remove('dragging'); save(); }; }; }