-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.html
More file actions
136 lines (110 loc) · 4.56 KB
/
main.html
File metadata and controls
136 lines (110 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html>
<body>
<link rel="stylesheet" type="text/css" href="iso_style.css" />
<script type="text/javascript" src="d3.v3.min.js"></script>
<script type="text/javascript" src="jquery-1.8.2.js"></script>
<script type="text/javascript" src="util.js"></script>
<script type="text/javascript" src="draw_position.js"></script>
<script type="text/javascript" src="structures.js"></script>
<script type="text/javascript" src="input.js"></script>
<script type="text/javascript" src="algorithms.js"></script>
<script type="text/javascript">
var width = 500;
var height = 500;
$(document).ready(function(){
height = $(window).height()-20;
width = $(window).width()-20;
//alert("in");
var svg = d3.select("svg").attr("width", width).attr("height", height); //.append("g");
var v = new Vertex("a");
v.edges.push(new Edge("a", "b", 3));
graph.push(v);
v = new Vertex("b");
v.edges.push(new Edge("b","a",2));
graph.push(v);
function redraw(){
var graphIn = svg.selectAll(".vertex").data(graph, function(d){return d.name + graph.length;});
//Why that key function: Because its not relevant to positioning whether the edges have changed or not.
//console.log(graph);
//ENTER
graphIn.enter().append("g").classed("vertex", true)
.attr("id",function(d){console.log("creating "+d.name);return "v"+d.name;})
.each(function(d){
d3.select(this).append("circle");
d3.select(this).append("text");
});
//UPDATE
graphIn.each(function(d,i){
////Make vertices
var ind = i;
d3.select(this).select("circle")
.attr("cx", function(d){
return completeGraphPosition( 2*Math.PI*(ind/graph.length) )[0];})
.attr("cy", function(d){
return completeGraphPosition( 2*Math.PI*(ind/graph.length) )[1];})
.attr("r", 10);
////Make labels for those vertices. Pretty lame right now.
d3.select(this).select("text")
.attr("x", function(d){
return completeGraphPosition( 2*Math.PI*(ind/graph.length) )[0]-4;})
.attr("y", function(d){
return completeGraphPosition( 2*Math.PI*(ind/graph.length) )[1]+5;})
.text(function(d){if(d.code !== -1){return d.code;}else{return d.name;}});
});
var allEdges = new Array();
graphIn.each(function(d){
allEdges = allEdges.concat(d.edges);
});
var edgeSet = svg.selectAll(".edge")//.edge is the class of the "g" around each edge
.data(allEdges, function(d){return d.returnKey();});
var edgeEnter = edgeSet.enter().append("g");
//.each(function(d){console.log(["enter:",d.from,d.to,d.weight].join(" "));});
//Parts of the arrow, labelled for control in calculateEdge
edgeEnter.append("line").classed("shaft",true);
edgeEnter.append("line").classed("rightArrow",true);
edgeEnter.append("line").classed("leftArrow",true);
edgeEnter.append("text").classed("edgeWeight",true);
//resetGraphCodes();
edgeSet.each(calculateEdge);
//Fix double edges:
graphIn.call(shiftDoubleEdges);
edgeSet.exit()
//.each(function(d){console.log(["exit:",d.from,d.to,d.weight].join(" "));})
.remove();
//EXIT
graphIn.exit().each(function(d){d3.selectAll(".e"+d.name).remove();}).remove();//Remove all edges leading to/from vertex before removing vertex
//Set click controls:
d3.select("body").on("keydown", function(){ if(InputService.keyDown(d3.event)){
redraw();
}});
d3.select("body").on("keyup", function(){InputService.keyUp(d3.event);}
);
//console.log(svg.select("#drawArea"));
svg.selectAll(".vertex").on("mousedown", function(){console.log("vertex mousedown detected");InputService.mouseDownOnVertex($("#drawArea")[0],this);});//This unbelievably hackish solution brought to you by d3.js selections that refuse to return the DOM element.
svg.selectAll(".vertex").on("mouseclick", function(){console.log("mouseclick detected"); InputService.mouseClickOnVertex($("#drawArea")[0],this);});
svg.on("mouseup",function(){console.log("mouseup detected");
if(InputService.mouseUpOnSVG($("#drawArea")[0])){
redraw();
}
});
}
redraw();
$('#submitInput').click(function(e){
graph = parseGraphInput($('#input')[0].value);
// edgeFixer();
redraw();
});
$("#BFSButton").click(function(e){
Algorithms.BFS(d3.select(".selectedVertex").data()[0]);
});
$("resetButton").click(function(e){
Algorithms.clearCodes();
});
});
</script>
<div id="drawArea"><svg></svg></div>
<textarea id="input" rows="10" cols="90"></textarea><button id="submitInput" >Go!</button><button id="BFSButton" >BFS!</button><button id="resetButton" >Reset</button>
<div id="selectedVertexInfo"></div>
</body>
</html>