Context menu in Vue Diagram component
11 Jun 202424 minutes to read
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.
Customize context menu
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.
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :nodes='nodes' :connectors='connectors'
:contextMenuSettings='contextMenuSettings'></ejs-diagram>
</div>
</template>
<script setup>
import { provide } from "vue";
import { DiagramComponent as EjsDiagram, DiagramContextMenu, Diagram } from '@syncfusion/ej2-vue-diagrams';
let 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'
}
}]
}];
let connectors = [{
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
strokeWidth: 2,
targetDecorator: {
style: {
fill: '#6BA5D7',
strokeColor: '#6BA5D7'
}
}
}
}]
const width = "100%";
const height = "350px";
const contextMenuSettings = {
show: true,
}
provide('diagram', [DiagramContextMenu]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :nodes='nodes' :connectors='connectors'
:contextMenuSettings='contextMenuSettings'></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent, DiagramContextMenu, Diagram } from '@syncfusion/ej2-vue-diagrams';
let 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'
}
}]
}];
let connectors = [{
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
strokeWidth: 2,
targetDecorator: {
style: {
fill: '#6BA5D7',
strokeColor: '#6BA5D7'
}
}
}
}];
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "350px",
nodes: nodes,
connectors: connectors,
contextMenuSettings: {
show: true,
}
}
},
provide: {
diagram: [DiagramContextMenu]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>
Context menu can be defined for individual node with the desired context menu items.
-
Apart from the default context menu items, define some additional context menu items. Those additional items have to be defined and added to the
items
property of the context menu. -
Set text and ID for context menu item using the context menu
text
andID
properties respectively. -
Set an image for the context menu item using the context menu url property.
-
The
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. -
The
target
property used to set the target to show the menu item. -
The
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.
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :nodes='nodes' :connectors='connectors'
:contextMenuSettings='contextMenuSettings'></ejs-diagram>
</div>
</template>
<script setup>
import { provide } from "vue";
import { DiagramComponent as EjsDiagram, DiagramContextMenu } from '@syncfusion/ej2-vue-diagrams';
let 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'
}
}]
}];
let connectors = [{
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
strokeWidth: 2,
targetDecorator: {
style: {
fill: '#6BA5D7',
strokeColor: '#6BA5D7'
}
}
}
}]
const width = "100%";
const height = "350px";
const contextMenuSettings = {
//Enables the context menu
show: true,
// Defines the custom context menu items
items: [{
// Text to be displayed
text: 'Save',
//Sets the id for the item
id: 'save',
//ContextMenu can be visible based on the target in which you open the ContextMenu.
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
}
provide('diagram', [DiagramContextMenu]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :nodes='nodes' :connectors='connectors'
:contextMenuSettings='contextMenuSettings'></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent, DiagramContextMenu } from '@syncfusion/ej2-vue-diagrams';
let 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'
}
}]
}];
let connectors = [{
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
strokeWidth: 2,
targetDecorator: {
style: {
fill: '#6BA5D7',
strokeColor: '#6BA5D7'
}
}
}
}];
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "350px",
nodes: nodes,
connectors: connectors,
contextMenuSettings: {
//Enables the context menu
show: true,
// Defines the custom context menu items
items: [{
// Text to be displayed
text: 'Save',
//Sets the id for the item
id: 'save',
//ContextMenu can be visible based on the target in which you open the ContextMenu.
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,
}
}
},
provide: {
diagram: [DiagramContextMenu]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>
To display the custom context menu items alone, set the showCustomMenuOnly
property to true.
Template Support for Context menu
-
Diagram provides template support for context menu. The context menu items can be customized by using the
contextMenuBeforeItemRender
event. The contextMenuBeforeItemRender event triggers while rendering each menu item. -
In the following sample, the menu item is rendered with key code for specified action in Context Menu using the template. Here, the key code is specified for the cut and copy at right corner of the menu items by adding a span element in the
contextMenuBeforeItemRender
event.
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :nodes='nodes' :connectors='connectors'
:contextMenuSettings='contextMenuSettings'></ejs-diagram>
</div>
</template>
<script setup>
import { provide } from "vue";
import { DiagramComponent as EjsDiagram, DiagramContextMenu } from '@syncfusion/ej2-vue-diagrams';
const 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'
}
}]
}
];
const connectors = [{
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
strokeWidth: 2,
targetDecorator: {
style: {
fill: '#6BA5D7',
strokeColor: '#6BA5D7'
}
}
}
}]
const width = "100%";
const height = "350px";
const contextMenuSettings = {
//Enables the context menu
show: true,
items: [{
text: 'Cut', id: 'Cut', target: '.e-diagramcontent',
iconCss: 'e-Cut'
},
{
text: 'Copy', id: 'Copy', target: '.e-diagramcontent',
iconCss: 'e-Copy'
}],
// Hides the default context menu items
showCustomMenuOnly: true,
contextMenuBeforeItemRender: function (args) {
// To render template in li.
let shortCutSpan = createElement('span');
let text = args.item.text;
let shortCutText = text === 'Cut' ? 'Ctrl + S' : (text === 'Copy' ?
'Ctrl + U' : 'Ctrl + Shift + I');
shortCutSpan.textContent = shortCutText;
args.element.appendChild(shortCutSpan);
shortCutSpan.setAttribute('class', 'shortcut');
}
}
provide('diagram', [DiagramContextMenu]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :nodes='nodes' :connectors='connectors'
:contextMenuSettings='contextMenuSettings'></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent, DiagramContextMenu } from '@syncfusion/ej2-vue-diagrams';
let 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'
}
}]
}
];
let connectors = [{
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
strokeWidth: 2,
targetDecorator: {
style: {
fill: '#6BA5D7',
strokeColor: '#6BA5D7'
}
}
}
}];
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "350px",
nodes: nodes,
connectors: connectors,
contextMenuSettings: {
//Enables the context menu
show: true,
items: [{
text: 'Cut', id: 'Cut', target: '.e-diagramcontent',
iconCss: 'e-Cut'
},
{
text: 'Copy', id: 'Copy', target: '.e-diagramcontent',
iconCss: 'e-Copy'
}],
// Hides the default context menu items
showCustomMenuOnly: true,
contextMenuBeforeItemRender: function (args) {
// To render template in li.
let shortCutSpan = createElement('span');
let text = args.item.text;
let shortCutText = text === 'Cut' ? 'Ctrl + S' : (text === 'Copy' ?
'Ctrl + U' : 'Ctrl + Shift + I');
shortCutSpan.textContent = shortCutText;
args.element.appendChild(shortCutSpan);
shortCutSpan.setAttribute('class', 'shortcut');
}
},
}
},
provide: {
diagram: [DiagramContextMenu]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>
Context menu events
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.
<template>
<div id="app">
<ejs-diagram id="diagram" ref="diagram" :width='width' :height='height' :nodes='nodes' :connectors='connectors'
:contextMenuSettings='contextMenuSettings' :contextMenuOpen="contextMenuOpen"
:contextMenuClick="contextMenuClick"></ejs-diagram>
</div>
</template>
<script setup>
import { provide, ref } from "vue";
import { DiagramComponent as EjsDiagram, DiagramContextMenu } from '@syncfusion/ej2-vue-diagrams';
const diagram = ref(null);
const 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'
}
}]
}
];
const connectors = [{
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
strokeWidth: 2,
targetDecorator: {
style: {
fill: '#6BA5D7',
strokeColor: '#6BA5D7'
}
}
}
}]
const width = "100%";
const height = "350px";
const contextMenuSettings = {
//Enables the context menu
show: true,
items: [{
text: 'delete',
id: 'delete',
}],
// Hides the default context menu items
showCustomMenuOnly: false,
};
const contextMenuOpen = (args) => {
var diagram = diagram.value.ej2Instances;
//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.id);
}
}
}
}
const contextMenuClick = (args) => {
var diagram = diagram.value.ej2Instances;
//do your custom action here.
if (args.item.id === 'delete') {
if ((diagram.selectedItems.nodes.length + diagram.selectedItems.connectors.length) > 0) {
diagram.cut();
}
}
}
provide('diagram', [DiagramContextMenu]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>
<template>
<div id="app">
<ejs-diagram id="diagram" ref="diagram" :width='width' :height='height' :nodes='nodes' :connectors='connectors'
:contextMenuSettings='contextMenuSettings' :contextMenuOpen="contextMenuOpen"
:contextMenuClick="contextMenuClick"></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent, DiagramContextMenu } from '@syncfusion/ej2-vue-diagrams';
let 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'
}
}]
}
];
let connectors = [{
id: 'connector1',
sourceID: 'node1',
targetID: 'node2',
type: 'Straight',
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
strokeWidth: 2,
targetDecorator: {
style: {
fill: '#6BA5D7',
strokeColor: '#6BA5D7'
}
}
}
}]
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "350px",
nodes: nodes,
connectors: connectors,
contextMenuSettings: {
//Enables the context menu
show: true,
items: [{
text: 'delete',
id: 'delete',
}],
// Hides the default context menu items
showCustomMenuOnly: false,
},
contextMenuOpen: (args) => {
var diagram = this.$refs.diagram.ej2Instances;
//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.id);
}
}
}
},
contextMenuClick: (args) => {
var diagram = this.$refs.diagram.ej2Instances;
//do your custom action here.
if (args.item.id === 'delete') {
if ((diagram.selectedItems.nodes.length + diagram.selectedItems.connectors.length) > 0) {
diagram.cut();
}
}
}
}
},
provide: {
diagram: [DiagramContextMenu]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>