Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #31 from lcpopa/main
Browse files Browse the repository at this point in the history
Added click event on graph links (edges)
  • Loading branch information
lcpopa authored Apr 19, 2022
2 parents 684aad6 + a9a9e47 commit 9f1ce28
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
43 changes: 43 additions & 0 deletions happi-graph-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,54 @@ export const getNodeAnchorPoint = (node, point) => {
}
};

export const adjustDestinationNode = (from, to, toPoint) => {
let offset = 5;

// upwards right
if(from.x < to.x && from.y > to.y) {
if (toPoint == 'BOTTOM' ) {
to.x = to.x - offset;
}else if (toPoint == 'LEFT'){
to.y = to.y + offset;
}
}

// upwards left
if(from.x > to.x && from.y > to.y){
if(toPoint == 'BOTTOM'){
to.x = to.x + offset;
}else if (toPoint == 'RIGHT'){
to.y = to.y + offset;
}
}

// downwards right
if(from.x < to.x && from.y < to.y){
if(toPoint == 'TOP'){
to.x = to.x - offset;
}else if (toPoint == 'LEFT'){
to.y = to.y - offset;
}
}

// downwards left
if(from.x > to.x && from.y < to.y){
if(toPoint == 'TOP'){
to.x = to.x + offset;
}else if (toPoint == 'LEFT'){
to.y = to.y - offset;
}
}

return to;
};

export const getLinkCoordinates = (nodeA, nodeB, graphDirection) => {
let _relativeTo = relativeTo(nodeA, nodeB, graphDirection);

let from = getNodeAnchorPoint(nodeA, _relativeTo.a);
let to = getNodeAnchorPoint(nodeB, _relativeTo.b);
to = adjustDestinationNode(from, to, _relativeTo.b);

return {
from: { x: from.x, y: from.y },
Expand Down
27 changes: 26 additions & 1 deletion happi-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class HappiGraph extends PolymerElement {

this.zooming = this.zooming.bind(this);
this.onNodeClick = this.onNodeClick.bind(this);
this.onLinkClick = this.onLinkClick.bind(this);
}

static get properties() {
Expand Down Expand Up @@ -160,7 +161,7 @@ class HappiGraph extends PolymerElement {
this.links = [
...newGraphData.links.map(e => {
return {
id: `${e.from}-${e.to}`,
id: e.id,
label: e.label,

from: this.nodes.filter(n => n.id === e.from).pop(),
Expand Down Expand Up @@ -504,8 +505,10 @@ class HappiGraph extends PolymerElement {
})
.attr('marker-start', (d) => (d.connectionFrom) ? 'url(#arrow-start)' : null)
.attr('marker-end', (d) => (d.connectionTo) ? 'url(#arrow-end)' : null)
.attr('id', function(d) { return d.id; })
.attr('from', function(d) { return d.from.id; })
.attr('to', function(d) { return d.to.id; })
.on('click', this.onLinkClick)
.attr('x1', (d) => {
let { from, to } = getLinkCoordinates(d.from, d.to, self.graphDirection);

Expand Down Expand Up @@ -634,6 +637,18 @@ class HappiGraph extends PolymerElement {
);
}

onLinkClick(link) {
let element = this.shadowRoot.querySelector('#svg .all-group .links-group [id="'+ link.id +'"]')
this.dispatchEvent(
new CustomEvent('happi-graph-on-link-click', {
bubbles: true,
detail: {
linkElement: element
}
})
);
}

hasSize(a) {
if(a) {
return a.length > 0;
Expand Down Expand Up @@ -777,6 +792,16 @@ class HappiGraph extends PolymerElement {
markerUnits="strokeWidth">
<path d="M0,0 L0,6 L9,3 z" fill="#000" />
</marker>
<marker id="arrow-end-selected"
markerWidth="10"
markerHeight="10"
refx="7"
refy="3"
orient="auto"
markerUnits="strokeWidth">
<path d="M0,0 L0,6 L9,3 z" fill="var(--happi-graph-primary-color)" />
</marker>
</defs>
</svg>
</div>
Expand Down

0 comments on commit 9f1ce28

Please sign in to comment.