gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/components/virtual-list/virtual-list.hl

10.7 KB

  1. // 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
  2. // 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
  3. // 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.
  4. // 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
  5. // `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
  6. // 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
  7. // 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),
  8. // 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)`.
  9. //
  10. // VirtualList { itemCount = 13449 rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId }
  11. //
  12. // Anything written inside the tag (`VirtualList { … h1 { "Title" } }`) stands above the rows and scrolls away with them.
  13. // 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
  14. // first row (after the host changed the sort order or the filter). Use one per page for the vlTop() document query.
  15. // Each composed list otherwise owns its state; parameters and bound callbacks connect it to its host.
  16. import Icon from '../icons/icons.hl'
  17. Number itemCount = 0
  18. Number rowHeight = 56
  19. Number overscan = 10
  20. Hybrid loaded = []
  21. Number loadedFrom = 0
  22. String selectedId = ''
  23. String centerId = ''
  24. Number scrollTop = null
  25. onScroll = null
  26. Boolean vlRestored = false
  27. Boolean vlScrollPending = false
  28. String listLabel = 'List'
  29. String emptyText = ''
  30. String thumbs = 'true'
  31. onRange = null
  32. onPick = null
  33. slot = null
  34. // what the scroll position says: the first row that exists in the page and how many exist
  35. Number vlFirst = 0
  36. Number vlCount = 30
  37. String spacerStyle = 'height:' + (itemCount * rowHeight) + 'px;--vl-row:' + rowHeight + 'px'
  38. String windowStyle = 'transform:translateY(' + (vlFirst * rowHeight) + 'px)'
  39. String rootClass = thumbs == 'true' ? 'vl-thumbs' : 'vl-plain'
  40. Boolean isEmpty = itemCount == 0 && emptyText != ''
  41. Hybrid shown = shownOf(loaded, loadedFrom, vlFirst, vlCount, itemCount, selectedId)
  42. // the rows that exist in the page: from the host's loaded rows, a blank row where the host has not delivered one yet
  43. shownOf(rows, from0, first0, count0, n, sel) {
  44. out = []
  45. let i = first0
  46. let end = first0 + count0
  47. if (end > n) { end = n }
  48. while (i < end) {
  49. let k = i - from0
  50. if (k >= 0 && k < rows.length) {
  51. r = rows[k]
  52. 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 })
  53. } else {
  54. out.push({ idx = '' + i id = '' title = '' sub = '' aside = '' img = '' hasImg = false cls = 'vl-row blank' pos = i + 1 })
  55. }
  56. i = i + 1
  57. }
  58. return out
  59. }
  60. Style {
  61. virtualList {
  62. display = 'block'
  63. position = 'relative'
  64. height = '100%'
  65. minHeight = '0'
  66. color = 'var(--color-text, #000)'
  67. // Class set by the View or row data; quoted compound selector.
  68. '.vl-scroller' {
  69. position = 'absolute'
  70. inset = '0'
  71. overflowY = 'auto'
  72. overflowX = 'hidden'
  73. overscrollBehavior = 'contain'
  74. animation = 'virtual-list-mount 100ms linear infinite'
  75. focusVisible {
  76. outline = '2px solid var(--color-accent, #555)'
  77. outlineOffset = '-2px'
  78. }
  79. }
  80. // Class set by the View or row data; quoted compound selector.
  81. '.vl-head' {
  82. display = 'block'
  83. empty {
  84. display = 'none'
  85. }
  86. }
  87. // Class set by the View or row data; quoted compound selector.
  88. '.vl-spacer' {
  89. position = 'relative'
  90. width = '100%'
  91. }
  92. // Class set by the View or row data; quoted compound selector.
  93. '.vl-window' {
  94. position = 'absolute'
  95. left = '0'
  96. right = '0'
  97. top = '0'
  98. willChange = 'transform'
  99. }
  100. // Class set by the View or row data; quoted compound selector.
  101. '.vl-row' {
  102. display = 'flex'
  103. alignItems = 'center'
  104. gap = '.7rem'
  105. boxSizing = 'border-box'
  106. width = '100%'
  107. height = 'var(--vl-row)'
  108. margin = '0'
  109. padding = '0 .6rem'
  110. font = 'inherit'
  111. fontWeight = '400'
  112. textAlign = 'left'
  113. color = 'var(--color-text, #000)'
  114. background = 'transparent'
  115. border = '0'
  116. borderRadius = 'var(--radius-small, 5px)'
  117. cursor = 'pointer'
  118. overflow = 'hidden'
  119. hover {
  120. background = 'var(--color-surface, #f2f2f2)'
  121. }
  122. focusVisible {
  123. outline = '2px solid var(--color-accent, #555)'
  124. outlineOffset = '-2px'
  125. }
  126. }
  127. // Class set by the View or row data; quoted compound selector.
  128. '.vl-row.current' {
  129. color = 'var(--color-accent, #000)'
  130. background = 'var(--color-surface, #f2f2f2)'
  131. // Class set by the View or row data; quoted compound selector.
  132. '.vl-thumb' {
  133. background = 'var(--color-background-sunken, #ccc)'
  134. }
  135. // Class set by the View or row data; quoted compound selector.
  136. '.vl-sub' {
  137. color = 'inherit'
  138. }
  139. // Class set by the View or row data; quoted compound selector.
  140. '.vl-aside' {
  141. color = 'inherit'
  142. }
  143. }
  144. // Class set by the View or row data; quoted compound selector.
  145. '.vl-row.blank' {
  146. cursor = 'default'
  147. pointerEvents = 'none'
  148. // Class set by the View or row data; quoted compound selector.
  149. '.vl-thumb' {
  150. background = 'transparent'
  151. }
  152. }
  153. // Class set by the View or row data; quoted compound selector.
  154. '.vl-thumb' {
  155. display = 'block'
  156. flex = 'none'
  157. width = 'calc(var(--vl-row) - 1rem)'
  158. height = 'calc(var(--vl-row) - 1rem)'
  159. borderRadius = 'var(--radius-small, 5px)'
  160. background = 'var(--color-surface, #ddd)'
  161. overflow = 'hidden'
  162. img {
  163. display = 'block'
  164. width = '100%'
  165. height = '100%'
  166. objectFit = 'cover'
  167. }
  168. }
  169. // Class set by the View or row data; quoted compound selector.
  170. '.vl-text' {
  171. display = 'flex'
  172. flexDirection = 'column'
  173. justifyContent = 'center'
  174. flex = '1 1 auto'
  175. minWidth = '0'
  176. }
  177. // Class set by the View or row data; quoted compound selector.
  178. '.vl-title' {
  179. display = 'block'
  180. overflow = 'hidden'
  181. textOverflow = 'ellipsis'
  182. whiteSpace = 'nowrap'
  183. lineHeight = '1.3'
  184. }
  185. // Class set by the View or row data; quoted compound selector.
  186. '.vl-sub' {
  187. display = 'block'
  188. overflow = 'hidden'
  189. textOverflow = 'ellipsis'
  190. whiteSpace = 'nowrap'
  191. lineHeight = '1.3'
  192. fontSize = '.85rem'
  193. color = 'var(--color-text-muted, #555)'
  194. }
  195. // Class set by the View or row data; quoted compound selector.
  196. '.vl-aside' {
  197. display = 'block'
  198. flex = 'none'
  199. fontSize = '.85rem'
  200. fontVariantNumeric = 'tabular-nums'
  201. color = 'var(--color-text-muted, #555)'
  202. }
  203. // Class set by the View or row data; quoted compound selector.
  204. '.vl-empty' {
  205. margin = '0'
  206. padding = '1rem .6rem'
  207. color = 'var(--color-text-muted, #555)'
  208. }
  209. }
  210. '@keyframes virtual-list-mount' {
  211. 'from' {
  212. opacity = '.999'
  213. }
  214. to {
  215. opacity = '1'
  216. }
  217. }
  218. // Class set by the View or row data; quoted compound selector.
  219. 'virtual-list.vl-plain' {
  220. // Class set by the View or row data; quoted compound selector.
  221. '.vl-thumb' {
  222. display = 'none'
  223. }
  224. }
  225. }
  226. View {
  227. virtualList { class = rootClass
  228. div { class = "vl-scroller" role = "list" tabindex = "0" 'aria-label' = listLabel 'aria-rowcount' = itemCount
  229. on scroll(e) { emit vlScroll(e) } on animationiteration(e) { emit vlMount(e) }
  230. div { class = "vl-head" slot }
  231. div { class = "vl-spacer" style = spacerStyle
  232. div { class = "vl-window" style = windowStyle
  233. for (r of shown) {
  234. 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.title
  235. span { class = "vl-thumb" if (r.hasImg) { img { src = r.img alt = "" loading = "lazy" decoding = "async" } } }
  236. span { class = "vl-text"
  237. span { class = "vl-title" r.title }
  238. span { class = "vl-sub" r.sub }
  239. }
  240. span { class = "vl-aside" r.aside }
  241. on click(e) { emit vlPick(e) }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. if (isEmpty) { p { class = "vl-empty" emptyText } }
  248. }
  249. }
  250. // ---- the scroll position -> the rows that exist ------------------------------------------------------------------------------
  251. // look at the scroll box: which rows should exist, and ask the host for them when it has not delivered all
  252. vlSync(box) {
  253. // what the host put in front of the rows (a title, a picture) scrolls away with them: the rows start below it
  254. head = box.querySelector('.vl-head')
  255. let top = head == null ? box.scrollTop : box.scrollTop - head.offsetHeight
  256. if (top < 0) { top = 0 }
  257. idx = (top - top % rowHeight) / rowHeight
  258. let first = idx - overscan
  259. if (first < 0) { first = 0 }
  260. fit = box.clientHeight > 0 ? box.clientHeight : window.innerHeight
  261. count = (fit - fit % rowHeight) / rowHeight + 2 + 2 * overscan
  262. if (first != vlFirst) { vlFirst = first }
  263. if (count != vlCount) { vlCount = count }
  264. let last = first + count
  265. if (last > itemCount) { last = itemCount }
  266. if (onRange != null && itemCount > 0 && (first < loadedFrom || last > loadedFrom + loaded.length)) { onRange(first, last - first) }
  267. }
  268. on vlScroll(e) {
  269. vlSync(e.target)
  270. if (!vlScrollPending && onScroll != null) {
  271. vlScrollPending = true
  272. box = e.target
  273. window.setTimeout(() => {
  274. vlScrollPending = false
  275. onScroll(box.scrollTop)
  276. }, 100)
  277. }
  278. return null
  279. }
  280. // the first tick of the mount animation: the page is live, read the real size and position, then stop the animation
  281. on vlMount(e) {
  282. e.target.style.animationName = 'none'
  283. if (!vlRestored) {
  284. vlRestored = true
  285. box = e.target
  286. if (scrollTop != null) { box.scrollTop = scrollTop }
  287. if (centerId != '') {
  288. let i = 0
  289. for (row of loaded) {
  290. if (row.id == centerId) {
  291. head = box.querySelector('.vl-head')
  292. height = head == null ? 0 : head.offsetHeight
  293. box.scrollTop = height + (loadedFrom + i) * rowHeight + rowHeight / 2 - box.clientHeight / 2
  294. }
  295. i = i + 1
  296. }
  297. }
  298. }
  299. vlSync(e.target)
  300. return null
  301. }
  302. // the host changed the order or the filter: back to the first row
  303. on vlTop() {
  304. box = document.querySelector('virtual-list .vl-scroller')
  305. if (box != null) { box.scrollTop = 0 vlSync(box) }
  306. return null
  307. }
  308. on vlPick(e) {
  309. b = e.target.closest('button')
  310. if (b == null) { return null }
  311. id = b.getAttribute('data-id')
  312. if (id == '' || onPick == null) { return null }
  313. onPick(id, toNumber(b.getAttribute('data-index')))
  314. return null
  315. }

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