gitoria

jukebox

All repositories: gitoria

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

8.2 KB

  1. // lib/rows.hl — WHAT THE SCREENS SHOW: the database's songs, artists and albums turned into the rows of the virtual list
  2. // (`{ id, title, sub, aside, img }`) and into the items of the player's queue. The one place that decides how a row reads.
  3. // A screen asks `rowsFor(ctx, offset, limit)` with its context: which list it shows. Pure reading, no writes.
  4. import { songsPage, artistsPage, albumsPage, searchAll } from './browse.hl'
  5. import { artistsTable, albumsTable, songsTable } from './db.hl'
  6. import { idAt } from './queue.hl'
  7. // 3,966 → "3,966"
  8. static thousands = (n) => {
  9. s = '' + n
  10. let out = ''
  11. let i = 0
  12. while (i < s.length) {
  13. if (i > 0 && (s.length - i) % 3 == 0) { out = out + ',' }
  14. out = out + s[i]
  15. i = i + 1
  16. }
  17. return out
  18. }
  19. // 221.4 seconds → "3:41"; 3725 seconds → "1:02:05"; unknown → ""
  20. static clock = (seconds) => {
  21. if (seconds == null || seconds <= 0) { return '' }
  22. whole = seconds - seconds % 1
  23. h = (whole - whole % 3600) / 3600
  24. m = ((whole - h * 3600) - (whole - h * 3600) % 60) / 60
  25. s = whole - h * 3600 - m * 60
  26. ss = (s < 10 ? '0' : '') + s
  27. if (h > 0) { return h + ':' + (m < 10 ? '0' : '') + m + ':' + ss }
  28. return m + ':' + ss
  29. }
  30. // the small picture of a cover (96 px, made once by lib/covers.hl) and the large one
  31. static thumbUrl = (coverId) => { return coverId == null ? '' : '/api/thumb/' + coverId }
  32. static coverUrl = (coverId) => { return coverId == null ? '' : '/api/cover/' + coverId }
  33. // the player shows title and artist only beside a cover: a song without one gets this empty picture (the cover box is the surface colour)
  34. static noPicture = 'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%2F%3E'
  35. static songRow = (s, sort) => {
  36. return { id = s.id title = s.title sub = s.artist + ' · ' + s.album + (s.copies > 1 ? ' · ' + s.copies + ' copies' : '') aside = sort == 'plays' ? s.playCount + '×' : clock(s.duration) img = thumbUrl(s.coverId) }
  37. }
  38. // inside one album the track number leads and the artist is left out
  39. static albumSongRow = (s, multi) => {
  40. return { id = s.id title = (multi && s.disc != null ? s.disc + '-' : '') + (s.track != null && s.track > 0 ? s.track + '. ' : '') + s.title sub = s.artist == s.albumArtist ? '' : s.artist aside = clock(s.duration) img = '' }
  41. }
  42. static artistRow = (a) => {
  43. return { id = a.id title = a.name sub = thousands(a.albumCount) + (a.albumCount == 1 ? ' album · ' : ' albums · ') + thousands(a.songCount) + (a.songCount == 1 ? ' song' : ' songs') aside = '' img = thumbUrl(a.coverId) hasImg = a.coverId != null noImg = a.coverId == null }
  44. }
  45. static albumRow = (a) => {
  46. return { id = a.id title = a.name sub = a.albumArtist + (a.year == null ? '' : ' · ' + a.year) aside = thousands(a.songCount) img = thumbUrl(a.coverId) hasImg = a.coverId != null noImg = a.coverId == null }
  47. }
  48. // kind of row: 'song', 'albumSong', 'artist', 'album'
  49. static rowsOf = (page, kind, sort) => {
  50. out = []
  51. for (s of page.items) {
  52. if (kind == 'artist') { out.push(artistRow(s)) }
  53. else if (kind == 'album') { out.push(albumRow(s)) }
  54. else if (kind == 'albumSong') { out.push(albumSongRow(s, sort == 'multi')) }
  55. else { out.push(songRow(s, sort)) }
  56. }
  57. return { total = page.total offset = page.offset rows = out }
  58. }
  59. // the queue item of the player for one song: its stream, its title, its length
  60. static queueItem = (s, index) => {
  61. return { src = '/stream/' + s.id title = s.title dur = clock(s.duration) artist = s.artist album = s.album cover = s.coverId == null ? noPicture : thumbUrl(s.coverId) art = s.coverId == null ? noPicture : coverUrl(s.coverId) idx = index id = s.id }
  62. }
  63. // ctx.kind: songs (sort, minPlays) | artists | albums | artist-songs (artist, sort) | artist-albums (artist) | album (album) | search (q)
  64. static pageOfSongs = (ctx, offset, limit) => {
  65. let sort = ctx.sort == null ? 'artist' : ctx.sort
  66. if (ctx.kind == 'album') { return songsPage('artist', offset, limit, null, null, ctx.album) }
  67. if (ctx.kind == 'artist-songs') { return songsPage(sort, offset, limit, null, ctx.artist, null) }
  68. if (ctx.kind == 'search') { return searchAll(ctx.q, offset, limit).songs }
  69. return songsPage(sort, offset, limit, ctx.minPlays, null, null)
  70. }
  71. static rowsFor = (ctx, offset, limit) => {
  72. if (ctx.kind == 'artists') { return rowsOf(artistsPage(offset, limit), 'artist', null) }
  73. if (ctx.kind == 'albums') { return rowsOf(albumsPage(offset, limit, null), 'album', null) }
  74. if (ctx.kind == 'artist-albums') { return rowsOf(albumsPage(offset, limit, ctx.artist), 'album', null) }
  75. page = pageOfSongs(ctx, offset, limit)
  76. if (ctx.kind == 'album') {
  77. a = albumsTable.fetch(ctx.album)
  78. return rowsOf(page, 'albumSong', a != null && a.discs > 1 ? 'multi' : '')
  79. }
  80. return rowsOf(page, 'song', ctx.sort)
  81. }
  82. // where a song is in the list as it is NOW: the list is searched around the place the screen remembers (counted plays move songs a few places), then all of it.
  83. // Answers the place, or -1 when the song is no longer in the list.
  84. static placeOf = (ctx, index, id) => {
  85. let lo = index - 200
  86. if (lo < 0) { lo = 0 }
  87. let page = pageOfSongs(ctx, lo, 400)
  88. let i = 0
  89. while (i < page.items.length) {
  90. if (page.items[i].id == id) { return lo + i }
  91. i = i + 1
  92. }
  93. let off = 0
  94. let total = 1
  95. while (off < total) {
  96. page = pageOfSongs(ctx, off, 500)
  97. total = page.total
  98. if (page.items.length == 0) { break }
  99. let j = 0
  100. while (j < page.items.length) {
  101. if (page.items[j].id == id) { return off + j }
  102. j = j + 1
  103. }
  104. off = off + page.items.length
  105. }
  106. return -1
  107. }
  108. // the next `count` songs of the list a song was picked from, starting at its place: the player's queue.
  109. // A song is named by its id: `index` is only where the screen saw it, and plays counted since then may have moved it, so the song is looked up in the list as it is now;
  110. // a song that is no longer in the list still plays first, and the list goes on from the place it was seen at.
  111. // `step` moves the start from the id's place: 0 = the song itself, 1 = the song after it (Next), -1 = the song before it (Previous).
  112. static queueFor = (ctx, index, count, id, step) => {
  113. if (ctx.kind == 'shuffle') { return shuffleQueue(ctx, index, count) }
  114. let start = index
  115. out = []
  116. let by = step == null ? 0 : step
  117. if (id != null && id != '') {
  118. at = placeOf(ctx, index, id)
  119. if (at >= 0) { start = at + by }
  120. else if (by == 0) {
  121. gone = songsTable.fetch(id)
  122. if (gone != null) { out.push(queueItem(gone, index)) start = index + 1 }
  123. }
  124. else { start = index + by }
  125. }
  126. if (start < 0) { start = 0 }
  127. page = pageOfSongs(ctx, start, count)
  128. let i = start
  129. for (s of page.items) {
  130. out.push(queueItem(s, i))
  131. i = i + 1
  132. }
  133. return out
  134. }
  135. // the next songs of a shuffle: the server holds the order, the browser gets `count` items from `index` on
  136. static shuffleQueue = (ctx, index, count) => {
  137. out = []
  138. let i = index
  139. while (out.length < count) {
  140. id = idAt(ctx, i)
  141. if (id == null) { break }
  142. s = songsTable.fetch(id)
  143. if (s != null) { out.push(queueItem(s, i)) }
  144. i = i + 1
  145. }
  146. return out
  147. }
  148. // everything the search page shows next to the songs: the artists and the albums that match (at most 10 each)
  149. static searchGroups = (q) => {
  150. r = searchAll(q, 0, 100)
  151. artists = []
  152. for (a of r.artists) { artists.push(artistRow(a)) }
  153. albums = []
  154. for (a of r.albums) { albums.push(albumRow(a)) }
  155. return { artists = artists albums = albums songs = rowsOf(r.songs, 'song', null) }
  156. }
  157. // the head of an artist's or an album's screen: its name, its numbers and its picture
  158. static artistInfo = (id) => {
  159. a = artistsTable.fetch(id)
  160. if (a == null) { return { missing = true name = '' line = '' img = '' } }
  161. return { missing = false name = a.name line = thousands(a.albumCount) + (a.albumCount == 1 ? ' album · ' : ' albums · ') + thousands(a.songCount) + (a.songCount == 1 ? ' song' : ' songs') img = thumbUrl(a.coverId) songs = a.songCount albums = a.albumCount }
  162. }
  163. static albumInfo = (id) => {
  164. a = albumsTable.fetch(id)
  165. if (a == null) { return { missing = true name = '' artist = '' line = '' img = '' } }
  166. len = clock(a.duration)
  167. return { missing = false name = a.name artist = a.albumArtist line = (a.year == null ? '' : a.year + ' · ') + (a.discs > 1 ? a.discs + ' discs · ' : '') + thousands(a.songCount) + (a.songCount == 1 ? ' song' : ' songs') + (len == '' ? '' : ' · ' + len) img = coverUrl(a.coverId) songs = a.songCount }
  168. }

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