Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**
16 changes: 10 additions & 6 deletions build/js/tempusdominus-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -288,7 +291,7 @@ var DateTimePicker = function ($, moment) {
enabledHours: false,
viewDate: false,
allowMultidate: false,
multidateSeparator: ','
multidateSeparator: ', '
};

// ReSharper restore InconsistentNaming
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"bugs": {
"url": "https://github.com/tempusdominus/core/issues"
},
"scripts": {
"build": "grunt"
},
"dependencies": {
"jquery": "^3.0",
"moment": "^2.22.2",
Expand Down Expand Up @@ -55,4 +58,4 @@
"src/js/**/*.js",
"Gruntfile.js"
]
}
}
33 changes: 24 additions & 9 deletions src/js/tempusdominus-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -287,7 +292,7 @@ const DateTimePicker = (($, moment) => {
enabledHours: false,
viewDate: false,
allowMultidate: false,
multidateSeparator: ','
multidateSeparator: ', '
};

// ReSharper restore InconsistentNaming
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down