Skip to content

Commit c086241

Browse files
committed
feature(Datepicker) implemented modal display plugin #557
1 parent 2c0fb63 commit c086241

File tree

1 file changed

+72
-50
lines changed

1 file changed

+72
-50
lines changed

components/datepicker/datepicker.ts

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Utils } from '../../src/utils';
22
import { BaseOptions, Component, I18nOptions, InitElements, MElement } from '../../src/component';
33
import { FormSelect } from '../text-fields/select';
44
import { DockedDisplayPlugin } from '../../src/dockedDisplayPlugin';
5+
import { ModalDisplayPlugin } from '../../src/modalDisplayPlugin';
56

67
export 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

299300
export 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,21 @@ export class Datepicker extends Component<DatepickerOptions> {
719674
);
720675
}
721676

677+
/**
678+
* Display plugin setup.
679+
*/
680+
_setupDisplayPlugin() {
681+
if (this.options.displayPlugin === 'docked') this.displayPlugin = DockedDisplayPlugin.init(this.el, this.containerEl, this.options.displayPluginOptions);
682+
if (this.options.displayPlugin === 'modal') {
683+
this.displayPlugin = ModalDisplayPlugin.init(this.el, this.containerEl, {
684+
...this.options.displayPluginOptions,
685+
...{ classList: ['datepicker-modal'] }
686+
});
687+
this.footer.remove();
688+
this.footer = this.displayPlugin.footer;
689+
}
690+
}
691+
722692
/**
723693
* Renders the date in the modal head section.
724694
*/
@@ -1225,6 +1195,56 @@ export class Datepicker extends Component<DatepickerOptions> {
12251195
};
12261196
}
12271197

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+
if (this.options.showClearBtn) {
1234+
Utils.createButton(
1235+
this.footer,
1236+
this.options.i18n.clear,
1237+
['datepicker-clear'],
1238+
true,
1239+
this._handleClearClick
1240+
);
1241+
}
1242+
1243+
if (!this.options.autoSubmit) {
1244+
Utils.createConfirmationContainer(this.footer, this.options.i18n.done, this.options.i18n.cancel, this._confirm, this._cancel);
1245+
}
1246+
}
1247+
12281248
_removeEventHandlers() {
12291249
this.el.removeEventListener('click', this._handleInputClick);
12301250
this.el.removeEventListener('keydown', this._handleInputKeydown);
@@ -1391,10 +1411,12 @@ export class Datepicker extends Component<DatepickerOptions> {
13911411

13921412
_confirm = () => {
13931413
this._finishSelection();
1414+
if (this.displayPlugin) this.displayPlugin.hide();
13941415
if (typeof this.options.onConfirm === 'function') this.options.onConfirm.call(this);
13951416
};
13961417

13971418
_cancel = () => {
1419+
if (this.displayPlugin) this.displayPlugin.hide();
13981420
if (typeof this.options.onCancel === 'function') this.options.onCancel.call(this);
13991421
};
14001422

0 commit comments

Comments
 (0)