gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmaina06fc778Reset virtual-list window when the host changes resetKeyAtlasmain/components/virtual-list/virtual-list.hl

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

Branches

Latest commits

  • a06fc778Reset virtual-list window when the host changes resetKeyAtlas
  • 3aab598cRe-vendor complete Hybriel day73 set for virtual-list resetAtlas
  • 337ca075MD1 gate check measures frames in the page, not milliseconds from outside, and takes the middle load (components#30)mre
  • f5f131c5Record AP29 autoplay comparisons and passing gatesmre
  • 51c38187Gate media autoplay on first load and after client mountmre
  • a3f2223dRecord successful B44 deployment and live proofmre
  • 06c1e021Record passing B44 browser rerunmre
  • c9861533Record B44 component proof and browser gate blockermre
  • 1b7413beIgnore detached media events and space demo option outputsmre
  • ca3b4959Re-vendor complete Hybriel plugins and runtime to day44mre
  • df9e7df9Last-item checks run in the browser gate (components#26 fixed on day41); media-player loop is a plain Booleanmre
  • 895a5afcRe-vendor complete Hybriel plugins and runtime to day41mre
  • 16006bb3Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 88397fc3Re-vendor complete Hybriel plugins and runtime to day39mre
  • 9b872cedRecord IC28 deployment and live icon verificationmre
  • 46fefaa7Count the icons entry in the offline home-page checkmre
  • c7e14cabRecord IC28 rerun and unchanged-main baseline comparisonmre
  • 5e090ffdRecord IC28 review evidence and gate resultsmre
  • adfe2c4dAdd shuffle, repeat and repeatOne shared iconsmre
  • c96709c8media-player: skip = false and repeat = 'one' options (jukebox#19)mre