gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc5dbf2ecc5dbf2ecRecords: R2 resultmrec5dbf2ec/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

  • 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
  • 76639655Records: B1 questionmre
  • d7906b36Records: mission 005 startedmre
  • d3df773cMission 005: doc linemre
  • 6a5d840aMission 005: jukebox#22mre
  • 13e5e748Records: mission 004 startedmre
  • a8098271Mission 004: jukebox#21mre
  • df8f5205Records: jukebox#19-21, #11 closedmre
  • b7a4e692Records: hybriel#298mre
  • 9bcbb017Records: day38b waits for day39 fixmre
  • f80504e1Records: day38b repro sentmre
  • 8048258bRecords: day38b jb3 regression, L1 Chromes stoppedmre
  • 68aace7fRecords: mission 003 day38bmre
  • d105d8a9Records: mpackdb integrity check cleanmre