2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* See app/common/NumberFormat for description of options we support.
|
|
|
|
*/
|
2023-01-11 17:57:42 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import {reportError} from 'app/client/models/errors';
|
2022-08-08 13:32:50 +00:00
|
|
|
import {cssLabel, cssRow} from 'app/client/ui/RightPanelStyles';
|
2022-10-14 10:07:19 +00:00
|
|
|
import {cssButtonSelect, ISelectorOption, makeButtonSelect} from 'app/client/ui2018/buttonSelect';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {testId, theme} from 'app/client/ui2018/cssVars';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
2022-10-14 10:07:19 +00:00
|
|
|
import {buildCurrencyPicker} from 'app/client/widgets/CurrencyPicker';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {NTextBox} from 'app/client/widgets/NTextBox';
|
|
|
|
import {clamp} from 'app/common/gutil';
|
|
|
|
import {buildNumberFormat, NumberFormatOptions, NumMode, NumSign} from 'app/common/NumberFormat';
|
2022-10-14 10:07:19 +00:00
|
|
|
import {BindableValue, Computed, dom, DomContents, DomElementArg,
|
|
|
|
fromKo, MultiHolder, Observable, styled} from 'grainjs';
|
|
|
|
import * as LocaleCurrency from 'locale-currency';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
|
2023-01-11 17:57:42 +00:00
|
|
|
const t = makeT('NumericTextBox');
|
2020-10-02 15:10:00 +00:00
|
|
|
const modeOptions: Array<ISelectorOption<NumMode>> = [
|
|
|
|
{value: 'currency', label: '$'},
|
|
|
|
{value: 'decimal', label: ','},
|
2021-08-26 16:35:11 +00:00
|
|
|
{value: 'percent', label: '%'},
|
|
|
|
{value: 'scientific', label: 'Exp'}
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const signOptions: Array<ISelectorOption<NumSign>> = [
|
|
|
|
{value: 'parens', label: '(-)'},
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NumericTextBox - The most basic widget for displaying numeric information.
|
|
|
|
*/
|
|
|
|
export class NumericTextBox extends NTextBox {
|
|
|
|
constructor(field: ViewFieldRec) {
|
|
|
|
super(field);
|
|
|
|
}
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
public buildConfigDom(): DomContents {
|
2020-10-02 15:10:00 +00:00
|
|
|
// Holder for all computeds created here. It gets disposed with the returned DOM element.
|
|
|
|
const holder = new MultiHolder();
|
|
|
|
|
|
|
|
// Resolved options, to show default min/max decimals, which change depending on numMode.
|
2021-08-26 16:35:11 +00:00
|
|
|
const resolved = Computed.create<Intl.ResolvedNumberFormatOptions>(holder, (use) => {
|
2022-10-14 10:07:19 +00:00
|
|
|
const {numMode} = use(this.field.config.options);
|
2021-08-26 16:35:11 +00:00
|
|
|
const docSettings = use(this.field.documentSettings);
|
|
|
|
return buildNumberFormat({numMode}, docSettings).resolvedOptions();
|
|
|
|
});
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Prepare various observables that reflect the options in the UI.
|
2022-10-14 10:07:19 +00:00
|
|
|
const fieldOptions = this.field.config.options;
|
|
|
|
const options = fromKo(fieldOptions);
|
2021-08-26 16:35:11 +00:00
|
|
|
const docSettings = fromKo(this.field.documentSettings);
|
|
|
|
const numMode = Computed.create(holder, options, (use, opts) => (opts.numMode as NumMode) || null);
|
2020-10-02 15:10:00 +00:00
|
|
|
const numSign = Computed.create(holder, options, (use, opts) => opts.numSign || null);
|
2021-08-26 16:35:11 +00:00
|
|
|
const currency = Computed.create(holder, options, (use, opts) => opts.currency);
|
2022-10-14 10:07:19 +00:00
|
|
|
const disabled = Computed.create(holder, use => use(this.field.config.options.disabled('currency')));
|
2020-10-02 15:10:00 +00:00
|
|
|
const minDecimals = Computed.create(holder, options, (use, opts) => numberOrDefault(opts.decimals, ''));
|
|
|
|
const maxDecimals = Computed.create(holder, options, (use, opts) => numberOrDefault(opts.maxDecimals, ''));
|
|
|
|
const defaultMin = Computed.create(holder, resolved, (use, res) => res.minimumFractionDigits);
|
|
|
|
const defaultMax = Computed.create(holder, resolved, (use, res) => res.maximumFractionDigits);
|
2021-08-26 16:35:11 +00:00
|
|
|
const docCurrency = Computed.create(holder, docSettings, (use, settings) =>
|
2022-04-27 17:46:24 +00:00
|
|
|
settings.currency ?? LocaleCurrency.getCurrency(settings.locale ?? 'en-US')
|
2021-08-26 16:35:11 +00:00
|
|
|
);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
// Save a value as the given property in fieldOptions observable. Set it, save, and revert
|
2020-10-02 15:10:00 +00:00
|
|
|
// on save error. This is similar to what modelUtil.setSaveValue() does.
|
|
|
|
const setSave = (prop: keyof NumberFormatOptions, value: unknown) => {
|
2022-10-14 10:07:19 +00:00
|
|
|
const orig = {...fieldOptions.peek()};
|
2020-10-02 15:10:00 +00:00
|
|
|
if (value !== orig[prop]) {
|
2022-10-14 10:07:19 +00:00
|
|
|
fieldOptions({...orig, [prop]: value, ...updateOptions(prop, value)});
|
|
|
|
fieldOptions.save().catch((err) => { reportError(err); fieldOptions(orig); });
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Prepare setters for the UI elements.
|
|
|
|
// Min/max fraction digits may range from 0 to 20; other values are invalid.
|
|
|
|
const setMinDecimals = (val?: number) => setSave('decimals', val && clamp(val, 0, 20));
|
|
|
|
const setMaxDecimals = (val?: number) => setSave('maxDecimals', val && clamp(val, 0, 20));
|
|
|
|
// Mode and Sign behave as toggles: clicking a selected on deselects it.
|
|
|
|
const setMode = (val: NumMode) => setSave('numMode', val !== numMode.get() ? val : undefined);
|
|
|
|
const setSign = (val: NumSign) => setSave('numSign', val !== numSign.get() ? val : undefined);
|
2021-08-26 16:35:11 +00:00
|
|
|
const setCurrency = (val: string|undefined) => setSave('currency', val);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
const disabledStyle = cssButtonSelect.cls('-disabled', disabled);
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
return [
|
|
|
|
super.buildConfigDom(),
|
2023-01-11 17:57:42 +00:00
|
|
|
cssLabel(t('Number Format')),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssRow(
|
2020-10-07 21:58:43 +00:00
|
|
|
dom.autoDispose(holder),
|
2022-10-14 10:07:19 +00:00
|
|
|
makeButtonSelect(numMode, modeOptions, setMode, disabledStyle, cssModeSelect.cls(''), testId('numeric-mode')),
|
|
|
|
makeButtonSelect(numSign, signOptions, setSign, disabledStyle, cssSignSelect.cls(''), testId('numeric-sign')),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2021-08-26 16:35:11 +00:00
|
|
|
dom.maybe((use) => use(numMode) === 'currency', () => [
|
2023-01-11 17:57:42 +00:00
|
|
|
cssLabel(t('Currency')),
|
2021-08-26 16:35:11 +00:00
|
|
|
cssRow(
|
|
|
|
dom.domComputed(docCurrency, (defaultCurrency) =>
|
|
|
|
buildCurrencyPicker(holder, currency, setCurrency,
|
2023-01-11 17:57:42 +00:00
|
|
|
{defaultCurrencyLabel: t(`Default currency ({{defaultCurrency}})`, {defaultCurrency}), disabled})
|
2021-08-26 16:35:11 +00:00
|
|
|
),
|
|
|
|
testId("numeric-currency")
|
|
|
|
)
|
|
|
|
]),
|
2023-01-11 17:57:42 +00:00
|
|
|
cssLabel(t('Decimals')),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssRow(
|
2022-10-14 10:07:19 +00:00
|
|
|
decimals('min', minDecimals, defaultMin, setMinDecimals, disabled, testId('numeric-min-decimals')),
|
|
|
|
decimals('max', maxDecimals, defaultMax, setMaxDecimals, disabled, testId('numeric-max-decimals')),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2020-10-07 21:58:43 +00:00
|
|
|
];
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:35:11 +00:00
|
|
|
|
|
|
|
function numberOrDefault<T>(value: unknown, def: T): number | T {
|
2022-10-14 10:07:19 +00:00
|
|
|
return value !== null && value !== undefined ? Number(value) : def;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper used by setSave() above to reset some properties when switching modes.
|
2021-08-26 16:35:11 +00:00
|
|
|
function updateOptions(prop: keyof NumberFormatOptions, value: unknown): Partial<NumberFormatOptions> {
|
2020-10-02 15:10:00 +00:00
|
|
|
// Reset the numSign to default when toggling mode to percent or scientific.
|
|
|
|
if (prop === 'numMode' && (!value || value === 'scientific' || value === 'percent')) {
|
|
|
|
return {numSign: undefined};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function decimals(
|
|
|
|
label: string,
|
2021-08-26 16:35:11 +00:00
|
|
|
value: Observable<number | ''>,
|
2020-10-02 15:10:00 +00:00
|
|
|
defaultValue: Observable<number>,
|
2022-10-14 10:07:19 +00:00
|
|
|
setFunc: (val?: number) => void,
|
|
|
|
disabled: BindableValue<boolean>,
|
|
|
|
...args: DomElementArg[]
|
2020-10-02 15:10:00 +00:00
|
|
|
) {
|
|
|
|
return cssDecimalsBox(
|
2022-10-14 10:07:19 +00:00
|
|
|
cssDecimalsBox.cls('-disabled', disabled),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssNumLabel(label),
|
|
|
|
cssNumInput({type: 'text', size: '2', min: '0'},
|
|
|
|
dom.prop('value', value),
|
|
|
|
dom.prop('placeholder', defaultValue),
|
|
|
|
dom.on('change', (ev, elem) => {
|
|
|
|
const newVal = parseInt(elem.value, 10);
|
|
|
|
// Set value explicitly before its updated via setFunc; this way the value reflects the
|
|
|
|
// observable in the case the observable is left unchanged (e.g. because of clamping).
|
|
|
|
elem.value = String(value.get());
|
|
|
|
setFunc(Number.isNaN(newVal) ? undefined : newVal);
|
|
|
|
elem.blur();
|
|
|
|
}),
|
|
|
|
dom.on('focus', (ev, elem) => elem.select()),
|
|
|
|
),
|
|
|
|
cssSpinner(
|
|
|
|
cssSpinnerBtn(cssSpinnerTop('DropdownUp'),
|
|
|
|
dom.on('click', () => setFunc(numberOrDefault(value.get(), defaultValue.get()) + 1))),
|
|
|
|
cssSpinnerBtn(cssSpinnerBottom('Dropdown'),
|
|
|
|
dom.on('click', () => setFunc(numberOrDefault(value.get(), defaultValue.get()) - 1))),
|
|
|
|
),
|
|
|
|
...args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssDecimalsBox = styled('div', `
|
|
|
|
position: relative;
|
|
|
|
flex: auto;
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.lightText};
|
|
|
|
color: ${theme.lightText};
|
2020-10-02 15:10:00 +00:00
|
|
|
font-weight: normal;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
&:first-child {
|
|
|
|
margin-right: 16px;
|
|
|
|
}
|
2022-10-14 10:07:19 +00:00
|
|
|
&-disabled {
|
|
|
|
background-color: ${theme.rightPanelToggleButtonDisabledBg};
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssNumLabel = styled('div', `
|
|
|
|
position: absolute;
|
|
|
|
padding-left: 8px;
|
|
|
|
pointer-events: none;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssNumInput = styled('input', `
|
|
|
|
padding: 4px 32px 4px 40px;
|
2022-09-06 01:51:57 +00:00
|
|
|
border: 1px solid ${theme.inputBorder};
|
2020-10-02 15:10:00 +00:00
|
|
|
border-radius: 3px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.inputBg};
|
|
|
|
color: ${theme.inputFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
width: 100%;
|
|
|
|
text-align: right;
|
|
|
|
appearance: none;
|
|
|
|
-moz-appearance: none;
|
|
|
|
-webkit-appearance: none;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSpinner = styled('div', `
|
|
|
|
position: absolute;
|
|
|
|
right: 8px;
|
|
|
|
width: 16px;
|
|
|
|
height: 100%;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSpinnerBtn = styled('div', `
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.controlSecondaryFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
flex: 1 1 0px;
|
|
|
|
min-height: 0px;
|
|
|
|
position: relative;
|
|
|
|
cursor: pointer;
|
|
|
|
overflow: hidden;
|
|
|
|
&:hover {
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.controlSecondaryHoverFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSpinnerTop = styled(icon, `
|
|
|
|
position: absolute;
|
|
|
|
top: 0px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSpinnerBottom = styled(icon, `
|
|
|
|
position: absolute;
|
|
|
|
bottom: 0px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssModeSelect = styled(makeButtonSelect, `
|
|
|
|
flex: 4 4 0px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.inputBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSignSelect = styled(makeButtonSelect, `
|
|
|
|
flex: 1 1 0px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.inputBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
margin-left: 16px;
|
|
|
|
`);
|