gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit502eeed5502eeed5jukebox#20: vendor virtual-list initial resetKey baseline fixmre502eeed5/components/virtual-list/virtual-list.hl

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

Branches

Latest commits

  • 502eeed5jukebox#20: vendor virtual-list initial resetKey baseline fixmre
  • 3b76651cjukebox#20: update virtual-list README pin and resetKey optionsmre
  • bc09e1e8|||PR jukebox#20: reset virtual lists through resetKey (partial validation)mre
  • 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