jukebox
All repositories: gitoria
6.8 KB
// components/songs.hl — `/`: ALL SONGS in one long list (the virtual list: only the rows near the viewport exist, 100 are fetched at a time from the server).// Sort by artist, title, album or plays; the Favourites button keeps only the songs played at least N times (N is adjustable, default 5) and the line// under it says how many songs that is. A tap on a song plays it with this list as the queue (the shell's `jbPlay`).parent './main.hl'import VirtualList from './virtual-list/virtual-list.hl'import { rowsFor, thousands } from '../lib/rows.hl'import { lastSort, rememberSort, lastScroll, rememberScroll, startRowFor } from '../lib/remember.hl'__title = 'Songs · Jukebox'String sortBy = lastSort()Boolean favOn = falseNumber favMin = 5Hybrid songCtx = { kind = 'songs' sort = sortBy minPlays = 0 }// where the list was left (kept per sort, see lib/remember.hl); the first rows are the ones around that placeNumber savedTop = lastScroll('songs:' + sortBy)Number firstRow = startRowFor(savedTop)Hybrid firstRows = rowsFor({ kind = 'songs' sort = sortBy minPlays = 0 }, firstRow, 100)Number songTotal = firstRows.totalHybrid chunk = firstRows.rowsNumber chunkFrom = firstRows.offsetString playingId = ''String favClass = favOn ? 'on' : ''Boolean selArtist = sortBy == 'artist'Boolean selTitle = sortBy == 'title'Boolean selAlbum = sortBy == 'album'Boolean selPlays = sortBy == 'plays'String countText = thousands(songTotal) + (songTotal == 1 ? ' song' : ' songs')// the list asks for the rows near the viewport; one request of 100 rows around themon songsLoad(first, wanted) {let start = first - 20if (start < 0) { start = 0 }r = emit server songsRows(songCtx, start, 100)songTotal = r.totalchunk = r.rowschunkFrom = r.offsetreturn null}// the list changed (order or filter): the first rows, back to the topon songsReload() {songCtx = { kind = 'songs' sort = sortBy minPlays = favOn ? favMin : 0 }r = emit server songsRows(songCtx, 0, 100)songTotal = r.totalchunk = r.rowschunkFrom = 0emit vlTop()return null}loadRange = (first, wanted) => { emit songsLoad(first, wanted) }// "12×" → 12. A row that shows a length has no count.playsIn = (aside) => {if (aside.length < 2 || aside[aside.length - 1] != '×') { return -1 }n = toNumber(aside.slice(0, aside.length - 1))return n == null ? -1 : n}// most played first, same records, ties keep their orderbyPlays = (rows) => {out = []for (r of rows) {let j = out.lengthpc = playsIn(r.aside)out.push(r)while (j > 0 && playsIn(out[j - 1].aside) < pc) {out[j] = out[j - 1]j = j - 1}out[j] = r}return out}// the rows of the current window, from the server: a song entered or left iton songsFill() {r = emit server songsRows(songCtx, chunkFrom, 100)if (r == null) { return null }songTotal = r.totalchunk = r.rowschunkFrom = r.offsetreturn null}// a play was counted. The count arrives with the event. The row is written here, then the rows already// held are re-sorted, which moves that row. One fetch only when the song is not in this window.on client jbPlays(id, plays) {if (songCtx.sort != 'plays') { return null }let i = 0let at = -1while (i < chunk.length) {if (chunk[i].id == id) { at = i }i = i + 1}if (at < 0) { emit songsFill() return null }chunk[at].aside = plays + '×'chunk = byPlays(chunk)return null}picked = (String id, Number index) => {playingId = id$parent.jbPlay(songCtx, index, id)emit songsLoad(index, 0)}on songsShuffleFav(Hybrid e) {$parent.jbShuffle({ kind = 'favourites' minPlays = favMin })return null}on songsShuffle(Hybrid e) {$parent.jbShuffle({ kind = 'list' ctx = songCtx })return null}on server songsRemember(sort) {return rememberSort(sort)}// the listener scrolled: keep the offset for the list as it is shown (its sort, and the favourites filter apart)on server songsRememberScroll(key, top) {return rememberScroll(key, top)}on songsScrolled(top) {emit server songsRememberScroll('songs:' + sortBy + (favOn ? ':fav' : ''), top)return null}rememberTop = (top) => { emit songsScrolled(top) }on songsSort(e) {sortBy = e.target.valueemit server songsRemember(sortBy)emit songsReload()return null}on songsFav(e) {favOn = !favOnemit songsReload()return null}on songsMin(e) {n = toNumber(e.target.value)if (n == null || n < 1) { return null }favMin = nif (favOn) { emit songsReload() }return null}Style {jbTools {display = 'flex'flexWrap = 'wrap'alignItems = 'center'gap = '.35rem .6rem'paddingBottom = '.25rem'select {flex = '1 1 6rem'minWidth = '0'@media {minWidth = '60rem'flex = '0 0 14rem'}}// Keep the quoted attribute selector and its original specificity.'input[type="search"]' { flex = '1 1 100%' }// Keep the quoted class selector and its original specificity.'button.on' {color = 'var(--color-accent-text)'background = 'var(--color-accent)'}}jbCount {display = 'flex'flexWrap = 'wrap'alignItems = 'center'gap = '.25rem .5rem'minHeight = '1.75rem'color = 'var(--color-text-muted)'fontSize = '.9rem'// Keep the quoted attribute selector and its original specificity.'input[type="number"]' {flex = 'none'width = '4.5rem'minHeight = '2.25rem'padding = '.2rem .5rem'textAlign = 'center'}button {minHeight = '2.25rem'padding = '.2rem .9rem'}}}View {jbScreen {jbTools {button { id = "shuffle-fav" type = "button" on click(e) { emit songsShuffleFav(e) } "Shuffle my favourites" }select { id = "sort" autocomplete = "off" 'aria-label' = "Sort by" on change(e) { emit songsSort(e) }option { value = "artist" selected = selArtist "Sort: artist" }option { value = "title" selected = selTitle "Sort: title" }option { value = "album" selected = selAlbum "Sort: album" }option { value = "plays" selected = selPlays "Sort: most played" }}button { id = "fav" type = "button" class = favClass 'aria-pressed' = favOn on click(e) { emit songsFav(e) } "Favourites" }button { id = "shuffle" type = "button" on click(e) { emit songsShuffle(e) } "Shuffle" }}jbCount {output { id = "count" countText }if (favOn) {span { "played at least" }input { id = "favmin" type = "number" min = "1" value = favMin inputmode = "numeric" 'aria-label' = "Minimum play count" on input(e) { emit songsMin(e) } }span { "times" }}}VirtualList { itemCount = songTotal rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId scrollTop = savedTop onScroll = rememberTop listLabel = "Songs" }}}// This screen owns its row request across the server boundary.on server songsRows(Hybrid ctx, Number offset, Number limit) {return rowsFor(ctx, offset, limit)}
Branches
Latest commits
- 10f8a8c8Merge branch 'code-order' into mainJukebox
- 204fc828Merge branch 'day72' into mainJukebox
- 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
- 33072bb5|||PR Record day72 gates and Chrome proofmre
- 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
- 6422a020Sync media-player at 51c3818mre
- b582ba1cVendor complete Hybriel day72 plugin setmre
- 02dccbc9Merge branch 'records-1119' into mainJukebox
- a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
- fc7db075Merge branch 'reconnect-retry' into mainJukebox
- ac6f4c6fL2: plain declaration for a in pickedNextmre
- 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
- 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
- 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
- 1f0cd77bMerge branch 'records-day41' into mainJukebox
- 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
- 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
- 04f5d9d5Merge branch 'day41' into mainJukebox
- 8ed6da45|||PR README: media-player pin df9e7dfmre
- d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre