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

69 lines
1.9 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({
2015-10-17 23:49:24 +02:00
renderValue: function(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;
},
getEditValue: function(value) {
2015-12-13 15:59:55 +01:00
return value ? Format.dStr(value) : '';
2015-10-17 23:49:24 +02:00
},
startEdit: function() {
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
}
});
_.defer(this.picker.show.bind(this.picker));
},
fieldValueBlur: function(e) {
if (!this.picker) {
FieldViewText.prototype.fieldValueBlur.call(this, e);
}
},
endEdit: function(newVal, extra) {
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);
},
pickerClose: function() {
this.endEdit(this.input.val());
},
pickerSelect: function(dt) {
this.endEdit(dt);
}
});
module.exports = FieldViewDate;