@@ -4,6 +4,7 @@ import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18
4
4
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js" ;
5
5
import { isShow } from "@ui5/webcomponents-base/dist/Keys.js" ;
6
6
import { isPhone } from "@ui5/webcomponents-base/dist/Device.js" ;
7
+ import Integer from "@ui5/webcomponents-base/dist/types/Integer.js" ;
7
8
import DurationPickerTemplate from "./generated/templates/DurationPickerTemplate.lit.js" ;
8
9
import PopoverPlacementType from "./types/PopoverPlacementType.js" ;
9
10
import PopoverHorizontalAlign from "./types/PopoverHorizontalAlign.js" ;
@@ -43,6 +44,30 @@ const metadata = {
43
44
defaultValue : "00:00:00" ,
44
45
} ,
45
46
47
+ /**
48
+ * Defines the selection step for the minutes
49
+ * @type {Integer }
50
+ * @public
51
+ * @defaultValue 1
52
+ * @since 1.0.0-rc.8
53
+ */
54
+ minutesStep : {
55
+ type : Integer ,
56
+ defaultValue : 1 ,
57
+ } ,
58
+
59
+ /**
60
+ * Defines the selection step for the seconds
61
+ * @type {Integer }
62
+ * @public
63
+ * @defaultValue 1
64
+ * @since 1.0.0-rc.8
65
+ */
66
+ secondsStep : {
67
+ type : Integer ,
68
+ defaultValue : 1 ,
69
+ } ,
70
+
46
71
/**
47
72
* Defines a formatted maximal time that the user will be able to adjust.
48
73
*
@@ -227,8 +252,8 @@ class DurationPicker extends UI5Element {
227
252
}
228
253
229
254
/**
230
- * reads string from format hh:mm:ss
231
- * @private
255
+ * reads string from format hh:mm:ss and returns an array which contains the hours, minutes and seconds
256
+ * @param { string } value string in formathh:mm:ss
232
257
*/
233
258
readFormattedValue ( value ) {
234
259
value = value . replace ( / \s / g, "" ) ; // Remove spaces
@@ -250,6 +275,9 @@ class DurationPicker extends UI5Element {
250
275
}
251
276
252
277
if ( currentMinutes > - 1 ) {
278
+ if ( parseInt ( currentMinutes ) % this . minutesStep !== 0 ) {
279
+ currentMinutes = this . findNearestStep ( currentMinutes , this . minutesStep ) ;
280
+ }
253
281
if ( this . _maxValue [ 0 ] && this . selectedHours === this . _maxValue [ 0 ] ) {
254
282
currentMinutes = currentMinutes > this . _maxValue [ 1 ] ? this . _maxValue [ 1 ] : currentMinutes ;
255
283
} else if ( currentMinutes > this . _maxValue [ 1 ] ) {
@@ -260,6 +288,9 @@ class DurationPicker extends UI5Element {
260
288
}
261
289
262
290
if ( currentSeconds > - 1 ) {
291
+ if ( parseInt ( currentSeconds ) % this . secondsStep !== 0 ) {
292
+ currentSeconds = this . findNearestStep ( currentSeconds , this . secondsStep ) ;
293
+ }
263
294
if ( this . _maxValue [ 0 ] && this . _maxValue [ 1 ] && this . selectedHours >= this . _maxValue [ 0 ] && this . selectedSeconds >= this . _maxValue [ 1 ] ) {
264
295
currentSeconds = currentSeconds > this . _maxValue [ 2 ] ? this . _maxValue [ 2 ] : currentSeconds ;
265
296
} else if ( currentSeconds > this . _maxValue [ 2 ] ) {
@@ -296,21 +327,47 @@ class DurationPicker extends UI5Element {
296
327
this [ `_${ name } ` ] = temp ;
297
328
}
298
329
330
+ findNearestStep ( currentValue , step ) {
331
+ const curr = parseInt ( currentValue ) ;
332
+ const biggerClosest = this . _getClosest ( curr , step , true ) ,
333
+ lowerClosest = this . _getClosest ( curr , step , false ) ;
334
+
335
+ const diffToBiggerClosest = biggerClosest - curr ,
336
+ diffToLowerClosest = curr - lowerClosest ;
337
+
338
+ return diffToBiggerClosest > diffToLowerClosest ? lowerClosest . toString ( ) : biggerClosest . toString ( ) ;
339
+ }
340
+
341
+ /**
342
+ * Finds the nearest lower/bigger number to the givent curr
343
+ * @param {Integer } curr the starting number
344
+ * @param {Boolean } larger defines if we are searching for bigger or lower number
345
+ */
346
+ _getClosest ( curr , step , larger = true ) {
347
+ while ( curr % step !== 0 ) {
348
+ curr = larger ? ++ curr : -- curr ;
349
+ }
350
+
351
+ return curr ;
352
+ }
353
+
299
354
_onkeydown ( event ) {
300
355
if ( isShow ( event ) ) {
301
356
this . togglePicker ( ) ;
302
357
}
303
358
}
304
359
305
- generateTimeItemsArray ( arrayLength ) {
360
+ generateTimeItemsArray ( arrayLength , step = 1 ) {
306
361
const resultArray = [ ] ;
307
362
for ( let i = 0 ; i < arrayLength ; i ++ ) {
308
363
let tempString = i . toString ( ) ;
309
364
if ( tempString . length === 1 ) {
310
365
tempString = `0${ tempString } ` ;
311
366
}
312
367
313
- resultArray . push ( tempString ) ;
368
+ if ( tempString % step === 0 ) {
369
+ resultArray . push ( tempString ) ;
370
+ }
314
371
}
315
372
316
373
return resultArray ;
@@ -394,13 +451,13 @@ class DurationPicker extends UI5Element {
394
451
get minutesArray ( ) {
395
452
const currentMinutes = parseInt ( this . readFormattedValue ( this . maxValue ) [ 1 ] ) ;
396
453
const minutes = currentMinutes && currentMinutes > 0 && currentMinutes < 60 ? currentMinutes + 1 : 60 ;
397
- return this . generateTimeItemsArray ( minutes ) ;
454
+ return this . generateTimeItemsArray ( minutes , this . minutesStep ) ;
398
455
}
399
456
400
457
get secondsArray ( ) {
401
458
const currentSeconds = parseInt ( this . readFormattedValue ( this . maxValue ) [ 2 ] ) ;
402
459
const seconds = currentSeconds && currentSeconds > 0 && currentSeconds < 60 ? currentSeconds + 1 : 60 ;
403
- return this . generateTimeItemsArray ( seconds ) ;
460
+ return this . generateTimeItemsArray ( seconds , this . secondsStep ) ;
404
461
}
405
462
406
463
get secondsSlider ( ) {
0 commit comments