2021-07-23 15:29:35 +00:00
|
|
|
import {DataRowModel} from 'app/client/models/DataRowModel';
|
2023-09-21 16:57:58 +00:00
|
|
|
import {testId} from 'app/client/ui2018/cssVars';
|
2021-07-23 15:29:35 +00:00
|
|
|
import {isList} from 'app/common/gristTypes';
|
|
|
|
import {dom} from 'grainjs';
|
2021-08-12 18:06:40 +00:00
|
|
|
import {cssChoiceList, cssToken} from "app/client/widgets/ChoiceListCell";
|
|
|
|
import {Reference} from "app/client/widgets/Reference";
|
|
|
|
import {choiceToken} from "app/client/widgets/ChoiceToken";
|
2021-07-23 15:29:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ReferenceList - The widget for displaying lists of references to another table's records.
|
|
|
|
*/
|
|
|
|
export class ReferenceList extends Reference {
|
|
|
|
public buildDom(row: DataRowModel) {
|
|
|
|
return cssChoiceList(
|
|
|
|
dom.cls('field_clip'),
|
|
|
|
cssChoiceList.cls('-wrap', this.wrapping),
|
|
|
|
dom.style('justify-content', use => use(this.alignment) === 'right' ? 'flex-end' : use(this.alignment)),
|
|
|
|
dom.domComputed((use) => {
|
|
|
|
|
|
|
|
if (use(row._isAddRow) || this.isDisposed() || use(this.field.displayColModel).isDisposed()) {
|
|
|
|
// Work around JS errors during certain changes (noticed when visibleCol field gets removed
|
|
|
|
// for a column using per-field settings).
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const value = row.cells[use(use(this.field.displayColModel).colId)];
|
|
|
|
if (!value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const content = use(value);
|
2021-08-12 18:06:40 +00:00
|
|
|
if (!content) { return null; }
|
|
|
|
// TODO: Figure out what the implications of this block are for ReferenceList.
|
|
|
|
// if (isVersions(content)) {
|
2021-07-23 15:29:35 +00:00
|
|
|
// // We can arrive here if the reference value is unchanged (viewed as a foreign key)
|
|
|
|
// // but the content of its displayCol has changed. Postponing doing anything about
|
|
|
|
// // this until we have three-way information for computed columns. For now,
|
|
|
|
// // just showing one version of the cell. TODO: elaborate.
|
|
|
|
// return use(this._formatValue)(content[1].local || content[1].parent);
|
|
|
|
// }
|
|
|
|
const items = isList(content) ? content.slice(1) : [content];
|
2022-01-13 10:04:56 +00:00
|
|
|
// Use field.visibleColFormatter instead of field.formatter
|
|
|
|
// because we're formatting each list element to render tokens, not the whole list.
|
|
|
|
const formatter = use(this.field.visibleColFormatter);
|
|
|
|
return items.map(item => formatter.formatAny(item));
|
2021-08-12 18:06:40 +00:00
|
|
|
},
|
|
|
|
(input) => {
|
2021-07-23 15:29:35 +00:00
|
|
|
if (!input) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-12 18:06:40 +00:00
|
|
|
return input.map(token => {
|
|
|
|
const isBlankReference = token.trim() === '';
|
|
|
|
return choiceToken(
|
|
|
|
isBlankReference ? '[Blank]' : token,
|
|
|
|
{
|
2023-09-21 16:57:58 +00:00
|
|
|
blank: isBlankReference,
|
2021-08-12 18:06:40 +00:00
|
|
|
},
|
2021-07-23 15:29:35 +00:00
|
|
|
dom.cls(cssToken.className),
|
|
|
|
testId('ref-list-cell-token')
|
2021-08-12 18:06:40 +00:00
|
|
|
);
|
|
|
|
});
|
2021-07-23 15:29:35 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|