@@ -129,26 +129,8 @@ class Calendar extends BaseComponent {
129129 super ( element )
130130
131131 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 ( )
152134 this . _createCalendar ( )
153135 this . _addEventListeners ( )
154136 }
@@ -169,31 +151,37 @@ class Calendar extends BaseComponent {
169151 // Public
170152 update ( config ) {
171153 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
172165 this . _calendarDate = convertToDateObject (
173166 this . _config . calendarDate || this . _config . startDate || this . _config . endDate || new Date ( ) , this . _config . selectionType
174167 )
175168 this . _startDate = convertToDateObject ( this . _config . startDate , this . _config . selectionType )
176169 this . _endDate = convertToDateObject ( this . _config . endDate , this . _config . selectionType )
177170 this . _hoverDate = null
178171 this . _selectEndDate = this . _config . selectEndDate
172+ }
179173
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'
190180 }
191181
192- this . _element . innerHTML = ''
193- this . _createCalendar ( )
182+ this . _view = viewMap [ this . _config . selectionType ] || 'days'
194183 }
195184
196- // Private
197185 _getDate ( target ) {
198186 if ( this . _config . selectionType === 'week' ) {
199187 const firstCell = SelectorEngine . findOne ( SELECTOR_CALENDAR_CELL , target . closest ( SELECTOR_CALENDAR_ROW ) )
@@ -432,43 +420,37 @@ class Calendar extends BaseComponent {
432420 } )
433421
434422 // 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 ( )
466424
467425 EventHandler . on ( this . _element , EVENT_MOUSELEAVE , 'table' , ( ) => {
468426 EventHandler . trigger ( this . _element , EVENT_CALENDAR_MOUSE_LEAVE )
469427 } )
470428 }
471429
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+
472454 _setCalendarDate ( date ) {
473455 this . _calendarDate = date
474456
0 commit comments