2024-04-11 06:50:30 +00:00
|
|
|
import {
|
|
|
|
FormFieldRulesConfig,
|
|
|
|
FormOptionsSortConfig,
|
|
|
|
FormSelectConfig,
|
|
|
|
} from 'app/client/components/Forms/FormConfig';
|
2023-01-11 17:57:42 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2020-10-07 21:58:43 +00:00
|
|
|
import {DataRowModel} from 'app/client/models/DataRowModel';
|
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import {KoSaveableObservable} from 'app/client/models/modelUtil';
|
2022-04-07 14:58:16 +00:00
|
|
|
import {Style} from 'app/client/models/Styles';
|
2022-08-08 13:32:50 +00:00
|
|
|
import {cssLabel, cssRow} from 'app/client/ui/RightPanelStyles';
|
2023-09-21 16:57:58 +00:00
|
|
|
import {testId, theme} from 'app/client/ui2018/cssVars';
|
2023-06-13 17:14:01 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
2022-04-07 14:58:16 +00:00
|
|
|
import {ChoiceListEntry} from 'app/client/widgets/ChoiceListEntry';
|
2023-09-21 16:57:58 +00:00
|
|
|
import {choiceToken, DEFAULT_BACKGROUND_COLOR, DEFAULT_COLOR} from 'app/client/widgets/ChoiceToken';
|
2020-10-07 21:58:43 +00:00
|
|
|
import {NTextBox} from 'app/client/widgets/NTextBox';
|
2024-02-14 21:18:09 +00:00
|
|
|
import {Computed, dom, styled} from 'grainjs';
|
2021-07-08 21:35:16 +00:00
|
|
|
|
2022-04-07 14:58:16 +00:00
|
|
|
export type IChoiceOptions = Style
|
2021-07-08 21:35:16 +00:00
|
|
|
export type ChoiceOptions = Record<string, IChoiceOptions | undefined>;
|
|
|
|
export type ChoiceOptionsByName = Map<string, IChoiceOptions | undefined>;
|
|
|
|
|
2023-01-11 17:57:42 +00:00
|
|
|
const t = makeT('ChoiceTextBox');
|
|
|
|
|
2022-04-07 14:58:16 +00:00
|
|
|
export function getRenderFillColor(choiceOptions?: IChoiceOptions) {
|
2023-09-21 16:57:58 +00:00
|
|
|
return choiceOptions?.fillColor ?? DEFAULT_BACKGROUND_COLOR;
|
2021-07-08 21:35:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 14:58:16 +00:00
|
|
|
export function getRenderTextColor(choiceOptions?: IChoiceOptions) {
|
2023-09-21 16:57:58 +00:00
|
|
|
return choiceOptions?.textColor ?? DEFAULT_COLOR;
|
2021-07-08 21:35:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
/**
|
|
|
|
* ChoiceTextBox - A textbox for choice values.
|
|
|
|
*/
|
|
|
|
export class ChoiceTextBox extends NTextBox {
|
|
|
|
private _choices: KoSaveableObservable<string[]>;
|
|
|
|
private _choiceValues: Computed<string[]>;
|
2022-06-06 17:42:51 +00:00
|
|
|
private _choiceValuesSet: Computed<Set<string>>;
|
2021-07-08 21:35:16 +00:00
|
|
|
private _choiceOptions: KoSaveableObservable<ChoiceOptions | null | undefined>;
|
(core) Speed up and upgrade build.
Summary:
- Upgrades to build-related packages:
- Upgrade typescript, related libraries and typings.
- Upgrade webpack, eslint; add tsc-watch, node-dev, eslint_d.
- Build organization changes:
- Build webpack from original typescript, transpiling only; with errors still
reported by a background tsc watching process.
- Typescript-related changes:
- Reduce imports of AWS dependencies (very noticeable speedup)
- Avoid auto-loading global @types
- Client code is now built with isolatedModules flag (for safe transpilation)
- Use allowJs to avoid copying JS files manually.
- Linting changes
- Enhance Arcanist ESLintLinter to run before/after commands, and set up to use eslint_d
- Update eslint config, and include .eslintignore to avoid linting generated files.
- Include a bunch of eslint-prompted and eslint-generated fixes
- Add no-unused-expression rule to eslint, and fix a few warnings about it
- Other items:
- Refactor cssInput to avoid circular dependency
- Remove a bit of unused code, libraries, dependencies
Test Plan: No behavior changes, all existing tests pass. There are 30 tests fewer reported because `test_gpath.py` was removed (it's been unused for years)
Reviewers: paulfitz
Reviewed By: paulfitz
Subscribers: paulfitz
Differential Revision: https://phab.getgrist.com/D3498
2022-06-27 20:09:41 +00:00
|
|
|
private _choiceOptionsByName: Computed<ChoiceOptionsByName>;
|
2020-10-07 21:58:43 +00:00
|
|
|
|
|
|
|
constructor(field: ViewFieldRec) {
|
|
|
|
super(field);
|
|
|
|
this._choices = this.options.prop('choices');
|
2021-07-08 21:35:16 +00:00
|
|
|
this._choiceOptions = this.options.prop('choiceOptions');
|
2020-10-07 21:58:43 +00:00
|
|
|
this._choiceValues = Computed.create(this, (use) => use(this._choices) || []);
|
2022-06-06 17:42:51 +00:00
|
|
|
this._choiceValuesSet = Computed.create(this, this._choiceValues, (_use, values) => new Set(values));
|
2021-07-08 21:35:16 +00:00
|
|
|
this._choiceOptionsByName = Computed.create(this, (use) => toMap(use(this._choiceOptions)));
|
2020-10-07 21:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom(row: DataRowModel) {
|
|
|
|
const value = row.cells[this.field.colId()];
|
2023-06-13 17:14:01 +00:00
|
|
|
const isSingle = this.field.viewSection().parentKey() === "single";
|
|
|
|
const maybeDropDownCssChoiceEditIcon = isSingle ? cssChoiceEditIcon('Dropdown') : null;
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
return cssChoiceField(
|
2021-07-08 21:35:16 +00:00
|
|
|
cssChoiceTextWrapper(
|
|
|
|
dom.style('justify-content', (use) => use(this.alignment) === 'right' ? 'flex-end' : use(this.alignment)),
|
2023-06-13 17:14:01 +00:00
|
|
|
maybeDropDownCssChoiceEditIcon,
|
2021-07-08 21:35:16 +00:00
|
|
|
dom.domComputed((use) => {
|
2021-12-15 14:50:55 +00:00
|
|
|
if (this.isDisposed() || use(row._isAddRow)) { return null; }
|
2021-07-08 21:35:16 +00:00
|
|
|
|
2022-01-13 10:04:56 +00:00
|
|
|
const formattedValue = use(this.valueFormatter).formatAny(use(value));
|
2021-07-15 15:50:28 +00:00
|
|
|
if (formattedValue === '') { return null; }
|
2021-07-08 21:35:16 +00:00
|
|
|
|
2021-07-15 15:50:28 +00:00
|
|
|
return choiceToken(
|
2021-07-08 21:35:16 +00:00
|
|
|
formattedValue,
|
2022-06-06 17:42:51 +00:00
|
|
|
{
|
|
|
|
...(use(this._choiceOptionsByName).get(formattedValue) || {}),
|
|
|
|
invalid: !use(this._choiceValuesSet).has(formattedValue),
|
|
|
|
},
|
2021-07-15 15:50:28 +00:00
|
|
|
dom.cls(cssChoiceText.className),
|
2022-06-06 17:42:51 +00:00
|
|
|
testId('choice-token')
|
2021-07-08 21:35:16 +00:00
|
|
|
);
|
|
|
|
}),
|
2020-10-07 21:58:43 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildConfigDom() {
|
2024-02-14 21:18:09 +00:00
|
|
|
return [
|
|
|
|
super.buildConfigDom(),
|
2024-04-11 06:50:30 +00:00
|
|
|
this.buildChoicesConfigDom(),
|
2024-02-14 21:18:09 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildTransformConfigDom() {
|
|
|
|
return this.buildConfigDom();
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildFormConfigDom() {
|
|
|
|
return [
|
2024-04-11 06:50:30 +00:00
|
|
|
this.buildChoicesConfigDom(),
|
|
|
|
dom.create(FormSelectConfig, this.field),
|
|
|
|
dom.create(FormOptionsSortConfig, this.field),
|
|
|
|
dom.create(FormFieldRulesConfig, this.field),
|
2024-02-14 21:18:09 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildFormTransformConfigDom() {
|
|
|
|
return [
|
2024-04-11 06:50:30 +00:00
|
|
|
this.buildChoicesConfigDom(),
|
2024-02-14 21:18:09 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getChoiceValuesSet(): Computed<Set<string>> {
|
|
|
|
return this._choiceValuesSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getChoiceOptions(): Computed<ChoiceOptionsByName> {
|
|
|
|
return this._choiceOptionsByName;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected save(choices: string[], choiceOptions: ChoiceOptionsByName, renames: Record<string, string>) {
|
|
|
|
const options = {
|
|
|
|
choices,
|
|
|
|
choiceOptions: toObject(choiceOptions)
|
|
|
|
};
|
|
|
|
return this.field.config.updateChoices(renames, options);
|
|
|
|
}
|
|
|
|
|
2024-04-11 06:50:30 +00:00
|
|
|
protected buildChoicesConfigDom() {
|
2022-10-14 10:07:19 +00:00
|
|
|
const disabled = Computed.create(null,
|
|
|
|
use => use(this.field.disableModify)
|
|
|
|
|| use(use(this.field.column).disableEditData)
|
|
|
|
|| use(this.field.config.options.disabled('choices'))
|
|
|
|
);
|
|
|
|
|
|
|
|
const mixed = Computed.create(null,
|
|
|
|
use => !use(disabled)
|
|
|
|
&& (use(this.field.config.options.mixed('choices')) || use(this.field.config.options.mixed('choiceOptions')))
|
|
|
|
);
|
2024-01-18 17:23:50 +00:00
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
return [
|
2023-01-11 17:57:42 +00:00
|
|
|
cssLabel(t('CHOICES')),
|
2020-10-07 21:58:43 +00:00
|
|
|
cssRow(
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.autoDispose(disabled),
|
|
|
|
dom.autoDispose(mixed),
|
2021-07-08 21:35:16 +00:00
|
|
|
dom.create(
|
|
|
|
ChoiceListEntry,
|
|
|
|
this._choiceValues,
|
|
|
|
this._choiceOptionsByName,
|
2022-01-18 11:48:57 +00:00
|
|
|
this.save.bind(this),
|
2022-10-14 10:07:19 +00:00
|
|
|
disabled,
|
|
|
|
mixed
|
2021-07-08 21:35:16 +00:00
|
|
|
)
|
2020-10-07 21:58:43 +00:00
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:35:16 +00:00
|
|
|
// Converts a POJO containing choice options to an ES6 Map
|
|
|
|
function toMap(choiceOptions?: ChoiceOptions | null): ChoiceOptionsByName {
|
|
|
|
if (!choiceOptions) { return new Map(); }
|
|
|
|
|
|
|
|
return new Map(Object.entries(choiceOptions));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts an ES6 Map containing choice options to a POJO
|
|
|
|
function toObject(choiceOptions: ChoiceOptionsByName): ChoiceOptions {
|
|
|
|
const object: ChoiceOptions = {};
|
|
|
|
for (const [choice, options] of choiceOptions.entries()) {
|
|
|
|
object[choice] = options;
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
const cssChoiceField = styled('div.field_clip', `
|
2021-07-08 21:35:16 +00:00
|
|
|
padding: 0 3px;
|
2020-10-07 21:58:43 +00:00
|
|
|
`);
|
|
|
|
|
2021-07-08 21:35:16 +00:00
|
|
|
const cssChoiceTextWrapper = styled('div', `
|
|
|
|
display: flex;
|
2020-10-07 21:58:43 +00:00
|
|
|
width: 100%;
|
2021-07-08 21:35:16 +00:00
|
|
|
min-width: 0px;
|
|
|
|
overflow: hidden;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssChoiceText = styled('div', `
|
|
|
|
margin: 2px;
|
|
|
|
height: min-content;
|
|
|
|
line-height: 16px;
|
2020-10-07 21:58:43 +00:00
|
|
|
`);
|
2023-06-13 17:14:01 +00:00
|
|
|
|
|
|
|
const cssChoiceEditIcon = styled(icon, `
|
2023-09-21 16:57:58 +00:00
|
|
|
background-color: ${theme.lightText};
|
2023-06-13 17:14:01 +00:00
|
|
|
display: block;
|
|
|
|
height: inherit;
|
|
|
|
`);
|