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

82 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const FieldViewText = require('./field-view-text');
const Locale = require('../../util/locale');
const Pikaday = require('pikaday');
const Format = require('../../util/format');
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const FieldViewDate = FieldViewText.extend({
2019-08-18 10:17:09 +02:00
renderValue(value) {
2017-01-31 07:50:28 +01:00
let result = value ? Format.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-08-18 10:17:09 +02:00
getEditValue(value) {
2015-12-13 15:59:55 +01:00
return value ? Format.dStr(value) : '';
2015-10-17 23:49:24 +02:00
},
2019-08-18 10:17:09 +02:00
startEdit() {
2015-10-17 23:49:24 +02:00
FieldViewText.prototype.startEdit.call(this);
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);
2015-10-17 23:49:24 +02:00
_.defer(this.picker.show.bind(this.picker));
},
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'));
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-08-18 10:17:09 +02:00
fieldValueBlur(e) {
2015-10-17 23:49:24 +02:00
if (!this.picker) {
FieldViewText.prototype.fieldValueBlur.call(this, e);
}
},
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;
}
FieldViewText.prototype.endEdit.call(this, newVal, extra);
},
2019-08-18 10:17:09 +02:00
pickerClose() {
2015-10-17 23:49:24 +02:00
this.endEdit(this.input.val());
},
2019-08-18 10:17:09 +02:00
pickerSelect(dt) {
2015-10-17 23:49:24 +02:00
this.endEdit(dt);
}
});
module.exports = FieldViewDate;