@@ -129,26 +129,8 @@ class Calendar extends BaseComponent {
129
129
super ( element )
130
130
131
131
this . _config = this . _getConfig ( config )
132
- this . _calendarDate = convertToDateObject (
133
- this . _config . calendarDate || this . _config . startDate || this . _config . endDate || new Date ( ) , this . _config . selectionType
134
- )
135
- this . _startDate = convertToDateObject ( this . _config . startDate , this . _config . selectionType )
136
- this . _endDate = convertToDateObject ( this . _config . endDate , this . _config . selectionType )
137
- this . _hoverDate = null
138
- this . _selectEndDate = this . _config . selectEndDate
139
-
140
- if ( this . _config . selectionType === 'day' || this . _config . selectionType === 'week' ) {
141
- this . _view = 'days'
142
- }
143
-
144
- if ( this . _config . selectionType === 'month' ) {
145
- this . _view = 'months'
146
- }
147
-
148
- if ( this . _config . selectionType === 'year' ) {
149
- this . _view = 'years'
150
- }
151
-
132
+ this . _initializeDates ( )
133
+ this . _initializeView ( )
152
134
this . _createCalendar ( )
153
135
this . _addEventListeners ( )
154
136
}
@@ -169,31 +151,37 @@ class Calendar extends BaseComponent {
169
151
// Public
170
152
update ( config ) {
171
153
this . _config = this . _getConfig ( config )
154
+ this . _initializeDates ( )
155
+ this . _initializeView ( )
156
+
157
+ // Clear the current calendar content
158
+ this . _element . innerHTML = ''
159
+ this . _createCalendar ( )
160
+ }
161
+
162
+ // Private
163
+ _initializeDates ( ) {
164
+ // Convert dates to date objects based on the selection type
172
165
this . _calendarDate = convertToDateObject (
173
166
this . _config . calendarDate || this . _config . startDate || this . _config . endDate || new Date ( ) , this . _config . selectionType
174
167
)
175
168
this . _startDate = convertToDateObject ( this . _config . startDate , this . _config . selectionType )
176
169
this . _endDate = convertToDateObject ( this . _config . endDate , this . _config . selectionType )
177
170
this . _hoverDate = null
178
171
this . _selectEndDate = this . _config . selectEndDate
172
+ }
179
173
180
- if ( this . _config . selectionType === 'day' || this . _config . selectionType === 'week' ) {
181
- this . _view = 'days'
182
- }
183
-
184
- if ( this . _config . selectionType === 'month' ) {
185
- this . _view = 'months'
186
- }
187
-
188
- if ( this . _config . selectionType === 'year' ) {
189
- this . _view = 'years'
174
+ _initializeView ( ) {
175
+ const viewMap = {
176
+ day : 'days' ,
177
+ week : 'days' ,
178
+ month : 'months' ,
179
+ year : 'years'
190
180
}
191
181
192
- this . _element . innerHTML = ''
193
- this . _createCalendar ( )
182
+ this . _view = viewMap [ this . _config . selectionType ] || 'days'
194
183
}
195
184
196
- // Private
197
185
_getDate ( target ) {
198
186
if ( this . _config . selectionType === 'week' ) {
199
187
const firstCell = SelectorEngine . findOne ( SELECTOR_CALENDAR_CELL , target . closest ( SELECTOR_CALENDAR_ROW ) )
@@ -432,43 +420,37 @@ class Calendar extends BaseComponent {
432
420
} )
433
421
434
422
// Navigation
435
- EventHandler . on ( this . _element , EVENT_CLICK_DATA_API , SELECTOR_BTN_PREV , event => {
436
- event . preventDefault ( )
437
- this . _modifyCalendarDate ( 0 , - 1 )
438
- } )
439
-
440
- EventHandler . on ( this . _element , EVENT_CLICK_DATA_API , SELECTOR_BTN_DOUBLE_PREV , event => {
441
- event . preventDefault ( )
442
- this . _modifyCalendarDate ( this . _view === 'years' ? - 10 : - 1 )
443
- } )
444
-
445
- EventHandler . on ( this . _element , EVENT_CLICK_DATA_API , SELECTOR_BTN_NEXT , event => {
446
- event . preventDefault ( )
447
- this . _modifyCalendarDate ( 0 , 1 )
448
- } )
449
-
450
- EventHandler . on ( this . _element , EVENT_CLICK_DATA_API , SELECTOR_BTN_DOUBLE_NEXT , event => {
451
- event . preventDefault ( )
452
- this . _modifyCalendarDate ( this . _view === 'years' ? 10 : 1 )
453
- } )
454
-
455
- EventHandler . on ( this . _element , EVENT_CLICK_DATA_API , SELECTOR_BTN_MONTH , event => {
456
- event . preventDefault ( )
457
- this . _view = 'months'
458
- this . _updateCalendar ( )
459
- } )
460
-
461
- EventHandler . on ( this . _element , EVENT_CLICK_DATA_API , SELECTOR_BTN_YEAR , event => {
462
- event . preventDefault ( )
463
- this . _view = 'years'
464
- this . _updateCalendar ( )
465
- } )
423
+ this . _addNavigationEventListeners ( )
466
424
467
425
EventHandler . on ( this . _element , EVENT_MOUSELEAVE , 'table' , ( ) => {
468
426
EventHandler . trigger ( this . _element , EVENT_CALENDAR_MOUSE_LEAVE )
469
427
} )
470
428
}
471
429
430
+ _addNavigationEventListeners ( ) {
431
+ const navigationSelectors = {
432
+ [ SELECTOR_BTN_PREV ] : ( ) => this . _modifyCalendarDate ( 0 , - 1 ) ,
433
+ [ SELECTOR_BTN_DOUBLE_PREV ] : ( ) => this . _modifyCalendarDate ( this . _view === 'years' ? - 10 : - 1 ) ,
434
+ [ SELECTOR_BTN_NEXT ] : ( ) => this . _modifyCalendarDate ( 0 , 1 ) ,
435
+ [ SELECTOR_BTN_DOUBLE_NEXT ] : ( ) => this . _modifyCalendarDate ( this . _view === 'years' ? 10 : 1 ) ,
436
+ [ SELECTOR_BTN_MONTH ] : ( ) => {
437
+ this . _view = 'months'
438
+ this . _updateCalendar ( )
439
+ } ,
440
+ [ SELECTOR_BTN_YEAR ] : ( ) => {
441
+ this . _view = 'years'
442
+ this . _updateCalendar ( )
443
+ }
444
+ }
445
+
446
+ for ( const [ selector , handler ] of Object . entries ( navigationSelectors ) ) {
447
+ EventHandler . on ( this . _element , EVENT_CLICK_DATA_API , selector , event => {
448
+ event . preventDefault ( )
449
+ handler ( )
450
+ } )
451
+ }
452
+ }
453
+
472
454
_setCalendarDate ( date ) {
473
455
this . _calendarDate = date
474
456
0 commit comments