@@ -2,6 +2,7 @@ import { Utils } from '../../src/utils';
22import { BaseOptions , Component , I18nOptions , InitElements , MElement } from '../../src/component' ;
33import { FormSelect } from '../text-fields/select' ;
44import { DockedDisplayPlugin } from '../../src/dockedDisplayPlugin' ;
5+ import { ModalDisplayPlugin } from '../../src/modalDisplayPlugin' ;
56
67export interface DateI18nOptions extends I18nOptions {
78 previousMonth : string ;
@@ -293,7 +294,7 @@ const _defaults: DatepickerOptions = {
293294 displayPlugin : null ,
294295 displayPluginOptions : null ,
295296 onConfirm : null ,
296- onCancel : null
297+ onCancel : null ,
297298} ;
298299
299300export class Datepicker extends Component < DatepickerOptions > {
@@ -321,7 +322,7 @@ export class Datepicker extends Component<DatepickerOptions> {
321322 calendars : [ { month : number ; year : number } ] ;
322323 private _y : number ;
323324 private _m : number ;
324- private displayPlugin : DockedDisplayPlugin ;
325+ private displayPlugin : DockedDisplayPlugin | ModalDisplayPlugin ;
325326 private footer : HTMLElement ;
326327 static _template : string ;
327328
@@ -348,47 +349,8 @@ export class Datepicker extends Component<DatepickerOptions> {
348349 this . _setupVariables ( ) ;
349350 this . _insertHTMLIntoDOM ( ) ;
350351 this . _setupEventHandlers ( ) ;
351-
352- if ( ! this . options . defaultDate ) {
353- this . options . defaultDate = new Date ( Date . parse ( this . el . value ) ) ;
354- }
355-
356- const defDate = this . options . defaultDate ;
357- if ( Datepicker . _isDate ( defDate ) ) {
358- if ( this . options . setDefaultDate ) {
359- this . setDate ( defDate , true ) ;
360- this . setInputValue ( this . el , defDate ) ;
361- } else {
362- this . gotoDate ( defDate ) ;
363- }
364- } else {
365- this . gotoDate ( new Date ( ) ) ;
366- }
367- if ( this . options . isDateRange ) {
368- this . multiple = true ;
369- const defEndDate = this . options . defaultEndDate ;
370- if ( Datepicker . _isDate ( defEndDate ) ) {
371- if ( this . options . setDefaultEndDate ) {
372- this . setDate ( defEndDate , true , true ) ;
373- this . setInputValue ( this . endDateEl , defEndDate ) ;
374- }
375- }
376- }
377- if ( this . options . isMultipleSelection ) {
378- this . multiple = true ;
379- this . dates = [ ] ;
380- this . dateEls = [ ] ;
381- this . dateEls . push ( el ) ;
382- }
383- if ( this . options . displayPlugin ) {
384- if ( this . options . displayPlugin === 'docked' )
385- this . displayPlugin = DockedDisplayPlugin . init (
386- this . el ,
387- this . containerEl ,
388- this . options . displayPluginOptions
389- ) ;
390- if ( this . options . openByDefault ) this . displayPlugin . show ( ) ;
391- }
352+ if ( this . options . displayPlugin ) this . _setupDisplayPlugin ( ) ;
353+ this . _pickerSetup ( ) ;
392354 }
393355
394356 static get defaults ( ) {
@@ -511,12 +473,6 @@ export class Datepicker extends Component<DatepickerOptions> {
511473 }
512474 }
513475
514- /*if (this.options.showClearBtn) {
515- this.clearBtn.style.visibility = '';
516- this.clearBtn.innerText = this.options.i18n.clear;
517- }
518- this.doneBtn.innerText = this.options.i18n.done;
519- this.cancelBtn.innerText = this.options.i18n.cancel;*/
520476 Utils . createButton (
521477 this . footer ,
522478 this . options . i18n . clear ,
@@ -541,7 +497,6 @@ export class Datepicker extends Component<DatepickerOptions> {
541497 optEl instanceof HTMLElement ? optEl : ( document . querySelector ( optEl ) as HTMLElement ) ;
542498 this . options . container . append ( this . containerEl ) ;
543499 } else {
544- //this.containerEl.before(this.el);
545500 const appendTo = ! this . endDateEl ? this . el : this . endDateEl ;
546501 appendTo . parentElement . after ( this . containerEl ) ;
547502 }
@@ -719,6 +674,38 @@ export class Datepicker extends Component<DatepickerOptions> {
719674 ) ;
720675 }
721676
677+ /**
678+ * Display plugin setup.
679+ */
680+ _setupDisplayPlugin ( ) {
681+ const displayPluginOptions = {
682+ ...this . options . displayPluginOptions ,
683+ ...{
684+ onOpen : ( ) => {
685+ document . querySelectorAll ( '.select-dropdown' ) . forEach ( ( e : HTMLInputElement ) => {
686+ e . tabIndex = 0 ;
687+ } ) ;
688+ } ,
689+ onClose : ( ) => {
690+ document . querySelectorAll ( '.select-dropdown' ) . forEach ( ( e : HTMLInputElement ) => {
691+ e . tabIndex = - 1 ;
692+ } ) ;
693+ }
694+ }
695+ }
696+
697+ if ( this . options . displayPlugin === 'docked' ) this . displayPlugin = DockedDisplayPlugin . init ( this . el , this . containerEl , displayPluginOptions ) ;
698+ if ( this . options . displayPlugin === 'modal' ) {
699+ this . displayPlugin = ModalDisplayPlugin . init ( this . el , this . containerEl , {
700+ ...displayPluginOptions ,
701+ ...{ classList : [ 'datepicker-modal' ] }
702+ } ) ;
703+ this . footer . remove ( ) ;
704+ this . footer = this . displayPlugin . footer ;
705+ }
706+ if ( this . options . openByDefault ) this . displayPlugin . show ( ) ;
707+ }
708+
722709 /**
723710 * Renders the date in the modal head section.
724711 */
@@ -1159,12 +1146,6 @@ export class Datepicker extends Component<DatepickerOptions> {
11591146 this . el . addEventListener ( 'keydown' , this . _handleInputKeydown ) ;
11601147 this . el . addEventListener ( 'change' , this . _handleInputChange ) ;
11611148 this . calendarEl . addEventListener ( 'click' , this . _handleCalendarClick ) ;
1162- /* this.doneBtn.addEventListener('click', this._confirm);
1163- this.cancelBtn.addEventListener('click', this._cancel);
1164-
1165- if (this.options.showClearBtn) {
1166- this.clearBtn.addEventListener('click', this._handleClearClick);
1167- }*/
11681149 }
11691150
11701151 _setupVariables ( ) {
@@ -1176,11 +1157,6 @@ export class Datepicker extends Component<DatepickerOptions> {
11761157 this . calendarEl = this . containerEl . querySelector ( '.datepicker-calendar' ) ;
11771158 this . yearTextEl = this . containerEl . querySelector ( '.year-text' ) ;
11781159 this . dateTextEl = this . containerEl . querySelector ( '.date-text' ) ;
1179- /* if (this.options.showClearBtn) {
1180- this.clearBtn = this.containerEl.querySelector('.datepicker-clear');
1181- }
1182- this.doneBtn = this.containerEl.querySelector('.datepicker-done');
1183- this.cancelBtn = this.containerEl.querySelector('.datepicker-cancel');*/
11841160 this . footer = this . containerEl . querySelector ( '.datepicker-footer' ) ;
11851161
11861162 this . formats = {
@@ -1219,6 +1195,42 @@ export class Datepicker extends Component<DatepickerOptions> {
12191195 } ;
12201196 }
12211197
1198+ _pickerSetup ( ) {
1199+ if ( ! this . options . defaultDate ) {
1200+ this . options . defaultDate = new Date ( Date . parse ( this . el . value ) ) ;
1201+ }
1202+
1203+ const defDate = this . options . defaultDate ;
1204+ if ( Datepicker . _isDate ( defDate ) ) {
1205+ if ( this . options . setDefaultDate ) {
1206+ this . setDate ( defDate , true ) ;
1207+ this . setInputValue ( this . el , defDate ) ;
1208+ } else {
1209+ this . gotoDate ( defDate ) ;
1210+ }
1211+ } else {
1212+ this . gotoDate ( new Date ( ) ) ;
1213+ }
1214+
1215+ if ( this . options . isDateRange ) {
1216+ this . multiple = true ;
1217+ const defEndDate = this . options . defaultEndDate ;
1218+ if ( Datepicker . _isDate ( defEndDate ) ) {
1219+ if ( this . options . setDefaultEndDate ) {
1220+ this . setDate ( defEndDate , true , true ) ;
1221+ this . setInputValue ( this . endDateEl , defEndDate ) ;
1222+ }
1223+ }
1224+ }
1225+
1226+ if ( this . options . isMultipleSelection ) {
1227+ this . multiple = true ;
1228+ this . dates = [ ] ;
1229+ this . dateEls = [ ] ;
1230+ this . dateEls . push ( this . el ) ;
1231+ }
1232+ }
1233+
12221234 _removeEventHandlers ( ) {
12231235 this . el . removeEventListener ( 'click' , this . _handleInputClick ) ;
12241236 this . el . removeEventListener ( 'keydown' , this . _handleInputKeydown ) ;
@@ -1272,10 +1284,15 @@ export class Datepicker extends Component<DatepickerOptions> {
12721284 }
12731285
12741286 if ( this . options . isDateRange ) {
1287+ const confirmAfterSelection = Datepicker . _isDate ( this . date ) && this . options . autoSubmit ;
12751288 this . _handleDateRangeCalendarClick ( selectedDate ) ;
1289+
1290+ if ( confirmAfterSelection ) {
1291+ this . _confirm ( ) ;
1292+ }
12761293 }
12771294
1278- if ( this . options . autoSubmit ) this . _finishSelection ( ) ;
1295+ if ( ! this . options . isDateRange && this . options . autoSubmit ) this . _confirm ( ) ;
12791296 } else if ( target . closest ( '.month-prev' ) ) {
12801297 this . prevMonth ( ) ;
12811298 } else if ( target . closest ( '.month-next' ) ) {
@@ -1290,7 +1307,7 @@ export class Datepicker extends Component<DatepickerOptions> {
12901307 return ;
12911308 }
12921309
1293- this . setDate ( date , false , Datepicker . _isDate ( this . date ) ) ;
1310+ this . setDate ( date , true , Datepicker . _isDate ( this . date ) ) ;
12941311 return ;
12951312 }
12961313
@@ -1385,10 +1402,12 @@ export class Datepicker extends Component<DatepickerOptions> {
13851402
13861403 _confirm = ( ) => {
13871404 this . _finishSelection ( ) ;
1405+ if ( this . displayPlugin ) this . displayPlugin . hide ( ) ;
13881406 if ( typeof this . options . onConfirm === 'function' ) this . options . onConfirm . call ( this ) ;
13891407 } ;
13901408
13911409 _cancel = ( ) => {
1410+ if ( this . displayPlugin ) this . displayPlugin . hide ( ) ;
13921411 if ( typeof this . options . onCancel === 'function' ) this . options . onCancel . call ( this ) ;
13931412 } ;
13941413
0 commit comments