-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
349 lines (280 loc) · 9.14 KB
/
main.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
*
* @authors Tom Hu ([email protected])
* @date 2015-11-09 12:03:20
* @version 1.2
*/
'use strict';
$(document).ready(function() {
var graph = {};
var sourceName;
var problem;
var needUpdate = false;
// Page elements
var sourceInput = document.getElementById("source");
var fileInput = document.getElementById('input');
var graphContetOutput = document.getElementById('graph-content');
var feedbackOutput = document.getElementById('feedback');
// Visulization
var network = $('#network-visulization');
$('#input').change(function (event) {
if (this.files.length > 0) {
readFile(this, function (text) {
graph = parseGraph(text);
console.log(graph);
createGraphVisulazation(graph, network.width(), 300);
needUpdate = true;
$('#source').val('');
$('.source-related').trigger('sourceEmpty');
$('.graph-related').trigger('graph'); // Trigger event
});
} else {
display("Input is empty");
$('.graph-related').trigger('graphEmpty');
}
});
$('#source').bind("propertychange change click keyup input paste", function (event) {
// If value has changed...
if ($(this).data('oldVal') != $(this).val()) {
// Updated stored value
$(this).data('oldVal', $(this).val());
$(this).trigger('myChange');
}
});
$('#source').on('myChange', function (event) {
var content = this.value;
console.log(content);
sourceName = content;
if (content) {
needUpdate = true;
$('.source-related').trigger('source');
} else {
needUpdate = false;
$('.source-related').trigger('sourceEmpty');
}
});
// Add event listeners
$('input.graph-related').on('graph', function (event) {
$(this).attr('disabled', false);
});
$('button.graph-related').on('graph', function (event) {
$(this).removeClass('disabled');
});
$('input.graph-related').on('graphEmpty', function (event) {
$(this).attr('disabled', true);
});
$('button.graph-related').on('graphEmpty', function (event) {
$(this).addClass('disabled');
});
$('button.source-related').on('source', function (event) {
$(this).removeClass('disabled');
});
$('button.source-related').on('sourceEmpty', function (event) {
$(this).addClass('disabled');
needUpdate = false;
});
$('#graph-content.graph-related').on('graph', function (event) {
this.innerText = prettyPrint(graph);
});
$('#graph-content.graph-related').on('graphUpdate', function (event) {
needUpdate = true;
// 清空现有的feedback输出
feedbackOutput.innerText = '(Empty)';
// 更新graph输出
var graphStr = prettyPrint(graph);
if (graphStr) {
this.innerText = prettyPrint(graph);
createGraphVisulazation(graph, network.width(), 300); // 更新图
} else {
$('.graph-related').trigger('graphEmpty');
}
});
$('#graph-content.graph-related').on('graphEmpty', function (event) {
this.innerText = '(Empty graph)';
removeGraphVisulazation();
});
// Compute
$('#compute-all-btn').click(function (event) {
event.preventDefault();
initDijkstraProblem();
compute();
});
// Single Step
$('#single-step-btn').click(function (event) {
event.preventDefault();
initDijkstraProblem();
singleStep();
});
// Reset
$('#reset-btn').click(function (event) {
event.preventDefault();
reset();
});
// Add / Update Edge
$('#update-edge-btn').click(function (event) {
event.preventDefault();
updateEdge();
});
// Add New Node
$('#add-new-node-btn').click(function (event) {
event.preventDefault();
addNewNode();
});
// Delete Old Node
$('#delete-node-btn').click(function (event) {
event.preventDefault();
deleteNode();
});
// Delete Edge
$('#delete-edge-btn').click(function (event) {
event.preventDefault();
deleteEdge();
});
function initDijkstraProblem() {
if (needUpdate) {
problem = new Dijkstra(graph, sourceName);
needUpdate = false;
}
}
function reset() {
// 清空所有
// 清空文件输入
$('#input').val('');
$('.graph-related').trigger('graphEmpty');
// 清空source输入
$('#source').val('');
$('.source-related').trigger('sourceEmpty');
// 清空feedback
feedbackOutput.innerText = '(Empty)';
// init
graph = {};
sourceName = '';
problem = undefined;
needUpdate = false;
}
function singleStep() {
if (!problem.singleStep()) {
// Done
console.log('Done!');
alert('Done!');
}
var result = problem.getResult();
var resultStr = getResultStr(result);
display(resultStr);
updateGraphVisulazation(result);
}
function compute() {
var result = problem.solve();
var resultStr = getResultStr(result);
graphContetOutput.innerText = prettyPrint(graph);
display(resultStr);
updateGraphVisulazation(result);
}
function display(text) {
feedbackOutput.innerText = text;
}
// Example of using update.js
// load update.js before this
var update = updateExport(display);
// Re-export
function updateEdge() {
var srcVal = $('#update-edge-src').val();
var destVal = $('#update-edge-dest').val();
var weightVal = $('#update-edge-weight').val();
if(srcVal.length && destVal.length && weightVal.length) {
if (update.updateEdge(graph, srcVal, destVal, parseInt(weightVal))) {
$('.graph-related').trigger('graphUpdate'); // 触发graph update事件
}
} else {
display("error: you don't enter everything!");
}
// 清空输入
$('#update-edge-src').val('');
$('#update-edge-dest').val('');
$('#update-edge-weight').val('');
}
function addNewNode() {
var nodeIdVal = $('#add-new-node-id').val();
if(nodeIdVal.length) {
if (update.addNode(graph, nodeIdVal, {})) {
$('.graph-related').trigger('graphUpdate');
}
} else {
display("error: you don't enter everything!");
}
// 清空输入
$('#add-new-node-id').val('');
}
function deleteNode() {
var nodeIdVal = $('#delete-node-id').val();
if (nodeIdVal.length) {
if (update.deleteNode(graph, nodeIdVal)) { // 成功update
$('.graph-related').trigger('graphUpdate');
if (nodeIdVal === sourceName) {
// 清空source input
$('#source').val('');
$('.source-related').trigger('sourceEmpty');
}
}
} else {
display("error: you don't enter everything!");
}
// 清空输入
$('#delete-node-id').val('');
}
function deleteEdge() {
var srcVal = $('#delete-edge-src').val();
var destVal = $('#delete-edge-dest').val();
if(srcVal.length && destVal.length) {
if (update.deleteEdge(graph, srcVal, destVal)) {
$('.graph-related').trigger('graphUpdate');
if (srcVal === sourceName || destVal == sourceName) {
// 清空source input
$('#source').val('');
$('.source-related').trigger('sourceEmpty');
}
}
} else {
display("error: you don't enter everything!");
}
// 清空输入
$('#delete-edge-src').val('');
$('#delete-edge-dest').val('');
}
// Helpers
function prettyPrint(graph) {
var str = "";
for (var v in graph) {
str += (v + ": ");
for (var u in graph[v]) {
if(u == v) continue;
str += (u + ":" + graph[v][u] + " ");
}
str += "\n";
}
return str;
}
function getResultStr(result) {
var retulrStr = '';
for (var key in result) {
var node = key;
var distance = result[key].distance;
var path = result[key].path;
if (distance === Infinity) continue; // 不输出无法到达的点
var pathStr = '';
for (var i = 0; i < path.length; i++) {
pathStr += (path[i] + ' > ');
}
pathStr += node;
retulrStr += (node + ': ' + pathStr + ', ' + distance + '\n');
}
return retulrStr;
}
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
});