2021-07-08 21:35:16 +00:00
|
|
|
import {ChoiceListEntry} from 'app/client/widgets/ChoiceListEntry';
|
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';
|
|
|
|
import {cssLabel, cssRow} from 'app/client/ui/RightPanel';
|
2021-07-15 15:50:28 +00:00
|
|
|
import {testId} from 'app/client/ui2018/cssVars';
|
2020-10-07 21:58:43 +00:00
|
|
|
import {NTextBox} from 'app/client/widgets/NTextBox';
|
|
|
|
import {Computed, dom, styled} from 'grainjs';
|
2021-07-15 15:50:28 +00:00
|
|
|
import {choiceToken, DEFAULT_FILL_COLOR, DEFAULT_TEXT_COLOR} from 'app/client/widgets/ChoiceToken';
|
2020-10-07 21:58:43 +00:00
|
|
|
|
2021-07-08 21:35:16 +00:00
|
|
|
export interface IChoiceOptions {
|
|
|
|
textColor: string;
|
|
|
|
fillColor: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ChoiceOptions = Record<string, IChoiceOptions | undefined>;
|
|
|
|
export type ChoiceOptionsByName = Map<string, IChoiceOptions | undefined>;
|
|
|
|
|
|
|
|
export function getFillColor(choiceOptions?: IChoiceOptions) {
|
|
|
|
return choiceOptions?.fillColor ?? DEFAULT_FILL_COLOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTextColor(choiceOptions?: IChoiceOptions) {
|
|
|
|
return choiceOptions?.textColor ?? DEFAULT_TEXT_COLOR;
|
|
|
|
}
|
|
|
|
|
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[]>;
|
2021-07-08 21:35:16 +00:00
|
|
|
private _choiceOptions: KoSaveableObservable<ChoiceOptions | null | undefined>;
|
|
|
|
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) || []);
|
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()];
|
|
|
|
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)),
|
|
|
|
dom.domComputed((use) => {
|
2021-07-15 15:50:28 +00:00
|
|
|
if (use(row._isAddRow)) { return null; }
|
2021-07-08 21:35:16 +00:00
|
|
|
|
|
|
|
const formattedValue = use(this.valueFormatter).format(use(value));
|
2021-07-15 15:50:28 +00:00
|
|
|
if (formattedValue === '') { return null; }
|
2021-07-08 21:35:16 +00:00
|
|
|
|
|
|
|
const choiceOptions = use(this._choiceOptionsByName).get(formattedValue);
|
2021-07-15 15:50:28 +00:00
|
|
|
return choiceToken(
|
2021-07-08 21:35:16 +00:00
|
|
|
formattedValue,
|
2021-07-15 15:50:28 +00:00
|
|
|
choiceOptions || {},
|
|
|
|
dom.cls(cssChoiceText.className),
|
2021-07-08 21:35:16 +00:00
|
|
|
testId('choice-text')
|
|
|
|
);
|
|
|
|
}),
|
2020-10-07 21:58:43 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildConfigDom() {
|
|
|
|
return [
|
2021-05-12 14:34:49 +00:00
|
|
|
super.buildConfigDom(),
|
2021-07-08 21:35:16 +00:00
|
|
|
cssLabel('CHOICES'),
|
2020-10-07 21:58:43 +00:00
|
|
|
cssRow(
|
2021-07-08 21:35:16 +00:00
|
|
|
dom.create(
|
|
|
|
ChoiceListEntry,
|
|
|
|
this._choiceValues,
|
|
|
|
this._choiceOptionsByName,
|
2021-10-06 13:12:45 +00:00
|
|
|
this.save.bind(this)
|
2021-07-08 21:35:16 +00:00
|
|
|
)
|
2020-10-07 21:58:43 +00:00
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildTransformConfigDom() {
|
|
|
|
return this.buildConfigDom();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:34:49 +00:00
|
|
|
protected getChoiceValues(): Computed<string[]> {
|
|
|
|
return this._choiceValues;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:35:16 +00:00
|
|
|
protected getChoiceOptions(): Computed<ChoiceOptionsByName> {
|
|
|
|
return this._choiceOptionsByName;
|
|
|
|
}
|
2021-10-06 13:12:45 +00:00
|
|
|
|
|
|
|
protected save(choices: string[], choiceOptions: ChoiceOptionsByName, renames: Record<string, string>) {
|
|
|
|
const options = {
|
|
|
|
...this.options.peek(),
|
|
|
|
choices,
|
|
|
|
choiceOptions: toObject(choiceOptions)
|
|
|
|
};
|
|
|
|
return this.field.updateChoices(renames, options);
|
|
|
|
}
|
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
|
|
|
`);
|