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/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 8c4b5b3..d149d74 100644 --- a/src/js/tempusdominus-core.js +++ b/src/js/tempusdominus-core.js @@ -3,8 +3,13 @@ import moment from 'moment'; // ReSharper disable once InconsistentNaming const DateTimePicker = (($, moment) => { + function escapeRegExp(text) { + return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); + } + // 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 +292,7 @@ const DateTimePicker = (($, moment) => { enabledHours: false, viewDate: false, allowMultidate: false, - multidateSeparator: ',' + multidateSeparator: ', ' }; // ReSharper restore InconsistentNaming @@ -424,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'); @@ -461,10 +471,11 @@ 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); } + outpValue = trim(outpValue) if (this.input !== undefined) { this.input.val(outpValue); this.input.trigger('input'); @@ -615,7 +626,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)) { @@ -783,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() { @@ -1499,8 +1514,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;