2021-12-06 12:07:52 +00:00
|
|
|
import {DocData} from 'app/client/models/DocData';
|
2021-11-09 12:11:37 +00:00
|
|
|
import {ColumnRec} from 'app/client/models/entities/ColumnRec';
|
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
2021-12-06 12:07:52 +00:00
|
|
|
import {TableData} from 'app/client/models/TableData';
|
2021-11-09 12:11:37 +00:00
|
|
|
import {getReferencedTableId, isRefListType} from 'app/common/gristTypes';
|
|
|
|
import {BaseFormatter} from 'app/common/ValueFormatter';
|
2021-11-01 15:48:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utilities for common operations involving Ref[List] fields.
|
|
|
|
*/
|
|
|
|
export class ReferenceUtils {
|
|
|
|
public readonly refTableId: string;
|
|
|
|
public readonly tableData: TableData;
|
|
|
|
public readonly formatter: BaseFormatter;
|
|
|
|
public readonly visibleColModel: ColumnRec;
|
|
|
|
public readonly visibleColId: string;
|
2021-11-09 12:11:37 +00:00
|
|
|
public readonly isRefList: boolean;
|
2021-11-01 15:48:08 +00:00
|
|
|
|
|
|
|
constructor(public readonly field: ViewFieldRec, docData: DocData) {
|
|
|
|
const colType = field.column().type();
|
|
|
|
const refTableId = getReferencedTableId(colType);
|
|
|
|
if (!refTableId) {
|
|
|
|
throw new Error("Non-Reference column of type " + colType);
|
|
|
|
}
|
|
|
|
this.refTableId = refTableId;
|
|
|
|
|
|
|
|
const tableData = docData.getTable(refTableId);
|
|
|
|
if (!tableData) {
|
|
|
|
throw new Error("Invalid referenced table " + refTableId);
|
|
|
|
}
|
|
|
|
this.tableData = tableData;
|
|
|
|
|
2021-12-15 22:31:53 +00:00
|
|
|
this.formatter = field.visibleColFormatter();
|
2021-11-01 15:48:08 +00:00
|
|
|
this.visibleColModel = field.visibleColModel();
|
|
|
|
this.visibleColId = this.visibleColModel.colId() || 'id';
|
2021-11-09 12:11:37 +00:00
|
|
|
this.isRefList = isRefListType(colType);
|
2021-11-01 15:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public idToText(value: unknown) {
|
|
|
|
if (typeof value === 'number') {
|
|
|
|
return this.formatter.formatAny(this.tableData.getValue(value, this.visibleColId));
|
|
|
|
}
|
|
|
|
return String(value || '');
|
|
|
|
}
|
|
|
|
|
|
|
|
public autocompleteSearch(text: string) {
|
|
|
|
const acIndex = this.tableData.columnACIndexes.getColACIndex(this.visibleColId, this.formatter);
|
|
|
|
return acIndex.search(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function nocaseEqual(a: string, b: string) {
|
|
|
|
return a.trim().toLowerCase() === b.trim().toLowerCase();
|
|
|
|
}
|