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';
|
2023-11-20 00:46:32 +00:00
|
|
|
import {TableRec} from 'app/client/models/DocModel';
|
2020-10-07 21:58:43 +00:00
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
2023-11-20 00:46:32 +00:00
|
|
|
import {urlState} from 'app/client/models/gristUrlState';
|
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 {hideInPrintView, testId, theme} from 'app/client/ui2018/cssVars';
|
2020-10-07 21:58:43 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
|
|
|
import {IOptionFull, select} from 'app/client/ui2018/menus';
|
|
|
|
import {NTextBox} from 'app/client/widgets/NTextBox';
|
2021-07-23 15:29:35 +00:00
|
|
|
import {isFullReferencingType, isVersions} from 'app/common/gristTypes';
|
2023-11-20 00:46:32 +00:00
|
|
|
import {UIRowId} from 'app/plugin/GristAPI';
|
2024-02-14 21:18:09 +00:00
|
|
|
import {Computed, dom, styled} from 'grainjs';
|
2020-10-07 21:58:43 +00:00
|
|
|
|
2023-01-11 17:57:42 +00:00
|
|
|
|
|
|
|
const t = makeT('Reference');
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
/**
|
|
|
|
* Reference - The widget for displaying references to another table's records.
|
|
|
|
*/
|
|
|
|
export class Reference extends NTextBox {
|
2023-11-21 20:16:38 +00:00
|
|
|
protected _refTable: Computed<TableRec | null>;
|
2020-10-07 21:58:43 +00:00
|
|
|
private _visibleColRef: Computed<number>;
|
|
|
|
private _validCols: Computed<Array<IOptionFull<number>>>;
|
|
|
|
|
|
|
|
constructor(field: ViewFieldRec) {
|
|
|
|
super(field);
|
|
|
|
|
|
|
|
this._visibleColRef = Computed.create(this, (use) => use(this.field.visibleColRef));
|
|
|
|
// Note that saveOnly is used here to prevent display value flickering on visible col change.
|
|
|
|
this._visibleColRef.onWrite((val) => this.field.visibleColRef.saveOnly(val));
|
|
|
|
|
2023-11-20 00:46:32 +00:00
|
|
|
this._refTable = Computed.create(this, (use) => use(use(this.field.column).refTable));
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
this._validCols = Computed.create(this, (use) => {
|
2023-11-20 00:46:32 +00:00
|
|
|
const refTable = use(this._refTable);
|
2020-10-07 21:58:43 +00:00
|
|
|
if (!refTable) { return []; }
|
|
|
|
return use(use(refTable.columns).getObservable())
|
|
|
|
.filter(col => !use(col.isHiddenCol))
|
|
|
|
.map<IOptionFull<number>>(col => ({
|
|
|
|
label: use(col.label),
|
|
|
|
value: col.getRowId(),
|
|
|
|
icon: 'FieldColumn',
|
2021-07-23 15:29:35 +00:00
|
|
|
disabled: isFullReferencingType(use(col.type)) || use(col.isTransforming)
|
2020-10-07 21:58:43 +00:00
|
|
|
}))
|
2023-01-11 17:57:42 +00:00
|
|
|
.concat([{label: t('Row ID'), value: 0, icon: 'FieldColumn'}]);
|
2020-10-07 21:58:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildConfigDom() {
|
2021-06-08 21:29:54 +00:00
|
|
|
return [
|
|
|
|
this.buildTransformConfigDom(),
|
2024-02-14 21:18:09 +00:00
|
|
|
cssLabel(t('CELL FORMAT')),
|
|
|
|
super.buildConfigDom(),
|
2021-06-08 21:29:54 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildTransformConfigDom() {
|
2022-10-14 10:07:19 +00:00
|
|
|
const disabled = Computed.create(null, use => use(this.field.config.multiselect));
|
2020-10-07 21:58:43 +00:00
|
|
|
return [
|
2023-01-11 17:57:42 +00:00
|
|
|
cssLabel(t('SHOW COLUMN')),
|
2020-10-07 21:58:43 +00:00
|
|
|
cssRow(
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.autoDispose(disabled),
|
|
|
|
select(this._visibleColRef, this._validCols, {
|
|
|
|
disabled
|
|
|
|
}),
|
2020-10-07 21:58:43 +00:00
|
|
|
testId('fbuilder-ref-col-select')
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-02-14 21:18:09 +00:00
|
|
|
public buildFormConfigDom() {
|
|
|
|
return [
|
|
|
|
this.buildTransformConfigDom(),
|
2024-04-11 06:50:30 +00:00
|
|
|
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 this.buildTransformConfigDom();
|
|
|
|
}
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
public buildDom(row: DataRowModel) {
|
2021-08-12 18:06:40 +00:00
|
|
|
// Note: we require 2 observables here because changes to the cell value (reference id)
|
|
|
|
// and the display value (display column) are not bundled. This can cause `formattedValue`
|
|
|
|
// to briefly display incorrect values (e.g. [Blank] when adding a reference to an empty cell)
|
|
|
|
// because the cell value changes before the display column has a chance to update.
|
|
|
|
//
|
|
|
|
// TODO: Look into a better solution (perhaps updating the display formula to return [Blank]).
|
|
|
|
const referenceId = Computed.create(null, (use) => {
|
|
|
|
const id = row.cells[use(this.field.colId)];
|
|
|
|
return id && use(id);
|
|
|
|
});
|
2020-10-07 21:58:43 +00:00
|
|
|
const formattedValue = Computed.create(null, (use) => {
|
2023-11-20 00:46:32 +00:00
|
|
|
let [value, hasBlankReference, hasRecordCard] = ['', false, false];
|
2020-10-07 21:58:43 +00:00
|
|
|
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).
|
2023-11-20 00:46:32 +00:00
|
|
|
return {value, hasBlankReference, hasRecordCard};
|
2021-08-12 18:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const displayValueObs = row.cells[use(use(this.field.displayColModel).colId)];
|
|
|
|
if (!displayValueObs) {
|
2023-11-20 00:46:32 +00:00
|
|
|
return {value, hasBlankReference, hasRecordCard};
|
2020-10-07 21:58:43 +00:00
|
|
|
}
|
2021-08-12 18:06:40 +00:00
|
|
|
|
|
|
|
const displayValue = use(displayValueObs);
|
|
|
|
value = isVersions(displayValue) ?
|
2020-10-07 21:58:43 +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.
|
2022-01-13 10:04:56 +00:00
|
|
|
use(this.field.formatter).formatAny(displayValue[1].local || displayValue[1].parent) :
|
|
|
|
use(this.field.formatter).formatAny(displayValue);
|
2021-08-12 18:06:40 +00:00
|
|
|
|
|
|
|
hasBlankReference = referenceId.get() !== 0 && value.trim() === '';
|
2023-11-20 00:46:32 +00:00
|
|
|
const refTable = use(this._refTable);
|
|
|
|
if (refTable) {
|
|
|
|
hasRecordCard = !use(use(refTable.recordCardViewSection).disabled);
|
|
|
|
}
|
2021-08-12 18:06:40 +00:00
|
|
|
|
2023-11-20 00:46:32 +00:00
|
|
|
return {value, hasBlankReference, hasRecordCard};
|
2020-10-07 21:58:43 +00:00
|
|
|
});
|
2021-08-12 18:06:40 +00:00
|
|
|
|
|
|
|
return cssRef(
|
2020-10-07 21:58:43 +00:00
|
|
|
dom.autoDispose(formattedValue),
|
2021-08-12 18:06:40 +00:00
|
|
|
dom.autoDispose(referenceId),
|
|
|
|
cssRef.cls('-blank', use => use(formattedValue).hasBlankReference),
|
2021-06-08 21:29:54 +00:00
|
|
|
dom.style('text-align', this.alignment),
|
|
|
|
dom.cls('text_wrapping', this.wrapping),
|
2023-11-20 00:46:32 +00:00
|
|
|
cssRefIcon('FieldReference',
|
|
|
|
cssRefIcon.cls('-view-as-card', use =>
|
2023-11-21 20:16:38 +00:00
|
|
|
use(referenceId) !== 0 && use(formattedValue).hasRecordCard),
|
2023-11-20 00:46:32 +00:00
|
|
|
dom.on('click', async () => {
|
|
|
|
if (referenceId.get() === 0 || !formattedValue.get().hasRecordCard) { return; }
|
|
|
|
|
|
|
|
const rowId = referenceId.get() as UIRowId;
|
|
|
|
const sectionId = this._refTable.get()?.recordCardViewSectionRef();
|
|
|
|
if (sectionId === undefined) {
|
2023-11-21 20:16:38 +00:00
|
|
|
throw new Error('Unable to open Record Card: undefined section id');
|
2023-11-20 00:46:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const anchorUrlState = {hash: {rowId, sectionId, recordCard: true}};
|
|
|
|
await urlState().pushUrl(anchorUrlState, {replace: true});
|
|
|
|
}),
|
|
|
|
dom.on('mousedown', (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
}),
|
|
|
|
hideInPrintView(),
|
|
|
|
testId('ref-link-icon'),
|
|
|
|
),
|
|
|
|
dom('span',
|
|
|
|
dom.text(use => {
|
|
|
|
if (use(referenceId) === 0) { return ''; }
|
|
|
|
if (use(formattedValue).hasBlankReference) { return '[Blank]'; }
|
|
|
|
return use(formattedValue).value;
|
|
|
|
}),
|
|
|
|
testId('ref-text'),
|
|
|
|
),
|
2020-10-07 21:58:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssRefIcon = styled(icon, `
|
2021-06-08 21:29:54 +00:00
|
|
|
float: left;
|
2023-09-21 16:57:58 +00:00
|
|
|
--icon-color: ${theme.lightText};
|
2020-10-07 21:58:43 +00:00
|
|
|
margin: -1px 2px 2px 0;
|
2023-11-20 00:46:32 +00:00
|
|
|
|
|
|
|
&-view-as-card {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
&-view-as-card:hover {
|
|
|
|
--icon-color: ${theme.controlFg};
|
|
|
|
}
|
2020-10-07 21:58:43 +00:00
|
|
|
`);
|
2021-08-12 18:06:40 +00:00
|
|
|
|
|
|
|
const cssRef = styled('div.field_clip', `
|
|
|
|
&-blank {
|
2023-09-21 16:57:58 +00:00
|
|
|
color: ${theme.lightText}
|
2021-08-12 18:06:40 +00:00
|
|
|
}
|
|
|
|
`);
|