keeweb/app/scripts/views/fields/field-view-date.js

84 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-09-16 19:55:06 +02:00
import Pikaday from 'pikaday';
2019-09-15 14:16:32 +02:00
import { DateFormat } from 'util/formatting/date-format';
import { Locale } from 'util/locale';
import { FieldViewText } from 'views/fields/field-view-text';
2015-10-17 23:49:24 +02:00
2019-09-16 19:55:06 +02:00
class FieldViewDate extends FieldViewText {
hasOptions = false;
2019-08-18 10:17:09 +02:00
renderValue(value) {
2019-09-15 08:11:11 +02:00
let result = value ? DateFormat.dStr(value) : '';
2015-10-17 23:49:24 +02:00
if (value && this.model.lessThanNow && value < new Date()) {
result += ' ' + this.model.lessThanNow;
}
return result;
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
getEditValue(value) {
2019-09-15 08:11:11 +02:00
return value ? DateFormat.dStr(value) : '';
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
startEdit() {
2019-09-16 19:55:06 +02:00
super.startEdit();
2015-10-17 23:49:24 +02:00
this.picker = new Pikaday({
field: this.input[0],
onSelect: this.pickerSelect.bind(this),
onClose: this.pickerClose.bind(this),
defaultDate: this.value,
minDate: new Date(),
firstDay: 1,
i18n: {
previousMonth: '',
nextMonth: '',
2015-12-17 19:25:25 +01:00
months: Locale.months,
weekdays: Locale.weekdays,
weekdaysShort: Locale.weekdaysShort
2015-10-17 23:49:24 +02:00
}
});
2019-09-08 11:32:31 +02:00
this.picker.adjustPosition = this.adjustPickerPosition.bind(this);
2019-09-17 23:44:17 +02:00
setTimeout(() => this.picker.show(), 0);
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-09-08 11:32:31 +02:00
adjustPickerPosition(...args) {
window.Pikaday = Pikaday;
Pikaday.prototype.adjustPosition.apply(this.picker, args);
const shadowSpread = parseInt(this.input.css('--focus-shadow-spread')) || 0;
2019-09-08 11:32:31 +02:00
if (shadowSpread) {
const isOnTop = this.picker.el.classList.contains('top-aligned');
const offset = isOnTop ? -shadowSpread : shadowSpread;
const newTop = parseInt(this.picker.el.style.top) + offset;
this.picker.el.style.top = `${newTop}px`;
}
2019-09-16 19:55:06 +02:00
}
2019-09-08 11:32:31 +02:00
2019-08-18 10:17:09 +02:00
fieldValueBlur(e) {
2015-10-17 23:49:24 +02:00
if (!this.picker) {
2019-09-16 19:55:06 +02:00
super.fieldValueBlur(e);
2015-10-17 23:49:24 +02:00
}
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
endEdit(newVal, extra) {
2015-10-17 23:49:24 +02:00
if (this.picker) {
2019-08-16 23:05:39 +02:00
try {
this.picker.destroy();
} catch (e) {}
2015-10-17 23:49:24 +02:00
this.picker = null;
}
newVal = new Date(newVal);
if (!newVal || isNaN(newVal.getTime())) {
newVal = null;
}
2019-09-16 19:55:06 +02:00
super.endEdit(newVal, extra);
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
pickerClose() {
2015-10-17 23:49:24 +02:00
this.endEdit(this.input.val());
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
pickerSelect(dt) {
2015-10-17 23:49:24 +02:00
this.endEdit(dt);
}
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-09-15 14:16:32 +02:00
export { FieldViewDate };