jukebox
All repositories: gitoria
12.9 KB
// virtual-list.hl — THE VIRTUAL LIST: a very long list (tens of thousands of rows) that scrolls smoothly on a phone because only the rows// near the viewport exist in the page. Every row has the same fixed height, so the list knows where row N is without measuring: a spacer// as tall as the whole list (`itemCount * rowHeight`) makes the scroll bar honest, and the few rows that are near the viewport are placed in it.// The component does not fetch anything. The HOST owns the data: it hands in the rows it has loaded (`loaded`, the first of them is row number// `loadedFrom`), and the component calls the host's `onRange(first, count)` when the rows near the viewport are not all there yet; the host loads// them (usually page by page from its server) and hands in the new `loaded` / `loadedFrom`. Until they arrive the missing rows are blank rows of the same// height, so the list never jumps. A row is `{ id, title, sub, aside, img }`: a thumbnail (`img`, loaded lazily; a plain grey square when it is empty),// the title with a second line under it, and a short text at the right (a play count, a length). A tap calls `onPick(id, index)`.//// VirtualList { itemCount = 13449 rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId }//// Anything written inside the tag (`VirtualList { … h1 { "Title" } }`) stands above the rows and scrolls away with them.// The host gives the list a height (it scrolls inside its own box): `virtual-list { height: 70dvh }` or a flex child. `emit vlTop()` scrolls back to the// first row (after the host changed the sort order or the filter). USE ONE PER PAGE: a composed component's members are the page's state, so the// public names below are chosen to stay clear of a host's own (no `rows`, `total`, `current`, `offset`).import Icon from '../icons/icons.hl'Number itemCount = 0Number rowHeight = 56Number overscan = 10Hybrid loaded = []Number loadedFrom = 0String selectedId = ''String listLabel = 'List'String emptyText = ''String thumbs = 'true'onRange = nullonPick = nullslot = null// what the scroll position says: the first row that exists in the page and how many existNumber vlFirst = 0Number vlCount = 30// the finger handle (touch screens only, see the Style): its height, where it sits (px from the top of the list box), whether the list scrolls at all,// and while a finger holds it: how far below the handle's top edge the finger grabbed itNumber vlHandleH = 56Number vlThumbTop = 0String vlScrolls = 'false'Number vlGrab = 0String vlDragging = 'false'String spacerStyle = 'height:' + (itemCount * rowHeight) + 'px;--vl-row:' + rowHeight + 'px'String windowStyle = 'transform:translateY(' + (vlFirst * rowHeight) + 'px)'String rootClass = thumbs == 'true' ? 'vl-thumbs' : 'vl-plain'String handleStyle = 'transform:translateY(' + vlThumbTop + 'px);height:' + vlHandleH + 'px;visibility:' + (vlScrolls == 'true' ? 'visible' : 'hidden')Boolean isEmpty = itemCount == 0 && emptyText != ''Hybrid shown = shownOf(loaded, loadedFrom, vlFirst, vlCount, itemCount, selectedId)// the rows that exist in the page: from the host's loaded rows, a blank row where the host has not delivered one yetshownOf(rows, from0, first0, count0, n, sel) {out = []let i = first0let end = first0 + count0if (end > n) { end = n }while (i < end) {let k = i - from0if (k >= 0 && k < rows.length) {r = rows[k]out.push({ idx = '' + i id = r.id title = r.title sub = r.sub aside = r.aside img = r.img hasImg = r.img != '' cls = r.id == sel ? 'vl-row current' : 'vl-row' pos = i + 1 })} else {out.push({ idx = '' + i id = '' title = '' sub = '' aside = '' img = '' hasImg = false cls = 'vl-row blank' pos = i + 1 })}i = i + 1}return out}Style {'virtual-list' { display = 'block' position = 'relative' height = '100%' minHeight = '0' color = 'var(--color-text, #000)' }// the box that scrolls (the host gives virtual-list its height); the mount tick (the code editor's trick) reads the real size once the page is live'@keyframes virtual-list-mount' { 'from' { opacity = '.999' } to { opacity = '1' } }'virtual-list .vl-scroller' { position = 'absolute' inset = '0' overflowY = 'auto' overflowX = 'hidden' overscrollBehavior = 'contain' animation = 'virtual-list-mount 100ms linear infinite' }'virtual-list .vl-scroller:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-2px' }'virtual-list .vl-head' { display = 'block' }'virtual-list .vl-head:empty' { display = 'none' }'virtual-list .vl-spacer' { position = 'relative' width = '100%' }'virtual-list .vl-window' { position = 'absolute' left = '0' right = '0' top = '0' willChange = 'transform' }// a row: exactly rowHeight tall (the tap area is the row), thumbnail · title over sub · aside; no frames, no gaps between rows'virtual-list .vl-row' { display = 'flex' alignItems = 'center' gap = '.7rem' boxSizing = 'border-box' width = '100%' height = 'var(--vl-row)' margin = '0' padding = '0 .6rem' font = 'inherit' fontWeight = '400' textAlign = 'left' color = 'var(--color-text, #000)' background = 'transparent' border = '0' borderRadius = 'var(--radius-small, 5px)' cursor = 'pointer' overflow = 'hidden' }'virtual-list .vl-row:hover' { background = 'var(--color-surface, #f2f2f2)' }'virtual-list .vl-row:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-2px' }'virtual-list .vl-row.current' { color = 'var(--color-accent, #000)' background = 'var(--color-surface, #f2f2f2)' }'virtual-list .vl-row.blank' { cursor = 'default' pointerEvents = 'none' }'virtual-list .vl-thumb' { display = 'block' flex = 'none' width = 'calc(var(--vl-row) - 1rem)' height = 'calc(var(--vl-row) - 1rem)' borderRadius = 'var(--radius-small, 5px)' background = 'var(--color-surface, #ddd)' overflow = 'hidden' }// on the marked row the grey of the empty thumbnail would be the row's own surface: it takes the darker sunken colour'virtual-list .vl-row.current .vl-thumb' { background = 'var(--color-background-sunken, #ccc)' }'virtual-list .vl-row.blank .vl-thumb' { background = 'transparent' }'virtual-list .vl-thumb img' { display = 'block' width = '100%' height = '100%' objectFit = 'cover' }'virtual-list.vl-plain .vl-thumb' { display = 'none' }'virtual-list .vl-text' { display = 'flex' flexDirection = 'column' justifyContent = 'center' flex = '1 1 auto' minWidth = '0' }'virtual-list .vl-title' { display = 'block' overflow = 'hidden' textOverflow = 'ellipsis' whiteSpace = 'nowrap' lineHeight = '1.3' }'virtual-list .vl-sub' { display = 'block' overflow = 'hidden' textOverflow = 'ellipsis' whiteSpace = 'nowrap' lineHeight = '1.3' fontSize = '.85rem' color = 'var(--color-text-muted, #555)' }'virtual-list .vl-row.current .vl-sub' { color = 'inherit' }'virtual-list .vl-aside' { display = 'block' flex = 'none' fontSize = '.85rem' fontVariantNumeric = 'tabular-nums' color = 'var(--color-text-muted, #555)' }'virtual-list .vl-row.current .vl-aside' { color = 'inherit' }// THE FINGER HANDLE (jukebox#12): on a touch screen (pointer: coarse) the browser's thin scroll bar cannot be grabbed, so the list draws its own handle at the right edge.// Its tap area is 44 px wide and 56 px tall; what is drawn stays slim (the transparent left border carries the width, background-clip keeps the colour inside the padding box).// A mouse and a keyboard never see it (display none): the desktop look is the browser's own scroll bar as before.vlRail { display = 'none' position = 'absolute' top = '0' bottom = '0' right = '0' width = '44px' pointerEvents = 'none'vlHandle { display = 'block' position = 'absolute' top = '0' right = '0' boxSizing = 'border-box' width = '44px' pointerEvents = 'auto' touchAction = 'none' cursor = 'grab' border = '0 solid transparent' borderLeftWidth = '32px' borderRightWidth = '3px' borderRadius = '9px' background = 'var(--color-text-muted, #888)' backgroundClip = 'padding-box' opacity = '.75'active { opacity = '1' }}}'@media' = { pointer = 'coarse'vlRail { display = 'block' }// quoted: the scroller is a div with a class (the host's gates and the mount tick find it by `.vl-scroller`), nothing else can name it.// On a touch screen the native bar gives way to the handle and the rows keep clear of the handle's drawn strip.'.vl-scroller' { scrollbarWidth = 'none' paddingRight = '14px' boxSizing = 'border-box' }}'virtual-list .vl-empty' { margin = '0' padding = '1rem .6rem' color = 'var(--color-text-muted, #555)' }}View {virtualList { class = rootClassdiv { class = "vl-scroller" role = "list" tabindex = "0" 'aria-label' = listLabel 'aria-rowcount' = itemCounton scroll(e) { emit vlScroll(e) } on animationiteration(e) { emit vlMount(e) }div { class = "vl-head" slot }div { class = "vl-spacer" style = spacerStylediv { class = "vl-window" style = windowStylefor (r of shown) {button { type = "button" class = r.cls role = "listitem" 'data-index' = r.idx 'data-id' = r.id 'aria-posinset' = r.pos 'aria-setsize' = itemCount title = r.titlespan { class = "vl-thumb" if (r.hasImg) { img { src = r.img alt = "" loading = "lazy" decoding = "async" } } }span { class = "vl-text"span { class = "vl-title" r.title }span { class = "vl-sub" r.sub }}span { class = "vl-aside" r.aside }on click(e) { emit vlPick(e) }}}}}}vlRail { vlHandle { role = "scrollbar" 'aria-orientation' = "vertical" 'aria-label' = "Drag to scroll the list" style = handleStyleon pointerdown(e) { emit vlGrabDown(e) } on pointermove(e) { emit vlGrabMove(e) } on pointerup(e) { emit vlGrabUp(e) } on pointercancel(e) { emit vlGrabUp(e) }} }if (isEmpty) { p { class = "vl-empty" emptyText } }}}// ---- the scroll position -> the rows that exist ------------------------------------------------------------------------------// look at the scroll box: which rows should exist, and ask the host for them when it has not delivered allvlSync(box) {// what the host put in front of the rows (a title, a picture) scrolls away with them: the rows start below ithead = box.querySelector('.vl-head')let top = head == null ? box.scrollTop : box.scrollTop - head.offsetHeightif (top < 0) { top = 0 }idx = (top - top % rowHeight) / rowHeightlet first = idx - overscanif (first < 0) { first = 0 }fit = box.clientHeight > 0 ? box.clientHeight : window.innerHeightcount = (fit - fit % rowHeight) / rowHeight + 2 + 2 * overscan// the handle shows where the list is: the share of the scrolling distance already scrolled, spread over the free room of the boxmaxScroll = box.scrollHeight - box.clientHeightlet room = box.clientHeight - vlHandleHif (room < 0) { room = 0 }let at = maxScroll > 0 ? box.scrollTop / maxScroll * room : 0at = at - at % 1if (at != vlThumbTop) { vlThumbTop = at }sc = maxScroll > 0 ? 'true' : 'false'if (sc != vlScrolls) { vlScrolls = sc }if (first != vlFirst) { vlFirst = first }if (count != vlCount) { vlCount = count }let last = first + countif (last > itemCount) { last = itemCount }if (onRange != null && itemCount > 0 && (first < loadedFrom || last > loadedFrom + loaded.length)) { onRange(first, last - first) }}on vlScroll(e) {vlSync(e.target)return null}// the first tick of the mount animation: the page is live, read the real size and position, then stop the animationon vlMount(e) {e.target.style.animationName = 'none'vlSync(e.target)return null}// the host changed the order or the filter: back to the first rowon vlTop() {box = document.querySelector('virtual-list .vl-scroller')if (box != null) { box.scrollTop = 0 vlSync(box) }return null}on vlPick(e) {b = e.target.closest('button')if (b == null) { return null }id = b.getAttribute('data-id')if (id == '' || onPick == null) { return null }onPick(id, toNumber(b.getAttribute('data-index')))return null}// ---- the finger handle: drag it and the list follows proportionally -----------------------------------------------------------// the list box that belongs to a handleboxOfHandle(h) {return h.closest('virtual-list').querySelector('.vl-scroller')}// scroll the box so the handle's top edge would be at y (a pointer y minus the grab offset), in the whole distance the list hasvlGoTo(h, y) {box = boxOfHandle(h)r = box.getBoundingClientRect()room = box.clientHeight - vlHandleHif (room <= 0) { return null }let share = (y - r.top) / roomif (share < 0) { share = 0 }if (share > 1) { share = 1 }box.scrollTop = share * (box.scrollHeight - box.clientHeight)return null}on vlGrabDown(e) {h = e.currentTargeth.setPointerCapture(e.pointerId)vlDragging = 'true'vlGrab = e.clientY - h.getBoundingClientRect().tope.preventDefault()return null}on vlGrabMove(e) {if (vlDragging != 'true') { return null }vlGoTo(e.currentTarget, e.clientY - vlGrab)return null}on vlGrabUp(e) {vlDragging = 'false'return null}
Branches
Latest commits
- b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
- 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
- 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
- 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
- c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
- 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
- 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
- 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