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() {
|
|
|
|
return moment.localeData().longDateFormat('LT').replace(/HH/i, 'H');
|
|
|
|
}
|
|
|
|
|
2017-10-14 01:38:25 +02:00
|
|
|
DatePicker = BlazeComponent.extendComponent({
|
2018-05-18 10:24:51 +02:00
|
|
|
template() {
|
|
|
|
return 'datepicker';
|
|
|
|
},
|
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;
|
2018-05-18 10:24:51 +02:00
|
|
|
},
|
2017-10-14 01:38:25 +02:00
|
|
|
|
2020-04-22 14:44:08 +02:00
|
|
|
startDayOfWeek() {
|
|
|
|
const currentUser = Meteor.user();
|
|
|
|
if (currentUser) {
|
|
|
|
return currentUser.getStartDayOfWeek();
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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(),
|
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();
|
|
|
|
if (!timeInput.value) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
},
|
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 '';
|
|
|
|
},
|
|
|
|
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 '';
|
|
|
|
},
|
|
|
|
dateFormat() {
|
|
|
|
return moment.localeData().longDateFormat('L');
|
|
|
|
},
|
|
|
|
timeFormat() {
|
|
|
|
return moment.localeData().longDateFormat('LT');
|
|
|
|
},
|
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 03:54:12 +01: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');
|
|
|
|
const dateString = `${evt.target.date.value} ${time}`;
|
2021-02-11 03:54:12 +01:00
|
|
|
const newDate = moment(dateString, 'L ' + adjustedTimeFormat(), true);
|
2019-06-28 12:52:09 -05:00
|
|
|
if (newDate.isValid()) {
|
|
|
|
this._storeDate(newDate.toDate());
|
|
|
|
Popup.close();
|
|
|
|
} else {
|
2021-02-11 03:54:12 +01:00
|
|
|
this.error.set('invalid');
|
2019-06-28 12:52:09 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'click .js-delete-date'(evt) {
|
|
|
|
evt.preventDefault();
|
|
|
|
this._deleteDate();
|
2018-05-18 10:24:51 +02:00
|
|
|
Popup.close();
|
2019-06-28 12:52:09 -05:00
|
|
|
},
|
2018-05-18 10:24:51 +02:00
|
|
|
},
|
2019-06-28 12:52:09 -05:00
|
|
|
];
|
2018-05-18 10:24:51 +02:00
|
|
|
},
|
|
|
|
});
|