7
7
} from "@ui5/webcomponents-base/src/Keys.js" ;
8
8
import "@ui5/webcomponents-icons/dist/icons/navigation-up-arrow.js" ;
9
9
import "@ui5/webcomponents-icons/dist/icons/navigation-down-arrow.js" ;
10
+ import ScrollEnablement from "@ui5/webcomponents-base/dist/delegate/ScrollEnablement.js" ;
10
11
import WheelSliderTemplate from "./generated/templates/WheelSliderTemplate.lit.js" ;
11
12
import Button from "./Button.js" ;
12
13
@@ -153,6 +154,10 @@ class WheelSlider extends UI5Element {
153
154
this . _currentElementIndex = 0 ;
154
155
this . _itemCellHeight = 0 ;
155
156
this . _itemsToShow = [ ] ;
157
+ this . _scroller = new ScrollEnablement ( this ) ;
158
+ this . _scroller . attachEvent ( "scroll" , this . _updateScrolling . bind ( this ) ) ;
159
+ this . _scroller . attachEvent ( "mouseup" , this . _handleScrollTouchEnd . bind ( this ) ) ;
160
+ this . _scroller . attachEvent ( "touchend" , this . _handleScrollTouchEnd . bind ( this ) ) ;
156
161
}
157
162
158
163
onBeforeRendering ( ) {
@@ -169,15 +174,19 @@ class WheelSlider extends UI5Element {
169
174
this . _updateItemCellHeight ( ) ;
170
175
}
171
176
172
- _updateItemCellHeight ( ) {
173
- this . _itemCellHeight = this . shadowRoot . querySelectorAll ( ".ui5-wheelslider-item" ) . length && Number ( getComputedStyle ( this . shadowRoot . querySelector ( ".ui5-wheelslider-item" ) ) . getPropertyValue ( "--_ui5_wheelslider_item_height" ) . replace ( "rem" , "" ) ) ;
174
- }
175
-
176
177
static async onDefine ( ) {
177
178
await Button . define ( ) ;
178
179
}
179
180
180
181
onAfterRendering ( ) {
182
+ if ( ! this . _scroller . scrollContainer ) {
183
+ this . _scroller . scrollContainer = this . shadowRoot . querySelector ( `#${ this . _id } --wrapper` ) ;
184
+ }
185
+
186
+ if ( ! this . _expanded ) {
187
+ this . _scroller . scrollTo ( 0 , 0 ) ;
188
+ }
189
+
181
190
if ( this . _expanded ) {
182
191
const elements = this . shadowRoot . querySelectorAll ( ".ui5-wheelslider-item" ) ;
183
192
for ( let i = 0 ; i < elements . length ; i ++ ) {
@@ -191,25 +200,6 @@ class WheelSlider extends UI5Element {
191
200
}
192
201
}
193
202
194
- _timesMultipliedOnCyclic ( ) {
195
- const minElementsInCyclicWheelSlider = 70 ;
196
- const repetitionCount = Math . round ( minElementsInCyclicWheelSlider / this . _items . length ) ;
197
- const minRepetitionCount = 3 ;
198
-
199
- return Math . max ( minRepetitionCount , repetitionCount ) ;
200
- }
201
-
202
- _buildItemsToShow ( ) {
203
- this . _itemsToShow = this . _items ;
204
- if ( this . cyclic ) {
205
- if ( this . _itemsToShow . length < this . _items . length * this . _timesMultipliedOnCyclic ( ) ) {
206
- for ( let i = 0 ; i < this . _timesMultipliedOnCyclic ( ) ; i ++ ) {
207
- this . _itemsToShow = this . _itemsToShow . concat ( this . _items ) ;
208
- }
209
- }
210
- }
211
- }
212
-
213
203
get classes ( ) {
214
204
return {
215
205
root : {
@@ -219,49 +209,56 @@ class WheelSlider extends UI5Element {
219
209
} ;
220
210
}
221
211
222
- _handleWheel ( e ) {
223
- if ( ! e ) {
224
- return ;
225
- }
212
+ expandSlider ( ) {
213
+ this . _expanded = true ;
214
+ this . fireEvent ( "expand" , { } ) ;
215
+ }
226
216
227
- e . stopPropagation ( ) ;
228
- e . preventDefault ( ) ;
217
+ collapseSlider ( ) {
218
+ this . _expanded = false ;
219
+ this . fireEvent ( "collapse" , { } ) ;
220
+ }
229
221
230
- if ( e . timeStamp === this . _prevWheelTimestamp || ! this . _expanded ) {
231
- return ;
222
+ _updateItemCellHeight ( ) {
223
+ if ( this . shadowRoot . querySelectorAll ( ".ui5-wheelslider-item" ) . length ) {
224
+ const itemComputedStyle = getComputedStyle ( this . shadowRoot . querySelector ( ".ui5-wheelslider-item" ) ) ;
225
+ const itemHeightValue = itemComputedStyle . getPropertyValue ( "--_ui5_wheelslider_item_height" ) ;
226
+ const onlyDigitsValue = itemHeightValue . replace ( "rem" , "" ) ;
227
+
228
+ this . _itemCellHeight = Number ( onlyDigitsValue ) ;
232
229
}
230
+ }
233
231
234
- if ( e . deltaY > 0 ) {
235
- this . _onArrowUp ( e ) ;
236
- } else if ( e . deltaY < 0 ) {
237
- this . _onArrowDown ( e ) ;
232
+ _updateScrolling ( ) {
233
+ const sizeOfOneElementInPixels = this . _itemCellHeight * 16 ,
234
+ scrollWhere = this . _scroller . scrollContainer . scrollTop ;
235
+ let offsetIndex ;
236
+
237
+ if ( ! scrollWhere ) {
238
+ return ;
238
239
}
239
240
240
- this . _prevWheelTimestamp = e . timeStamp ;
241
- }
241
+ offsetIndex = Math . round ( scrollWhere / sizeOfOneElementInPixels ) ;
242
242
243
- _onclick ( e ) {
244
- if ( ! e . target . classList . contains ( "ui5-wheelslider-item" ) ) {
243
+ if ( this . value === this . _itemsToShow [ offsetIndex ] ) {
245
244
return ;
246
245
}
247
246
248
- if ( this . _expanded ) {
249
- this . value = e . target . textContent ;
250
- this . _selectElement ( e . target ) ;
251
- this . fireEvent ( "valueSelect" , { value : this . value } ) ;
252
- } else {
253
- this . _expanded = true ;
247
+ if ( this . cyclic ) {
248
+ const newIndex = this . _handleArrayBorderReached ( offsetIndex ) ;
249
+ if ( offsetIndex !== newIndex ) {
250
+ offsetIndex = newIndex ;
251
+ }
254
252
}
255
- }
256
253
257
- expandSlider ( ) {
258
- this . _expanded = true ;
259
- this . fireEvent ( "expand" , { } ) ;
254
+ this . value = this . _itemsToShow [ offsetIndex ] ;
255
+ this . _currentElementIndex = offsetIndex ;
260
256
}
261
257
262
- collapseSlider ( ) {
263
- this . _expanded = false ;
264
- this . fireEvent ( "collapse" , { } ) ;
258
+ _handleScrollTouchEnd ( ) {
259
+ if ( this . _expanded ) {
260
+ this . _selectElementByIndex ( this . _currentElementIndex ) ;
261
+ }
265
262
}
266
263
267
264
_selectElement ( element ) {
@@ -280,28 +277,48 @@ class WheelSlider extends UI5Element {
280
277
}
281
278
282
279
_selectElementByIndex ( currentIndex ) {
283
- const sliderElement = this . shadowRoot . getElementById ( `${ this . _id } --items-list` ) ;
284
- const itemsCount = this . _itemsToShow . length ;
285
- const itemCellHeight = this . _itemCellHeight ? this . _itemCellHeight : 2.875 ;
286
- const offsetStep = isPhone ( ) ? 4 : 2 ;
287
280
let index = currentIndex ;
281
+ const itemsCount = this . _itemsToShow . length ;
282
+ const sizeOfCellInCompactInRem = 2 ;
283
+ const sizeOfCellInCozyInRem = 2.875 ;
284
+ const sizeOfCellInCompactInPixels = sizeOfCellInCompactInRem * 16 ;
285
+ const sizeOfCellInCozyInPixels = sizeOfCellInCozyInRem * 16 ;
286
+ const scrollBy = this . isCompact ? sizeOfCellInCompactInPixels * index : sizeOfCellInCozyInPixels * index ;
288
287
289
288
if ( this . cyclic ) {
290
- index = this . handleArrayBorderReached ( index ) ;
289
+ index = this . _handleArrayBorderReached ( index ) ;
291
290
}
292
291
293
292
if ( index < itemsCount && index > - 1 ) {
294
- const offsetSelectedElement = offsetStep * itemCellHeight - ( index * itemCellHeight ) ;
295
- sliderElement . setAttribute ( "style" , `top:${ offsetSelectedElement } rem` ) ;
293
+ this . _scroller . scrollTo ( 0 , scrollBy ) ;
296
294
this . _currentElementIndex = index ;
297
295
this . value = this . _items [ index - ( this . _getCurrentRepetition ( ) * this . _items . length ) ] ;
298
296
this . fireEvent ( "valueSelect" , { value : this . value } ) ;
299
297
}
300
298
}
301
299
302
- handleArrayBorderReached ( currentIndex ) {
300
+ _timesMultipliedOnCyclic ( ) {
301
+ const minElementsInCyclicWheelSlider = 70 ;
302
+ const repetitionCount = Math . round ( minElementsInCyclicWheelSlider / this . _items . length ) ;
303
+ const minRepetitionCount = 3 ;
304
+
305
+ return Math . max ( minRepetitionCount , repetitionCount ) ;
306
+ }
307
+
308
+ _buildItemsToShow ( ) {
309
+ this . _itemsToShow = this . _items ;
310
+ if ( this . cyclic ) {
311
+ if ( this . _itemsToShow . length < this . _items . length * this . _timesMultipliedOnCyclic ( ) ) {
312
+ for ( let i = 0 ; i < this . _timesMultipliedOnCyclic ( ) ; i ++ ) {
313
+ this . _itemsToShow = this . _itemsToShow . concat ( this . _items ) ;
314
+ }
315
+ }
316
+ }
317
+ }
318
+
319
+ _handleArrayBorderReached ( currentIndex ) {
303
320
const arrayLength = this . _itemsToShow . length ;
304
- const maxVisibleElementsOnOneSide = 5 ;
321
+ const maxVisibleElementsOnOneSide = 7 ;
305
322
let index = currentIndex ;
306
323
307
324
if ( maxVisibleElementsOnOneSide > index ) {
@@ -313,29 +330,72 @@ class WheelSlider extends UI5Element {
313
330
return index ;
314
331
}
315
332
333
+ _handleWheel ( e ) {
334
+ if ( ! e ) {
335
+ return ;
336
+ }
337
+
338
+ e . stopPropagation ( ) ;
339
+ e . preventDefault ( ) ;
340
+
341
+ if ( e . timeStamp === this . _prevWheelTimestamp || ! this . _expanded ) {
342
+ return ;
343
+ }
344
+
345
+ if ( e . deltaY > 0 ) {
346
+ this . _itemUp ( ) ;
347
+ } else if ( e . deltaY < 0 ) {
348
+ this . _itemDown ( ) ;
349
+ }
350
+
351
+ this . _prevWheelTimestamp = e . timeStamp ;
352
+ }
353
+
354
+ _onclick ( e ) {
355
+ if ( ! e . target . classList . contains ( "ui5-wheelslider-item" ) ) {
356
+ return ;
357
+ }
358
+
359
+ if ( this . _expanded ) {
360
+ this . value = e . target . textContent ;
361
+ this . _selectElement ( e . target ) ;
362
+ this . fireEvent ( "valueSelect" , { value : this . value } ) ;
363
+ } else {
364
+ this . _expanded = true ;
365
+ }
366
+ }
367
+
316
368
_onArrowDown ( e ) {
317
369
e . preventDefault ( ) ;
318
- const nextElementIndex = this . _currentElementIndex + 1 ;
319
- this . _selectElementByIndex ( nextElementIndex ) ;
370
+ this . _itemDown ( ) ;
320
371
}
321
372
322
373
_onArrowUp ( e ) {
323
374
e . preventDefault ( ) ;
375
+ this . _itemUp ( ) ;
376
+ }
377
+
378
+ _itemDown ( ) {
379
+ const nextElementIndex = this . _currentElementIndex + 1 ;
380
+ this . _selectElementByIndex ( nextElementIndex ) ;
381
+ }
382
+
383
+ _itemUp ( ) {
324
384
const nextElementIndex = this . _currentElementIndex - 1 ;
325
385
this . _selectElementByIndex ( nextElementIndex ) ;
326
386
}
327
387
328
- _onkeydown ( event ) {
388
+ _onkeydown ( е ) {
329
389
if ( ! this . _expanded ) {
330
390
return ;
331
391
}
332
392
333
- if ( isUp ( event ) ) {
334
- this . _onArrowUp ( event ) ;
393
+ if ( isUp ( е ) ) {
394
+ this . _onArrowUp ( е ) ;
335
395
}
336
396
337
- if ( isDown ( event ) ) {
338
- this . _onArrowDown ( event ) ;
397
+ if ( isDown ( е ) ) {
398
+ this . _onArrowDown ( е ) ;
339
399
}
340
400
}
341
401
0 commit comments