2020-10-02 15:10:00 +00:00
|
|
|
var _ = require('underscore');
|
|
|
|
var ko = require('knockout');
|
|
|
|
var moment = require('moment-timezone');
|
|
|
|
var dom = require('../lib/dom');
|
|
|
|
var dispose = require('../lib/dispose');
|
|
|
|
var kd = require('../lib/koDom');
|
|
|
|
var kf = require('../lib/koForm');
|
|
|
|
var DateTextBox = require('./DateTextBox');
|
|
|
|
var gutil = require('app/common/gutil');
|
|
|
|
|
|
|
|
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");
|
2021-07-22 22:17:55 +00:00
|
|
|
const {dom: gdom, styled, fromKo} = require('grainjs');
|
2021-07-08 07:29:30 +00:00
|
|
|
const {select} = require('app/client/ui2018/menus');
|
2021-07-22 22:17:55 +00:00
|
|
|
const {buildTZAutocomplete} = require('app/client/widgets/TZAutocomplete');
|
2022-02-21 14:45:17 +00:00
|
|
|
const {timeFormatOptions} = require("app/common/parseDate");
|
2021-07-08 07:29:30 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DateTimeTextBox - The most basic widget for displaying date and time information.
|
|
|
|
*/
|
|
|
|
function DateTimeTextBox(field) {
|
|
|
|
DateTextBox.call(this, field);
|
|
|
|
|
|
|
|
// Returns the timezone from the end of the type string
|
2021-07-22 22:17:55 +00:00
|
|
|
this._timezone = this.autoDispose(ko.computed(() =>
|
|
|
|
gutil.removePrefix(field.column().type(), "DateTime:")));
|
|
|
|
|
|
|
|
this._setTimezone = (val) => field.column().type.setAndSave('DateTime:' + val);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
this.timeFormat = this.field.config.options.prop('timeFormat');
|
|
|
|
this.isCustomTimeFormat = this.field.config.options.prop('isCustomTimeFormat');
|
|
|
|
this.mixedTimeFormat = ko.pureComputed(() => this.timeFormat() === null || this.isCustomTimeFormat() === null);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Helper to set 'timeFormat' and 'isCustomTimeFormat' from the set of default time format strings.
|
|
|
|
this.standardTimeFormat = this.autoDispose(ko.computed({
|
|
|
|
owner: this,
|
|
|
|
read: function() { return this.isCustomTimeFormat() ? 'Custom' : this.timeFormat(); },
|
|
|
|
write: function(val) {
|
|
|
|
if (val === 'Custom') { this.isCustomTimeFormat.setAndSave(true); }
|
|
|
|
else {
|
|
|
|
this.isCustomTimeFormat.setAndSave(false);
|
|
|
|
this.timeFormat.setAndSave(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
dispose.makeDisposable(DateTimeTextBox);
|
|
|
|
_.extend(DateTimeTextBox.prototype, DateTextBox.prototype);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the config dom for the DateTime TextBox. If isTransformConfig is true,
|
|
|
|
* builds only the necessary dom for the transform config menu.
|
|
|
|
*/
|
|
|
|
DateTimeTextBox.prototype.buildConfigDom = function(isTransformConfig) {
|
2022-10-14 10:07:19 +00:00
|
|
|
const disabled = ko.pureComputed(() => {
|
|
|
|
return this.field.config.options.disabled('timeFormat')() || this.field.column().disableEditData();
|
|
|
|
});
|
|
|
|
const alignment = fromKoSave(this.field.config.options.prop('alignment'));
|
2020-10-02 15:10:00 +00:00
|
|
|
return dom('div',
|
2021-07-08 07:29:30 +00:00
|
|
|
cssLabel("Timezone"),
|
2022-01-18 11:48:57 +00:00
|
|
|
cssRow(
|
|
|
|
gdom.create(buildTZAutocomplete, moment, fromKo(this._timezone), this._setTimezone,
|
2022-10-14 10:07:19 +00:00
|
|
|
{ disabled : fromKo(disabled)}),
|
2022-01-18 11:48:57 +00:00
|
|
|
),
|
2022-10-14 10:07:19 +00:00
|
|
|
this.buildDateConfigDom(),
|
2021-07-08 07:29:30 +00:00
|
|
|
cssLabel("Time Format"),
|
2022-10-14 10:07:19 +00:00
|
|
|
cssRow(dom(
|
|
|
|
select(
|
|
|
|
fromKo(this.standardTimeFormat),
|
|
|
|
[...timeFormatOptions, "Custom"],
|
|
|
|
{ disabled : fromKo(disabled), defaultLabel: 'Mixed format' }
|
|
|
|
),
|
|
|
|
dom.testId("Widget_timeFormat")
|
|
|
|
)),
|
|
|
|
kd.maybe(() => !this.mixedTimeFormat() && this.isCustomTimeFormat(), () => {
|
|
|
|
return cssRow(
|
|
|
|
dom(
|
|
|
|
textbox(this.timeFormat, { disabled: this.field.config.options.disabled('timeFormat')}),
|
|
|
|
dom.testId("Widget_timeCustomFormat")
|
|
|
|
)
|
|
|
|
);
|
2020-10-02 15:10:00 +00:00
|
|
|
}),
|
|
|
|
isTransformConfig ? null : cssRow(
|
2022-10-14 10:07:19 +00:00
|
|
|
alignmentSelect(
|
|
|
|
alignment,
|
|
|
|
cssButtonSelect.cls('-disabled', this.field.config.options.disabled('alignment')),
|
|
|
|
)
|
2020-10-02 15:10:00 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DateTimeTextBox.prototype.buildTransformConfigDom = function() {
|
|
|
|
return this.buildConfigDom(true);
|
|
|
|
};
|
|
|
|
|
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 #5e9ed6;
|
|
|
|
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 = DateTimeTextBox;
|