|
| 1 | +import Base from './Base.mjs'; |
| 2 | +import DomAccess from '../DomAccess.mjs'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @summary Main Thread Addon for High-Performance Grid Column Scroll Pinning (Synchronous Engine). |
| 6 | + * |
| 7 | + * This addon uses a Synchronous Scroll Listener architecture to prevent visual |
| 8 | + * column tearing during rapid horizontal scrolling. |
| 9 | + * |
| 10 | + * **The Synchronous Architecture:** |
| 11 | + * 1. **Scroll Driven:** A native `scroll` listener on the Grid Container triggers the logic. |
| 12 | + * 2. **Synchronous Injection:** The scroll event synchronously calculates `scrollLeft` and |
| 13 | + * immediately applies CSS custom properties (`--grid-locked-start-offset`, `--grid-locked-end-offset`) |
| 14 | + * to the root container node. This ensures the optical pinning is injected perfectly in phase |
| 15 | + * with the native scroll event, before the browser paints the next frame. |
| 16 | + * |
| 17 | + * @class Neo.main.addon.GridColumnScrollPinning |
| 18 | + * @extends Neo.main.addon.Base |
| 19 | + */ |
| 20 | +class GridColumnScrollPinning extends Base { |
| 21 | + static config = { |
| 22 | + /** |
| 23 | + * @member {String} className='Neo.main.addon.GridColumnScrollPinning' |
| 24 | + * @protected |
| 25 | + */ |
| 26 | + className: 'Neo.main.addon.GridColumnScrollPinning', |
| 27 | + /** |
| 28 | + * Remote method access for other workers |
| 29 | + * @member {Object} remote |
| 30 | + * @protected |
| 31 | + */ |
| 32 | + remote: { |
| 33 | + app: [ |
| 34 | + 'register', |
| 35 | + 'unregister' |
| 36 | + ] |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Stores state per registered Grid Container. |
| 42 | + * Shape: { id, containerId, containerNode } |
| 43 | + * @member {Map<String, Object>} registrations=new Map() |
| 44 | + * @protected |
| 45 | + */ |
| 46 | + registrations = new Map() |
| 47 | + |
| 48 | + /** |
| 49 | + * We bind the scroll handler once to avoid creating new functions per registration. |
| 50 | + * @member {Function} boundOnScroll |
| 51 | + * @protected |
| 52 | + */ |
| 53 | + boundOnScroll = this.onScroll.bind(this) |
| 54 | + |
| 55 | + /** |
| 56 | + * Re-calculates and applies pinning offsets based on the actual horizontal scroll position. |
| 57 | + * |
| 58 | + * @param {Object} state Registration state object |
| 59 | + * @protected |
| 60 | + */ |
| 61 | + applyPinning(state) { |
| 62 | + let node = state.containerNode, |
| 63 | + {clientWidth, scrollLeft, scrollWidth} = node; |
| 64 | + |
| 65 | + // Apply CSS variables to the root container node for inheritance |
| 66 | + node.style.setProperty('--grid-locked-start-offset', `${scrollLeft}px`); |
| 67 | + node.style.setProperty('--grid-locked-end-offset', `${scrollLeft - (scrollWidth - clientWidth)}px`) |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Triggered by native browser scroll. Synchronous execution. |
| 72 | + * @param {Event} event |
| 73 | + * @protected |
| 74 | + */ |
| 75 | + onScroll(event) { |
| 76 | + let me = this, |
| 77 | + containerNode = event.target, |
| 78 | + state; |
| 79 | + |
| 80 | + // Find which registration this scroll event belongs to based on the container node |
| 81 | + for (const reg of me.registrations.values()) { |
| 82 | + if (reg.containerNode === containerNode) { |
| 83 | + state = reg; |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (state) { |
| 89 | + me.applyPinning(state) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Registers a grid for column scroll pinning and attaches native scroll listener. |
| 95 | + * @param {Object} data |
| 96 | + * @param {String} data.containerId The ID of the grid container |
| 97 | + * @param {String} data.id Unique identifier for the registration (e.g. ScrollManager id) |
| 98 | + */ |
| 99 | + register({containerId, id}) { |
| 100 | + let me = this, |
| 101 | + containerNode = DomAccess.getElement(containerId); |
| 102 | + |
| 103 | + if (containerNode) { |
| 104 | + me.registrations.set(id, { |
| 105 | + id, |
| 106 | + containerId, |
| 107 | + containerNode |
| 108 | + }); |
| 109 | + |
| 110 | + containerNode.addEventListener('scroll', me.boundOnScroll, {passive: true}); |
| 111 | + |
| 112 | + // Apply initial state |
| 113 | + me.applyPinning(me.registrations.get(id)) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Unregisters a grid and cleans up listeners. |
| 119 | + * @param {Object} data |
| 120 | + * @param {String} data.id |
| 121 | + */ |
| 122 | + unregister({id}) { |
| 123 | + let me = this, |
| 124 | + state = me.registrations.get(id); |
| 125 | + |
| 126 | + if (state) { |
| 127 | + if (state.containerNode) { |
| 128 | + state.containerNode.removeEventListener('scroll', me.boundOnScroll) |
| 129 | + } |
| 130 | + |
| 131 | + me.registrations.delete(id) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +export default Neo.setupClass(GridColumnScrollPinning); |
0 commit comments