Skip to content

EspoCRM – Open Source CRM Application

timepicker ref

Yuri Kuznetsov4feb3ab3188c
diff --git a/client/src/ui/timepicker.js b/client/src/ui/timepicker.js
new file mode 100644
index 0000000000..e0030d7fdd
--- /dev/null
+++ b/client/src/ui/timepicker.js
@@ -0,0 +1,106 @@
+/************************************************************************
+ * 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';
+
+/**
+ * A timepicker.
+ */
+class Timepicker {
+
+ /**
+ * @param {HTMLElement} element
+ * @param {{
+ * step: number,
+ * timeFormat: string,
+ * scrollDefaultNow?: boolean,
+ * }} options
+ */
+ constructor(element, options) {
+ /**
+ * @private
+ */
+ this.$element = $(element);
+
+ const modalBodyElement = element.closest('.modal-body');
+
+ this.$element.timepicker({
+ step: options.step,
+ timeFormat: options.timeFormat,
+ appendTo: modalBodyElement ? $(modalBodyElement) : 'body',
+ scrollDefaultNow: options.scrollDefaultNow || false,
+ });
+ }
+
+ /**
+ * Set the min time.
+ *
+ * @param {string|null} minTime
+ */
+ setMinTime(minTime) {
+ this.$element.timepicker('option', 'minTime', minTime);
+ }
+
+ /**
+ * Set the max time.
+ *
+ * @param {string|null} maxTime
+ */
+ setMaxTime(maxTime) {
+ this.$element.timepicker('option', 'maxTime', maxTime);
+ }
+
+ /**
+ * Add a 'change' event listener.
+ *
+ * @param {function} callback
+ */
+ addChangeEventListener(callback) {
+ this.$element.on('change', callback);
+ }
+
+ /**
+ * Show.
+ */
+ show() {
+ this.$element.timepicker('show');
+ }
+
+ /**
+ * Destroy.
+ */
+ destroy() {
+ if (!this.$element[0]) {
+ return;
+ }
+
+ this.$element.timepicker('remove');
+ }
+}
+
+export default Timepicker;
diff --git a/client/src/views/working-time-calendar/fields/time-ranges/item-edit.js b/client/src/views/working-time-calendar/fields/time-ranges/item-edit.js
index c3970e54c4..8c3817186e 100644
--- a/client/src/views/working-time-calendar/fields/time-ranges/item-edit.js
+++ b/client/src/views/working-time-calendar/fields/time-ranges/item-edit.js
@@ -28,9 +28,9 @@
import View from 'view';
import moment from 'moment';
+import Timepicker from 'ui/timepicker';
-export default class extends View {
-
+export default class TimeRangeItemEdit extends View {
// language=Handlebars
templateContent = `
@@ -80,6 +80,30 @@ export default class extends View {
minuteStep = 30
+ /**
+ * @private
+ * @type {HTMLInputElement}
+ */
+ startElement
+
+ /**
+ * @private
+ * @type {HTMLInputElement}
+ */
+ endElement
+
+ /**
+ * @private
+ * @type {import('ui/timepicker').default}
+ */
+ startTimepicker
+
+ /**
+ * @private
+ * @type {import('ui/timepicker').default}
+ */
+ endTimepicker
+
data () {
const data = {};
@@ -94,6 +118,8 @@ export default class extends View {
setup () {
this.value = this.options.value || [null, null];
this.key = this.options.key;
+
+ this.on('remove', () => this.destroyTimepickers());
}
convertTimeToDisplay(value) {
@@ -129,30 +155,32 @@ export default class extends View {
}
afterRender() {
- this.$start = this.$el.find('[data-name="start"]');
- this.$end = this.$el.find('[data-name="end"]');
+ this.startElement = this.element.querySelector('[data-name="start"]');
+ this.endElement = this.element.querySelector('[data-name="end"]');
- this.initTimepicker(this.$start);
- this.initTimepicker(this.$end);
+ if (this.startElement) {
+ this.startTimepicker = this.initTimepicker(this.startElement);
+ this.endTimepicker = this.initTimepicker(this.endElement);
- this.setMinTime();
+ this.setMinTime();
- this.$start.on('change', () => this.setMinTime());
+ this.startTimepicker.addChangeEventListener(() => this.setMinTime());
+ }
}
setMinTime() {
- const value = this.$start.val();
+ const value = this.startElement.value;
const parsedValue = this.convertTimeFromDisplay(value);
if (parsedValue !== '00:00') {
- this.$end.timepicker('option', 'maxTime', this.convertTimeToDisplay('24:00'));
+ this.endTimepicker.setMaxTime(this.convertTimeToDisplay('24:00'));
} else {
- this.$end.timepicker('option', 'maxTime', null);
+ this.endTimepicker.setMaxTime(null);
}
if (!value) {
- this.$end.timepicker('option', 'minTime', null);
+ this.endTimepicker.setMinTime(null);
return;
}
@@ -161,23 +189,41 @@ export default class extends View {
.add(this.minuteStep, 'minute')
.format(this.getDateTime().timeFormat);
- this.$end.timepicker('option', 'minTime', minValue);
+ this.endTimepicker.setMinTime(minValue);
}
- initTimepicker($el) {
- $el.timepicker({
+ /**
+ * @private
+ * @param {HTMLInputElement} element
+ * @return {Timepicker}
+ */
+ initTimepicker(element) {
+ const timepicker = new Timepicker(element, {
step: this.minuteStep,
timeFormat: this.timeFormatMap[this.getDateTime().timeFormat],
- });
+ })
+
+ timepicker.addChangeEventListener(() => this.trigger('change'));
- $el.on('change', () => this.trigger('change'));
- $el.attr('autocomplete', 'espo-time-range-item');
+ element.setAttribute('autocomplete', 'espo-time-range-item');
+
+ return timepicker;
+ }
+
+ destroyTimepickers() {
+ if (this.startTimepicker) {
+ this.startTimepicker.destroy();
+ }
+
+ if (this.endTimepicker) {
+ this.endTimepicker.destroy();
+ }
}
fetch() {
return [
- this.convertTimeFromDisplay(this.$start.val()),
- this.convertTimeFromDisplay(this.$end.val()),
+ this.convertTimeFromDisplay(this.startElement.value),
+ this.convertTimeFromDisplay(this.endElement.value),
];
}
}