@@ -2,6 +2,7 @@ import { Utils } from './utils';
2
2
import { FormSelect } from './select' ;
3
3
import { BaseOptions , Component , I18nOptions , InitElements , MElement } from './component' ;
4
4
import { DockedDisplayPlugin } from './plugin/dockedDisplayPlugin' ;
5
+ import { ModalDisplayPlugin } from './plugin/modalDisplayPlugin' ;
5
6
6
7
export interface DateI18nOptions extends I18nOptions {
7
8
previousMonth : string ;
@@ -321,7 +322,7 @@ export class Datepicker extends Component<DatepickerOptions> {
321
322
calendars : [ { month : number ; year : number } ] ;
322
323
private _y : number ;
323
324
private _m : number ;
324
- private displayPlugin : DockedDisplayPlugin ;
325
+ private displayPlugin : DockedDisplayPlugin | ModalDisplayPlugin ;
325
326
private footer : HTMLElement ;
326
327
static _template : string ;
327
328
@@ -348,41 +349,8 @@ export class Datepicker extends Component<DatepickerOptions> {
348
349
this . _setupVariables ( ) ;
349
350
this . _insertHTMLIntoDOM ( ) ;
350
351
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' ) this . displayPlugin = DockedDisplayPlugin . init ( this . el , this . containerEl , this . options . displayPluginOptions ) ;
385
- }
352
+ if ( this . options . displayPlugin ) this . _setupDisplayPlugin ( ) ;
353
+ this . _pickerSetup ( ) ;
386
354
}
387
355
388
356
static get defaults ( ) {
@@ -503,27 +471,13 @@ export class Datepicker extends Component<DatepickerOptions> {
503
471
}
504
472
}
505
473
506
- /*if (this.options.showClearBtn) {
507
- this.clearBtn.style.visibility = '';
508
- this.clearBtn.innerText = this.options.i18n.clear;
509
- }
510
- this.doneBtn.innerText = this.options.i18n.done;
511
- this.cancelBtn.innerText = this.options.i18n.cancel;*/
512
- Utils . createButton ( this . footer , this . options . i18n . clear , [ 'datepicker-clear' ] , this . options . showClearBtn , this . _handleClearClick ) ;
513
-
514
- if ( ! this . options . autoSubmit ) {
515
- Utils . createConfirmationContainer ( this . footer , this . options . i18n . done , this . options . i18n . cancel , this . _confirm , this . _cancel ) ;
516
- }
517
-
518
474
if ( this . options . container ) {
519
475
const optEl = this . options . container ;
520
476
this . options . container =
521
477
optEl instanceof HTMLElement ? optEl : ( document . querySelector ( optEl ) as HTMLElement ) ;
522
478
this . options . container . append ( this . containerEl ) ;
523
479
} else {
524
- //this.containerEl.before(this.el);
525
480
const appendTo = ! this . endDateEl ? this . el : this . endDateEl ;
526
- if ( ! this . options . openByDefault ) ( this . containerEl as HTMLElement ) . setAttribute ( 'style' , 'display: none; visibility: hidden;' ) ;
527
481
appendTo . parentElement . after ( this . containerEl ) ;
528
482
}
529
483
}
@@ -700,6 +654,21 @@ export class Datepicker extends Component<DatepickerOptions> {
700
654
) ;
701
655
}
702
656
657
+ /**
658
+ * Display plugin setup.
659
+ */
660
+ _setupDisplayPlugin ( ) {
661
+ if ( this . options . displayPlugin === 'docked' ) this . displayPlugin = DockedDisplayPlugin . init ( this . el , this . containerEl , this . options . displayPluginOptions ) ;
662
+ if ( this . options . displayPlugin === 'modal' ) {
663
+ this . displayPlugin = ModalDisplayPlugin . init ( this . el , this . containerEl , {
664
+ ...this . options . displayPluginOptions ,
665
+ ...{ classList : [ 'datepicker-modal' ] }
666
+ } ) ;
667
+ this . footer . remove ( ) ;
668
+ this . footer = this . displayPlugin . footer ;
669
+ }
670
+ }
671
+
703
672
/**
704
673
* Renders the date in the modal head section.
705
674
*/
@@ -1186,6 +1155,56 @@ export class Datepicker extends Component<DatepickerOptions> {
1186
1155
} ;
1187
1156
}
1188
1157
1158
+ _pickerSetup ( ) {
1159
+ if ( ! this . options . defaultDate ) {
1160
+ this . options . defaultDate = new Date ( Date . parse ( this . el . value ) ) ;
1161
+ }
1162
+
1163
+ const defDate = this . options . defaultDate ;
1164
+ if ( Datepicker . _isDate ( defDate ) ) {
1165
+ if ( this . options . setDefaultDate ) {
1166
+ this . setDate ( defDate , true ) ;
1167
+ this . setInputValue ( this . el , defDate ) ;
1168
+ } else {
1169
+ this . gotoDate ( defDate ) ;
1170
+ }
1171
+ } else {
1172
+ this . gotoDate ( new Date ( ) ) ;
1173
+ }
1174
+
1175
+ if ( this . options . isDateRange ) {
1176
+ this . multiple = true ;
1177
+ const defEndDate = this . options . defaultEndDate ;
1178
+ if ( Datepicker . _isDate ( defEndDate ) ) {
1179
+ if ( this . options . setDefaultEndDate ) {
1180
+ this . setDate ( defEndDate , true , true ) ;
1181
+ this . setInputValue ( this . endDateEl , defEndDate ) ;
1182
+ }
1183
+ }
1184
+ }
1185
+
1186
+ if ( this . options . isMultipleSelection ) {
1187
+ this . multiple = true ;
1188
+ this . dates = [ ] ;
1189
+ this . dateEls = [ ] ;
1190
+ this . dateEls . push ( this . el ) ;
1191
+ }
1192
+
1193
+ if ( this . options . showClearBtn ) {
1194
+ Utils . createButton (
1195
+ this . footer ,
1196
+ this . options . i18n . clear ,
1197
+ [ 'datepicker-clear' ] ,
1198
+ true ,
1199
+ this . _handleClearClick
1200
+ ) ;
1201
+ }
1202
+
1203
+ if ( ! this . options . autoSubmit ) {
1204
+ Utils . createConfirmationContainer ( this . footer , this . options . i18n . done , this . options . i18n . cancel , this . _confirm , this . _cancel ) ;
1205
+ }
1206
+ }
1207
+
1189
1208
_removeEventHandlers ( ) {
1190
1209
this . el . removeEventListener ( 'click' , this . _handleInputClick ) ;
1191
1210
this . el . removeEventListener ( 'keydown' , this . _handleInputKeydown ) ;
@@ -1352,10 +1371,12 @@ export class Datepicker extends Component<DatepickerOptions> {
1352
1371
1353
1372
_confirm = ( ) => {
1354
1373
this . _finishSelection ( ) ;
1374
+ if ( this . displayPlugin ) this . displayPlugin . hide ( ) ;
1355
1375
if ( typeof this . options . onConfirm === 'function' ) this . options . onConfirm . call ( this ) ;
1356
1376
}
1357
1377
1358
1378
_cancel = ( ) => {
1379
+ if ( this . displayPlugin ) this . displayPlugin . hide ( ) ;
1359
1380
if ( typeof this . options . onCancel === 'function' ) this . options . onCancel . call ( this ) ;
1360
1381
} ;
1361
1382
0 commit comments