Inspector
The plugin implements an editor and viewer of Cell model properties.
To learn more about the plugin, check out the Property Editor and Viewer section.
constructor​
constructor(opt: Inspector.Options);
The Inspector constructor accepts several parameters:
cell​
cell?: mvc.Model;
[optional] The object whose model properties the Inspector will be showing to the user for viewing and editing. In this case, the Inspector retrieves the properties of the provided Cell.
Either this parameter or cellView
is required. The two options are mutually exclusive.
cellView​
cellView?: dia.CellView;
[optional] The object whose model properties the Inspector will be showing to the user for viewing and editing. In this case, the Inspector retrieves the properties of the CellView's associated Cell (cellView.model
).
Either this parameter or cell
is required. The two options are mutually exclusive.
container​
container?: mvc.$Element;
[optional] A DOM Element (or a CSS selector identifying one) that should serve as the container into which the options of any select-box
or color-palette
input fields should be appended.
The default is null
- in that case, the plugins use their respective default target values (as specified in the API reference: ui.SelectBox.options.target
, ui.ColorPalette.options.target
).
focusField()​
focusField?: (opt: { [key: string]: any }, path: string, element: HTMLElement, inspector: Inspector) => void;
[optional] A callback function that is called by the Inspector whenever it is trying to focus an input field (e.g. via inspector.focusField()
).
This function allows you to specify the focus behavior of custom input field types.
The same function is used for all input field definitions; you need to disambiguate between different cases on your own (e.g. via a switch
block).
The callback function is provided with the following arguments, which you may use in your logic:
opt
is theinputConfig
object provided within the Inspector'sinputs
object as a definition of the current input field.path
is a string containing the path to the definition of the current input field within the Inspector'sinputs
object, separated by slashes ('/'
).element
the rendered HTMLElement of this field (i.e. the value returned byrenderFieldContent()
callback)inspector
is a reference to the current Inspector instance (for context).
getFieldValue()​
getFieldValue?: (attribute: HTMLElement, type: string, inspector: Inspector) => any;
[optional] A callback function that is called by the Inspector whenever user input is detected in an input field. It is expected to return an object with a single property:
value
is the value read from the field (to be saved on the Cell model).
In other words, when used together with the renderFieldContent()
option, this option allows you to define custom input field types.
The same function is used for all input field definitions; you need to disambiguate between different cases on your own (e.g. via a switch
block). If the callback function is defined but returns undefined
in some cases, the Inspector will try to understand the input field definition in question as a built-in input field type (as if this function were not provided).
The callback function is provided with the following arguments, which you may use in your logic:
attribute
is the DOM Element container of the current input field in the Inspector (i.e. the value returned byrenderFieldContent()
callback).type
is theinputConfig.type
identifier of the current input field definition.inspector
is a reference to the current Inspector instance (for context).
groups​
groups?: any;
[optional] An object that defines the groups in the Inspector. Each group may contain any number of input fields from the Inspector.
The keys of this object are strings that uniquely identify the groups and values are groupConfig
objects that contain properties for a given group:
-
[optional]
closed
is a boolean specifying whether the group should be closed by default (when the Inspector is first opened). Default isfalse
(the group is open). -
[optional]
index
is a number specifying the index of the group relative to other groups. It isundefined
by default (groups are presented in no particular order within the Inspector). -
[optional]
label
is a string specifying a label for the group. This label is displayed as a header of the group section in the accordion-like Inspector. It is the group's key by default. -
[optional]
when
is an object containing conditional expressions that determine whether the group should be shown or hidden based on the value of other Cell model properties. There are no conditions by default, which means that the group is always shown. See the Property Editor and Viewer Conditional expressions section for more information.
inputs​
inputs?: any;
[optional] An object that defines the input fields in the Inspector. (An input field is in charge of setting user input on the Cell model.)
The structure of the object mimics the structure of properties of the Cell model. The keys of this object are strings that match the properties within the Cell model (possibly nested) - until the deepest-level values, which specify inputConfig
objects that contain properties for a given input field.
The following is a list of inputConfig
options which are shared by all input fields. Some input field types may recognize additional options - those are listed inside their entries within the Input field types section.
-
[optional]
attrs
is an object of the form<selector>: { <attributeName>: <attributeValue>, ... }
, which allows you to set arbitrary HTML attributes on the HTML code generated for this input field. This is useful if you want to mark certain input fields (e.g. via an additionalclassName
) or to store some additional content in them (e.g. in order to display a tooltip). -
[optional]
defaultValue
specifies the value displayed in the input field when the corresponding Cell property isundefined
. It can either be a primitive value or a callback function with the signature(cell: dia.Cell, path: string) => any
, which dynamically returns the default value. -
[optional]
group
is a string matching a key of the Inspector'sgroups
object, indicating that the input field belongs to the specified group. It isundefined
by default (no group membership). See the Property Editor and Viewer Groups section for more information. -
[optional]
index
is a number indicating the index of the input field within itsgroup
(if one is specified). It isundefined
by default (input fields are presented in no particular order within the group). See the Property Editor and Viewer Groups section for more information. -
[optional]
label
is a string specifying the label that the input field should have in the Inspector. It is the input field's path by default. -
[optional]
overwrite
is a boolean switching whether a value entered into the input field should overwrite the current contents of the Cell (true
) or be merged with the previous contents (false
). The default isfalse
. Example resolution:// `overwrite: false` (default)
// - previous value of Cell model property:
{ existingProperty: 'value1' }
// - user input:
{ newProperty: 'value2' }
// => new value of Cell model property:
{ newProperty: 'value2', existingProperty: 'value1' }
// `overwrite: true`
// - previous value of Cell model property:
{ existingProperty: 'value1' }
// - user input:
{ newProperty: 'value2' }
// => new value of Cell model property:
{ newProperty: 'value2' } -
[required]
type
is a string specifying the type of the input field. See the Input field types section for a list of supported types and their additionalinputConfig
options. -
[optional]
valueRegExp
is a string specifying a regular expression that is used to interpret (and set) a value on the Cell. The default isundefined
(no modification of values). Example resolution:// `valueRegExp: '(prefix: )(.*)()'`
// - old value of Cell model property:
'prefix: Hello World!'
// => value presented in the input field:
'Hello World!'
// - user input:
'test'
// => new value of Cell model property:
'prefix: test'Throws the error
Inspector: defaultValue must be present when valueRegExp is used
if anundefined
value is encountered. You should use this option in combination with thedefaultValue
option to ensure thatundefined
is never encountered as a value. -
[optional]
when
is an object containing conditional expressions that determine whether the input field should be shown or hidden based on the value of other Cell model properties. See the Property Editor and Viewer Conditional expressions section for more information. There are no conditions by default, which means that the input field is always shown.Conditional expressions are objects with the following properties:
- [required] one property whose key identifies the operator of the expression, and whose value depends on the type of the operator (primitive, unary, multiary, custom primitive)
- [optional]
options
property which is an object that modifies the evaluation of the expression:- [optional]
dependencies
is an array of string paths pointing to Cell model properties whose values are necessary for the evaluation of the expression (relevant for custom operators), - [optional]
otherwise
is an object that modifies what happens when the expression evaluates tofalse
:- [optional]
unset
is a boolean which determines whether the input field should be cleared (or reverted to itsdefaultValue
) when it is hidden by Inspector (true
), or should remember its user-submitted content and present it again whenever the input field becomes visible (false
). The default isfalse
.
- [optional]
- [optional]
live​
live?: boolean;
[optional] Should the Inspector update the Cell properties immediately (i.e. in reaction to an onchange
event triggered on a form input)? The default is true
.
multiOpenGroups​
multiOpenGroups?: boolean;
[optional] Is the user allowed to have multiple groups opened in the Inspector at the same time? The default is true
.
If you are looking for the classical accordion
type of Inspector (only one group open at a time), set this option to false
.
operators​
operators?: { [operatorName: string]: Operator };
[optional] An object that allows you to define custom primitive operators for use in conditional expressions.
The keys of this object are strings that uniquely identify the custom operators and values are callback functions which are expected to return true
when the custom operator's condition is met and false
when it is not. The callback function is provided with the following arguments which you may use in your logic:
cell
is a reference to the Cell modified by the current Inspector instance.value
is the value of the property at the path provided by the first operand of the expression....arguments
is anything that was passed to the operator, spread into arguments - if a single value is passed to the operator, it becomes a single argument, but if an array is passed to the operator, its contents become individual arguments.valuePath
(the last argument) is a string containing the resolved path to the property specified in the first operand of the expression (wildcards replaced).
renderFieldContent()​
renderFieldContent?: (opt: { [key: string]: any }, path: string, value: any, inspector: Inspector) => mvc.$HTMLElement;
[optional] A callback function that is called by the Inspector whenever it needs to render an input field. It is expected to return the content as an HTMLElement or a DOM Element.
In other words, this function allows you to define custom input field types.
The same function is used for all input field definitions; you need to disambiguate between different cases on your own (e.g. via a switch
block). If the function is defined but returns undefined
in some cases, the Inspector will try to understand the input field definition in question as a built-in input field type (as if this function were not provided).
The callback function is provided with the following arguments, which you may use in your logic:
opt
is theinputConfig
object provided within the Inspector'sinputs
object as a definition of the current input field.path
is a string containing the path to the definition of the current input field within the Inspector'sinputs
object, separated by slashes ('/'
).value
is the value read from the Cell property at the corresponding path at the time of rendering of the current input field. It takes the current input field definition'sinputConfig.defaultValue
andinputConfig.valueRegExp
into account (if any are specified).inspector
is a reference to the current Inspector instance (for context).
renderLabel()​
renderLabel?: (opt: { [key: string]: any }, path: string, inspector: Inspector) => mvc.$HTMLElement;
[optional] A callback function that is called by the Inspector whenever it needs to render the label of an input field. It is expected to return the content as an HTMLElement or a DOM Element.
This function allows you to specify custom input field labels.
The same function is used for all input field definitions; you need to disambiguate between different cases on your own (e.g. via a switch
block). If the function is defined but returns undefined
in some cases, the Inspector will create the label in question in the usual way (i.e. according to current input field definition's inputConfig.label
, as if this function were not provided).
The callback function is provided with the following arguments, which you may use in your logic:
opt
is theinputConfig
object provided within the Inspector'sinputs
object as a definition of the current input field.path
is a string containing the path to the definition of the current input field within the Inspector'sinputs
object, separated by slashes ('/'
).inspector
is a reference to the current Inspector instance (for context).
stateKey()​
stateKey?: (model: dia.Cell) => string;
[optional] A callback function that is expected to return a string specifying a unique identifier for saving/restoring the state of the groups (i.e. remembering which groups are opened and which ones are closed). The default function returns the id
of the current Cell, which means that every Cell has its own group state. (And that Inspectors opened on different instances of the same Cell type do not remember the previously opened/closed groups.)
The callback function is provided with the following argument, which you may use in your logic:
cell
is a reference to the Cell modified by the current Inspector instance.
One commonly used alternative function stores group state per Cell type:
function(model) { return model.get('type')}
When the above function is used as stateKey
, all Inspectors opened for a type (e.g. standard.Rectangle
) share the same group state.
validateInput()​
validateInput?: (input: any, path: string, type: string, inspector: Inspector) => boolean;
[optional] A callback function that is called by the Inspector to check whether user input into an input field is valid. The function is expected to return true
when the input is valid and false
when it is not. If false
is returned, the input field value is not saved to the Cell model.
See the Property Editor and Viewer Validation section for more information.
The same function is used for all input field definitions; you need to disambiguate between different cases on your own (e.g. via a switch
block).
The callback function is provided with the following arguments, which you may use in your logic:
input
is a reference to the input field's HTMLElement (usually<input>
) with which the user has interacted.path
is a string containing the path to the input field within the Inspector'sinputs
object, separated by slashes ('/'
).type
is a string containing the type of the input field.inspector
is a reference to the current Inspector instance (for context).
The default function checks the validity
property of input
:
function(input, path, type, inspector) {
return (input.validity ? input.validity.valid : true);
}
Methods​
closeGroup()​
closeGroup(name: string): void;
Close the group identified by name
.
closeGroups()​
closeGroups(): void;
Close all groups.
focusField()​
focusField(path: string): void;
Focus the input field identified by the provided slash-separated ('/'
) path
within the Inspector's inputs
object.
getGroupsState()​
getGroupsState(): string[];
Get the array of groups which are currently stored as closed in the group state. (This may differ from the currently rendered state if storeGroupsState()
has not been called recently.)
openGroup()​
openGroup(name: string): void;
Open the group identified by name
.
openGroups()​
openGroups(): void;
Open all groups.
refreshSource()​
refreshSource(path: string): void;
Manually refresh the source of the input field identified by the provided slash-separated ('/'
) path
within the Inspector's inputs
object. See the Property Editor and Viewer Options section (object rule) for more information.
refreshSources()​
refreshSources(): void;
Manually refresh sources of all input fields. See the Property Editor and Viewer Options section (object rule) for more information.
remove()​
remove(): this;
Remove the Inspector from the DOM. This method should be called once you are finished with using the Inspector.
render()​
render(): this;
Render the Inspector based on the options passed to the constructor function.
Note that this does not add the Inspector to the live DOM tree - that must be done manually by appending the Inspector DOM Element (accessible as the el
property) to a live DOM Element on the page. See the Property Editor and Viewer section for more information.
restoreGroupsState()​
restoreGroupsState(): void;
Apply the stored group state (which groups were opened and which ones were closed) by opening and closing groups to match. The state information is looked up by the current return value of the stateKey()
callback function.
Use together with storeGroupsState()
to manually manipulate group states (useful if you do not create your Inspector via the create()
method).
storeGroupsState()​
storeGroupsState(): void;
Save the current group state (which groups are opened and which ones are closed). The key for storing the state is the current return value of the stateKey()
callback function.
Use together with restoreGroupsState()
to manually manipulate group states (useful if you do not create your Inspector via the create()
method).
toggleGroup()​
toggleGroup(name: string): void;
Toggle the group identified by name
.
updateCell()​
updateCell(): void;
Manually update the Inspector's associated Cell based on the current values in the Inspector's input fields. This is especially useful if you use the Inspector with the live
mode disabled.
Static methods​
close()​
static close(): void;
A helper method for closing Inspector instances which were created via the create()
method.
create()​
static create(container: mvc.$HTMLElement, opt?: Inspector.CreateOptions): ui.Inspector;
A helper method for creating an instance of Inspector, where container
is an DOM Element (or a CSS selector identifying one) that should serve as the container into which the Inspector should be rendered, and opt
is an object specifying Inspector constructor options.
In addition to all Inspector constructor options, the opt
object of the create()
static method accepts three additional options:
- [optional]
storeGroupsState
is a boolean specifying whether current Inspector's group state should be saved before new Inspector is created by this method. (That is, whether Inspector should remember which groups were opened and which ones were closed when it is reopened for a Cell with the samestateKey()
return value.) The default istrue
. - [optional]
restoreGroupsState
is a boolean specifying whether previous group state should be restored for the new Inspector created by this method (if any group state had previously been saved for the current Cell'sstateKey()
return value). The default istrue
. - [optional]
updateCellOnClose
is a boolean specifying whether current Inspector input field values should be saved to the Inspector's associatedcell
before a new Inspector is created by this method. The default istrue
.
Events​
The plugin fires the following events:
render​
Triggered when the Inspector re-renders itself partially.
If you are adding event listeners or your own custom HTML into the Inspector, you should always do it inside the render
event handler.
inspector.on('render', () => {
// Inspector has been rendered
});
change:[path]​
Triggered when the user changes a Cell model property at [path]
via an input field of the Inspector (only if the validateInput()
callback function returns true
).
inspector.on(`change:[path]`, (
value: any,
inputEl: any
) => {
// the Cell property `[path]` was changed
})
value
is the new value at the property path,inputEl
is a reference to the input field's HTMLElement (usually<input>
) with which the user has interacted.
Example usage:
inspector.on(`change:myProperty`, (value, inputEl) => {
console.log(`new value of myProperty: ${value}`);
})
close​
Triggered when the Inspector gets closed.
inspector.on('close', () => {
// Inspector has been closed
});
Types​
The plugin uses the following types:
CreateOptions​
interface CreateOptions extends Options {
storeGroupsState?: boolean;
restoreGroupsState?: boolean;
updateCellOnClose?: boolean;
}