wekan/client/lib/datepicker.js

126 lines
3.5 KiB
JavaScript
Raw Normal View History

import { ReactiveCache } from '/imports/reactiveCache';
import { TAPi18n } from '/imports/i18n';
import moment from 'moment/min/moment-with-locales';
// 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');
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());
this.defaultTime = defaultTime;
2021-04-06 18:00:03 +02:00
}
2017-10-14 01:38:25 +02:00
startDayOfWeek() {
const currentUser = ReactiveCache.getCurrentUser();
if (currentUser) {
return currentUser.getStartDayOfWeek();
} else {
return 1;
}
2021-04-06 18:00:03 +02:00
}
2018-05-18 10:24:51 +02:00
onRendered() {
// Set initial values for native HTML inputs
2018-05-18 10:24:51 +02:00
if (this.date.get().isValid()) {
const dateInput = this.find('#date');
const timeInput = this.find('#time');
if (dateInput) {
dateInput.value = this.date.get().format('YYYY-MM-DD');
}
if (timeInput && !timeInput.value && this.defaultTime) {
const defaultMoment = moment(this.defaultTime);
timeInput.value = defaultMoment.format('HH:mm');
} else if (timeInput && this.date.get().isValid()) {
timeInput.value = this.date.get().format('HH:mm');
}
2018-05-18 10:24:51 +02:00
}
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() {
if (this.date.get().isValid()) return this.date.get().format('YYYY-MM-DD');
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() {
if (this.date.get().isValid()) return this.date.get().format('HH:mm');
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() {
return [
{
'change .js-date-field'() {
// Native HTML date input validation
const dateValue = this.find('#date').value;
if (dateValue) {
const dateMoment = moment(dateValue, 'YYYY-MM-DD', true);
if (dateMoment.isValid()) {
this.error.set('');
} else {
this.error.set('invalid-date');
}
}
},
'change .js-time-field'() {
// Native HTML time input validation
const timeValue = this.find('#time').value;
if (timeValue) {
const timeMoment = moment(timeValue, 'HH:mm', true);
if (timeMoment.isValid()) {
this.error.set('');
} else {
this.error.set('invalid-time');
}
}
},
'submit .edit-date'(evt) {
evt.preventDefault();
const dateValue = evt.target.date.value;
const timeValue = evt.target.time.value || '12:00'; // Default to 12:00 if no time given
if (!dateValue) {
this.error.set('invalid-date');
evt.target.date.focus();
return;
}
const newCompleteDate = moment(`${dateValue} ${timeValue}`, 'YYYY-MM-DD HH:mm', true);
if (!newCompleteDate.isValid()) {
this.error.set('invalid');
return;
}
this._storeDate(newCompleteDate.toDate());
Popup.back();
},
'click .js-delete-date'(evt) {
evt.preventDefault();
this._deleteDate();
Popup.back();
},
2018-05-18 10:24:51 +02:00
},
];
2021-04-06 18:00:03 +02:00
}
}