gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2a4cfc552a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre2a4cfc55/lib/rows.hl

5.9 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 } from './db.hl'
  6. // 3,966 → "3,966"
  7. static thousands = (n) => {
  8. s = '' + n
  9. let out = ''
  10. let i = 0
  11. while (i < s.length) {
  12. if (i > 0 && (s.length - i) % 3 == 0) { out = out + ',' }
  13. out = out + s[i]
  14. i = i + 1
  15. }
  16. return out
  17. }
  18. // 221.4 seconds → "3:41"; 3725 seconds → "1:02:05"; unknown → ""
  19. static clock = (seconds) => {
  20. if (seconds == null || seconds <= 0) { return '' }
  21. whole = seconds - seconds % 1
  22. h = (whole - whole % 3600) / 3600
  23. m = ((whole - h * 3600) - (whole - h * 3600) % 60) / 60
  24. s = whole - h * 3600 - m * 60
  25. ss = (s < 10 ? '0' : '') + s
  26. if (h > 0) { return h + ':' + (m < 10 ? '0' : '') + m + ':' + ss }
  27. return m + ':' + ss
  28. }
  29. // the small picture of a cover (96 px, made once by lib/covers.hl) and the large one
  30. static thumbUrl = (coverId) => { return coverId == null ? '' : '/api/thumb/' + coverId }
  31. static coverUrl = (coverId) => { return coverId == null ? '' : '/api/cover/' + coverId }
  32. // 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)
  33. static noPicture = 'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%2F%3E'
  34. static songRow = (s, sort) => {
  35. return { id = s.id title = s.title sub = s.artist + ' · ' + s.album aside = sort == 'plays' ? s.playCount + '×' : clock(s.duration) img = thumbUrl(s.coverId) }
  36. }
  37. // inside one album the track number leads and the artist is left out
  38. static albumSongRow = (s) => {
  39. return { id = s.id title = (s.track != null && s.track > 0 ? s.track + '. ' : '') + s.title sub = s.artist == s.albumArtist ? '' : s.artist aside = clock(s.duration) img = '' }
  40. }
  41. static artistRow = (a) => {
  42. 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 }
  43. }
  44. static albumRow = (a) => {
  45. 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 }
  46. }
  47. // kind of row: 'song', 'albumSong', 'artist', 'album'
  48. static rowsOf = (page, kind, sort) => {
  49. out = []
  50. for (s of page.items) {
  51. if (kind == 'artist') { out.push(artistRow(s)) }
  52. else if (kind == 'album') { out.push(albumRow(s)) }
  53. else if (kind == 'albumSong') { out.push(albumSongRow(s)) }
  54. else { out.push(songRow(s, sort)) }
  55. }
  56. return { total = page.total offset = page.offset rows = out }
  57. }
  58. // the queue item of the player for one song: its stream, its title, its length
  59. static queueItem = (s, index) => {
  60. return { src = '/stream/' + s.id title = s.title dur = clock(s.duration) artist = s.artist cover = s.coverId == null ? noPicture : thumbUrl(s.coverId) idx = index id = s.id }
  61. }
  62. // ctx.kind: songs (sort, minPlays) | artists | albums | artist-songs (artist, sort) | artist-albums (artist) | album (album) | search (q)
  63. static pageOfSongs = (ctx, offset, limit) => {
  64. let sort = ctx.sort == null ? 'artist' : ctx.sort
  65. if (ctx.kind == 'album') { return songsPage('artist', offset, limit, null, null, ctx.album) }
  66. if (ctx.kind == 'artist-songs') { return songsPage(sort, offset, limit, null, ctx.artist, null) }
  67. if (ctx.kind == 'search') { return searchAll(ctx.q, offset, limit).songs }
  68. return songsPage(sort, offset, limit, ctx.minPlays, null, null)
  69. }
  70. static rowsFor = (ctx, offset, limit) => {
  71. if (ctx.kind == 'artists') { return rowsOf(artistsPage(offset, limit), 'artist', null) }
  72. if (ctx.kind == 'albums') { return rowsOf(albumsPage(offset, limit, null), 'album', null) }
  73. if (ctx.kind == 'artist-albums') { return rowsOf(albumsPage(offset, limit, ctx.artist), 'album', null) }
  74. page = pageOfSongs(ctx, offset, limit)
  75. if (ctx.kind == 'album') { return rowsOf(page, 'albumSong', null) }
  76. return rowsOf(page, 'song', ctx.sort)
  77. }
  78. // the next `count` songs of the list a song was picked from, starting at its place: the player's queue
  79. static queueFor = (ctx, index, count) => {
  80. page = pageOfSongs(ctx, index, count)
  81. out = []
  82. let i = index
  83. for (s of page.items) {
  84. out.push(queueItem(s, i))
  85. i = i + 1
  86. }
  87. return out
  88. }
  89. // everything the search page shows next to the songs: the artists and the albums that match (at most 10 each)
  90. static searchGroups = (q) => {
  91. r = searchAll(q, 0, 100)
  92. artists = []
  93. for (a of r.artists) { artists.push(artistRow(a)) }
  94. albums = []
  95. for (a of r.albums) { albums.push(albumRow(a)) }
  96. return { artists = artists albums = albums songs = rowsOf(r.songs, 'song', null) }
  97. }
  98. // the head of an artist's or an album's screen: its name, its numbers and its picture
  99. static artistInfo = (id) => {
  100. a = artistsTable.fetch(id)
  101. if (a == null) { return { missing = true name = '' line = '' img = '' } }
  102. 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 }
  103. }
  104. static albumInfo = (id) => {
  105. a = albumsTable.fetch(id)
  106. if (a == null) { return { missing = true name = '' artist = '' line = '' img = '' } }
  107. len = clock(a.duration)
  108. return { missing = false name = a.name artist = a.albumArtist line = (a.year == null ? '' : a.year + ' · ') + thousands(a.songCount) + (a.songCount == 1 ? ' song' : ' songs') + (len == '' ? '' : ' · ' + len) img = coverUrl(a.coverId) songs = a.songCount }
  109. }

Branches

Latest commits

  • 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