gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9efc40ff9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre9efc40ff/components/songs.hl

5.9 KB

  1. // 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).
  2. // 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
  3. // 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`).
  4. parent './main.hl'
  5. import VirtualList from './virtual-list/virtual-list.hl'
  6. import { rowsFor, thousands } from '../lib/rows.hl'
  7. import { lastSort, rememberSort } from '../lib/remember.hl'
  8. __title = 'Songs · Jukebox'
  9. String sortBy = lastSort()
  10. Boolean favOn = false
  11. Number favMin = 5
  12. Hybrid songCtx = { kind = 'songs' sort = sortBy minPlays = 0 }
  13. Hybrid firstRows = rowsFor({ kind = 'songs' sort = sortBy minPlays = 0 }, 0, 100)
  14. Number songTotal = firstRows.total
  15. Hybrid chunk = firstRows.rows
  16. Number chunkFrom = 0
  17. String playingId = ''
  18. String favClass = favOn ? 'on' : ''
  19. Boolean selArtist = sortBy == 'artist'
  20. Boolean selTitle = sortBy == 'title'
  21. Boolean selAlbum = sortBy == 'album'
  22. Boolean selPlays = sortBy == 'plays'
  23. String countText = thousands(songTotal) + (songTotal == 1 ? ' song' : ' songs')
  24. // the list asks for the rows near the viewport; one request of 100 rows around them
  25. on songsLoad(first, wanted) {
  26. let start = first - 20
  27. if (start < 0) { start = 0 }
  28. r = emit server jbRows(songCtx, start, 100)
  29. songTotal = r.total
  30. chunk = r.rows
  31. chunkFrom = r.offset
  32. return null
  33. }
  34. // the list changed (order or filter): the first rows, back to the top
  35. on songsReload() {
  36. songCtx = { kind = 'songs' sort = sortBy minPlays = favOn ? favMin : 0 }
  37. r = emit server jbRows(songCtx, 0, 100)
  38. songTotal = r.total
  39. chunk = r.rows
  40. chunkFrom = 0
  41. emit vlTop()
  42. return null
  43. }
  44. loadRange = (first, wanted) => { emit songsLoad(first, wanted) }
  45. // "12×" → 12. A row that shows a length has no count.
  46. playsIn = (aside) => {
  47. if (aside.length < 2 || aside[aside.length - 1] != '×') { return -1 }
  48. n = toNumber(aside.slice(0, aside.length - 1))
  49. return n == null ? -1 : n
  50. }
  51. // most played first, same records, ties keep their order
  52. byPlays = (rows) => {
  53. out = []
  54. for (r of rows) {
  55. let j = out.length
  56. pc = playsIn(r.aside)
  57. out.push(r)
  58. while (j > 0 && playsIn(out[j - 1].aside) < pc) {
  59. out[j] = out[j - 1]
  60. j = j - 1
  61. }
  62. out[j] = r
  63. }
  64. return out
  65. }
  66. // the rows of the current window, from the server: a song entered or left it
  67. on songsFill() {
  68. r = emit server jbRows(songCtx, chunkFrom, 100)
  69. if (r == null) { return null }
  70. songTotal = r.total
  71. chunk = r.rows
  72. chunkFrom = r.offset
  73. return null
  74. }
  75. // a play was counted. The count arrives with the event. The row is written here, then the rows already
  76. // held are re-sorted, which moves that row. One fetch only when the song is not in this window.
  77. on client jbPlays(id, plays) {
  78. if (songCtx.sort != 'plays') { return null }
  79. let i = 0
  80. let at = -1
  81. while (i < chunk.length) {
  82. if (chunk[i].id == id) { at = i }
  83. i = i + 1
  84. }
  85. if (at < 0) { emit songsFill() return null }
  86. chunk[at].aside = plays + '×'
  87. chunk = byPlays(chunk)
  88. return null
  89. }
  90. picked = (id, index) => {
  91. playingId = id
  92. emit jbPlay(songCtx, index, id)
  93. emit songsLoad(index, 0)
  94. }
  95. on songsShuffleFav(e) {
  96. emit jbShuffle({ kind = 'favourites' minPlays = favMin })
  97. return null
  98. }
  99. on songsShuffle(e) {
  100. emit jbShuffle({ kind = 'list' ctx = songCtx })
  101. return null
  102. }
  103. on server songsRemember(sort) {
  104. return rememberSort(sort)
  105. }
  106. on songsSort(e) {
  107. sortBy = e.target.value
  108. emit server songsRemember(sortBy)
  109. emit songsReload()
  110. return null
  111. }
  112. on songsFav(e) {
  113. favOn = !favOn
  114. emit songsReload()
  115. return null
  116. }
  117. on songsMin(e) {
  118. n = toNumber(e.target.value)
  119. if (n == null || n < 1) { return null }
  120. favMin = n
  121. if (favOn) { emit songsReload() }
  122. return null
  123. }
  124. Style {
  125. 'jb-tools' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.35rem .6rem' paddingBottom = '.25rem' }
  126. 'jb-tools select' { flex = '1 1 6rem' minWidth = '0' }
  127. 'jb-tools input[type="search"]' { flex = '1 1 100%' }
  128. 'jb-count input[type="number"]' { flex = 'none' width = '4.5rem' minHeight = '2.25rem' padding = '.2rem .5rem' textAlign = 'center' }
  129. 'jb-tools button.jb-big' { flex = '1 1 100%' minHeight = '3.25rem' fontSize = '1.1rem' color = 'var(--color-accent-text)' background = 'var(--color-accent)' }
  130. 'jb-tools button.on' { color = 'var(--color-accent-text)' background = 'var(--color-accent)' }
  131. 'jb-count' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.25rem .5rem' minHeight = '1.75rem' color = 'var(--color-text-muted)' fontSize = '.9rem' }
  132. 'jb-count button' { minHeight = '2.25rem' padding = '.2rem .9rem' }
  133. '@media (min-width: 60rem)' {
  134. 'jb-tools select' { flex = '0 0 14rem' }
  135. }
  136. }
  137. View {
  138. jbScreen {
  139. jbTools {
  140. button { id = "shuffle-fav" type = "button" class = "jb-big" on click(e) { emit songsShuffleFav(e) } "Shuffle my favourites" }
  141. select { id = "sort" autocomplete = "off" 'aria-label' = "Sort by" on change(e) { emit songsSort(e) }
  142. option { value = "artist" selected = selArtist "Sort: artist" }
  143. option { value = "title" selected = selTitle "Sort: title" }
  144. option { value = "album" selected = selAlbum "Sort: album" }
  145. option { value = "plays" selected = selPlays "Sort: most played" }
  146. }
  147. button { id = "fav" type = "button" class = favClass 'aria-pressed' = favOn on click(e) { emit songsFav(e) } "Favourites" }
  148. button { id = "shuffle" type = "button" on click(e) { emit songsShuffle(e) } "Shuffle" }
  149. }
  150. jbCount {
  151. output { id = "count" countText }
  152. if (favOn) {
  153. span { "played at least" }
  154. input { id = "favmin" type = "number" min = "1" value = favMin inputmode = "numeric" 'aria-label' = "Minimum play count" on input(e) { emit songsMin(e) } }
  155. span { "times" }
  156. }
  157. }
  158. VirtualList { itemCount = songTotal rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId listLabel = "Songs" }
  159. }
  160. }

Branches

Latest commits

  • 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
  • 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
  • 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
  • cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
  • 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
  • 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
  • c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
  • 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
  • 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
  • 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
  • 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
  • bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre