Selector provides a visual representation of selected elements. It behaves like a container and allows to update the size, position, and rotation angle of the selected elements through interaction and by using program. Single or multiple elements can be selected at a time.
An element can be selected by clicking that element. During single click, all previously selected items are cleared. The following image shows how the selected elements are visually represented.
selectionChange
event gets triggered.When a child element of any group is clicked, its contained group is selected instead of the child element. With consecutive clicks on the selected element, selection is changed from top to bottom in the hierarchy of parent group to its children.
Multiple elements can be selected with the following ways:
During single click, any existing item in the selection list be cleared, and only the item clicked recently is there in the selection list. To avoid cleaning the old selected item, Ctrl key must be on hold when clicking.
Clicking and dragging the diagram area allows to create a rectangular region. The elements that are covered under the rectangular region are selected at the end.
The client-side methods select
and clearSelection
help to select or clear the selection of the elements at runtime. The following code example illustrates how to select or clear the selection of an item using program.
Get the current selected items from the nodes
and connectors
collection of the selectedItems
property of the diagram model.
The client-side method selectAll
used to select all the elements such as nodes/connectors in the diagram. Refer to the following link which shows how to use selectAll
method on the diagram.
positionChange
event gets triggered and to do customization in this event.
sizeChange
event gets triggered.
Note: While dragging and resizing, the objects are snapped towards the nearest objects to make better alignments. For better alignments, refer to Snapping
.
You can change the size of the node resize thumb and the connector end point handle by using the handleSize
property. The appearance such as fill, stroke, and stroke width of the node resize thumb and connector end point handle can be customized by overriding the e-diagram-resize-handle and e-diagram-endpoint-handle classes respectively.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let node: NodeModel = {
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white'
},
// Text(label) added to the node
};
let diagram: Diagram = new Diagram({
width: '100%', height: 900,
nodes: [node],
selectedItems : {handleSize : 40},
});
diagram.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
Note: For connector editing, you have to inject the ConnectorEditing
module.
Source and target points of the selected connectors are represented with two handles. Clicking and dragging those handles help you to adjust the source and target points.
sourcePointChange
event gets triggered.targetPointChange
event gets triggered.connectionChange
event gets triggered.
Diagram provides support to drop a node/connector over another node/connector. The drop
event is raised to notify that an element is dropped over another one and it is disabled, by default. It can enabled with the constraints property.
userHandles
collection of the selectedItems
property.User handles can be aligned relative to the node boundaries. It has margin
, offset
, side
, horizontalAlignment
, and verticalAlignment
settings. It is quite tricky when all four alignments are used together but gives more control over alignment.
The offset
property of userHandles
is used to align the user handle based on fractions. 0 represents top/left corner, 1 represents bottom/right corner, and 0.5 represents half of width/height.
The side
property of userHandles
is used to align the user handle by using the Top
, Bottom
, Left
, and Right
options.
The horizontalAlignment
property of userHandles
is used to set how the user handle is horizontally aligned at the position based on the offset
. The verticalAlignment
property is used to set how user handle is vertically aligned at the position.
Margin is an absolute value used to add some blank space in any one of its four sides. The userHandles
can be displaced with the margin
property.
The diagram component notifies the mouse button clicked. For example, when the right mouse button is clicked, the clicked button is notified as right. The mouse click is notified with,
Notification | Description |
---|---|
Left | When the left mouse button is clicked, left is notified |
Middle | When the mouse wheel is clicked, middle is notified |
Right | When the right mouse button is clicked, right is notified |
let diagram: Diagram = new Diagram({
width: '100%', height: 900
});
diagram.appendTo('#diagram');
diagram.click = function (args: IClickEventArgs) {
// Obtains the mouse button clicked
let clickedButtons = args.button
};
The appearance of the user handle can be customized by using the size
, borderColor
, backgroundColor
, visible
, pathData
, and pathColor
properties of the userHandles
.
import { Diagram, NodeModel, BasicShapeModel, MoveTool, MouseEventArgs, IElement, UserHandleModel, ToolBase, SelectorConstraints, Actions, randomId, cloneObject } from '@syncfusion/ej2-diagrams';
let shape: BasicShapeModel = { type: 'Basic', shape: 'Rectangle' };
let node1: NodeModel = { id: 'node', offsetX: 100, offsetY: 100, shape: shape };
let handles: UserHandleModel[] = [{
name: 'clone', pathData: 'M60.3,18H27.5c-3,0-5.5,2.4-5.5,5.5v38.2h5.5V23.5h32.7V18z M68.5,28.9h-30c-3, 0-5.5,2.4-5.5,5.5v38.2c0,3,2.4,5.5,5.5,5.5h30c3,0,5.5-2.4,5.5-5.5V34.4C73.9,31.4,71.5,28.9,68.5,28.9z M68.5,72.5h-30V34.4h30V72.5z', visible: true, offset: 0, side: 'Bottom',
margin: { top: 0, bottom: 0, left: 0, right: 0 }
}];
let diagram: Diagram = new Diagram({
width: '100%', height: '600px', nodes: [node1],
selectedItems: { constraints: SelectorConstraints.All, userHandles: handles },
getNodeDefaults: (node: NodeModel): NodeModel => {
node.height = 100;
node.width = 100;
node.style.fill = '#6BA5D7';
node.style.strokeColor = '#6BA5D7';
return node;
},
getCustomTool: getTool
});
diagram.appendTo('#element');
function getTool(action: string): ToolBase {
let tool: ToolBase;
if (action === 'clone') {
tool = new CloneTool(diagram.commandHandler);
}
return tool;
}
//Defines the clone tool used to copy Node/Connector
class CloneTool extends MoveTool {
public mouseDown(args: MouseEventArgs): void {
let newObject: any;
if (diagram.selectedItems.nodes.length > 0) {
newObject = cloneObject(diagram.selectedItems.nodes[0]) as NodeModel;
} else {
newObject = cloneObject(diagram.selectedItems.connectors[0]) as ConnectorModel;
}
newObject.id += randomId();
diagram.paste([newObject]);
args.source = diagram.nodes[diagram.nodes.length - 1] as IElement;
args.sourceWrapper = args.source.wrapper;
super.mouseDown(args);
this.inAction = true;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
scrollChange
event gets triggered.
Diagram provides the support to notify the pan status of the zoom pan tool. Whenever the diagram is panning scrollChange
event is triggered and hence the pan status can be obtained. The pan status is notified with Start, Progress, and Completed.
Pan Status | Description |
---|---|
Start | When the mouse is clicked and dragged the status is notified as start. |
Progress | When the mouse is in motion the status is notified as progress. |
Completed | When panning is stopped the status is notified with completed. |
let diagram: Diagram = new Diagram({
width: '100%', height: 900
tool: DiagramTools.ZoomPan, scrollChange:function (args:IScrollChangeEventArgs | IBlazorScrollChangeEventArgs) {
// Obtains the zoom pan status
let mouseButton = args.panState
}
});
diagram.appendTo('#element');
Diagram provides support to interact with the elements with key gestures. By default, some in-built commands are bound with a relevant set of key combinations.
The following table illustrates those commands with the associated key values.
Shortcut Key | Command | Description |
---|---|---|
Ctrl + A | selectAll |
Select all nodes/connectors in the diagram. |
Ctrl + C | copy | Copy the diagram selected elements. |
Ctrl + V | paste | Pastes the copied elements. |
Ctrl + X | cut | Cuts the selected elements. |
Ctrl + Z | undo | Reverses the last editing action performed on the diagram. |
Ctrl + Y | redo | Restores the last editing action when no other actions have occurred since the last undo on the diagram. |
Delete | delete | Deletes the selected elements. |
Ctrl/Shift + Click on object | Multiple selection (Selector binds all selected nodes/connectors). | |
Up Arrow | nudge(“up”) | nudgeUp : Moves the selected elements towards up by one pixel. |
Down Arrow | nudge(“down”) | nudgeDown : Moves the selected elements towards down by one pixel. |
Left Arrow | nudge(“left”) | nudgeLeft : Moves the selected elements towards left by one pixel. |
Right Arrow | nudge(“right”) | nudgeRight : Moves the selected elements towards right by one pixel. |
Ctrl + MouseWheel | zoom | Zoom (Zoom in/Zoom out the diagram). |
F2 | startLabelEditing |
Starts to edit the label of selected element. |
Esc | endLabelEditing |
Sets the label mode as view and stops editing. |