2023-01-16 23:00:10 +01:00
|
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
2021-07-10 10:55:54 +02:00
|
|
|
import { TAPi18n } from '/imports/i18n';
|
2022-04-27 12:59:14 +02:00
|
|
|
import moment from 'moment/min/moment-with-locales';
|
2021-07-10 10:55:54 +02:00
|
|
|
|
2021-02-11 03:54:12 +01:00
|
|
|
// Helper function to replace HH with H for 24 hours format, because H allows also single-digit hours
|
|
|
|
function adjustedTimeFormat() {
|
2021-02-11 19:07:34 +02:00
|
|
|
return moment
|
|
|
|
.localeData()
|
|
|
|
.longDateFormat('LT')
|
|
|
|
.replace(/HH/i, 'H');
|
2021-02-11 03:54:12 +01:00
|
|
|
}
|
|
|
|
|
2021-04-06 18:00:03 +02:00
|
|
|
export class DatePicker extends BlazeComponent {
|
2018-05-18 10:24:51 +02:00
|
|
|
template() {
|
|
|
|
return 'datepicker';
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
2017-10-14 01:38:25 +02:00
|
|
|
|
2019-10-28 08:25:55 +01:00
|
|
|
onCreated(defaultTime = '1970-01-01 08:00:00') {
|
2018-05-18 10:24:51 +02:00
|
|
|
this.error = new ReactiveVar('');
|
|
|
|
this.card = this.data();
|
|
|
|
this.date = new ReactiveVar(moment.invalid());
|
2019-10-24 12:57:07 +02:00
|
|
|
this.defaultTime = defaultTime;
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
2017-10-14 01:38:25 +02:00
|
|
|
|
2020-04-22 14:44:08 +02:00
|
|
|
startDayOfWeek() {
|
2023-01-16 23:00:10 +01:00
|
|
|
const currentUser = ReactiveCache.getCurrentUser();
|
2020-04-22 14:44:08 +02:00
|
|
|
if (currentUser) {
|
|
|
|
return currentUser.getStartDayOfWeek();
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
2020-04-22 14:44:08 +02:00
|
|
|
|
2018-05-18 10:24:51 +02:00
|
|
|
onRendered() {
|
2019-06-28 12:52:09 -05:00
|
|
|
const $picker = this.$('.js-datepicker')
|
|
|
|
.datepicker({
|
|
|
|
todayHighlight: true,
|
|
|
|
todayBtn: 'linked',
|
|
|
|
language: TAPi18n.getLanguage(),
|
2020-04-22 14:44:08 +02:00
|
|
|
weekStart: this.startDayOfWeek(),
|
2021-10-04 15:34:32 +03:00
|
|
|
calendarWeeks: true,
|
2019-06-28 12:52:09 -05:00
|
|
|
})
|
|
|
|
.on(
|
|
|
|
'changeDate',
|
|
|
|
function(evt) {
|
|
|
|
this.find('#date').value = moment(evt.date).format('L');
|
|
|
|
this.error.set('');
|
2019-09-26 10:53:40 -04:00
|
|
|
const timeInput = this.find('#time');
|
|
|
|
timeInput.focus();
|
2021-04-06 18:00:03 +02:00
|
|
|
if (!timeInput.value && this.defaultTime) {
|
2019-09-26 10:53:40 -04:00
|
|
|
const currentHour = evt.date.getHours();
|
|
|
|
const defaultMoment = moment(
|
2019-10-24 12:57:07 +02:00
|
|
|
currentHour > 0 ? evt.date : this.defaultTime,
|
2019-09-26 10:53:40 -04:00
|
|
|
); // default to 8:00 am local time
|
|
|
|
timeInput.value = defaultMoment.format('LT');
|
|
|
|
}
|
2019-06-28 12:52:09 -05:00
|
|
|
}.bind(this),
|
|
|
|
);
|
2017-10-14 01:38:25 +02:00
|
|
|
|
2018-05-18 10:24:51 +02:00
|
|
|
if (this.date.get().isValid()) {
|
|
|
|
$picker.datepicker('update', this.date.get().toDate());
|
|
|
|
}
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
2017-10-14 01:38:25 +02:00
|
|
|
|
2018-05-18 10:24:51 +02:00
|
|
|
showDate() {
|
2019-06-28 12:52:09 -05:00
|
|
|
if (this.date.get().isValid()) return this.date.get().format('L');
|
2018-05-18 10:24:51 +02:00
|
|
|
return '';
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
2018-05-18 10:24:51 +02:00
|
|
|
showTime() {
|
2019-06-28 12:52:09 -05:00
|
|
|
if (this.date.get().isValid()) return this.date.get().format('LT');
|
2018-05-18 10:24:51 +02:00
|
|
|
return '';
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
2018-05-18 10:24:51 +02:00
|
|
|
dateFormat() {
|
|
|
|
return moment.localeData().longDateFormat('L');
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
2018-05-18 10:24:51 +02:00
|
|
|
timeFormat() {
|
|
|
|
return moment.localeData().longDateFormat('LT');
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
2017-10-14 01:38:25 +02:00
|
|
|
|
2018-05-18 10:24:51 +02:00
|
|
|
events() {
|
2019-06-28 12:52:09 -05:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
'keyup .js-date-field'() {
|
|
|
|
// parse for localized date format in strict mode
|
|
|
|
const dateMoment = moment(this.find('#date').value, 'L', true);
|
|
|
|
if (dateMoment.isValid()) {
|
|
|
|
this.error.set('');
|
|
|
|
this.$('.js-datepicker').datepicker('update', dateMoment.toDate());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'keyup .js-time-field'() {
|
|
|
|
// parse for localized time format in strict mode
|
2021-02-11 19:07:34 +02:00
|
|
|
const dateMoment = moment(
|
|
|
|
this.find('#time').value,
|
|
|
|
adjustedTimeFormat(),
|
|
|
|
true,
|
|
|
|
);
|
2019-06-28 12:52:09 -05:00
|
|
|
if (dateMoment.isValid()) {
|
|
|
|
this.error.set('');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'submit .edit-date'(evt) {
|
|
|
|
evt.preventDefault();
|
2017-10-14 01:38:25 +02:00
|
|
|
|
2019-06-28 12:52:09 -05:00
|
|
|
// if no time was given, init with 12:00
|
|
|
|
const time =
|
|
|
|
evt.target.time.value ||
|
|
|
|
moment(new Date().setHours(12, 0, 0)).format('LT');
|
2021-02-11 03:55:16 +01:00
|
|
|
const newTime = moment(time, adjustedTimeFormat(), true);
|
|
|
|
const newDate = moment(evt.target.date.value, 'L', true);
|
2019-06-28 12:52:09 -05:00
|
|
|
const dateString = `${evt.target.date.value} ${time}`;
|
2021-02-11 19:07:34 +02:00
|
|
|
const newCompleteDate = moment(
|
|
|
|
dateString,
|
2021-04-06 18:00:03 +02:00
|
|
|
`L ${adjustedTimeFormat()}`,
|
2021-02-11 19:07:34 +02:00
|
|
|
true,
|
|
|
|
);
|
2021-02-11 03:55:16 +01:00
|
|
|
if (!newTime.isValid()) {
|
|
|
|
this.error.set('invalid-time');
|
|
|
|
evt.target.time.focus();
|
|
|
|
}
|
|
|
|
if (!newDate.isValid()) {
|
|
|
|
this.error.set('invalid-date');
|
|
|
|
evt.target.date.focus();
|
|
|
|
}
|
|
|
|
if (newCompleteDate.isValid()) {
|
|
|
|
this._storeDate(newCompleteDate.toDate());
|
2021-10-20 08:57:27 +02:00
|
|
|
Popup.back();
|
2021-04-06 18:00:03 +02:00
|
|
|
} else if (!this.error) {
|
|
|
|
this.error.set('invalid');
|
2019-06-28 12:52:09 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'click .js-delete-date'(evt) {
|
|
|
|
evt.preventDefault();
|
|
|
|
this._deleteDate();
|
2021-10-20 08:57:27 +02:00
|
|
|
Popup.back();
|
2019-06-28 12:52:09 -05:00
|
|
|
},
|
2018-05-18 10:24:51 +02:00
|
|
|
},
|
2019-06-28 12:52:09 -05:00
|
|
|
];
|
2021-04-06 18:00:03 +02:00
|
|
|
}
|
|
|
|
}
|