Skip to content

Commit

Permalink
add functions to reduce repetition of code
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilshetye committed Oct 29, 2024
1 parent 88053a3 commit 3282e2c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'mxgraph/javascript/src/css/common.css'

import mxGraphFactory from 'mxgraph'

import { getRotationParameters, PORTDIRECTIONS } from './ToolbarTools'
import { getRotationParameters, getPins, getPointXY, getXYPos } from './ToolbarTools'

const {
mxPoint
Expand Down Expand Up @@ -78,85 +78,16 @@ export function getSvgMetadata (graph, parent, evt, target, x, y, component) {
const portRotation = blockport.port_rotation
const rotationParameters = getRotationParameters(portOrientation, portRotation)

let pointX
let pointY
let pins
switch (rotationParameters.rotatename) {
case 'ExplicitInputPort':
pointX = -portSize
pointY = -portSize / 2
break
case 'ControlPort':
pointX = -portSize / 2
pointY = -portSize
break
case 'ExplicitOutputPort':
pointX = 0
pointY = -portSize / 2
break
case 'CommandPort':
pointX = -portSize / 2
pointY = 0
break
default:
pointX = -portSize / 2
pointY = -portSize / 2
break
}
switch (portOrientation) {
case 'ExplicitInputPort':
v1.explicitInputPorts += 1
pins = v1.pins.explicitInputPorts
break
case 'ImplicitInputPort':
v1.implicitInputPorts += 1
pins = v1.pins.implicitInputPorts
break
case 'ControlPort':
v1.controlPorts += 1
pins = v1.pins.controlPorts
break
case 'ExplicitOutputPort':
v1.explicitOutputPorts += 1
pins = v1.pins.explicitOutputPorts
break
case 'ImplicitOutputPort':
v1.implicitOutputPorts += 1
pins = v1.pins.implicitOutputPorts
break
case 'CommandPort':
v1.commandPorts += 1
pins = v1.pins.commandPorts
break
default:
pins = null
break
}
const pins = getPins(portOrientation, v1)

const pointXY = getPointXY(rotationParameters)
const pointX = pointXY.pointX
const pointY = pointXY.pointY

const xyPos = getXYPos(rotationParameters, xPos, yPos)
xPos = xyPos.xPos
yPos = xyPos.yPos

const xPosOld = xPos
switch (rotationParameters.portdirection) {
case PORTDIRECTIONS.L2T:
case PORTDIRECTIONS.T2L:
xPos = yPos
yPos = xPosOld
break
case PORTDIRECTIONS.L2R:
xPos = 1 - xPosOld
/* same yPos */
break
case PORTDIRECTIONS.L2B:
xPos = yPos
yPos = 1 - xPosOld
break
case PORTDIRECTIONS.T2R:
xPos = 1 - yPos
yPos = xPosOld
break
case PORTDIRECTIONS.T2B:
/* same xPos */
yPos = 1 - yPos
break
}
const point = new mxPoint(pointX, pointY)

const vp = graph.insertVertex(v1, null, null, xPos, yPos, portSize, portSize, portOrientation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export function renderXML () {
parseXmlToGraph(xmlDoc, graph)
}

export const PORTDIRECTIONS = {
const PORTDIRECTIONS = {
UNK: 0,
LOR: 4,
L2T: 5,
Expand Down Expand Up @@ -398,6 +398,96 @@ export function getRotationParameters (stylename, rotation) {
return { rotatename, portdirection }
}

export function getPins (portOrientation, v1) {
let pins
switch (portOrientation) {
case 'ExplicitInputPort':
v1.explicitInputPorts += 1
pins = v1.pins?.explicitInputPorts
break
case 'ImplicitInputPort':
v1.implicitInputPorts += 1
pins = v1.pins?.implicitInputPorts
break
case 'ControlPort':
v1.controlPorts += 1
pins = v1.pins?.controlPorts
break
case 'ExplicitOutputPort':
v1.explicitOutputPorts += 1
pins = v1.pins?.explicitOutputPorts
break
case 'ImplicitOutputPort':
v1.implicitOutputPorts += 1
pins = v1.pins?.implicitOutputPorts
break
case 'CommandPort':
v1.commandPorts += 1
pins = v1.pins?.commandPorts
break
default:
pins = null
break
}
return pins
}

export function getPointXY (rotationParameters) {
let pointX
let pointY
switch (rotationParameters.rotatename) {
case 'ExplicitInputPort':
pointX = -portSize
pointY = -portSize / 2
break
case 'ControlPort':
pointX = -portSize / 2
pointY = -portSize
break
case 'ExplicitOutputPort':
pointX = 0
pointY = -portSize / 2
break
case 'CommandPort':
pointX = -portSize / 2
pointY = 0
break
default:
pointX = -portSize / 2
pointY = -portSize / 2
break
}
return { pointX, pointY }
}

export function getXYPos (rotationParameters, xPos, yPos) {
const xPosOld = xPos
switch (rotationParameters.portdirection) {
case PORTDIRECTIONS.L2T:
case PORTDIRECTIONS.T2L:
xPos = yPos
yPos = xPosOld
break
case PORTDIRECTIONS.L2R:
xPos = 1 - xPosOld
/* same yPos */
break
case PORTDIRECTIONS.L2B:
xPos = yPos
yPos = 1 - xPosOld
break
case PORTDIRECTIONS.T2R:
xPos = 1 - yPos
yPos = xPosOld
break
case PORTDIRECTIONS.T2B:
/* same xPos */
yPos = 1 - yPos
break
}
return { xPos, yPos }
}

function parseXmlToGraph (xmlDoc, graph) {
const parent = graph.getDefaultParent()
let v1
Expand Down Expand Up @@ -515,76 +605,15 @@ function parseXmlToGraph (xmlDoc, graph) {
console.log('rotationParameters:', rotationParameters)
}

switch (stylename) {
case 'ExplicitInputPort':
v1.explicitInputPorts += 1
break
case 'ImplicitInputPort':
v1.implicitInputPorts += 1
break
case 'ControlPort':
v1.controlPorts += 1
break
case 'ExplicitOutputPort':
v1.explicitOutputPorts += 1
break
case 'ImplicitOutputPort':
v1.implicitOutputPorts += 1
break
case 'CommandPort':
v1.commandPorts += 1
break
}
getPins(stylename, v1)

let pointX
let pointY
switch (rotationParameters.rotatename) {
case 'ExplicitInputPort':
pointX = -portSize
pointY = -portSize / 2
break
case 'ControlPort':
pointX = -portSize / 2
pointY = -portSize
break
case 'ExplicitOutputPort':
pointX = 0
pointY = -portSize / 2
break
case 'CommandPort':
pointX = -portSize / 2
pointY = 0
break
default:
pointX = -portSize / 2
pointY = -portSize / 2
break
}
const pointXY = getPointXY(rotationParameters)
const pointX = pointXY.pointX
const pointY = pointXY.pointY

const xPosOld = xPos
switch (rotationParameters.portdirection) {
case PORTDIRECTIONS.L2T:
case PORTDIRECTIONS.T2L:
xPos = yPos
yPos = xPosOld
break
case PORTDIRECTIONS.L2R:
xPos = 1 - xPosOld
/* same yPos */
break
case PORTDIRECTIONS.L2B:
xPos = yPos
yPos = 1 - xPosOld
break
case PORTDIRECTIONS.T2R:
xPos = 1 - yPos
yPos = xPosOld
break
case PORTDIRECTIONS.T2B:
/* same xPos */
yPos = 1 - yPos
break
}
const xyPos = getXYPos(rotationParameters, xPos, yPos)
xPos = xyPos.xPos
yPos = xyPos.yPos

const point = new mxPoint(pointX, pointY)
const vp = graph.insertVertex(v1, vertexId, null, xPos, yPos, portSize, portSize, style)
Expand Down

0 comments on commit 3282e2c

Please sign in to comment.