<template>
<div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<button class="btn btn-primary btn-sm" @click="addConnector()">
Add Connector
</button>
<button class="btn btn-primary btn-sm" @click="addNode()">
Add Event
</button>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div id="app">
<ejs-diagram
id="diagram"
ref="diagramObj"
:width="width"
:height="height"
style="display: block"
:node-template="nodeTemplate"
:collection-change="diagramCollectionChange"
:source-point-change="connectorSourcePointChange"
:target-point-change="connectorTargetPointChange"
/>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import Vue from 'vue'
import { DiagramPlugin, DiagramTools, NodeConstraints, ConnectorConstraints, PortVisibility } from '@syncfusion/ej2-vue-diagrams'
import NodeDesignTemplate from '@/components/NodeDesignTemplate.vue'
Vue.use(DiagramPlugin)
let diagramInstance
export default {
name: 'App',
data () {
return {
width: '100%',
height: '700px',
nodeCounter: 0,
nodeArray: [],
connectorCounter: 0,
connectorArray: [],
nodeTemplate () {
return { template: NodeDesignTemplate }
}
}
},
mounted () {
diagramInstance = this.$refs.diagramObj.ej2Instances
},
methods: {
// On click of the add node button add the node to diagram
addNode () {
const ports = [
{ name: 'port1', offset: { x: 0, y: 0.5 }, shape: 'circle', visibility: PortVisibility.Visible },
{ name: 'port2', offset: { x: 1, y: 0.5 }, shape: 'circle', visibility: PortVisibility.Visible },
{ name: 'port3', offset: { x: 0.5, y: 0 }, shape: 'circle', visibility: PortVisibility.Visible },
{ name: 'port4', offset: { x: 0.5, y: 1 }, shape: 'circle', visibility: PortVisibility.Visible }
]
const name = 'node' + this.nodeCounter
// const label = [{ text: name, offset: { x: 0.5, y: 0.06 } }]
const drawingshape = {
type: 'HTML',
value: {
select: this.nodeCounter
}
}
const node = {
addInfo: { name },
name,
shape: drawingshape,
ports,
style: {
fill: 'white',
strokeColor: 'black'
},
annotations: [
{
content: name,
offset: { x: 0.5, y: 0.06 }
}
],
constraints: NodeConstraints.Default & ~(NodeConstraints.OutConnect | NodeConstraints.InConnect)
}
diagramInstance.drawingObject = node
// To draw an object once, activate draw once
diagramInstance.tool = DiagramTools.DrawOnce
diagramInstance.dataBind()
// Store Node information in Node Array
this.nodeCounter++
const nodeObj = {}
nodeObj.name = name
this.nodeArray.push(nodeObj)
},
// On click of the add Connector button add the connector between nodes
addConnector () {
const name = 'Connector' + this.connectorCounter
const connectors = {
name,
type: 'Straight',
addInfo: { name },
constraints: (ConnectorConstraints.Default & ~ConnectorConstraints.ConnectToNearByNode)
}
diagramInstance.drawingObject = connectors
// To draw an object once, activate draw once
diagramInstance.tool = DiagramTools.DrawOnce
diagramInstance.dataBind()
// Store connector information in Connector Array
this.connectorCounter++
const connectorObj = {}
connectorObj.name = name
this.connectorArray.push(connectorObj)
},
// when we add a new Node/Connector into the diagram, collectionchange event triggers
diagramCollectionChange (args) {
// If new connector has been added then add the label to the connector
if (args.element.propName === 'connectors' && args.type === 'Addition' && args.state === 'Changed' && args.element.status === 'New') {
// Save the SourceNode and TargetNode associated with the Connectors
const connector = this.connectorArray.find(con => con.name === args.element.addInfo.name)
connector.sourceNode = args.element.sourceWrapper !== undefined ? args.element.sourceWrapper.description : ''
connector.targetNode = args.element.targetWrapper !== undefined ? args.element.targetWrapper.description : ''
} else if (args.element.propName === 'connectors' && args.type === 'Removal' && args.state === 'Changed') {
// If the connector has been deleted then remove the respective information from Connector Array
this.connectorArray.splice(this.connectorArray.findIndex(con => con.name === args.element.addInfo.name), 1)
} else if (args.element.propName === 'nodes' && args.type === 'Removal' && args.state === 'Changed') {
// If the Node has been deleted then remove the respective information from Node Array
this.nodeArray.splice(this.nodeArray.findIndex(node => node.name === args.element.addInfo.name), 1)
}
},
// On change of the Connector Source call the function to get the sourcePoint value and store it in connectorArray
connectorSourcePointChange (args) {
if (args.state === 'Completed' && args.name === 'sourcePointChange' && args.connector.sourceID !== undefined) {
const connector = this.connectorArray.find(con => con.name === args.connector.addInfo.name)
connector.sourceNode = args.connector.sourceWrapper !== undefined ? args.connector.sourceWrapper.description : ''
}
},
// On change of the Connector Target call the function to get targetPoint value and store it in connectorArray
connectorTargetPointChange (args) {
if (args.state === 'Completed' && args.name === 'targetPointChange' && args.connector.targetID !== undefined) {
const connector = this.connectorArray.find(con => con.name === args.connector.addInfo.name)
connector.targetNode = args.connector.targetWrapper !== undefined ? args.connector.targetWrapper.description : ''
}
},
}
}
</script>
<style>
@import "@/node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>