Display Scripts
Display scripts a javascript code snippets that allow for automated actions related to the display of settings within the Fig Web application. They can be added to any setting and will be executed when the web application is loaded or when the setting value is changed.
Display scripts provide the freedom to automate how settings are displayed or validated to make the configuration experience as intuitive as possibe. For example, Fig supports validation of settings based on a regular expression. However, with display scripts, settings can be validated via code and can even take into account the values of other settings within that client. All settings within that client are loaded into the javascript context and can be read or written to.
The javascript engine is powered by Jint. It has limited functionality as there are no modules or packages loaded so this means only vanilla javascript code is permitted.
Code Interface
The following interface is available for each non data grid setting within a client:
string Name { get; }
bool IsValid { get; set; }
string ValidationExplanation { get; set; }
bool Advanced { get; set; }
int EditorLineCount { get; set; }
int DisplayOrder { get; set; }
bool IsVisible { get; set; }
string CategoryColor { get; set; }
string CategoryName { get; set; }
string[] ValidValues { get; set; }
bool IsReadOnly { get; set; }
object? Value { get; set; }
void log(string message)
For example, if you wanted to change if a setting (``ModeASetting) is visible depending on the value of another setting (
Mode`), you could write the following:
if (Mode.Value == 'Mode A') {
ModeASetting.IsVisible = true;
} else {
ModeASetting.IsVisible = false;
}
Data grids have a different interface to allow developers to apply rules to individual cells within the grid. The interface is as follows:
string Name { get; }
ExpandoObject[] ValidationErrors { get; set; }
bool Advanced { get; set; }
ExpandoObject[] EditorLineCount { get; set; }
int DisplayOrder { get; set; }
bool IsVisible { get; set; }
string CategoryColor { get; set; }
string CategoryName { get; set; }
ExpandoObject[] ValidValues { get; set; }
ExpandoObject[] IsReadOnly { get; set; }
ExpandoObject[]? Value { get; set; }
void log(string message)
The expando object maps to a Dictionary<string, object?>
where the string is the property name (the name of the column) and the object is the relevent value. Depending on the property, it may be a bool, string array, string, etc.
For example, to set the age for the second row of a data grid called People
with 3 rows and columns Name, Age, Pet, the following code will do that:
People.Value[1].Age = 55
Alternatively, the following code will set the valid values for the pet property:
People.ValidValues[1].Pet = ['Cat', 'Dog', 'Fish']
Property Name | Description | Example |
---|---|---|
Name | The name of the setting. This value can only be read and not written to. | log(MySetting.Name) |
IsValid | False if the value of this setting is not valid. In this case, the ValidationExplanation will be shown below the setting. | MySetting.IsValid = false |
ValidationExplanation | The text that will show if the setting is not valid. | MySetting.ValidationExplanation = 'Some reason' |
ValidationErrors | This is an array of expando objects which is a string for each cell in the data grid. If any cell has a validation error, it will be automatically displayed below the setting. If multiple have errors, they will be summarized below. Note that you'll need to set the individual validation error property to null to clear that validation error. | MySetting.ValidationErrors[0].Name = 'Name must be set' |
Advanced | True if this setting should be hidden unless the advanced flag is set to show those settings. | MySetting.Advanced = true |
EditorLineCount | The number of lines that the setting should have displayed. Note: to use this setting, you must have set this property via attribute first. | MySetting.EditorLineCount = 4 MySetting.EditorLineCount[2].Description = 2 |
DisplayOrder | The order that the setting will appear within the UI | MySetting.DisplayOrder = 6 |
IsVisible | If this setting should be displayed or hidden. | MySetting.IsVisible = false |
CategoryColor | The color used on the left hand side of the setting to show its category. Note that this must be a hex value | MySetting.CategoryColor = '#8a2d69' |
CategoryName | The name used in the category tooltip. | MySetting.CategoryName = 'Important' |
ValidValues | The values presented in the dropdown for setting or data grid cell. Note: To use valid values, there must be at least one value set via attribute. | MySetting.ValidValues = ['Cat', 'Dog', 'Fish'] MySetting.ValidValues[2].Pet = ['Cat', 'Dog', 'Fish'] |
IsReadOnly | If the setting or cell shoud be read only. | MySetting.IsReadOnly = true MySetting.IsReadOnly[2].Name = true |
Value | The value for the setting or cell. | MySetting.Value = 'new value' MySetting.Value[2].Name = 'new value' |
log(string message) | This is a method that allows you to log to the browser console which can be useful when troubleshooting scripts. | log('my message') |
Enums
Fig treats enum values as strings outside of the application so they should be considered strings when working with them in the display scripts.