gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2e7f6a432e7f6a43Vendor virtual-list 489afcf from components.hybrielmre2e7f6a43/components/songs.hl

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

Branches

Latest commits

  • 2e7f6a43Vendor virtual-list 489afcf from components.hybrielmre
  • 0ba85799Merge branch 'reset-classes' into mainJukebox
  • 502eeed5jukebox#20: vendor virtual-list initial resetKey baseline fixmre
  • 3b76651cjukebox#20: update virtual-list README pin and resetKey optionsmre
  • bc09e1e8|||PR jukebox#20: reset virtual lists through resetKey (partial validation)mre
  • 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