|
|
|
|
@@ -0,0 +1,163 @@
|
|
|
|
|
/* ---------------- smart playlists ---------------- */
|
|
|
|
|
/// Smart playlists (#476): the episodes of the podcasts chosen, those marked to play first ahead of
|
|
|
|
|
/// the rest, newest or oldest first, unplayed only or all. A playlist is a place, `:playlist:<id>`,
|
|
|
|
|
/// in a folder of their own in the feed list, as Signals are.
|
|
|
|
|
const PLAYLISTS=':playlists';
|
|
|
|
|
let playlists: Playlist[]=[];
|
|
|
|
|
/// The playlist the episode playing was started from, which goes on to its next when it ends, once
|
|
|
|
|
/// Up Next has nothing.
|
|
|
|
|
let playingFrom: Entry[]|null=null;
|
|
|
|
|
|
|
|
|
|
function playlistOf(view: string|null){
|
|
|
|
|
const m=/^:playlist:(\d+)$/.exec(view||'');
|
|
|
|
|
return m ? playlists.find(p=>p.id===Number(m[1])) : undefined;
|
|
|
|
|
}
|
|
|
|
|
function loadPlaylists(){
|
|
|
|
|
return api('/api/playlists').then((r: Playlist[])=>{ playlists=r; renderFeeds(); }).catch(()=>{});
|
|
|
|
|
}
|
|
|
|
|
/// The playlist's own art: its podcasts' artwork, four together, as a folder's is.
|
|
|
|
|
const playlistArt=(p: Playlist)=>folderArt({id:'',title:p.name,image:null} as Feed,S.feeds.filter(f=>p.feeds.includes(f.id)));
|
|
|
|
|
|
|
|
|
|
function playlistFolder(list: HTMLElement, q: string){
|
|
|
|
|
const mine=playlists.filter(p=>!q||p.name.toLowerCase().includes(q));
|
|
|
|
|
if(q&&!mine.length) return;
|
|
|
|
|
const open=expanded.has(PLAYLISTS)||!!q;
|
|
|
|
|
list.appendChild(feedRow(PLAYLISTS,'plists',
|
|
|
|
|
`<button class="chev" aria-expanded="${open}" title="Show or hide your playlists" aria-label="Show or hide your playlists">${ICON.caret}</button>`+
|
|
|
|
|
`<span class="fart"><div class="art">${ICON.queue}</div></span><div class="txt"><b>Playlists</b><small>${playlists.length?plural(playlists.length,'playlist'):'Episodes from the podcasts you choose'}</small></div>`,
|
|
|
|
|
()=>selectFeed(PLAYLISTS)));
|
|
|
|
|
$('.chev',list.lastElementChild!).onclick=(ev: Event)=>{ ev.stopPropagation(); toggleGroup(PLAYLISTS); };
|
|
|
|
|
if(!open) return;
|
|
|
|
|
for(const p of mine)
|
|
|
|
|
list.appendChild(feedRow(':playlist:'+p.id,'child',
|
|
|
|
|
`<span class="fart">${playlistArt(p)}</span><div class="txt"><b>${esc(p.name)}</b><small>${plural(p.feeds.length,'podcast')}</small></div>`+badgeHTML(p.items),
|
|
|
|
|
()=>selectFeed(':playlist:'+p.id)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The folder's page: every playlist, and New playlist.
|
|
|
|
|
function renderPlaylists(){
|
|
|
|
|
$('#count').textContent=`Playlists: ${plural(playlists.length,'playlist')}`;
|
|
|
|
|
$<HTMLButtonElement>('#tbRemove').disabled=true;
|
|
|
|
|
syncTools(null);
|
|
|
|
|
const box=$('#content');
|
|
|
|
|
box.classList.add('plain');
|
|
|
|
|
box.innerHTML=`
|
|
|
|
|
<div class="fhead slim">
|
|
|
|
|
<div class="art">${ICON.queue}</div>
|
|
|
|
|
<div class="meta"><h2>Playlists</h2><div class="sub">Episodes from the podcasts you choose, kept up to date as new ones come.</div></div>
|
|
|
|
|
<div class="acts"><button class="btn ico" data-a="new" title="New playlist" aria-label="New playlist">${ICON.plus}</button></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="childlist" id="plists"></div>`;
|
|
|
|
|
$<HTMLButtonElement>('#content .acts [data-a="new"]').onclick=()=>playlistModal();
|
|
|
|
|
const list=$('#plists');
|
|
|
|
|
if(!playlists.length){ list.innerHTML='<p class="empty">No playlists yet. Make one from the podcasts you want to hear in a row.</p>'; return; }
|
|
|
|
|
for(const p of playlists){
|
|
|
|
|
const el=document.createElement('div');
|
|
|
|
|
el.className='childrow';
|
|
|
|
|
el.innerHTML=playlistArt(p)+`<div class="txt"><b>${esc(p.name)}</b><small class="meta">${plural(p.feeds.length,'podcast')}</small></div>`+badgeHTML(p.items);
|
|
|
|
|
el.onclick=()=>selectFeed(':playlist:'+p.id);
|
|
|
|
|
list.appendChild(el);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A playlist's page: its episodes in its order. Play goes on through it; the cross marks one
|
|
|
|
|
/// played, which takes it out of an unplayed-only playlist.
|
|
|
|
|
async function renderPlaylist(p: Playlist){
|
|
|
|
|
$<HTMLButtonElement>('#tbRemove').disabled=true;
|
|
|
|
|
syncTools(null);
|
|
|
|
|
const box=$('#content');
|
|
|
|
|
box.classList.add('plain');
|
|
|
|
|
const how=[p.only_unplayed?'Unplayed episodes':'Every episode',p.oldest_first?'oldest first':'newest first',
|
|
|
|
|
p.priority.length?`${plural(p.priority.length,'podcast')} first`:''].filter(Boolean).join(', ');
|
|
|
|
|
box.innerHTML=`
|
|
|
|
|
<div class="fhead slim">
|
|
|
|
|
${playlistArt(p)}
|
|
|
|
|
<div class="meta"><h2>${esc(p.name)}</h2><div class="sub">${how}, from ${plural(p.feeds.length,'podcast')}</div></div>
|
|
|
|
|
<div class="acts">
|
|
|
|
|
<button class="btn ico" data-a="edit" title="Edit this playlist" aria-label="Edit this playlist">${ICON.settings}</button>
|
|
|
|
|
<button class="btn ico danger" data-a="rm" title="Delete this playlist" aria-label="Delete this playlist">${ICON.trash}</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="childlist" id="listening"></div>`;
|
|
|
|
|
$<HTMLButtonElement>('#content .acts [data-a="edit"]').onclick=()=>playlistModal(p);
|
|
|
|
|
$<HTMLButtonElement>('#content .acts [data-a="rm"]').onclick=async()=>{
|
|
|
|
|
if(!await ask(`Delete the playlist ${p.name}? Its episodes stay in their podcasts.`,'Delete')) return;
|
|
|
|
|
try{ await api(`/api/playlists/${p.id}`,{method:'DELETE'}); toast('Deleted'); await loadPlaylists(); selectFeed(PLAYLISTS); }
|
|
|
|
|
catch(e){ toast(why(e),true); }
|
|
|
|
|
};
|
|
|
|
|
const list=$('#listening');
|
|
|
|
|
listLoading(list);
|
|
|
|
|
let rows: Entry[];
|
|
|
|
|
// shortcut: the first 200 only, with no Load more; page it as feeds do if a playlist is listened
|
|
|
|
|
// through that far.
|
|
|
|
|
try{ rows=(await api(`/api/playlists/${p.id}/entries?limit=200`) as ListPage).entries; }
|
|
|
|
|
catch(e){ list.innerHTML=`<p class="hint">${esc(why(e))}</p>`; return; }
|
|
|
|
|
if(!list.isConnected) return;
|
|
|
|
|
$('#count').textContent=`${p.name}: ${plural(p.items,'episode')}`;
|
|
|
|
|
list.innerHTML=rows.length?'':'<p class="hint">Nothing here yet. Episodes show up as your podcasts publish them.</p>';
|
|
|
|
|
for(const e of rows){
|
|
|
|
|
const el=listenRow(e,'Mark played',async()=>{ try{ await setRead(e,true); }catch(err){ toast(why(err),true); } renderPlaylist(p); loadPlaylists(); });
|
|
|
|
|
el.onclick=ev=>{
|
|
|
|
|
if((ev.target as Element).closest('[data-a=remove]')) return;
|
|
|
|
|
if(el.classList.contains('now')&&!audio.paused){ audio.pause(); return; }
|
|
|
|
|
playFrom(rows,e);
|
|
|
|
|
};
|
|
|
|
|
list.appendChild(el);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// Plays an episode of a list that goes on to the list's next.
|
|
|
|
|
function playFrom(list: Entry[],e: Entry){
|
|
|
|
|
play(e);
|
|
|
|
|
// After play, which forgets the list it came from, and has painted Next and told the phone
|
|
|
|
|
// what follows without it.
|
|
|
|
|
if(inPlayer(e)){ playingFrom=list; hostNext(); }
|
|
|
|
|
}
|
|
|
|
|
/// The next in the playlist after the one playing, if it was started from one.
|
|
|
|
|
function nextInPlaylist(){
|
|
|
|
|
if(!playingFrom) return undefined;
|
|
|
|
|
const i=playingFrom.findIndex(inPlayer);
|
|
|
|
|
return i<0 ? undefined : nextUp(playingFrom.slice(i+1),null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Making a playlist, or changing one: a name, the podcasts in it and which play first, and which
|
|
|
|
|
/// of their episodes.
|
|
|
|
|
function playlistModal(p?: Playlist){
|
|
|
|
|
const shows=S.feeds.filter(f=>f.podcast);
|
|
|
|
|
const row=(f: Feed)=>{
|
|
|
|
|
const id=esc(f.id), inIt=!!p?.feeds.includes(f.id), first=!!p?.priority.includes(f.id);
|
|
|
|
|
return `<div class="plshow">${artHTML(f.image,f.title||f.id)}
|
|
|
|
|
<label class="check"><input type="checkbox" data-in="${id}"${inIt?' checked':''}> ${esc(f.title||f.id)}</label>
|
|
|
|
|
<label class="check first"><input type="checkbox" data-first="${id}"${first?' checked':''}${inIt?'':' disabled'}> Play first</label></div>`;
|
|
|
|
|
};
|
|
|
|
|
openModal(`<h3>${p?'Edit playlist':'New playlist'}</h3>
|
|
|
|
|
<div class="field"><label for="plname">Name</label>
|
|
|
|
|
<input type="text" id="plname" value="${esc(p?.name||'')}" placeholder="Morning commute"></div>
|
|
|
|
|
<div class="field"><label>Podcasts</label>
|
|
|
|
|
${shows.length?`<div class="plshows">${shows.map(row).join('')}</div>`:'<p class="hint">Subscribe to a podcast first.</p>'}
|
|
|
|
|
<span class="hint">Episodes of those set to play first come before the rest.</span></div>
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label class="check"><input type="checkbox" id="plunplayed"${p?.only_unplayed??true?' checked':''}> Only episodes you have not played</label>
|
|
|
|
|
<label class="check"><input type="checkbox" id="ploldest"${p?.oldest_first?' checked':''}> Oldest first</label></div>
|
|
|
|
|
<div class="cardacts"><button class="btn ico" onclick="closeModal()" title="Cancel" aria-label="Cancel">${ICON.close}</button>
|
|
|
|
|
<button class="btn ico primary" id="plsave" title="${p?'Save':'Make playlist'}" aria-label="${p?'Save':'Make playlist'}">${p?ICON.check:ICON.plus}</button></div>`);
|
|
|
|
|
// Play first means something only for a podcast in the playlist.
|
|
|
|
|
for(const box of $$<HTMLInputElement>('[data-in]')) box.onchange=()=>{
|
|
|
|
|
const first=$<HTMLInputElement>(`[data-first="${CSS.escape(box.dataset.in!)}"]`);
|
|
|
|
|
first.disabled=!box.checked; if(!box.checked) first.checked=false;
|
|
|
|
|
};
|
|
|
|
|
$<HTMLInputElement>('#plname').focus();
|
|
|
|
|
$<HTMLButtonElement>('#plsave').onclick=async()=>{
|
|
|
|
|
const name=$<HTMLInputElement>('#plname').value.trim();
|
|
|
|
|
const feeds=$$<HTMLInputElement>('[data-in]').filter(b=>b.checked).map(b=>b.dataset.in!);
|
|
|
|
|
if(!name){ $<HTMLInputElement>('#plname').focus(); return; }
|
|
|
|
|
if(!feeds.length){ toast('Choose at least one podcast.',true); return; }
|
|
|
|
|
const body=JSON.stringify({name,feeds,priority:$$<HTMLInputElement>('[data-first]').filter(b=>b.checked).map(b=>b.dataset.first!),
|
|
|
|
|
only_unplayed:$<HTMLInputElement>('#plunplayed').checked,oldest_first:$<HTMLInputElement>('#ploldest').checked});
|
|
|
|
|
try{
|
|
|
|
|
const r=p ? await api(`/api/playlists/${p.id}`,{method:'PATCH',body}) : await api('/api/playlists',{method:'POST',body});
|
|
|
|
|
closeModal(); toast(p?'Saved':'Playlist made');
|
|
|
|
|
if(!expanded.has(PLAYLISTS)) toggleGroup(PLAYLISTS);
|
|
|
|
|
await loadPlaylists(); selectFeed(':playlist:'+(p?p.id:r.id));
|
|
|
|
|
}catch(e){ toast(why(e),true); }
|
|
|
|
|
};
|
|
|
|
|
}
|