@@ -244,65 +244,27 @@ class DateRangePicker extends BaseComponent {
244
244
}
245
245
246
246
cancel ( ) {
247
- this . _startDate = this . _initialStartDate
248
- this . _startInput . value = this . _setInputValue ( this . _initialStartDate )
249
- this . _startInput . dispatchEvent ( new Event ( 'change' ) )
247
+ this . _changeStartDate ( this . _initialStartDate )
250
248
251
249
if ( this . _config . range ) {
252
- this . _endDate = this . _initialEndDate
253
- this . _endInput . value = this . _setInputValue ( this . _initialEndDate )
254
- this . _endInput . dispatchEvent ( new Event ( 'change' ) )
250
+ this . _changeEndDate ( this . _initialEndDate )
255
251
}
256
252
257
253
this . hide ( )
258
254
259
255
this . _calendar . update ( this . _getCalendarConfig )
260
-
261
- if ( this . _timePickerStart ) {
262
- this . _timePickerStart . update ( this . _getTimePickerConfig ( true ) )
263
- }
264
-
265
- if ( this . _timePickerEnd ) {
266
- this . _timePickerEnd . update ( this . _getTimePickerConfig ( false ) )
267
- }
268
256
}
269
257
270
258
clear ( ) {
271
- this . _endDate = null
272
- this . _endInput . value = ''
273
- this . _endInput . dispatchEvent ( new Event ( 'change' ) )
274
- this . _startDate = null
275
- this . _startInput . value = ''
276
- this . _startInput . dispatchEvent ( new Event ( 'change' ) )
277
-
259
+ this . _changeStartDate ( null )
260
+ this . _changeEndDate ( null )
278
261
this . _calendar . update ( this . _getCalendarConfig ( ) )
279
-
280
- if ( this . _timePickerStart ) {
281
- this . _timePickerStart . update ( this . _getTimePickerConfig ( true ) )
282
- }
283
-
284
- if ( this . _timePickerEnd ) {
285
- this . _timePickerEnd . update ( this . _getTimePickerConfig ( false ) )
286
- }
287
262
}
288
263
289
264
reset ( ) {
290
- this . _endDate = this . _config . endDate
291
- this . _endInput . value = this . _setInputValue ( this . _config . endDate )
292
- this . _endInput . dispatchEvent ( new Event ( 'change' ) )
293
- this . _startDate = this . _config . startDate
294
- this . _startInput . value = this . _setInputValue ( this . _config . startDate )
295
- this . _startInput . dispatchEvent ( new Event ( 'change' ) )
296
-
265
+ this . _changeStartDate ( this . _config . startDate )
266
+ this . _changeEndDate ( this . _config . endDate )
297
267
this . _calendar . update ( this . _getCalendarConfig ( ) )
298
-
299
- if ( this . _timePickerStart ) {
300
- this . _timePickerStart . update ( this . _getTimePickerConfig ( true ) )
301
- }
302
-
303
- if ( this . _timePickerEnd ) {
304
- this . _timePickerEnd . update ( this . _getTimePickerConfig ( false ) )
305
- }
306
268
}
307
269
308
270
update ( config ) {
@@ -398,39 +360,19 @@ class DateRangePicker extends BaseComponent {
398
360
_addCalendarEventListeners ( ) {
399
361
for ( const calendar of SelectorEngine . find ( SELECTOR_CALENDAR , this . _element ) ) {
400
362
EventHandler . on ( calendar , 'startDateChange.coreui.calendar' , event => {
401
- this . _startDate = event . date
402
- this . _startInput . value = this . _setInputValue ( event . date )
403
- this . _startInput . dispatchEvent ( new Event ( 'change' ) )
404
-
405
- if ( this . _timePickerStart ) {
406
- this . _timePickerStart . update ( this . _getTimePickerConfig ( true ) )
407
- }
363
+ this . _changeStartDate ( event . date )
408
364
409
365
if ( ! this . _config . range && ( ! this . _config . footer && ! this . _config . timepicker ) ) {
410
366
this . hide ( )
411
367
}
412
-
413
- EventHandler . trigger ( this . _element , EVENT_START_DATE_CHANGE , {
414
- date : event . date
415
- } )
416
368
} )
417
369
418
370
EventHandler . on ( calendar , 'endDateChange.coreui.calendar' , event => {
419
- this . _endDate = event . date
420
- this . _endInput . value = this . _setInputValue ( event . date )
421
- this . _startInput . dispatchEvent ( new Event ( 'change' ) )
422
-
423
- if ( this . _timePickerEnd ) {
424
- this . _timePickerEnd . update ( this . _getTimePickerConfig ( false ) )
425
- }
371
+ this . _changeEndDate ( event . date )
426
372
427
373
if ( this . _startDate && ( ! this . _config . footer && ! this . _config . timepicker ) ) {
428
374
this . hide ( )
429
375
}
430
-
431
- EventHandler . trigger ( this . _element , EVENT_END_DATE_CHANGE , {
432
- date : event . date
433
- } )
434
376
} )
435
377
436
378
EventHandler . on ( calendar , 'cellHover.coreui.calendar' , event => {
@@ -448,6 +390,34 @@ class DateRangePicker extends BaseComponent {
448
390
}
449
391
}
450
392
393
+ _changeStartDate ( value , skipTimePickerUpdate = false ) {
394
+ this . _startDate = value
395
+ this . _startInput . value = this . _setInputValue ( value )
396
+ this . _startInput . dispatchEvent ( new Event ( 'change' ) )
397
+
398
+ EventHandler . trigger ( this . _element , EVENT_START_DATE_CHANGE , {
399
+ date : value
400
+ } )
401
+
402
+ if ( this . _timePickerStart && ! skipTimePickerUpdate ) {
403
+ this . _timePickerStart . update ( this . _getTimePickerConfig ( true ) )
404
+ }
405
+ }
406
+
407
+ _changeEndDate ( value , skipTimePickerUpdate = false ) {
408
+ this . _endDate = value
409
+ this . _endInput . value = this . _setInputValue ( value )
410
+ this . _endInput . dispatchEvent ( new Event ( 'change' ) )
411
+
412
+ EventHandler . trigger ( this . _element , EVENT_END_DATE_CHANGE , {
413
+ date : value
414
+ } )
415
+
416
+ if ( this . _timePickerEnd && ! skipTimePickerUpdate ) {
417
+ this . _timePickerEnd . update ( this . _getTimePickerConfig ( false ) )
418
+ }
419
+ }
420
+
451
421
_getCalendarConfig ( ) {
452
422
return {
453
423
calendarDate : this . _calendarDate ,
@@ -579,24 +549,10 @@ class DateRangePicker extends BaseComponent {
579
549
buttonEl . classList . add ( ...this . _getButtonClasses ( this . _config . rangesButtonsClasses ) )
580
550
buttonEl . role = 'button'
581
551
buttonEl . addEventListener ( 'click' , ( ) => {
582
- this . _startDate = this . _config . ranges [ key ] [ 0 ]
583
- this . _endDate = this . _config . ranges [ key ] [ 1 ]
584
- this . _startInput . value = this . _setInputValue ( this . _startDate )
585
- this . _startInput . dispatchEvent ( new Event ( 'change' ) )
586
- this . _endInput . value = this . _setInputValue ( this . _endDate )
587
- this . _endInput . dispatchEvent ( new Event ( 'change' ) )
552
+ this . _changeStartDate ( this . _config . ranges [ key ] [ 0 ] )
553
+ this . _changeEndDate ( this . _config . ranges [ key ] [ 1 ] )
588
554
589
555
this . _calendar . update ( this . _getCalendarConfig ( ) )
590
-
591
- EventHandler . trigger ( this . _element , EVENT_START_DATE_CHANGE , {
592
- date : this . _startDate ,
593
- formatedDate : this . _formatDate ( this . _startDate )
594
- } )
595
-
596
- EventHandler . trigger ( this . _element , EVENT_END_DATE_CHANGE , {
597
- date : this . _endDate ,
598
- formatedDate : this . _formatDate ( this . _endDate )
599
- } )
600
556
} )
601
557
602
558
buttonEl . innerHTML = key
@@ -632,7 +588,7 @@ class DateRangePicker extends BaseComponent {
632
588
633
589
this . _calendar = new Calendar ( calendarEl , this . _getCalendarConfig ( ) )
634
590
635
- EventHandler . one ( calendarEl , 'calendarDateChange.coreui.calendar' , event => {
591
+ EventHandler . on ( calendarEl , 'calendarDateChange.coreui.calendar' , event => {
636
592
this . _calendarDate = event . date
637
593
} )
638
594
@@ -654,9 +610,10 @@ class DateRangePicker extends BaseComponent {
654
610
655
611
calendarEl . append ( timePickerStartEl )
656
612
657
- EventHandler . one ( timePickerStartEl , 'timeChange.coreui.time-picker' , event => {
658
- this . _startDate = event . date
659
- this . _startInput . value = this . _setInputValue ( this . _startDate )
613
+ EventHandler . on ( timePickerStartEl , 'timeChange.coreui.time-picker' , event => {
614
+ this . _changeStartDate ( event . date , true )
615
+ // this._startDate = event.date
616
+ // this._startInput.value = this._setInputValue(this._startDate)
660
617
this . _calendar . update ( this . _getCalendarConfig ( ) )
661
618
} )
662
619
@@ -666,9 +623,10 @@ class DateRangePicker extends BaseComponent {
666
623
667
624
this . _timepickers . append ( timePickerEndEl )
668
625
669
- EventHandler . one ( timePickerEndEl , 'timeChange.coreui.time-picker' , event => {
670
- this . _endDate = event . date
671
- this . _endInput . value = this . _setInputValue ( this . _endDate )
626
+ EventHandler . on ( timePickerEndEl , 'timeChange.coreui.time-picker' , event => {
627
+ this . _changeEndDate ( event . date , true )
628
+ // this._endDate = event.date
629
+ // this._endInput.value = this._setInputValue(this._endDate)
672
630
this . _calendar . update ( this . _getCalendarConfig ( ) )
673
631
} )
674
632
} else {
@@ -686,13 +644,15 @@ class DateRangePicker extends BaseComponent {
686
644
687
645
this . _timepickers . append ( timePickerEl )
688
646
689
- EventHandler . one ( timePickerEl , 'timeChange.coreui.time-picker' , event => {
647
+ EventHandler . on ( timePickerEl , 'timeChange.coreui.time-picker' , event => {
690
648
if ( index === 0 ) {
691
- this . _startDate = event . date
692
- this . _startInput . value = this . _setInputValue ( this . _startDate )
649
+ this . _changeStartDate ( event . date , true )
650
+ // this._startDate = event.date
651
+ // this._startInput.value = this._setInputValue(this._startDate)
693
652
} else {
694
- this . _endDate = event . date
695
- this . _endInput . value = this . _setInputValue ( this . _endDate )
653
+ this . _changeEndDate ( event . date , true )
654
+ // this._endDate = event.date
655
+ // this._endInput.value = this._setInputValue(this._endDate)
696
656
}
697
657
698
658
this . _calendar . update ( this . _getCalendarConfig ( ) )
@@ -714,14 +674,10 @@ class DateRangePicker extends BaseComponent {
714
674
todayButtonEl . addEventListener ( 'click' , ( ) => {
715
675
const date = new Date ( )
716
676
this . _calendarDate = date
717
- this . _startDate = date
718
- this . _startInput . value = this . _setInputValue ( date )
719
- this . _startInput . dispatchEvent ( new Event ( 'change' ) )
677
+ this . _changeStartDate ( date )
720
678
721
679
if ( this . _config . range ) {
722
- this . _endDate = date
723
- this . _endInput . value = this . _setInputValue ( date )
724
- this . _endInput . dispatchEvent ( new Event ( 'change' ) )
680
+ this . _changeEndDate ( date )
725
681
}
726
682
727
683
this . _calendar . update ( this . _getCalendarConfig ( ) )
@@ -877,7 +833,6 @@ class DateRangePicker extends BaseComponent {
877
833
}
878
834
879
835
// Static
880
-
881
836
static dateRangePickerInterface ( element , config ) {
882
837
const data = DateRangePicker . getOrCreateInstance ( element , config )
883
838
0 commit comments