-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
106 lines (98 loc) · 2.61 KB
/
script.js
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
$(function() {
var cy = cytoscape({
container: document.getElementById('cy'),
style: cytoscape.stylesheet()
.selector('node')
.css({
'content': 'data(id)'
})
.selector('edge')
.css({
'target-arrow-shape': 'none',
'width': 4,
'line-color': '#ddd',
'curve-style': 'bezier',
'content': 'data(weight)'
})
.selector('.highlighted')
.css({
'background-color': '#61bffc',
'line-color': '#61bffc',
'transition-duration': '0.5s'
})
});
var count = 0;
$('#cy').dblclick(function(e) {
// console.log(e.pageX)
// console.log(e.pageY)
cy.add({
group: "nodes",
data: {
id: count
},
position: {
x: e.pageX,
y: e.pageY
},
classes : "node"
});
count++;
});
$('#cy').click(function(e) {
selectedNodes = []
cy.elements().each(function(i, ele) {
if (ele.selected()) {
selectedNodes.push(ele.id())
}
// console.log( ele.id() + ' is ' + ( ele.selected() ? 'selected' : 'not selected' ) );
});
if (selectedNodes.length == 2) {
edgeWeight = prompt("Please enter the cost of the edge")
if (edgeWeight != null) {
cy.add({
group: "edges",
data: {
id: selectedNodes[0] + selectedNodes[1],
weight: edgeWeight,
source: selectedNodes[0],
target: selectedNodes[1],
classes: "edge"
}
})
}
for (var i = selectedNodes.length - 1; i >= 0; i--) {
cy.$('#' + selectedNodes[i]).unselect();
}
}
});
// $("#aStarBtn").click(function() {
// var aStar = cy.elements().aStar({
// root: "#" + $('#startNode').val(),
// goal: "#" + $('#endNode').val()
// });
// aStar.path.select();
// })
$("#aStarBtn").click(function() {
var dijkstra = cy.elements().dijkstra(("#" + $('#startNode').val()), function(){
console.log("id: " + this.data('id') + "\nweight: " + this.data('weight'));
return this.data('weight');
}, false);
var pathToJ = dijkstra.pathTo( cy.$("#" + $('#endNode').val()) );
for (var i = pathToJ.length - 1; i >= 0; i--) {
console.log(pathToJ[i])
pathToJ[i].addClass('highlighted');
}
// var i = 0;
// var highlightNextEle = function(){
// if( i < pathToJ.length ){
// pathToJ[i].addClass('highlighted');
// i++;
// setTimeout(highlightNextEle, 500);
// }
// };
// highlightNextEle();
})
$("#clearBtn").click(function() {
cy.$().remove();
})
});