Skip to content

Commit debb978

Browse files
committed
Add namespace for graph objects
1 parent 978199f commit debb978

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ This is implemented in graph-visualization/src/graph.js.
2626

2727
Usage:
2828

29-
var graph = new Graph({limit: 100}); // set maximum number of nodes, optional
30-
var node1 = new Node(1); // create nodes with id
31-
var node2 = new Node(2);
29+
var graph = new GRAPHVIS.Graph({limit: 100}); // set maximum number of nodes, optional
30+
var node1 = new GRAPHVIS.Node(1); // create nodes with id
31+
var node2 = new GRAPHVIS.Node(2);
3232
graph.addNode( node1 ); // add nodes
3333
graph.addNode( node2 );
3434
graph.addEdge( node1, node2 ); // create edge between nodes

examples/simple_graph/simple_graph.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Drawing.SimpleGraph = function(options) {
6464
var camera, controls, scene, renderer, interaction, geometry, object_selection;
6565
var stats;
6666
var info_text = {};
67-
var graph = new Graph({limit: options.limit});
67+
var graph = new GRAPHVIS.Graph({limit: options.limit});
6868

6969
var geometries = [];
7070

@@ -154,7 +154,7 @@ Drawing.SimpleGraph = function(options) {
154154
*/
155155
function createGraph() {
156156

157-
var node = new Node(0);
157+
var node = new GRAPHVIS.Node(0);
158158
node.data.title = "This is node " + node.id;
159159
graph.addNode(node);
160160
drawNode(node);
@@ -168,7 +168,7 @@ Drawing.SimpleGraph = function(options) {
168168

169169
var numEdges = randomFromTo(1, that.edges_count);
170170
for(var i=1; i <= numEdges; i++) {
171-
var target_node = new Node(i*steps);
171+
var target_node = new GRAPHVIS.Node(i*steps);
172172
if(graph.addNode(target_node)) {
173173
target_node.data.title = "This is node " + target_node.id;
174174

examples/sphere_graph/sphere_graph.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Drawing.SphereGraph = function(options) {
6363
var camera, controls, scene, renderer, interaction, geometry, object_selection;
6464
var stats;
6565
var info_text = {};
66-
var graph = new Graph({limit: options.limit});
66+
var graph = new GRAPHVIS.Graph({limit: options.limit});
6767

6868
var geometries = [];
6969

@@ -156,7 +156,7 @@ Drawing.SphereGraph = function(options) {
156156
* numNodes and numEdges.
157157
*/
158158
function createGraph() {
159-
var node = new Node(0);
159+
var node = new GRAPHVIS.Node(0);
160160
graph.addNode(node);
161161
drawNode(node);
162162

@@ -169,7 +169,7 @@ Drawing.SphereGraph = function(options) {
169169

170170
var numEdges = randomFromTo(1, that.edges_count);
171171
for(var i=1; i <= numEdges; i++) {
172-
var target_node = new Node(i*steps);
172+
var target_node = new GRAPHVIS.Node(i*steps);
173173
if(graph.addNode(target_node)) {
174174
drawNode(target_node);
175175
nodes.push(target_node);

src/graph.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
id, position and data.
1111
1212
Example:
13-
node = new Node(1);
13+
node = new GRAPHVIS.Node(1);
1414
node.position.x = 100;
1515
node.position.y = 100;
1616
node.data.title = "Title of the node";
@@ -23,7 +23,7 @@
2323
Connects to nodes together.
2424
2525
Example:
26-
edge = new Edge(node1, node2);
26+
edge = new GRAPHVIS.Edge(node1, node2);
2727
2828
An edge can also be extended with the data attribute. E.g. set a
2929
type like "friends", different types can then be draw in differnt ways.
@@ -48,15 +48,17 @@
4848
4949
*/
5050

51-
function Graph(options) {
51+
var GRAPHVIS = GRAPHVIS || {};
52+
53+
GRAPHVIS.Graph = function(options) {
5254
this.options = options || {};
5355
this.nodeSet = {};
5456
this.nodes = [];
5557
this.edges = [];
5658
this.layout = undefined;
57-
}
59+
};
5860

59-
Graph.prototype.addNode = function(node) {
61+
GRAPHVIS.Graph.prototype.addNode = function(node) {
6062
if(this.nodeSet[node.id] === undefined && !this.reached_limit()) {
6163
this.nodeSet[node.id] = node;
6264
this.nodes.push(node);
@@ -65,44 +67,44 @@ Graph.prototype.addNode = function(node) {
6567
return false;
6668
};
6769

68-
Graph.prototype.getNode = function(node_id) {
70+
GRAPHVIS.Graph.prototype.getNode = function(node_id) {
6971
return this.nodeSet[node_id];
7072
};
7173

72-
Graph.prototype.addEdge = function(source, target) {
74+
GRAPHVIS.Graph.prototype.addEdge = function(source, target) {
7375
if(source.addConnectedTo(target) === true) {
74-
var edge = new Edge(source, target);
76+
var edge = new GRAPHVIS.Edge(source, target);
7577
this.edges.push(edge);
7678
return true;
7779
}
7880
return false;
7981
};
8082

81-
Graph.prototype.reached_limit = function() {
83+
GRAPHVIS.Graph.prototype.reached_limit = function() {
8284
if(this.options.limit !== undefined)
8385
return this.options.limit <= this.nodes.length;
8486
else
8587
return false;
8688
};
8789

8890

89-
function Node(node_id) {
91+
GRAPHVIS.Node = function(node_id) {
9092
this.id = node_id;
9193
this.nodesTo = [];
9294
this.nodesFrom = [];
9395
this.position = {};
9496
this.data = {};
95-
}
97+
};
9698

97-
Node.prototype.addConnectedTo = function(node) {
99+
GRAPHVIS.Node.prototype.addConnectedTo = function(node) {
98100
if(this.connectedTo(node) === false) {
99101
this.nodesTo.push(node);
100102
return true;
101103
}
102104
return false;
103105
};
104106

105-
Node.prototype.connectedTo = function(node) {
107+
GRAPHVIS.Node.prototype.connectedTo = function(node) {
106108
for(var i=0; i < this.nodesTo.length; i++) {
107109
var connectedNode = this.nodesTo[i];
108110
if(connectedNode.id == node.id) {
@@ -113,8 +115,8 @@ Node.prototype.connectedTo = function(node) {
113115
};
114116

115117

116-
function Edge(source, target) {
118+
GRAPHVIS.Edge = function(source, target) {
117119
this.source = source;
118120
this.target = target;
119121
this.data = {};
120-
}
122+
};

0 commit comments

Comments
 (0)