jukebox
All repositories: gitoria
10.7 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 for the vlTop() document query.// Each composed list otherwise owns its state; parameters and bound callbacks connect it to its host.import Icon from '../icons/icons.hl'Number itemCount = 0Number rowHeight = 56Number overscan = 10Hybrid loaded = []Number loadedFrom = 0String selectedId = ''String centerId = ''Number scrollTop = nullonScroll = nullBoolean vlRestored = falseBoolean vlScrollPending = falseString 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 = 30String spacerStyle = 'height:' + (itemCount * rowHeight) + 'px;--vl-row:' + rowHeight + 'px'String windowStyle = 'transform:translateY(' + (vlFirst * rowHeight) + 'px)'String rootClass = thumbs == 'true' ? 'vl-thumbs' : 'vl-plain'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 {virtualList {display = 'block'position = 'relative'height = '100%'minHeight = '0'color = 'var(--color-text, #000)'// Class set by the View or row data; quoted compound selector.'.vl-scroller' {position = 'absolute'inset = '0'overflowY = 'auto'overflowX = 'hidden'overscrollBehavior = 'contain'animation = 'virtual-list-mount 100ms linear infinite'focusVisible {outline = '2px solid var(--color-accent, #555)'outlineOffset = '-2px'}}// Class set by the View or row data; quoted compound selector.'.vl-head' {display = 'block'empty {display = 'none'}}// Class set by the View or row data; quoted compound selector.'.vl-spacer' {position = 'relative'width = '100%'}// Class set by the View or row data; quoted compound selector.'.vl-window' {position = 'absolute'left = '0'right = '0'top = '0'willChange = 'transform'}// Class set by the View or row data; quoted compound selector.'.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'hover {background = 'var(--color-surface, #f2f2f2)'}focusVisible {outline = '2px solid var(--color-accent, #555)'outlineOffset = '-2px'}}// Class set by the View or row data; quoted compound selector.'.vl-row.current' {color = 'var(--color-accent, #000)'background = 'var(--color-surface, #f2f2f2)'// Class set by the View or row data; quoted compound selector.'.vl-thumb' {background = 'var(--color-background-sunken, #ccc)'}// Class set by the View or row data; quoted compound selector.'.vl-sub' {color = 'inherit'}// Class set by the View or row data; quoted compound selector.'.vl-aside' {color = 'inherit'}}// Class set by the View or row data; quoted compound selector.'.vl-row.blank' {cursor = 'default'pointerEvents = 'none'// Class set by the View or row data; quoted compound selector.'.vl-thumb' {background = 'transparent'}}// Class set by the View or row data; quoted compound selector.'.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'img {display = 'block'width = '100%'height = '100%'objectFit = 'cover'}}// Class set by the View or row data; quoted compound selector.'.vl-text' {display = 'flex'flexDirection = 'column'justifyContent = 'center'flex = '1 1 auto'minWidth = '0'}// Class set by the View or row data; quoted compound selector.'.vl-title' {display = 'block'overflow = 'hidden'textOverflow = 'ellipsis'whiteSpace = 'nowrap'lineHeight = '1.3'}// Class set by the View or row data; quoted compound selector.'.vl-sub' {display = 'block'overflow = 'hidden'textOverflow = 'ellipsis'whiteSpace = 'nowrap'lineHeight = '1.3'fontSize = '.85rem'color = 'var(--color-text-muted, #555)'}// Class set by the View or row data; quoted compound selector.'.vl-aside' {display = 'block'flex = 'none'fontSize = '.85rem'fontVariantNumeric = 'tabular-nums'color = 'var(--color-text-muted, #555)'}// Class set by the View or row data; quoted compound selector.'.vl-empty' {margin = '0'padding = '1rem .6rem'color = 'var(--color-text-muted, #555)'}}'@keyframes virtual-list-mount' {'from' {opacity = '.999'}to {opacity = '1'}}// Class set by the View or row data; quoted compound selector.'virtual-list.vl-plain' {// Class set by the View or row data; quoted compound selector.'.vl-thumb' {display = 'none'}}}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) }}}}}}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 * overscanif (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)if (!vlScrollPending && onScroll != null) {vlScrollPending = truebox = e.targetwindow.setTimeout(() => {vlScrollPending = falseonScroll(box.scrollTop)}, 100)}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'if (!vlRestored) {vlRestored = truebox = e.targetif (scrollTop != null) { box.scrollTop = scrollTop }if (centerId != '') {let i = 0for (row of loaded) {if (row.id == centerId) {head = box.querySelector('.vl-head')height = head == null ? 0 : head.offsetHeightbox.scrollTop = height + (loadedFrom + i) * rowHeight + rowHeight / 2 - box.clientHeight / 2}i = i + 1}}}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}
Branches
Latest commits
- 10f8a8c8Merge branch 'code-order' into mainJukebox
- 204fc828Merge branch 'day72' into mainJukebox
- 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
- 33072bb5|||PR Record day72 gates and Chrome proofmre
- 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
- 6422a020Sync media-player at 51c3818mre
- b582ba1cVendor complete Hybriel day72 plugin setmre
- 02dccbc9Merge branch 'records-1119' into mainJukebox
- a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
- fc7db075Merge branch 'reconnect-retry' into mainJukebox
- 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
- 1f0cd77bMerge branch 'records-day41' into mainJukebox
- 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
- 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
- 04f5d9d5Merge branch 'day41' into mainJukebox
- 8ed6da45|||PR README: media-player pin df9e7dfmre
- d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre