2020-10-07 21:58:43 +00:00
|
|
|
import {DataRowModel} from 'app/client/models/DataRowModel';
|
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import {cssLabel, cssRow} from 'app/client/ui/RightPanel';
|
|
|
|
import {colors, testId} from 'app/client/ui2018/cssVars';
|
|
|
|
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';
|
2020-10-07 21:58:43 +00:00
|
|
|
import {Computed, dom, styled} from 'grainjs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference - The widget for displaying references to another table's records.
|
|
|
|
*/
|
|
|
|
export class Reference extends NTextBox {
|
2021-07-23 15:29:35 +00:00
|
|
|
protected _formatValue: Computed<(val: any) => string>;
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
this._validCols = Computed.create(this, (use) => {
|
|
|
|
const refTable = use(use(this.field.column).refTable);
|
|
|
|
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
|
|
|
}))
|
|
|
|
.concat([{label: 'Row ID', value: 0, icon: 'FieldColumn'}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Computed returns a function that formats cell values.
|
|
|
|
this._formatValue = Computed.create(this, (use) => {
|
|
|
|
// If the field is pulling values from a display column, use a general-purpose formatter.
|
|
|
|
if (use(this.field.displayColRef) !== use(this.field.colRef)) {
|
2021-12-15 22:31:53 +00:00
|
|
|
const fmt = use(this.field.visibleColFormatter);
|
2020-10-07 21:58:43 +00:00
|
|
|
return (val: any) => fmt.formatAny(val);
|
|
|
|
} else {
|
|
|
|
const refTable = use(use(this.field.column).refTable);
|
|
|
|
const refTableId = refTable ? use(refTable.tableId) : "";
|
|
|
|
return (val: any) => val > 0 ? `${refTableId}[${val}]` : "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildConfigDom() {
|
2021-06-08 21:29:54 +00:00
|
|
|
return [
|
|
|
|
this.buildTransformConfigDom(),
|
|
|
|
cssLabel('CELL FORMAT'),
|
|
|
|
super.buildConfigDom()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildTransformConfigDom() {
|
2020-10-07 21:58:43 +00:00
|
|
|
return [
|
|
|
|
cssLabel('SHOW COLUMN'),
|
|
|
|
cssRow(
|
|
|
|
select(this._visibleColRef, this._validCols),
|
|
|
|
testId('fbuilder-ref-col-select')
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
2021-08-12 18:06:40 +00:00
|
|
|
let [value, hasBlankReference] = ['', 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).
|
2021-08-12 18:06:40 +00:00
|
|
|
return {value, hasBlankReference};
|
|
|
|
}
|
|
|
|
|
|
|
|
const displayValueObs = row.cells[use(use(this.field.displayColModel).colId)];
|
|
|
|
if (!displayValueObs) {
|
|
|
|
return {value, hasBlankReference};
|
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.
|
2021-08-12 18:06:40 +00:00
|
|
|
use(this._formatValue)(displayValue[1].local || displayValue[1].parent) :
|
|
|
|
use(this._formatValue)(displayValue);
|
|
|
|
|
|
|
|
hasBlankReference = referenceId.get() !== 0 && value.trim() === '';
|
|
|
|
|
|
|
|
return {value, hasBlankReference};
|
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),
|
2021-08-12 18:06:40 +00:00
|
|
|
cssRefIcon('FieldReference', testId('ref-link-icon')),
|
|
|
|
dom.text(use => {
|
|
|
|
if (use(referenceId) === 0) { return ''; }
|
|
|
|
if (use(formattedValue).hasBlankReference) { return '[Blank]'; }
|
|
|
|
return use(formattedValue).value;
|
|
|
|
})
|
2020-10-07 21:58:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssRefIcon = styled(icon, `
|
2021-06-08 21:29:54 +00:00
|
|
|
float: left;
|
2020-10-07 21:58:43 +00:00
|
|
|
background-color: ${colors.slate};
|
|
|
|
margin: -1px 2px 2px 0;
|
|
|
|
`);
|
2021-08-12 18:06:40 +00:00
|
|
|
|
|
|
|
const cssRef = styled('div.field_clip', `
|
|
|
|
&-blank {
|
|
|
|
color: ${colors.slate}
|
|
|
|
}
|
|
|
|
`);
|