1
1
import { Utils } from './utils' ;
2
2
import { Component , BaseOptions , InitElements , MElement , I18nOptions } from './component' ;
3
3
import { DockedDisplayPlugin } from './plugin/dockedDisplayPlugin' ;
4
+ import { ModalDisplayPlugin } from './plugin/modalDisplayPlugin' ;
4
5
5
6
export type Views = 'hours' | 'minutes' ;
6
7
@@ -45,6 +46,7 @@ export interface TimepickerOptions extends BaseOptions {
45
46
* Autosubmit timepicker selection to input field
46
47
* @default true
47
48
*/
49
+ // @todo this is only working on analog clock, should apply to hour/minute input fields and am/pm selector as well
48
50
autoSubmit : true ;
49
51
/**
50
52
* Default time to set on the timepicker 'now' or '13:14'.
@@ -176,7 +178,7 @@ export class Timepicker extends Component<TimepickerOptions> {
176
178
g : Element ;
177
179
toggleViewTimer : string | number | NodeJS . Timeout ;
178
180
vibrateTimer : NodeJS . Timeout | number ;
179
- private displayPlugin : DockedDisplayPlugin ;
181
+ private displayPlugin : DockedDisplayPlugin | ModalDisplayPlugin ;
180
182
181
183
constructor ( el : HTMLInputElement , options : Partial < TimepickerOptions > ) {
182
184
super ( el , options , Timepicker ) ;
@@ -190,11 +192,10 @@ export class Timepicker extends Component<TimepickerOptions> {
190
192
this . _setupVariables ( ) ;
191
193
this . _setupEventHandlers ( ) ;
192
194
this . _clockSetup ( ) ;
193
- this . _pickerSetup ( ) ;
194
-
195
195
if ( this . options . displayPlugin ) {
196
- if ( this . options . displayPlugin === 'docked' ) this . displayPlugin = DockedDisplayPlugin . init ( this . el , this . containerEl , this . options . displayPluginOptions ) ;
196
+ this . _setupDisplayPlugin ( ) ;
197
197
}
198
+ this . _pickerSetup ( ) ;
198
199
}
199
200
200
201
static get defaults ( ) : TimepickerOptions {
@@ -260,10 +261,12 @@ export class Timepicker extends Component<TimepickerOptions> {
260
261
261
262
_setupEventHandlers ( ) {
262
263
this . el . addEventListener ( 'click' , this . _handleInputClick ) ;
264
+ // @todo allow input field to fill values from input field when container/modal opens
263
265
this . el . addEventListener ( 'keydown' , this . _handleInputKeydown ) ;
264
266
this . plate . addEventListener ( 'mousedown' , this . _handleClockClickStart ) ;
265
267
this . plate . addEventListener ( 'touchstart' , this . _handleClockClickStart ) ;
266
268
this . digitalClock . addEventListener ( 'keyup' , this . _inputFromTextField ) ;
269
+
267
270
this . inputHours . addEventListener ( 'focus' , ( ) => this . showView ( 'hours' ) ) ;
268
271
this . inputHours . addEventListener ( 'focusout' , ( ) => this . formatHours ( ) ) ;
269
272
this . inputMinutes . addEventListener ( 'focus' , ( ) => this . showView ( 'minutes' ) ) ;
@@ -408,13 +411,15 @@ export class Timepicker extends Component<TimepickerOptions> {
408
411
// clearButton.classList.add('timepicker-clear');
409
412
// clearButton.addEventListener('click', this.clear);
410
413
// this.footer.appendChild(clearButton);
411
- Utils . createButton (
412
- this . footer ,
413
- this . options . i18n . clear ,
414
- [ 'timepicker-clear' ] ,
415
- this . options . showClearBtn ,
416
- this . clear
417
- ) ;
414
+ if ( this . options . showClearBtn ) {
415
+ Utils . createButton (
416
+ this . footer ,
417
+ this . options . i18n . clear ,
418
+ [ 'timepicker-clear' ] ,
419
+ true ,
420
+ this . clear
421
+ ) ;
422
+ }
418
423
419
424
if ( ! this . options . autoSubmit ) {
420
425
/*const confirmationBtnsContainer = document.createElement('div');
@@ -443,6 +448,18 @@ export class Timepicker extends Component<TimepickerOptions> {
443
448
this . showView ( 'hours' ) ;
444
449
}
445
450
451
+ private _setupDisplayPlugin ( ) {
452
+ if ( this . options . displayPlugin === 'docked' ) this . displayPlugin = DockedDisplayPlugin . init ( this . el , this . containerEl , this . options . displayPluginOptions ) ;
453
+ if ( this . options . displayPlugin === 'modal' ) {
454
+ this . displayPlugin = ModalDisplayPlugin . init ( this . el , this . containerEl , {
455
+ ...this . options . displayPluginOptions ,
456
+ ...{ classList : [ 'timepicker-modal' ] }
457
+ } ) ;
458
+ this . footer . remove ( ) ;
459
+ this . footer = this . displayPlugin . footer ;
460
+ }
461
+ }
462
+
446
463
_clockSetup ( ) {
447
464
if ( this . options . twelveHour ) {
448
465
// AM Button
@@ -820,16 +837,19 @@ export class Timepicker extends Component<TimepickerOptions> {
820
837
821
838
confirm = ( ) => {
822
839
this . done ( ) ;
840
+ if ( this . displayPlugin ) this . displayPlugin . hide ( ) ;
823
841
if ( typeof this . options . onDone === 'function' ) this . options . onDone . call ( this ) ;
824
842
}
825
843
826
844
cancel = ( ) => {
827
845
// not logical clearing the input field on cancel, since the end user might want to make use of the previously submitted value
828
846
// this.clear();
847
+ if ( this . displayPlugin ) this . displayPlugin . hide ( ) ;
829
848
if ( typeof this . options . onCancel === 'function' ) this . options . onCancel . call ( this ) ;
830
849
}
831
850
832
851
clear = ( ) => {
852
+ // @todo should clear timepicker hour/minute input elems and reset analog clock, currently clears input el
833
853
this . done ( true ) ;
834
854
} ;
835
855
0 commit comments