gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/components/songs.hl

6.8 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, lastScroll, rememberScroll, startRowFor } 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. // where the list was left (kept per sort, see lib/remember.hl); the first rows are the ones around that place
  14. Number savedTop = lastScroll('songs:' + sortBy)
  15. Number firstRow = startRowFor(savedTop)
  16. Hybrid firstRows = rowsFor({ kind = 'songs' sort = sortBy minPlays = 0 }, firstRow, 100)
  17. Number songTotal = firstRows.total
  18. Hybrid chunk = firstRows.rows
  19. Number chunkFrom = firstRows.offset
  20. String playingId = ''
  21. String favClass = favOn ? 'on' : ''
  22. Boolean selArtist = sortBy == 'artist'
  23. Boolean selTitle = sortBy == 'title'
  24. Boolean selAlbum = sortBy == 'album'
  25. Boolean selPlays = sortBy == 'plays'
  26. String countText = thousands(songTotal) + (songTotal == 1 ? ' song' : ' songs')
  27. // the list asks for the rows near the viewport; one request of 100 rows around them
  28. on songsLoad(first, wanted) {
  29. let start = first - 20
  30. if (start < 0) { start = 0 }
  31. r = emit server songsRows(songCtx, start, 100)
  32. songTotal = r.total
  33. chunk = r.rows
  34. chunkFrom = r.offset
  35. return null
  36. }
  37. // the list changed (order or filter): the first rows, back to the top
  38. on songsReload() {
  39. songCtx = { kind = 'songs' sort = sortBy minPlays = favOn ? favMin : 0 }
  40. r = emit server songsRows(songCtx, 0, 100)
  41. songTotal = r.total
  42. chunk = r.rows
  43. chunkFrom = 0
  44. emit vlTop()
  45. return null
  46. }
  47. loadRange = (first, wanted) => { emit songsLoad(first, wanted) }
  48. // "12×" → 12. A row that shows a length has no count.
  49. playsIn = (aside) => {
  50. if (aside.length < 2 || aside[aside.length - 1] != '×') { return -1 }
  51. n = toNumber(aside.slice(0, aside.length - 1))
  52. return n == null ? -1 : n
  53. }
  54. // most played first, same records, ties keep their order
  55. byPlays = (rows) => {
  56. out = []
  57. for (r of rows) {
  58. let j = out.length
  59. pc = playsIn(r.aside)
  60. out.push(r)
  61. while (j > 0 && playsIn(out[j - 1].aside) < pc) {
  62. out[j] = out[j - 1]
  63. j = j - 1
  64. }
  65. out[j] = r
  66. }
  67. return out
  68. }
  69. // the rows of the current window, from the server: a song entered or left it
  70. on songsFill() {
  71. r = emit server songsRows(songCtx, chunkFrom, 100)
  72. if (r == null) { return null }
  73. songTotal = r.total
  74. chunk = r.rows
  75. chunkFrom = r.offset
  76. return null
  77. }
  78. // a play was counted. The count arrives with the event. The row is written here, then the rows already
  79. // held are re-sorted, which moves that row. One fetch only when the song is not in this window.
  80. on client jbPlays(id, plays) {
  81. if (songCtx.sort != 'plays') { return null }
  82. let i = 0
  83. let at = -1
  84. while (i < chunk.length) {
  85. if (chunk[i].id == id) { at = i }
  86. i = i + 1
  87. }
  88. if (at < 0) { emit songsFill() return null }
  89. chunk[at].aside = plays + '×'
  90. chunk = byPlays(chunk)
  91. return null
  92. }
  93. picked = (String id, Number index) => {
  94. playingId = id
  95. $parent.jbPlay(songCtx, index, id)
  96. emit songsLoad(index, 0)
  97. }
  98. on songsShuffleFav(Hybrid e) {
  99. $parent.jbShuffle({ kind = 'favourites' minPlays = favMin })
  100. return null
  101. }
  102. on songsShuffle(Hybrid e) {
  103. $parent.jbShuffle({ kind = 'list' ctx = songCtx })
  104. return null
  105. }
  106. on server songsRemember(sort) {
  107. return rememberSort(sort)
  108. }
  109. // the listener scrolled: keep the offset for the list as it is shown (its sort, and the favourites filter apart)
  110. on server songsRememberScroll(key, top) {
  111. return rememberScroll(key, top)
  112. }
  113. on songsScrolled(top) {
  114. emit server songsRememberScroll('songs:' + sortBy + (favOn ? ':fav' : ''), top)
  115. return null
  116. }
  117. rememberTop = (top) => { emit songsScrolled(top) }
  118. on songsSort(e) {
  119. sortBy = e.target.value
  120. emit server songsRemember(sortBy)
  121. emit songsReload()
  122. return null
  123. }
  124. on songsFav(e) {
  125. favOn = !favOn
  126. emit songsReload()
  127. return null
  128. }
  129. on songsMin(e) {
  130. n = toNumber(e.target.value)
  131. if (n == null || n < 1) { return null }
  132. favMin = n
  133. if (favOn) { emit songsReload() }
  134. return null
  135. }
  136. Style {
  137. jbTools {
  138. display = 'flex'
  139. flexWrap = 'wrap'
  140. alignItems = 'center'
  141. gap = '.35rem .6rem'
  142. paddingBottom = '.25rem'
  143. select {
  144. flex = '1 1 6rem'
  145. minWidth = '0'
  146. @media {
  147. minWidth = '60rem'
  148. flex = '0 0 14rem'
  149. }
  150. }
  151. // Keep the quoted attribute selector and its original specificity.
  152. 'input[type="search"]' { flex = '1 1 100%' }
  153. // Keep the quoted class selector and its original specificity.
  154. 'button.on' {
  155. color = 'var(--color-accent-text)'
  156. background = 'var(--color-accent)'
  157. }
  158. }
  159. jbCount {
  160. display = 'flex'
  161. flexWrap = 'wrap'
  162. alignItems = 'center'
  163. gap = '.25rem .5rem'
  164. minHeight = '1.75rem'
  165. color = 'var(--color-text-muted)'
  166. fontSize = '.9rem'
  167. // Keep the quoted attribute selector and its original specificity.
  168. 'input[type="number"]' {
  169. flex = 'none'
  170. width = '4.5rem'
  171. minHeight = '2.25rem'
  172. padding = '.2rem .5rem'
  173. textAlign = 'center'
  174. }
  175. button {
  176. minHeight = '2.25rem'
  177. padding = '.2rem .9rem'
  178. }
  179. }
  180. }
  181. View {
  182. jbScreen {
  183. jbTools {
  184. button { id = "shuffle-fav" type = "button" on click(e) { emit songsShuffleFav(e) } "Shuffle my favourites" }
  185. select { id = "sort" autocomplete = "off" 'aria-label' = "Sort by" on change(e) { emit songsSort(e) }
  186. option { value = "artist" selected = selArtist "Sort: artist" }
  187. option { value = "title" selected = selTitle "Sort: title" }
  188. option { value = "album" selected = selAlbum "Sort: album" }
  189. option { value = "plays" selected = selPlays "Sort: most played" }
  190. }
  191. button { id = "fav" type = "button" class = favClass 'aria-pressed' = favOn on click(e) { emit songsFav(e) } "Favourites" }
  192. button { id = "shuffle" type = "button" on click(e) { emit songsShuffle(e) } "Shuffle" }
  193. }
  194. jbCount {
  195. output { id = "count" countText }
  196. if (favOn) {
  197. span { "played at least" }
  198. input { id = "favmin" type = "number" min = "1" value = favMin inputmode = "numeric" 'aria-label' = "Minimum play count" on input(e) { emit songsMin(e) } }
  199. span { "times" }
  200. }
  201. }
  202. VirtualList { itemCount = songTotal rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId scrollTop = savedTop onScroll = rememberTop listLabel = "Songs" }
  203. }
  204. }
  205. // This screen owns its row request across the server boundary.
  206. on server songsRows(Hybrid ctx, Number offset, Number limit) {
  207. return rowsFor(ctx, offset, limit)
  208. }

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