gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd18851fdd18851fdjb19: pressed switches look different (whole selector), queue column cannot widen past the panel, gates follow the new bar (jb3, jb11, jb15)mred18851fd/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. jbTools {
  126. display = 'flex'
  127. flexWrap = 'wrap'
  128. alignItems = 'center'
  129. gap = '.35rem .6rem'
  130. paddingBottom = '.25rem'
  131. select {
  132. flex = '1 1 6rem'
  133. minWidth = '0'
  134. @media {
  135. minWidth = '60rem'
  136. flex = '0 0 14rem'
  137. }
  138. }
  139. // Keep the quoted attribute selector and its original specificity.
  140. 'input[type="search"]' { flex = '1 1 100%' }
  141. // Keep the quoted class selector and its original specificity.
  142. 'button.on' {
  143. color = 'var(--color-accent-text)'
  144. background = 'var(--color-accent)'
  145. }
  146. }
  147. jbCount {
  148. display = 'flex'
  149. flexWrap = 'wrap'
  150. alignItems = 'center'
  151. gap = '.25rem .5rem'
  152. minHeight = '1.75rem'
  153. color = 'var(--color-text-muted)'
  154. fontSize = '.9rem'
  155. // Keep the quoted attribute selector and its original specificity.
  156. 'input[type="number"]' {
  157. flex = 'none'
  158. width = '4.5rem'
  159. minHeight = '2.25rem'
  160. padding = '.2rem .5rem'
  161. textAlign = 'center'
  162. }
  163. button {
  164. minHeight = '2.25rem'
  165. padding = '.2rem .9rem'
  166. }
  167. }
  168. }
  169. View {
  170. jbScreen {
  171. jbTools {
  172. button { id = "shuffle-fav" type = "button" on click(e) { emit songsShuffleFav(e) } "Shuffle my favourites" }
  173. select { id = "sort" autocomplete = "off" 'aria-label' = "Sort by" on change(e) { emit songsSort(e) }
  174. option { value = "artist" selected = selArtist "Sort: artist" }
  175. option { value = "title" selected = selTitle "Sort: title" }
  176. option { value = "album" selected = selAlbum "Sort: album" }
  177. option { value = "plays" selected = selPlays "Sort: most played" }
  178. }
  179. button { id = "fav" type = "button" class = favClass 'aria-pressed' = favOn on click(e) { emit songsFav(e) } "Favourites" }
  180. button { id = "shuffle" type = "button" on click(e) { emit songsShuffle(e) } "Shuffle" }
  181. }
  182. jbCount {
  183. output { id = "count" countText }
  184. if (favOn) {
  185. span { "played at least" }
  186. input { id = "favmin" type = "number" min = "1" value = favMin inputmode = "numeric" 'aria-label' = "Minimum play count" on input(e) { emit songsMin(e) } }
  187. span { "times" }
  188. }
  189. }
  190. VirtualList { itemCount = songTotal rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId listLabel = "Songs" }
  191. }
  192. }

Branches

Latest commits

  • d18851fdjb19: pressed switches look different (whole selector), queue column cannot widen past the panel, gates follow the new bar (jb3, jb11, jb15)mre
  • 82b54c0ejb19: bar Shuffle · Previous · Play · Next · Repeat, no 10 s buttons; Shuffle switch (shuffle from the playing song, back to the list), Repeat off/all/one, both remembered in ui.json; Shuffle my favourites an ordinary button; gate tests/jb19.mjsmre
  • ba354710jb19: icons pinned to components.hybriel 46fefaa (shuffle, repeat, repeatOne, prevTrack, nextTrack)mre
  • d8e8d059jb19 step 2: media-player from components.hybriel c96709c (skip, repeat options; the end of a song only calls onPlay), sync-components.sh and SOURCE point to it, jb11 counts media loads and metadata writes (from jb21 d3a026e)mre
  • 7b048c1bRecords: jb19 done, L2 live stepmre
  • 665185aeMission 009: jb19 livemre
  • 47b2d3beRecords: P1 resumedmre
  • 8e326e94Records: resetKey blockedmre
  • f72dd7edRecords: icons comingmre
  • b264e5deRecords: P1 icons stopmre
  • 514d7cbeRecords: mission 008 startedmre
  • 61c40127Mission 008: #19 + #21mre
  • 6dcaeb46Records: resetKey planmre
  • c5dbf2ecRecords: R2 resultmre
  • 956069b8Records: R1 result, mission 007mre
  • 3c8a2e12Mission 007: #20 sort scrollmre
  • deb8cef5Records: mission 006 startedmre
  • 4f0ad42fMission 006: jukebox#20mre
  • 800cae06Records: #21 planmre
  • 64bc7823Records: #21 causemre