Skip to content

Commit 7b38839

Browse files
committed
feature(Datepicker) implemented modal display plugin materializecss#557
1 parent 89c3e2f commit 7b38839

File tree

1 file changed

+71
-50
lines changed

1 file changed

+71
-50
lines changed

src/datepicker.ts

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

67
export interface DateI18nOptions extends I18nOptions {
78
previousMonth: string;
@@ -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,41 +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') this.displayPlugin = DockedDisplayPlugin.init(this.el, this.containerEl, this.options.displayPluginOptions);
385-
}
352+
if (this.options.displayPlugin) this._setupDisplayPlugin();
353+
this._pickerSetup();
386354
}
387355

388356
static get defaults() {
@@ -503,27 +471,13 @@ export class Datepicker extends Component<DatepickerOptions> {
503471
}
504472
}
505473

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-
518474
if (this.options.container) {
519475
const optEl = this.options.container;
520476
this.options.container =
521477
optEl instanceof HTMLElement ? optEl : (document.querySelector(optEl) as HTMLElement);
522478
this.options.container.append(this.containerEl);
523479
} else {
524-
//this.containerEl.before(this.el);
525480
const appendTo = !this.endDateEl ? this.el : this.endDateEl;
526-
if (!this.options.openByDefault) (this.containerEl as HTMLElement).setAttribute('style', 'display: none; visibility: hidden;');
527481
appendTo.parentElement.after(this.containerEl);
528482
}
529483
}
@@ -700,6 +654,21 @@ export class Datepicker extends Component<DatepickerOptions> {
700654
);
701655
}
702656

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+
703672
/**
704673
* Renders the date in the modal head section.
705674
*/
@@ -1186,6 +1155,56 @@ export class Datepicker extends Component<DatepickerOptions> {
11861155
};
11871156
}
11881157

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+
11891208
_removeEventHandlers() {
11901209
this.el.removeEventListener('click', this._handleInputClick);
11911210
this.el.removeEventListener('keydown', this._handleInputKeydown);
@@ -1352,10 +1371,12 @@ export class Datepicker extends Component<DatepickerOptions> {
13521371

13531372
_confirm = () => {
13541373
this._finishSelection();
1374+
if (this.displayPlugin) this.displayPlugin.hide();
13551375
if (typeof this.options.onConfirm === 'function') this.options.onConfirm.call(this);
13561376
}
13571377

13581378
_cancel = () => {
1379+
if (this.displayPlugin) this.displayPlugin.hide();
13591380
if (typeof this.options.onCancel === 'function') this.options.onCancel.call(this);
13601381
};
13611382

0 commit comments

Comments
 (0)