Custom Expression Function Scope
Summary
- Custom Expression Functions are available in every relevant Module by default
- Limit an individual function with its own
isAvailableproperty - Use
isExpressionFunctionAvailablefor one global rule covering many functions
By default a Custom Expression Function is available in all Modules that host that kind of Expression.
There are 2 ways in which this default scope can be restricted:
- using the
isAvailableproperty in the Function object - doing a global setting using the
isExpressionFunctionAvailableproperty in Expression Options
Hint
- Use
isAvailableto keep the rule next to the function it applies to - Use the global override for one rule that covers many functions (including System functions)
Using isAvailable
The Function object allows you to configure which Modules can access your Custom Function.
This is done by using the isAvailable property.
Hint
Omit isAvailable (or return true from the callback) to keep the default: available in every relevant Module
The isAvailable property can be one of 2 types:
- a hard-coded list of Modules
Note
The Modules are of type AdaptableStandardExpressionModule and AdaptableAggregatedExpressionModule
- a function that receives the context and returns
true/false
Hint
- Use the function option when a hard-coded list of Modules is not enough
- e.g. when the decision depends on
context.module,context.functionName, or (for Column Filter)context.column
Developer Guide
Limiting Custom Expression Functions to specific Modules
expressionOptions: {
customStandardFunctions: {
THIS_BUSINESS_YEAR: {
handler(args, context) { /* ... */ },
returnType: 'boolean',
operands: [['date']],
isAvailable: ['Alert'],
},
USD_CONVERT: {
handler(args, context) { /* ... */ },
returnType: 'number',
operands: [['number', 'text']],
isAvailable: (context) => context.module === 'CalculatedColumn',
},
},
}Define the Custom Functions in expressionOptions.customStandardFunctions
In this example we provide 2 functions: THIS_BUSINESS_YEAR and USD_CONVERT
In this example we use both forms of isAvailable:
THIS_BUSINESS_YEARprovides a hard-coded list ofAdaptableStandardExpressionModulenamesUSD_CONVERTprovides a callback that receivesExpressionFunctionAvailabilityContextand returnstrue/false
Using a Global Override
The other way to limit which modules can access your function is by setting a global override.
This is achieved by leveraging the isExpressionFunctionAvailable property in Expression Options.
The callback receives GlobalExpressionFunctionAvailabilityContext (the per-function context plus defaultAvailability).
Note
- This runs after each function's own
isAvailable, and receives that result ascontext.defaultAvailability - It has the final say: it can hide a function, and it can also re-enable one that
isAvailableexcluded
// hide every Custom Function from the Grid Filter, while leaving all other decisions unchanged
expressionOptions: {
isExpressionFunctionAvailable: (context) => {
if (context.isCustom && context.module === 'GridFilter') {
return false;
}
return context.defaultAvailability;
},
}Hint
- Use it for a cross-cutting rule (hide a System operator everywhere, or vary Column Filter operators per Column)
- Return
context.defaultAvailabilityto keep the per-function result
- This example creates 2 Custom Expression Functions, each limited to one Module using a different mechanism:
THIS_BUSINESS_YEAR—Alertonly, via its ownisAvailableproperty (hard-coded list)USD_CONVERT—Calculated Columnonly, via the globalisExpressionFunctionAvailableoverride in Expression Options
- Open an Alerts Expression —
THIS_BUSINESS_YEARis in the Custom menu;USD_CONVERTis not - Open the
Dollar PriceCalculated Column —USD_CONVERTis listed;THIS_BUSINESS_YEARis not - Open Format Column or Grid Filter — neither custom function is listed
Caution
- In both use cases,
context.columnis set only when AdapTable is resolving operators for a specific Column Filter - It is
undefinedon the module-level pass — guard before using it