jukebox
All repositories: gitoria
6.5 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 jbRows(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 jbRows(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 jbRows(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 = (id, index) => {playingId = idemit jbPlay(songCtx, index, id)emit songsLoad(index, 0)}on songsShuffleFav(e) {emit jbShuffle({ kind = 'favourites' minPlays = favMin })return null}on songsShuffle(e) {emit 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" }}}
Branches
Latest commits
- cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
- f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
- 9f5c6532Merge branch 'status-22-pr' into mainJukebox
- 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
- 29d2029cRecords: PR-only rule (Kunibert)mre
- adacb0abRecords: neutral ticket rule, #22 decision recordedmre
- b2d6f121Records: #308 hold liftedmre
- 7ab67533Records: day39 on hold (hybriel#308)mre
- 22f21739Records: note rebase before mergemre
- f7368ea3Records: mission 011 day39, waits for lisbethmre
- 9515d2cdRecords: jb20 livemre
- 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
- 277a2722README: provenance linemre
- 4169ca6aREADME: sync pinmre
- a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
- f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
- d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
- df7920ceRecords: R3 startedmre
- 0549f038Mission 010: jb20 livemre
- 273494e2STATUS rewrittenmre