Skip to content

EspoCRM – Open Source CRM Application

ui datepicker

Yuri Kuznetsovcb95eb0f5ad6
diff --git a/client/src/ui/datepicker.js b/client/src/ui/datepicker.js
new file mode 100644
index 0000000000..bb0187ed41
--- /dev/null
+++ b/client/src/ui/datepicker.js
@@ -0,0 +1,154 @@
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+import $ from 'jquery';
+import Language from 'language';
+import Settings from 'models/settings';
+import {inject} from 'di';
+
+/**
+ * A datepicker.
+ *
+ * @since 9.0.0
+ */
+class Datepicker {
+
+
+ /**
+ * @type {Language}
+ */
+ @inject(Language)
+ language
+
+ /**
+ * @type {Settings}
+ */
+ @inject(Settings)
+ config
+
+ /**
+ * @param {HTMLElement} element
+ * @param {{
+ * format: string,
+ * weekStart: number,
+ * todayButton?: boolean,
+ * startDate?: string|undefined,
+ * onChange?: function(),
+ * }} options
+ */
+ constructor(element, options) {
+ /**
+ * @private
+ */
+ this.$element = $(element);
+
+ if (element instanceof HTMLInputElement && 0) {
+ let wait = false;
+
+ this.$element.on('change', /** Record */e => {
+ if (!wait) {
+ if (options.onChange) {
+ options.onChange();
+ }
+
+ wait = true;
+ setTimeout(() => wait = false, 100);
+ }
+
+ if (e.isTrigger && document.activeElement !== this.$element.get(0)) {
+ this.$element.focus();
+ }
+ });
+
+ this.$element.on('click', () => this.show());
+ }
+
+ const modalBodyElement = element.closest('.modal-body');
+
+ const language = this.config.get('language');
+
+ const datepickerOptions = {
+ autoclose: true,
+ todayHighlight: true,
+ keyboardNavigation: true,
+ assumeNearbyYear: true,
+
+ format: options.format.toLowerCase(),
+ weekStart: options.weekStart,
+ todayBtn: options.todayButton || false,
+ startDate: options.startDate,
+ orientation: 'bottom auto',
+ templates: {
+ leftArrow: '<span class="fas fa-chevron-left fa-sm"></span>',
+ rightArrow: '<span class="fas fa-chevron-right fa-sm"></span>',
+ },
+ container: modalBodyElement ? $(modalBodyElement) : 'body',
+ language: language,
+ };
+
+ // noinspection JSUnresolvedReference
+ if (!(language in $.fn.datepicker.dates)) {
+ // noinspection JSUnresolvedReference
+ $.fn.datepicker.dates[language] = {
+ days: this.language.get('Global', 'lists', 'dayNames'),
+ daysShort: this.language.get('Global', 'lists', 'dayNamesShort'),
+ daysMin: this.language.get('Global', 'lists', 'dayNamesMin'),
+ months: this.language.get('Global', 'lists', 'monthNames'),
+ monthsShort: this.language.get('Global', 'lists', 'monthNamesShort'),
+ today: this.language.translate('Today'),
+ clear: this.language.translate('Clear'),
+ };
+ }
+
+ this.$element.datepicker(datepickerOptions);
+
+ if (element.classList.contains('input-group') && !(element instanceof HTMLInputElement)) {
+ element.querySelectorAll('input').forEach(input => {
+ $(input).on('click', () => $(input).datepicker('show'));
+ });
+ }
+ }
+
+ /**
+ * Set a start date.
+ *
+ * @param {string|undefined} startDate
+ */
+ setStartDate(startDate) {
+ this.$element.datepicker('setStartDate', startDate);
+ }
+
+ /**
+ * Show.
+ */
+ show() {
+ this.$element.datepicker('show');
+ }
+}
+
+export default Datepicker;
diff --git a/client/src/views/fields/date.js b/client/src/views/fields/date.js
index cb64e6fce1..5dd398e2d3 100644
--- a/client/src/views/fields/date.js
+++ b/client/src/views/fields/date.js
@@ -30,6 +30,7 @@
import BaseFieldView from 'views/fields/base';
import moment from 'moment';
+import Datepicker from 'ui/datepicker';
/**
* A date field.
@@ -112,6 +113,12 @@ class DateFieldView extends BaseFieldView {
initialSearchIsNotIdle = true
+ /**
+ * @private
+ * @type {import('ui/datepicker').default}
+ */
+ datepicker
+
setup() {
super.setup();
@@ -137,8 +144,7 @@ class DateFieldView extends BaseFieldView {
// Timeout prevents the picker popping one when the duration field adjusts the date end.
setTimeout(() => {
- // noinspection JSUnresolvedReference
- this.$element.datepicker('setStartDate', this.getStartDateForDatePicker());
+ this.datepicker.setStartDate(this.getStartDateForDatePicker());
}, 100);
});
}
@@ -296,85 +302,43 @@ class DateFieldView extends BaseFieldView {
afterRender() {
if (this.mode === this.MODE_EDIT || this.mode === this.MODE_SEARCH) {
- this.$element = this.$el.find('[data-name="' + this.name + '"]');
-
- let wait = false;
-
- // @todo Introduce ui/date-picker.
-
- this.$element.on('change', /** Record */e => {
- if (!wait) {
- this.trigger('change');
- wait = true;
- setTimeout(() => wait = false, 100);
- }
-
- if (e.isTrigger) {
- if (document.activeElement !== this.$element.get(0)) {
- this.$element.focus();
- }
- }
- });
+ this.$element = this.$el.find(`[data-name="${this.name}"]`);
- this.$element.on('click', () => {
- // noinspection JSUnresolvedReference
- this.$element.datepicker('show');
- });
+ /** @type {HTMLElement} */
+ const element = this.$element.get(0);
const options = {
- format: this.getDateTime().dateFormat.toLowerCase(),
+ format: this.getDateTime().dateFormat,
weekStart: this.getDateTime().weekStart,
- autoclose: true,
- todayHighlight: true,
- keyboardNavigation: true,
- assumeNearbyYear: true,
- todayBtn: this.getConfig().get('datepickerTodayButton') || false,
- orientation: 'bottom auto',
- templates: {
- leftArrow: '<span class="fas fa-chevron-left fa-sm"></span>',
- rightArrow: '<span class="fas fa-chevron-right fa-sm"></span>',
- },
- container: this.$el.closest('.modal-body').length ?
- this.$el.closest('.modal-body') :
- 'body',
startDate: this.getStartDateForDatePicker(),
+ todayButton: this.getConfig().get('datepickerTodayButton') || false,
};
- const language = this.getConfig().get('language');
-
- // noinspection JSUnresolvedReference
- if (!(language in $.fn.datepicker.dates)) {
- // noinspection JSUnresolvedReference
- $.fn.datepicker.dates[language] = {
- days: this.translate('dayNames', 'lists'),
- daysShort: this.translate('dayNamesShort', 'lists'),
- daysMin: this.translate('dayNamesMin', 'lists'),
- months: this.translate('monthNames', 'lists'),
- monthsShort: this.translate('monthNamesShort', 'lists'),
- today: this.translate('Today'),
- clear: this.translate('Clear'),
- };
- }
-
- options.language = language;
+ this.datepicker = undefined;
- // noinspection JSUnresolvedReference
- this.$element.datepicker(options);
+ if (element) {
+ this.datepicker = new Datepicker(element, {
+ ...options,
+ onChange: () => this.trigger('change'),
+ });
+ }
if (this.mode === this.MODE_SEARCH) {
this.$el.find('select.search-type').on('change', () => this.trigger('change'));
this.$el.find('input.number').on('change', () => this.trigger('change'));
- // noinspection JSUnresolvedReference
- this.$el.find('.input-group.additional').datepicker(options);
+ const element = this.$el.find('.input-group.additional').get(0);
+
+ if (element) {
+ new Datepicker(element, options)
- this.initDatePickerEventHandlers('input.filter-from');
- this.initDatePickerEventHandlers('input.filter-to');
+ this.initDatePickerEventHandlers('input.filter-from');
+ this.initDatePickerEventHandlers('input.filter-to');
+ }
}
this.$element.parent().find('button.date-picker-btn').on('click', () => {
- // noinspection JSUnresolvedReference
- this.$element.datepicker('show');
+ this.datepicker.show();
});
if (this.mode === this.MODE_SEARCH) {
@@ -401,11 +365,6 @@ class DateFieldView extends BaseFieldView {
}
}
});
-
- $input.on('click', () => {
- // noinspection JSUnresolvedReference
- $input.datepicker('show');
- });
}
handleSearchType(type) {