Simplifies using an ILookupDecorator.
Remarks
This class provides methods that help in performing common tasks related to decorating the lookup method for a certain TDecoratedType
:
- Set a single implementation
- Set a factory that creates item-specific implementations
- Wrap the default implementation
- Hide the default implementation
Each of these methods adds a new IContextLookupChainLink to the lookup chain which adds the wanted behavior. The link can be removed again by calling the remove method on the parent decorator, e.g., the NodeDecorator.
Typically, one does not create instances of this class but uses the item specific implementations provided by GraphDecorator: nodes, edges, labels, ports, and bends.
Examples
const decorator = graph.decorator.nodes.sizeConstraintProvider
graph.decorator.nodes.sizeConstraintProvider.addConstant(
new ConstantSizeConstraintProvider(),
)
graph.decorator.nodes.selectionRenderer.addFactory(
(node) => new RedSelectionRenderer(node),
)
graph.decorator.nodes.positionHandler.addWrapperFactory(
(node, original) =>
new ConstrainedPositionHandlerWrapper(original!, node!),
)
graph.decorator.nodes.positionHandler.hide()
Type Parameters
- TDecoratedType
- The type of the instances whose
behavior should be modified. - TInterface
- The interface or type that clients will use during the
call. Note that this is normally an interface and not the implementation of the interface since client code will use the interface for the query.
Type Details
- yFiles module
- view
See Also
Constructors
LookupDecorator
(decoratedType: Constructor<TDecoratedType>, interfaceType: Constructor<TInterface>, decorator: ILookupDecorator)Initializes a new instance of the LookupDecorator<TDecoratedType,TInterface> class.
Parameters
A map of options to pass to the method.
- decoratedType - Constructor<TDecoratedType>
- The type of the instances whose lookup behavior should be modified.
- interfaceType - Constructor<TInterface>
- The interface or type that clients will use during the lookup call. Note that this is normally an interface and not the implementation of the interface since client code will use the interface for the query.
- decorator - ILookupDecorator
- The decorator instance that will be used to extend the Lookup mechanism.
Methods
addConstant
(predicate: function(TDecoratedType):boolean, implementation: TInterface) : IContextLookupChainLinkSets the lookup of all items of type TDecoratedType
which match a predicate
to return the given TInterface
implementation
if TInterface
is queried on an item's lookup.
Remarks
This can be used to modify the lookup of all items matching a certain predicate in the same way.
To adjust the lookup of only some items, use a predicate that checks for that specific item. To adjust the lookup of all items, use addConstant.
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method.
Parameters
A map of options to pass to the method.
- predicate - function(TDecoratedType):boolean
- A predicate that determines which items are affected.
Signature Details
function(obj: TDecoratedType) : boolean
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.Parameters
- obj - TDecoratedType
- The object to compare against the criteria defined within the method represented by this delegate.
Returns
- boolean
true
if obj meets the criteria defined within the method represented by this delegate; otherwise,false
.
- implementation - TInterface
- The implementation to return if the
predicate
matches andTInterface
is queried.
Returns
- ↪IContextLookupChainLink
- An IContextLookupChainLink that has been added.
Examples
The following example shows how to decorate the lookup to return different values for different types of node (nodes with different tags in this example). Note that the decorations can be chained.
graph.decorator.nodes.sizeConstraintProvider.addConstant(
(node) => node.tag === 'type 1',
new NodeSizeConstraintProvider(new Size(10, 10), new Size(100, 100)),
)
graph.decorator.nodes.sizeConstraintProvider.addConstant(
(node) => node.tag === 'type 2',
new NodeSizeConstraintProvider(new Size(20, 20), new Size(200, 200)),
)
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method:
constructor() {}
registerLookup(graph: IGraph): void {
this.chainLink =
graph.decorator.nodes.sizeConstraintProvider.addConstant(
new ConstantSizeConstraintProvider(),
)!
}
deregisterLookup(graph: IGraph): void {
graph.decorator.nodes.remove(this.chainLink)
}
See Also
Sets the lookup of all items of type TDecoratedType
to return the given TInterface
implementation
if TInterface
is queried on an item's lookup.
Remarks
This can be used to modify the lookup of all items in the same way.
To adjust the lookup of a subset of the items, use addConstant.
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method.
Parameters
A map of options to pass to the method.
- implementation - TInterface
- The implementation to return unconditionally.
Returns
- ↪IContextLookupChainLink
- An IContextLookupChainLink that has been added.
Examples
The following example shows how to decorate the lookup to return a constant value for all nodes:
graph.decorator.nodes.sizeConstraintProvider.addConstant(
new ConstantSizeConstraintProvider(),
)
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method:
constructor() {}
registerLookup(graph: IGraph): void {
this.chainLink =
graph.decorator.nodes.sizeConstraintProvider.addConstant(
new ConstantSizeConstraintProvider(),
)!
}
deregisterLookup(graph: IGraph): void {
graph.decorator.nodes.remove(this.chainLink)
}
See Also
addFactory
(predicate: function(TDecoratedType):boolean, factory: function(TDecoratedType):TInterface) : IContextLookupChainLinkSets the lookup of items matching a predicate
with a factory
that creates a TInterface
instance if TInterface
is queried during the lookup.
Remarks
This can be used to modify the lookup of all items matching a certain predicate where the returned instance either depends on the item or uses it in some way.
To adjust the lookup of all items in this manner, use addFactory instead. To adjust the lookup of a single item, use addConstant.
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method.
Parameters
A map of options to pass to the method.
- predicate - function(TDecoratedType):boolean
- The predicate that determines for which items the factory shall be called.
Signature Details
function(obj: TDecoratedType) : boolean
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.Parameters
- obj - TDecoratedType
- The object to compare against the criteria defined within the method represented by this delegate.
Returns
- boolean
true
if obj meets the criteria defined within the method represented by this delegate; otherwise,false
.
- factory - function(TDecoratedType):TInterface
- The factory that is queried to create the result.
Signature Details
function(item: TDecoratedType) : TInterface
A function which provides a custom into a custom implementation ofTInterface
for the givenitem
.Parameters
- item - TDecoratedType
- The item to return the implementation for.
Returns
- TInterface
- The new implementation.
Returns
- ↪IContextLookupChainLink
- An IContextLookupChainLink that has been added.
Examples
The following example shows how to decorate the lookup to create different instances for different types of nodes when queried. The example creates a red selection decorator for nodes with tag "type 1", a blue one for nodes with tag "type 2", and the default for all other nodes.
graph.decorator.nodes.selectionRenderer.addFactory(
(node) => node.tag === 'type 1',
(node) => new RedSelectionRenderer(node),
)
graph.decorator.nodes.selectionRenderer.addFactory(
(node) => node.tag === 'type 2',
(node) => new BlueSelectionRenderer(node),
)
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method:
registerLookup(graph: IGraph): void {
this.chainLink =
graph.decorator.nodes.sizeConstraintProvider.addFactory(
(node) => new CustomSizeConstraintProvider(node),
)!
}
deregisterLookup(graph: IGraph): void {
graph.decorator.nodes.remove(this.chainLink)
}
See Also
Sets the lookup of all TDecoratedType
items, so that factory
is used to create a TInterface
instance if TInterface
is queried during the lookup.
Remarks
This can be used to modify the lookup of all items where the returned instance either depends on the item or uses it in some way.
To only adjust the lookup of items matching a predicate in this manner, use addFactory instead. To adjust the lookup of a single item, use addConstant.
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method.
Parameters
A map of options to pass to the method.
- factory - function(TDecoratedType):TInterface
- The factory that is queried to create the result.
Signature Details
function(item: TDecoratedType) : TInterface
A function which provides a custom into a custom implementation ofTInterface
for the givenitem
.Parameters
- item - TDecoratedType
- The item to return the implementation for.
Returns
- TInterface
- The new implementation.
Returns
- ↪IContextLookupChainLink
- An IContextLookupChainLink that has been added.
Examples
This method can be used for implementations which depend on the item they are queried from
graph.decorator.nodes.selectionRenderer.addFactory(
(node) => new RedSelectionRenderer(node),
)
The following example shows how to decorate the lookup to create different instances for different types of nodes when queried. The example creates a red selection decorator for nodes with tag "type 1", a blue one for nodes with tag "type 2", and the default for all other nodes.
graph.decorator.nodes.selectionRenderer.addFactory((node) => {
switch (node.tag) {
case 'type 1':
return new RedSelectionRenderer(node)
case 'type 2':
return new BlueSelectionRenderer(node)
default:
return null
}
})
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method:
registerLookup(graph: IGraph): void {
this.chainLink =
graph.decorator.nodes.sizeConstraintProvider.addFactory(
(node) => new CustomSizeConstraintProvider(node),
)!
}
deregisterLookup(graph: IGraph): void {
graph.decorator.nodes.remove(this.chainLink)
}
See Also
addWrapperFactory
(predicate: function(TDecoratedType):boolean, factory: function(TDecoratedType, TInterface):TInterface) : IContextLookupChainLinkSets the lookup of items matching a predicate
with a factory
callback that wraps the underlying implementation if TInterface
is queried during the lookup.
Remarks
This can be used to modify the lookup of all items matching a certain predicate where the factory implementation may delegate to the wrapped implementation in certain cases.
To adjust the lookup for all items in this manner, use addWrapperFactory instead. To adjust the lookup of a single item, use addConstant.
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method.
Parameters
A map of options to pass to the method.
- predicate - function(TDecoratedType):boolean
- The predicate that determines for which items the wrapping shall be performed.
Signature Details
function(obj: TDecoratedType) : boolean
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.Parameters
- obj - TDecoratedType
- The object to compare against the criteria defined within the method represented by this delegate.
Returns
- boolean
true
if obj meets the criteria defined within the method represented by this delegate; otherwise,false
.
- factory - function(TDecoratedType, TInterface):TInterface
- The factory that will be passed the underlying implementation and that is queried to create the result.
Signature Details
function(item: TDecoratedType, originalImplementation: TInterface) : TInterface
A function which wraps theoriginalImplementation
ofTInterface
into a custom implementation for the givenitem
.Parameters
- item - TDecoratedType
- The item to return the wrapped implementation for.
- originalImplementation - TInterface
- The original implementation to wrap.
Returns
- TInterface
- The new implementation.
Returns
- ↪IContextLookupChainLink
- An IContextLookupChainLink that has been added.
Examples
This method can be used for implementations which wrap the default implementation
graph.decorator.nodes.positionHandler.addWrapperFactory(
(node) => node.tag !== null,
(node, original) =>
new ConstrainedPositionHandlerWrapper(original!, node!),
)
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method:
constructor() {}
registerLookup(graph: IGraph): void {
this.chainLink =
graph.decorator.nodes.sizeConstraintProvider.addWrapperFactory(
(node, provider) =>
new SizeConstraintProviderWrapper(node, provider),
)!
}
deregisterLookup(graph: IGraph): void {
graph.decorator.nodes.remove(this.chainLink)
}
See Also
addWrapperFactory
(factory: function(TDecoratedType, TInterface):TInterface) : IContextLookupChainLinkSets the lookup of all items with a factory
callback that wraps the underlying implementation if TInterface
is queried during the lookup.
Remarks
This can be used to modify the lookup of all items matching where the factory implementation may delegate to the wrapped implementation in certain cases.
To adjust the lookup in this manner only for items matching a predicate, use addWrapperFactory instead. To adjust the lookup of a single item, use addConstant.
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method.
Parameters
A map of options to pass to the method.
- factory - function(TDecoratedType, TInterface):TInterface
- The factory that will be passed the underlying implementation and that is queried to create the result.
Signature Details
function(item: TDecoratedType, originalImplementation: TInterface) : TInterface
A function which wraps theoriginalImplementation
ofTInterface
into a custom implementation for the givenitem
.Parameters
- item - TDecoratedType
- The item to return the wrapped implementation for.
- originalImplementation - TInterface
- The original implementation to wrap.
Returns
- TInterface
- The new implementation.
Returns
- ↪IContextLookupChainLink
- An IContextLookupChainLink that has been added.
Examples
This method can be used for implementations which wrap the default implementation
graph.decorator.nodes.positionHandler.addWrapperFactory(
(node, original) =>
new ConstrainedPositionHandlerWrapper(original!, node!),
)
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method:
constructor() {}
registerLookup(graph: IGraph): void {
this.chainLink =
graph.decorator.nodes.sizeConstraintProvider.addWrapperFactory(
(node, provider) =>
new SizeConstraintProviderWrapper(node, provider),
)!
}
deregisterLookup(graph: IGraph): void {
graph.decorator.nodes.remove(this.chainLink)
}
See Also
Sets the lookup of all TDecoratedType
items matching a predicate
to return null
if TInterface
is queried during the lookup.
Remarks
This can be used to temporarily or permanently disable a certain behavior for all items. To do this only for items matching a predicate, use hide instead.
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method.
Parameters
A map of options to pass to the method.
- predicate - function(TDecoratedType):boolean
- The predicate that determines for which items the implementation shall be hidden.
Signature Details
function(obj: TDecoratedType) : boolean
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.Parameters
- obj - TDecoratedType
- The object to compare against the criteria defined within the method represented by this delegate.
Returns
- boolean
true
if obj meets the criteria defined within the method represented by this delegate; otherwise,false
.
Returns
- ↪IContextLookupChainLink
- An IContextLookupChainLink that has been added.
Examples
This method can be used to disable the default behavior by hiding the default implementation returned by the lookup. The following example prevents all nodes from being moved by hiding their position handler:
graph.decorator.nodes.positionHandler.hide()
The following example prevents only nodes with a tag from being moved:
graph.decorator.nodes.positionHandler.hide((node) => node.tag !== null)
The effects of this method call can be undone by passing the return value to removeLookup or to the containing decorator's remove method:
constructor() {}
registerLookup(graph: IGraph): void {
this.chainLink = graph.decorator.nodes.sizeConstraintProvider.hide()!
}
deregisterLookup(graph: IGraph): void {
graph.decorator.nodes.remove(this.chainLink)
}