jukebox
All repositories: gitoria
5.9 KB
// lib/rows.hl — WHAT THE SCREENS SHOW: the database's songs, artists and albums turned into the rows of the virtual list// (`{ id, title, sub, aside, img }`) and into the items of the player's queue. The one place that decides how a row reads.// A screen asks `rowsFor(ctx, offset, limit)` with its context: which list it shows. Pure reading, no writes.import { songsPage, artistsPage, albumsPage, searchAll } from './browse.hl'import { artistsTable, albumsTable } from './db.hl'// 3,966 → "3,966"static thousands = (n) => {s = '' + nlet out = ''let i = 0while (i < s.length) {if (i > 0 && (s.length - i) % 3 == 0) { out = out + ',' }out = out + s[i]i = i + 1}return out}// 221.4 seconds → "3:41"; 3725 seconds → "1:02:05"; unknown → ""static clock = (seconds) => {if (seconds == null || seconds <= 0) { return '' }whole = seconds - seconds % 1h = (whole - whole % 3600) / 3600m = ((whole - h * 3600) - (whole - h * 3600) % 60) / 60s = whole - h * 3600 - m * 60ss = (s < 10 ? '0' : '') + sif (h > 0) { return h + ':' + (m < 10 ? '0' : '') + m + ':' + ss }return m + ':' + ss}// the small picture of a cover (96 px, made once by lib/covers.hl) and the large onestatic thumbUrl = (coverId) => { return coverId == null ? '' : '/api/thumb/' + coverId }static coverUrl = (coverId) => { return coverId == null ? '' : '/api/cover/' + coverId }// 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)static noPicture = 'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%2F%3E'static songRow = (s, sort) => {return { id = s.id title = s.title sub = s.artist + ' · ' + s.album aside = sort == 'plays' ? s.playCount + '×' : clock(s.duration) img = thumbUrl(s.coverId) }}// inside one album the track number leads and the artist is left outstatic albumSongRow = (s) => {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 = '' }}static artistRow = (a) => {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 }}static albumRow = (a) => {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 }}// kind of row: 'song', 'albumSong', 'artist', 'album'static rowsOf = (page, kind, sort) => {out = []for (s of page.items) {if (kind == 'artist') { out.push(artistRow(s)) }else if (kind == 'album') { out.push(albumRow(s)) }else if (kind == 'albumSong') { out.push(albumSongRow(s)) }else { out.push(songRow(s, sort)) }}return { total = page.total offset = page.offset rows = out }}// the queue item of the player for one song: its stream, its title, its lengthstatic queueItem = (s, index) => {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 }}// ctx.kind: songs (sort, minPlays) | artists | albums | artist-songs (artist, sort) | artist-albums (artist) | album (album) | search (q)static pageOfSongs = (ctx, offset, limit) => {let sort = ctx.sort == null ? 'artist' : ctx.sortif (ctx.kind == 'album') { return songsPage('artist', offset, limit, null, null, ctx.album) }if (ctx.kind == 'artist-songs') { return songsPage(sort, offset, limit, null, ctx.artist, null) }if (ctx.kind == 'search') { return searchAll(ctx.q, offset, limit).songs }return songsPage(sort, offset, limit, ctx.minPlays, null, null)}static rowsFor = (ctx, offset, limit) => {if (ctx.kind == 'artists') { return rowsOf(artistsPage(offset, limit), 'artist', null) }if (ctx.kind == 'albums') { return rowsOf(albumsPage(offset, limit, null), 'album', null) }if (ctx.kind == 'artist-albums') { return rowsOf(albumsPage(offset, limit, ctx.artist), 'album', null) }page = pageOfSongs(ctx, offset, limit)if (ctx.kind == 'album') { return rowsOf(page, 'albumSong', null) }return rowsOf(page, 'song', ctx.sort)}// the next `count` songs of the list a song was picked from, starting at its place: the player's queuestatic queueFor = (ctx, index, count) => {page = pageOfSongs(ctx, index, count)out = []let i = indexfor (s of page.items) {out.push(queueItem(s, i))i = i + 1}return out}// everything the search page shows next to the songs: the artists and the albums that match (at most 10 each)static searchGroups = (q) => {r = searchAll(q, 0, 100)artists = []for (a of r.artists) { artists.push(artistRow(a)) }albums = []for (a of r.albums) { albums.push(albumRow(a)) }return { artists = artists albums = albums songs = rowsOf(r.songs, 'song', null) }}// the head of an artist's or an album's screen: its name, its numbers and its picturestatic artistInfo = (id) => {a = artistsTable.fetch(id)if (a == null) { return { missing = true name = '' line = '' img = '' } }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 }}static albumInfo = (id) => {a = albumsTable.fetch(id)if (a == null) { return { missing = true name = '' artist = '' line = '' img = '' } }len = clock(a.duration)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 }}
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