1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var MINIMUM_EDGE_SEPARATION = 20;
6
7function isEdgeInitiallyVisible(target, index, source, type) {
8  return type == "control" && (target.cfg || source.cfg);
9}
10
11var Edge = function(target, index, source, type) {
12  this.target = target;
13  this.source = source;
14  this.index = index;
15  this.type = type;
16  this.backEdgeNumber = 0;
17  this.visible = isEdgeInitiallyVisible(target, index, source, type);
18};
19
20Edge.prototype.stringID = function() {
21  return this.source.id + "," + this.index +  "," + this.target.id;
22};
23
24Edge.prototype.isVisible = function() {
25  return this.visible && this.source.visible && this.target.visible;
26};
27
28Edge.prototype.getInputHorizontalPosition = function(graph) {
29  if (this.backEdgeNumber > 0) {
30    return graph.maxGraphNodeX + this.backEdgeNumber * MINIMUM_EDGE_SEPARATION;
31  }
32  var source = this.source;
33  var target = this.target;
34  var index = this.index;
35  var input_x = target.x + target.getInputX(index);
36  var inputApproach = target.getInputApproach(this.index);
37  var outputApproach = source.getOutputApproach(graph);
38  if (inputApproach > outputApproach) {
39    return input_x;
40  } else {
41    var inputOffset = MINIMUM_EDGE_SEPARATION * (index + 1);
42    return (target.x < source.x)
43      ? (target.x + target.getTotalNodeWidth() + inputOffset)
44      : (target.x - inputOffset)
45  }
46}
47
48Edge.prototype.generatePath = function(graph) {
49  var target = this.target;
50  var source = this.source;
51  var input_x = target.x + target.getInputX(this.index);
52  var arrowheadHeight = 7;
53  var input_y = target.y - 2 * DEFAULT_NODE_BUBBLE_RADIUS - arrowheadHeight;
54  var output_x = source.x + source.getOutputX();
55  var output_y = source.y + graph.getNodeHeight(source) + DEFAULT_NODE_BUBBLE_RADIUS;
56  var inputApproach = target.getInputApproach(this.index);
57  var outputApproach = source.getOutputApproach(graph);
58  var horizontalPos = this.getInputHorizontalPosition(graph);
59
60  var result = "M" + output_x + "," + output_y +
61    "L" + output_x + "," + outputApproach +
62    "L" + horizontalPos + "," + outputApproach;
63
64  if (horizontalPos != input_x) {
65    result += "L" + horizontalPos + "," + inputApproach;
66  } else {
67    if (inputApproach < outputApproach) {
68      inputApproach = outputApproach;
69    }
70  }
71
72  result += "L" + input_x + "," + inputApproach +
73    "L" + input_x + "," + input_y;
74  return result;
75}
76
77Edge.prototype.isBackEdge = function() {
78  return this.target.hasBackEdges() && (this.target.rank < this.source.rank);
79}
80