2020-10-02 15:10:00 +00:00
|
|
|
var _ = require('underscore');
|
|
|
|
var ko = require('knockout');
|
|
|
|
var dom = require('../lib/dom');
|
|
|
|
var dispose = require('../lib/dispose');
|
|
|
|
var kd = require('../lib/koDom');
|
|
|
|
var kf = require('../lib/koForm');
|
|
|
|
var AbstractWidget = require('./AbstractWidget');
|
|
|
|
|
|
|
|
const {fromKoSave} = require('app/client/lib/fromKoSave');
|
2022-10-14 10:07:19 +00:00
|
|
|
const {alignmentSelect, cssButtonSelect} = require('app/client/ui2018/buttonSelect');
|
2022-08-08 13:32:50 +00:00
|
|
|
const {cssRow, cssLabel} = require('app/client/ui/RightPanelStyles');
|
2021-07-08 07:29:30 +00:00
|
|
|
const {cssTextInput} = require("app/client/ui2018/editableLabel");
|
|
|
|
const {styled, fromKo} = require('grainjs');
|
|
|
|
const {select} = require('app/client/ui2018/menus');
|
2022-02-21 14:45:17 +00:00
|
|
|
const {dateFormatOptions} = require('app/common/parseDate');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DateTextBox - The most basic widget for displaying simple date information.
|
|
|
|
*/
|
|
|
|
function DateTextBox(field) {
|
|
|
|
AbstractWidget.call(this, field);
|
|
|
|
|
|
|
|
this.alignment = this.options.prop('alignment');
|
2022-10-14 10:07:19 +00:00
|
|
|
|
|
|
|
// These properties are only used in configuration.
|
|
|
|
this.dateFormat = this.field.config.options.prop('dateFormat');
|
|
|
|
this.isCustomDateFormat = this.field.config.options.prop('isCustomDateFormat');
|
|
|
|
this.mixedDateFormat = ko.pureComputed(() => this.dateFormat() === null || this.isCustomDateFormat() === null);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Helper to set 'dateFormat' and 'isCustomDateFormat' from the set of default date format strings.
|
|
|
|
this.standardDateFormat = this.autoDispose(ko.computed({
|
|
|
|
owner: this,
|
2022-10-14 10:07:19 +00:00
|
|
|
read: function() { return this.mixedDateFormat() ? null : this.isCustomDateFormat() ? 'Custom' : this.dateFormat(); },
|
2020-10-02 15:10:00 +00:00
|
|
|
write: function(val) {
|
|
|
|
if (val === 'Custom') { this.isCustomDateFormat.setAndSave(true); }
|
|
|
|
else {
|
2022-10-14 10:07:19 +00:00
|
|
|
this.field.config.options.update({isCustomDateFormat: false, dateFormat: val});
|
|
|
|
this.field.config.options.save();
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// An observable that always returns `UTC`, eases DateTimeEditor inheritance.
|
|
|
|
this.timezone = ko.observable('UTC');
|
|
|
|
}
|
|
|
|
dispose.makeDisposable(DateTextBox);
|
|
|
|
_.extend(DateTextBox.prototype, AbstractWidget.prototype);
|
|
|
|
|
|
|
|
DateTextBox.prototype.buildDateConfigDom = function() {
|
2022-10-14 10:07:19 +00:00
|
|
|
const disabled = this.field.config.options.disabled('dateFormat');
|
2020-10-02 15:10:00 +00:00
|
|
|
return dom('div',
|
2021-07-08 07:29:30 +00:00
|
|
|
cssLabel("Date Format"),
|
2022-10-14 10:07:19 +00:00
|
|
|
cssRow(dom(select(
|
|
|
|
fromKo(this.standardDateFormat),
|
|
|
|
[...dateFormatOptions, "Custom"],
|
|
|
|
{ disabled, defaultLabel: "Mixed format" },
|
|
|
|
), dom.testId("Widget_dateFormat"))),
|
|
|
|
kd.maybe(() => !this.mixedDateFormat() && this.isCustomDateFormat(), () => {
|
|
|
|
return cssRow(dom(
|
|
|
|
textbox(this.dateFormat, { disabled }),
|
|
|
|
dom.testId("Widget_dateCustomFormat")));
|
2020-10-02 15:10:00 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DateTextBox.prototype.buildConfigDom = function() {
|
|
|
|
return dom('div',
|
|
|
|
this.buildDateConfigDom(),
|
|
|
|
cssRow(
|
2022-10-14 10:07:19 +00:00
|
|
|
alignmentSelect(
|
|
|
|
fromKoSave(this.field.config.options.prop('alignment')),
|
|
|
|
cssButtonSelect.cls('-disabled', this.field.config.options.disabled('alignment')),
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DateTextBox.prototype.buildTransformConfigDom = function() {
|
|
|
|
return this.buildDateConfigDom();
|
|
|
|
};
|
|
|
|
|
|
|
|
DateTextBox.prototype.buildDom = function(row) {
|
|
|
|
let value = row[this.field.colId()];
|
|
|
|
return dom('div.field_clip',
|
|
|
|
kd.style('text-align', this.alignment),
|
2023-08-22 07:17:01 +00:00
|
|
|
kd.text(() => row._isAddRow() || this.isDisposed() ? '' : this.valueFormatter().format(value()))
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-07-08 07:29:30 +00:00
|
|
|
// clean up old koform styles
|
|
|
|
const cssClean = styled('div', `
|
|
|
|
flex: 1;
|
|
|
|
margin: 0px;
|
|
|
|
`)
|
|
|
|
|
|
|
|
// override focus - to look like modern ui
|
|
|
|
const cssFocus = styled('div', `
|
|
|
|
&:focus {
|
|
|
|
outline: none;
|
|
|
|
box-shadow: 0 0 3px 2px var(--grist-color-cursor);
|
|
|
|
border: 1px solid transparent;
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
// helper method to create old style textbox that looks like a new one
|
2022-10-14 10:07:19 +00:00
|
|
|
function textbox(value, options) {
|
|
|
|
const textDom = kf.text(value, options ?? {});
|
2021-07-08 07:29:30 +00:00
|
|
|
const tzInput = textDom.querySelector('input');
|
|
|
|
dom(tzInput,
|
|
|
|
kd.cssClass(cssTextInput.className),
|
|
|
|
kd.cssClass(cssFocus.className)
|
|
|
|
);
|
|
|
|
dom(textDom,
|
|
|
|
kd.cssClass(cssClean.className)
|
|
|
|
);
|
|
|
|
return textDom;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
module.exports = DateTextBox;
|