In graphical user interface (GUI), a context menu is a type of menu that appears when you perform right-click operation. Nested level of context menu items can be created.
Diagram provides some in-built context menu items and allows to define custom menu items through the contextMenuSettings
property.
The show
property helps you to enable/disable the context menu. Diagram provides some default context menu items to ease the execution of some frequently used commands.
The following code illustrates how to enable the default context menu items.
var nodes = [{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white',
strokeWidth: 1
},
annotations: [{
id: 'label1',
content: 'Rectangle1',
offset: {
x: 0.5,
y: 0.5
},
style: {
color: 'white'
}
}]
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 300,
offsetY: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white',
strokeWidth: 1
},
annotations: [{
id: 'label2',
content: 'Rectangle2',
offset: {
x: 0.5,
y: 0.5
},
style: {
color: 'white'
}
}]
},
];
var connector = {
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor : '#6BA5D7',
fill: '#6BA5D7',
strokeWidth : 2,
targetDecorator: {
style: {
fill : '#6BA5D7',
strokeColor : '#6BA5D7'
}
}
}
};
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '350px',
nodes: nodes,
connectors: [connector],
//Enables the context menu
contextMenuSettings: {
show: true,
}
}, '#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">
<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="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Context menu can be defined for individual node with the desired context menu items.
items
property of the context menu.text
and ID
properties respectively.iconCss
property defines the class/multiple classes separated by a space for the menu item that is used to include an icon. Menu item can include font icon and sprite image.target
property used to set the target to show the menu item.separator
property defines the horizontal lines that are used to separate the menu items. You cannot select the separators. You can enable separators to group the menu items using the separator property.The following code example illustrates how to add custom context menu items.
var nodes = [{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white',
strokeWidth: 1
},
annotations: [{
id: 'label1',
content: 'Rectangle1',
offset: {
x: 0.5,
y: 0.5
},
style: {
color: 'white'
}
}]
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 300,
offsetY: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white',
strokeWidth: 1
},
annotations: [{
id: 'label2',
content: 'Rectangle2',
offset: {
x: 0.5,
y: 0.5
},
style: {
color: 'white'
}
}]
},
];
var connector = {
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor : '#6BA5D7',
fill: '#6BA5D7',
strokeWidth : 2,
targetDecorator: {
style: {
fill : '#6BA5D7',
strokeColor : '#6BA5D7'
}
}
}
};
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '350px',
nodes: nodes,
connectors: [connector],
contextMenuSettings: {
//Enables the context menu
show: true,
// Defines the custom context menu items
items: [{
// Text to be displayed
text: 'Save',
id: 'save',
target: '.e-elementcontent',
// Sets the css icons for the item
iconCss: 'e-save'
},
{
text: 'Load',
id: 'load',
target: '.e-elementcontent',
iconCss: 'e-load'
},
{
text: 'Clear',
id: 'clear',
target: '.e-elementcontent',
iconCss: 'e-clear'
}
],
// Hides the default context menu items
showCustomMenuOnly: false,
}
}, '#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">
<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="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
To display the custom context menu items alone, set the showCustomMenuOnly
property to true.
contextMenuBeforeItemRender
event. The contextMenuBeforeItemRender event triggers while rendering each menu item.contextMenuBeforeItemRender
event.var nodes = [{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white',
strokeWidth: 1
},
annotations: [{
id: 'label1',
content: 'Rectangle1',
offset: {
x: 0.5,
y: 0.5
},
style: {
color: 'white'
}
}]
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 300,
offsetY: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white',
strokeWidth: 1
},
annotations: [{
id: 'label2',
content: 'Rectangle2',
offset: {
x: 0.5,
y: 0.5
},
style: {
color: 'white'
}
}]
},
];
var connector = {
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor : '#6BA5D7',
fill: '#6BA5D7',
strokeWidth : 2,
targetDecorator: {
style: {
fill : '#6BA5D7',
strokeColor : '#6BA5D7'
}
}
}
};
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '350px',
nodes: nodes,
connectors: [connector],
contextMenuSettings: {
//Enables the context menu
show: true,
items: [{
text: 'delete',
id: 'delete'
}],
// Hides the default context menu items
showCustomMenuOnly: false,
},
contextMenuOpen: function(args) {
//do your custom action here.
for (let item of args.items) {
if (item.text === 'delete') {
if (!diagram.selectedItems.nodes.length && !diagram.selectedItems.connectors.length) {
args.hiddenItems.push(item.text);
}
}
}
},
contextMenuClick: function(args) {
//do your custom action here.
if (args.item.id === 'delete') {
if ((diagram.selectedItems.nodes.length + diagram.selectedItems.connectors.length) > 0) {
diagram.cut();
}
}
}
}, '#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">
<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="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
You would be notified with events, when you try to open the context menu items contextMenuOpen
and when you click the menu items contextMenuClick
.
The following code example illustrates how to define those events.
var nodes = [{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white',
strokeWidth: 1
},
annotations: [{
id: 'label1',
content: 'Rectangle1',
offset: {
x: 0.5,
y: 0.5
},
style: {
color: 'white'
}
}]
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 300,
offsetY: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white',
strokeWidth: 1
},
annotations: [{
id: 'label2',
content: 'Rectangle2',
offset: {
x: 0.5,
y: 0.5
},
style: {
color: 'white'
}
}]
},
];
var connector = {
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor : '#6BA5D7',
fill: '#6BA5D7',
strokeWidth : 2,
targetDecorator: {
style: {
fill : '#6BA5D7',
strokeColor : '#6BA5D7'
}
}
}
};
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '350px',
nodes: nodes,
connectors: [connector],
contextMenuSettings: {
//Enables the context menu
show: true,
items: [{
text: 'delete',
id: 'delete'
}],
// Hides the default context menu items
showCustomMenuOnly: false,
},
contextMenuOpen: function(args) {
//do your custom action here.
for (let item of args.items) {
if (item.text === 'delete') {
if (!diagram.selectedItems.nodes.length && !diagram.selectedItems.connectors.length) {
args.hiddenItems.push(item.text);
}
}
}
},
contextMenuClick: function(args) {
//do your custom action here.
if (args.item.id === 'delete') {
if ((diagram.selectedItems.nodes.length + diagram.selectedItems.connectors.length) > 0) {
diagram.cut();
}
}
}
}, '#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">
<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="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>