jukebox
All repositories: gitoria
8.2 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, songsTable } from './db.hl'import { idAt } from './queue.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 + (s.copies > 1 ? ' · ' + s.copies + ' copies' : '') 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, multi) => {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 = '' }}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, sort == 'multi')) }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 album = s.album cover = s.coverId == null ? noPicture : thumbUrl(s.coverId) art = s.coverId == null ? noPicture : coverUrl(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') {a = albumsTable.fetch(ctx.album)return rowsOf(page, 'albumSong', a != null && a.discs > 1 ? 'multi' : '')}return rowsOf(page, 'song', ctx.sort)}// 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.// Answers the place, or -1 when the song is no longer in the list.static placeOf = (ctx, index, id) => {let lo = index - 200if (lo < 0) { lo = 0 }let page = pageOfSongs(ctx, lo, 400)let i = 0while (i < page.items.length) {if (page.items[i].id == id) { return lo + i }i = i + 1}let off = 0let total = 1while (off < total) {page = pageOfSongs(ctx, off, 500)total = page.totalif (page.items.length == 0) { break }let j = 0while (j < page.items.length) {if (page.items[j].id == id) { return off + j }j = j + 1}off = off + page.items.length}return -1}// the next `count` songs of the list a song was picked from, starting at its place: the player's queue.// 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;// a song that is no longer in the list still plays first, and the list goes on from the place it was seen at.// `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).static queueFor = (ctx, index, count, id, step) => {if (ctx.kind == 'shuffle') { return shuffleQueue(ctx, index, count) }let start = indexout = []let by = step == null ? 0 : stepif (id != null && id != '') {at = placeOf(ctx, index, id)if (at >= 0) { start = at + by }else if (by == 0) {gone = songsTable.fetch(id)if (gone != null) { out.push(queueItem(gone, index)) start = index + 1 }}else { start = index + by }}if (start < 0) { start = 0 }page = pageOfSongs(ctx, start, count)let i = startfor (s of page.items) {out.push(queueItem(s, i))i = i + 1}return out}// the next songs of a shuffle: the server holds the order, the browser gets `count` items from `index` onstatic shuffleQueue = (ctx, index, count) => {out = []let i = indexwhile (out.length < count) {id = idAt(ctx, i)if (id == null) { break }s = songsTable.fetch(id)if (s != null) { 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 + ' · ') + (a.discs > 1 ? a.discs + ' discs · ' : '') + thousands(a.songCount) + (a.songCount == 1 ? ' song' : ' songs') + (len == '' ? '' : ' · ' + len) img = coverUrl(a.coverId) songs = a.songCount }}
Branches
Latest commits
- 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
- 6e980c53Records: mpackdb rule refinementmre
- 6d335ac4Records: storage copy rulingmre
- 7eb2898dRecords: mpackdb second-opener rule until day38mre
- b011f98eRecords: style form live on day35mre
- 60d90cb2Records: L1 woken for the restartmre