From af8fdc6cda1ff69e30af688926a36ef411a6705d Mon Sep 17 00:00:00 2001 From: "Anton Bagdatyev (Tonix)" Date: Thu, 30 Apr 2020 00:55:55 +0200 Subject: [PATCH 1/4] Fix for multidate mode --- .prettierignore | 1 + src/js/tempusdominus-core.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..fa29cdf --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +** \ No newline at end of file diff --git a/src/js/tempusdominus-core.js b/src/js/tempusdominus-core.js index 8c4b5b3..d4b9c26 100644 --- a/src/js/tempusdominus-core.js +++ b/src/js/tempusdominus-core.js @@ -615,7 +615,7 @@ const DateTimePicker = (($, moment) => { } _isValid(targetMoment, granularity) { - if (!targetMoment.isValid()) { + if (!targetMoment || !targetMoment.isValid()) { return false; } if (this._options.disabledDates && granularity === 'd' && this._isInDisabledDates(targetMoment)) { From a7f6412631668e6a7feb5b52815d679bccb67a0a Mon Sep 17 00:00:00 2001 From: "Anton Bagdatyev (Tonix)" Date: Fri, 1 May 2020 21:11:27 +0200 Subject: [PATCH 2/4] Allow multidateSeparator to include more than one character --- build/js/tempusdominus-core.js | 16 ++++++++++------ package.json | 5 ++++- src/js/tempusdominus-core.js | 10 ++++++---- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/build/js/tempusdominus-core.js b/build/js/tempusdominus-core.js index 6337cf9..f1f6e55 100644 --- a/build/js/tempusdominus-core.js +++ b/build/js/tempusdominus-core.js @@ -5,7 +5,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons // ReSharper disable once InconsistentNaming var DateTimePicker = function ($, moment) { // ReSharper disable InconsistentNaming - var NAME = 'datetimepicker', + var trim = function trim(str) { + return str.replace(/(^\s+)|(\s+$)/g, ''); + }, + NAME = 'datetimepicker', DATA_KEY = '' + NAME, EVENT_KEY = '.' + DATA_KEY, DATA_API_KEY = '.data-api', @@ -288,7 +291,7 @@ var DateTimePicker = function ($, moment) { enabledHours: false, viewDate: false, allowMultidate: false, - multidateSeparator: ',' + multidateSeparator: ', ' }; // ReSharper restore InconsistentNaming @@ -418,6 +421,7 @@ var DateTimePicker = function ($, moment) { } else { outpValue = this._dates[index].format(this.actualFormat); } + outpValue = trim(outpValue); if (this.input !== undefined) { this.input.val(outpValue); this.input.trigger('input'); @@ -526,7 +530,7 @@ var DateTimePicker = function ($, moment) { }; DateTimePicker.prototype._notifyEvent = function _notifyEvent(e) { - if (e.type === DateTimePicker.Event.CHANGE && (e.date && e.date.isSame(e.oldDate)) || !e.date && !e.oldDate) { + if (e.type === DateTimePicker.Event.CHANGE && e.date && e.date.isSame(e.oldDate) || !e.date && !e.oldDate) { return; } this._element.trigger(e); @@ -570,7 +574,7 @@ var DateTimePicker = function ($, moment) { }; DateTimePicker.prototype._isValid = function _isValid(targetMoment, granularity) { - if (!targetMoment.isValid()) { + if (!targetMoment || !targetMoment.isValid()) { return false; } if (this._options.disabledDates && granularity === 'd' && this._isInDisabledDates(targetMoment)) { @@ -1458,8 +1462,8 @@ var DateTimePicker = function ($, moment) { return this._options.multidateSeparator; } - if (typeof _multidateSeparator !== 'string' || _multidateSeparator.length > 1) { - throw new TypeError('multidateSeparator expects a single character string parameter'); + if (typeof _multidateSeparator !== 'string') { + throw new TypeError('multidateSeparator expects a string parameter'); } this._options.multidateSeparator = _multidateSeparator; diff --git a/package.json b/package.json index 4244977..f0f118f 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "bugs": { "url": "https://github.com/tempusdominus/core/issues" }, + "scripts": { + "build": "grunt" + }, "dependencies": { "jquery": "^3.0", "moment": "^2.22.2", @@ -55,4 +58,4 @@ "src/js/**/*.js", "Gruntfile.js" ] -} +} \ No newline at end of file diff --git a/src/js/tempusdominus-core.js b/src/js/tempusdominus-core.js index d4b9c26..a741c5f 100644 --- a/src/js/tempusdominus-core.js +++ b/src/js/tempusdominus-core.js @@ -4,7 +4,8 @@ import moment from 'moment'; // ReSharper disable once InconsistentNaming const DateTimePicker = (($, moment) => { // ReSharper disable InconsistentNaming - const NAME = 'datetimepicker', + const trim = str => str.replace(/(^\s+)|(\s+$)/g, ''), + NAME = 'datetimepicker', DATA_KEY = `${NAME}`, EVENT_KEY = `.${DATA_KEY}`, DATA_API_KEY = '.data-api', @@ -287,7 +288,7 @@ const DateTimePicker = (($, moment) => { enabledHours: false, viewDate: false, allowMultidate: false, - multidateSeparator: ',' + multidateSeparator: ', ' }; // ReSharper restore InconsistentNaming @@ -465,6 +466,7 @@ const DateTimePicker = (($, moment) => { } else { outpValue = this._dates[index].format(this.actualFormat); } + outpValue = trim(outpValue) if (this.input !== undefined) { this.input.val(outpValue); this.input.trigger('input'); @@ -1499,8 +1501,8 @@ const DateTimePicker = (($, moment) => { return this._options.multidateSeparator; } - if (typeof multidateSeparator !== 'string' || multidateSeparator.length > 1) { - throw new TypeError('multidateSeparator expects a single character string parameter'); + if (typeof multidateSeparator !== 'string') { + throw new TypeError('multidateSeparator expects a string parameter'); } this._options.multidateSeparator = multidateSeparator; From b83a3698330b30071df7c5e915a82132fdf18ea3 Mon Sep 17 00:00:00 2001 From: "Anton Bagdatyev (Tonix)" Date: Sat, 2 May 2020 18:22:58 +0200 Subject: [PATCH 3/4] Added handling of custom multidate separator different from default ', ' one --- src/js/tempusdominus-core.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/js/tempusdominus-core.js b/src/js/tempusdominus-core.js index a741c5f..87096e4 100644 --- a/src/js/tempusdominus-core.js +++ b/src/js/tempusdominus-core.js @@ -3,6 +3,10 @@ import moment from 'moment'; // ReSharper disable once InconsistentNaming const DateTimePicker = (($, moment) => { + function escapeRegExp(text) { + return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); + } + // ReSharper disable InconsistentNaming const trim = str => str.replace(/(^\s+)|(\s+$)/g, ''), NAME = 'datetimepicker', @@ -425,11 +429,16 @@ const DateTimePicker = (($, moment) => { this._dates = []; this._datesFormatted = []; } else { - outpValue = `${this._element.data('date')},`; - outpValue = outpValue.replace(`${oldDate.format(this.actualFormat)},`, '').replace(',,', '').replace(/,\s*$/, ''); + outpValue = `${this._element.data('date')}${this._options.multidateSeparator}`; + outpValue = outpValue.replace( + `${oldDate.format(this.actualFormat)}${this._options.multidateSeparator}`, '' + ) + .replace(`${this._options.multidateSeparator}${this._options.multidateSeparator}`, '') + .replace(new RegExp(`${escapeRegExp(this._options.multidateSeparator)}\\s*$`), ''); this._dates.splice(index, 1); this._datesFormatted.splice(index, 1); } + outpValue = trim(outpValue); if (this.input !== undefined) { this.input.val(outpValue); this.input.trigger('input'); @@ -462,7 +471,7 @@ const DateTimePicker = (($, moment) => { for (let i = 0; i < this._dates.length; i++) { outpValue += `${this._dates[i].format(this.actualFormat)}${this._options.multidateSeparator}`; } - outpValue = outpValue.replace(/,\s*$/, ''); + outpValue = outpValue.replace(new RegExp(`${this._options.multidateSeparator}\\s*$`), ''); } else { outpValue = this._dates[index].format(this.actualFormat); } From d53edb0f106f79fc41423f5de9b8cd0d04f784be Mon Sep 17 00:00:00 2001 From: "Anton Bagdatyev (Tonix)" Date: Sun, 3 May 2020 16:44:33 +0200 Subject: [PATCH 4/4] Fix multidate mode when decrementing or incrementing hours/minutes/seconds --- src/js/tempusdominus-core.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/js/tempusdominus-core.js b/src/js/tempusdominus-core.js index 87096e4..d149d74 100644 --- a/src/js/tempusdominus-core.js +++ b/src/js/tempusdominus-core.js @@ -794,7 +794,11 @@ const DateTimePicker = (($, moment) => { } _getLastPickedDate() { - return this._dates[this._getLastPickedDateIndex()]; + let lastPickedDate = this._dates[this._getLastPickedDateIndex()]; + if (!lastPickedDate && this._options.allowMultidate) { + lastPickedDate = moment(new Date()); + } + return lastPickedDate; } _getLastPickedDateIndex() {