5
5
*/
6
6
import { animationFrame , microTask , timeOut } from '@vaadin/component-base/src/async.js' ;
7
7
import { Debouncer } from '@vaadin/component-base/src/debounce.js' ;
8
+ import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js' ;
8
9
9
10
const timeouts = {
10
11
SCROLLING : 500
@@ -14,7 +15,7 @@ const timeouts = {
14
15
* @polymerMixin
15
16
*/
16
17
export const ScrollMixin = ( superClass ) =>
17
- class ScrollMixin extends superClass {
18
+ class ScrollMixin extends ResizeMixin ( superClass ) {
18
19
static get properties ( ) {
19
20
return {
20
21
/**
@@ -26,6 +27,15 @@ export const ScrollMixin = (superClass) =>
26
27
value : ( ) => [ ]
27
28
} ,
28
29
30
+ /**
31
+ * Cached array of cells frozen to end
32
+ * @private
33
+ */
34
+ _frozenToEndCells : {
35
+ type : Array ,
36
+ value : ( ) => [ ]
37
+ } ,
38
+
29
39
/** @private */
30
40
_rowWithFocusedElement : Element
31
41
} ;
@@ -67,6 +77,15 @@ export const ScrollMixin = (superClass) =>
67
77
this . $ . table . addEventListener ( 'scroll' , ( ) => this . _afterScroll ( ) ) ;
68
78
}
69
79
80
+ /**
81
+ * @protected
82
+ * @override
83
+ */
84
+ _onResize ( ) {
85
+ this . _updateOverflow ( ) ;
86
+ this . __updateHorizontalScrollPosition ( ) ;
87
+ }
88
+
70
89
/**
71
90
* Scroll to a specific row index in the virtual list. Note that the row index is
72
91
* not always the same for any particular item. For example, sorting/filtering/expanding
@@ -177,13 +196,14 @@ export const ScrollMixin = (superClass) =>
177
196
cell . style . transform = '' ;
178
197
} ) ;
179
198
this . _frozenCells = Array . prototype . slice . call ( this . $ . table . querySelectorAll ( '[frozen]' ) ) ;
199
+ this . _frozenToEndCells = Array . prototype . slice . call ( this . $ . table . querySelectorAll ( '[frozen-to-end]' ) ) ;
180
200
this . __updateHorizontalScrollPosition ( ) ;
181
201
} ) ;
182
- this . _updateLastFrozen ( ) ;
202
+ this . _updateFrozenColumn ( ) ;
183
203
}
184
204
185
205
/** @protected */
186
- _updateLastFrozen ( ) {
206
+ _updateFrozenColumn ( ) {
187
207
if ( ! this . _columnTree ) {
188
208
return ;
189
209
}
@@ -192,26 +212,58 @@ export const ScrollMixin = (superClass) =>
192
212
columnsRow . sort ( ( a , b ) => {
193
213
return a . _order - b . _order ;
194
214
} ) ;
195
- const lastFrozen = columnsRow . reduce ( ( prev , col , index ) => {
215
+
216
+ let lastFrozen ;
217
+ let firstFrozenToEnd ;
218
+
219
+ // Use for loop to only iterate columns once
220
+ for ( let i = 0 ; i < columnsRow . length ; i ++ ) {
221
+ const col = columnsRow [ i ] ;
222
+
196
223
col . _lastFrozen = false ;
197
- return col . frozen && ! col . hidden ? index : prev ;
198
- } , undefined ) ;
224
+ col . _firstFrozenToEnd = false ;
225
+
226
+ if ( firstFrozenToEnd === undefined && col . frozenToEnd && ! col . hidden ) {
227
+ firstFrozenToEnd = i ;
228
+ }
229
+
230
+ if ( col . frozen && ! col . hidden ) {
231
+ lastFrozen = i ;
232
+ }
233
+ }
234
+
199
235
if ( lastFrozen !== undefined ) {
200
236
columnsRow [ lastFrozen ] . _lastFrozen = true ;
201
237
}
238
+
239
+ if ( firstFrozenToEnd !== undefined ) {
240
+ columnsRow [ firstFrozenToEnd ] . _firstFrozenToEnd = true ;
241
+ }
202
242
}
203
243
204
244
/** @private */
205
245
__updateHorizontalScrollPosition ( ) {
246
+ const scrollWidth = this . $ . table . scrollWidth ;
247
+ const clientWidth = this . $ . table . clientWidth ;
248
+ const scrollLeft = this . __getNormalizedScrollLeft ( this . $ . table ) ;
249
+
250
+ // Position cells frozen to end
251
+ const remaining = scrollLeft + clientWidth - scrollWidth ;
252
+
253
+ this . $ . table . style . setProperty ( '--_grid-horizontal-scroll-remaining' , remaining + 'px' ) ;
206
254
this . $ . table . style . setProperty ( '--_grid-horizontal-scroll-position' , - this . _scrollLeft + 'px' ) ;
207
255
208
256
if ( this . __isRTL ) {
209
257
// Translating the sticky sections using a CSS variable works nicely on LTR.
210
258
// On RTL, it causes jumpy behavior (on Desktop Safari) so we need to translate manually.
211
- const x = this . __getNormalizedScrollLeft ( this . $ . table ) + this . $ . table . clientWidth - this . $ . table . scrollWidth ;
212
- const transform = `translate(${ x } px, 0)` ;
259
+ const transformFrozen = `translate(${ remaining } px, 0)` ;
213
260
for ( let i = 0 ; i < this . _frozenCells . length ; i ++ ) {
214
- this . _frozenCells [ i ] . style . transform = transform ;
261
+ this . _frozenCells [ i ] . style . transform = transformFrozen ;
262
+ }
263
+
264
+ const transformFrozenToEnd = `translate(${ scrollLeft } px, 0)` ;
265
+ for ( let i = 0 ; i < this . _frozenToEndCells . length ; i ++ ) {
266
+ this . _frozenToEndCells [ i ] . style . transform = transformFrozenToEnd ;
215
267
}
216
268
}
217
269
}
0 commit comments