@adaptabletools/adaptable
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
132 lines (131 loc) • 5.02 kB
JavaScript
import { ApiBase } from '../Implementation/ApiBase';
import ArrayExtensions from '../../Utilities/Extensions/ArrayExtensions';
export class ExpressionInternalApi extends ApiBase {
/**
* Checks if a column is referenced in a given Expression
* @param columnId Column to Check
* @param expression Expression to Lookup
*/
isColumnReferencedInExpression(columnId, expression) {
const colIds = this.getExpressionApi().getColumnsFromExpression(expression);
return colIds.some((c) => c === columnId);
}
evaluateExpressionInAdaptableQL(module, object, expression) {
const evaluateExpressionExternallyFn = this.getExpressionOptions()?.evaluateAdaptableQLExternally;
if (typeof evaluateExpressionExternallyFn !== 'function') {
return true;
}
const context = {
...this.getAdaptableInternalApi().buildBaseContext(),
module,
};
if (object) {
context.object = object;
}
if (expression) {
context.expression = expression;
context.referencedColumns = this.getExpressionApi()
.getColumnsFromExpression(expression)
.map((columnId) => this.getColumnApi().getColumnWithColumnId(columnId));
}
return !evaluateExpressionExternallyFn(context);
}
evaluatePredicatesInAdaptableQL(module, object, predicates) {
const evaluateExpressionExternallyFn = this.getExpressionOptions()?.evaluateAdaptableQLExternally;
if (typeof evaluateExpressionExternallyFn !== 'function') {
return true;
}
const context = {
...this.getAdaptableInternalApi().buildBaseContext(),
module,
};
if (object) {
context.object = object;
}
if (predicates) {
context.predicates = predicates;
// need to find a way to get the columns in the predicates
// cand do later
// this.getAdaptableApi().columnScopeApi.getColumnsInScope();
}
return !evaluateExpressionExternallyFn(context);
}
// Used later, when we extend to support inferring fields from data
// private getFieldsFromData<T extends Record<string, any>>(data: T): string[] {
// if (!data) {
// return [];
// }
// if (typeof data !== 'object' || Number.isNaN(data)) {
// return [];
// }
// const keys: string[] = [];
// for (const key in Object.keys(data)) {
// const candidate = data[key];
// if (
// typeof candidate === 'number' ||
// typeof candidate === 'string' ||
// typeof candidate === 'boolean' ||
// candidate instanceof Date
// ) {
// keys.push(key);
// continue;
// }
// // no support for arrays
// // for objects we go one level deep
// if (typeof candidate === 'object' && candidate !== null && !Array.isArray(candidate)) {
// const childKeys = this.getFieldsFromData(candidate);
// for (const childKey of childKeys) {
// keys.push(`${key}.${childKey}`);
// }
// }
// }
// return keys;
// }
getFieldsFromOptions() {
const fields = this.getExpressionOptions().fields;
if (fields != null && typeof fields === 'function') {
return fields(this.getAdaptableInternalApi().buildBaseContext());
}
else {
let arr = fields;
if (arr && ArrayExtensions.IsNotNullOrEmpty(arr)) {
return arr;
}
}
}
getAvailableFields(data) {
const optionFields = (this.getFieldsFromOptions() ?? [])?.map((field) => ({
...field,
label: field.label || field.name,
}));
// const existingFields = new Set<string>();
// Exclude fields already in options
// optionFields.forEach((field) => existingFields.add(field.value));
// const queriableColumns = this.getColumnApi().getQueryableColumns();
// Exclude queriable columns
// queriableColumns.forEach((column) => existingFields.add(column.field));
// const dataFields = this.getFieldsFromData(data).reduce<AdaptableField[]>((acc, field) => {
// if (!existingFields.has(field)) {
// acc.push({
// label: field,
// value: field,
// });
// }
// return acc;
// }, []);
return optionFields;
}
getFieldType(field) {
const fieldDef = this.getFieldsFromOptions()?.find((f) => f.name === field);
if (fieldDef) {
return fieldDef.dataType;
}
return 'text';
}
getFieldsOfType(type) {
if (!type) {
return this.getAvailableFields() ?? [];
}
return this.getAvailableFields()?.filter((f) => f.dataType === type) ?? [];
}
}