jukebox
All repositories: gitoria
9.1 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, poolSize } 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).// `again` (Repeat all): true = the queue never ends (a list starts again from its first song, a shuffle goes on with a new round); false = it ends with the list// (a shuffle after one round); null (callers that do not ask) = as before: a list ends, a shuffle goes on.static queueFor = (ctx, index, count, id, step, again) => {if (ctx.kind == 'shuffle') { return shuffleQueue(ctx, index, count, again) }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}if (again == true && out.length < count && out.length > 0) {for (w of wrapQueue(ctx, out[0].id, count - out.length)) { out.push(w) }}return out}// Repeat all: the end of the list is followed by its first songs, up to (not including) the song the queue starts with, so no song stands twice in one queuestatic wrapQueue = (ctx, startId, count) => {more = []page = pageOfSongs(ctx, 0, count)let i = 0for (s of page.items) {if (s.id == startId) { break }more.push(queueItem(s, i))i = i + 1}return more}// the next songs of a shuffle: the server holds the order, the browser gets `count` items from `index` onstatic shuffleQueue = (ctx, index, count, again) => {out = []let i = indexlet last = again == false ? poolSize(ctx) : -1while (out.length < count) {if (last >= 0 && i >= last) { break }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
- code-order
- day39
- day41
- day72
- mainmain branch
- reconnect-retry
- records-1119
- records-1123
- records-day39
- records-day41
- status-22-pr
Latest commits
- ac6f4c6fL2: plain declaration for a in pickedNextmre
- 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
- 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
- 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
- 04f5d9d5Merge branch 'day41' into mainJukebox
- 8ed6da45|||PR README: media-player pin df9e7dfmre
- d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
- 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
- 1fecd6a7Merge branch 'records-day39' into mainJukebox
- d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
- 5fa19e89Merge branch 'day39' into mainJukebox
- cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
- f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
- 9f5c6532Merge branch 'status-22-pr' into mainJukebox
- 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
- 29d2029cRecords: PR-only rule (Kunibert)mre
- adacb0abRecords: neutral ticket rule, #22 decision recordedmre
- b2d6f121Records: #308 hold liftedmre
- 7ab67533Records: day39 on hold (hybriel#308)mre
- 22f21739Records: note rebase before mergemre