Skip to content

Commit 0ef7897

Browse files
committed
let TracerManager manage all the Tracers
1 parent 01f90ef commit 0ef7897

9 files changed

Lines changed: 170 additions & 182 deletions

File tree

css/stylesheet.css

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,7 @@ body {
1111
}
1212

1313
*::-webkit-scrollbar {
14-
width: 15px;
15-
}
16-
17-
*::-webkit-scrollbar-track {
18-
background-color: rgb(60, 60, 60);
19-
}
20-
21-
*::-webkit-scrollbar-thumb {
22-
background-color: rgba(0, 0, 0, .1);
23-
}
24-
25-
*::-webkit-scrollbar-button {
26-
background-color: rgba(0, 0, 0, .3);
27-
}
28-
29-
*::-webkit-scrollbar-corner {
30-
background-color: rgb(60, 60, 60);
14+
display: none;
3115
}
3216

3317
a {

index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ <h3>Reference</h3>
116116
<script src="js/ace/ext-language_tools.js"></script>
117117
<script src="js/module/tracer_manager.js"></script>
118118
<script src="js/module/tracer.js"></script>
119+
<script src="js/module/log_tracer.js"></script>
120+
<script src="js/module/array2d.js"></script>
121+
<script src="js/module/array1d.js"></script>
119122
<script src="js/module/directed_graph.js"></script>
120123
<script src="js/module/undirected_graph.js"></script>
121124
<script src="js/module/weighted_directed_graph.js"></script>
122125
<script src="js/module/weighted_undirected_graph.js"></script>
123-
<script src="js/module/array2d.js"></script>
124-
<script src="js/module/array1d.js"></script>
125126
<script src="js/script.js"></script>
126127
<script>
127128
(function (i, s, o, g, r, a, m) {

js/module/array2d.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,24 @@ Array2DTracer.prototype = $.extend(true, Object.create(Tracer.prototype), {
5757
},
5858
_notify: function (x1, y1, x2, y2) {
5959
var second = x2 !== undefined && y2 !== undefined;
60-
this.pushStep({
60+
tm.pushStep(this.capsule, {
6161
type: 'notifying',
6262
x: x1,
6363
y: y1,
6464
value: this.D[x1][y1]
6565
}, !second);
66-
if (second) this.pushStep({
66+
if (second) tm.pushStep(this.capsule, {
6767
type: 'notifying',
6868
x: x2,
6969
y: y2,
7070
value: this.D[x2][y2]
71-
}, true);
72-
this.pushStep({
71+
});
72+
tm.pushStep(this.capsule, {
7373
type: 'notified',
7474
x: x1,
7575
y: y1
7676
}, false);
77-
if (second) this.pushStep({
77+
if (second) tm.pushStep(this.capsule, {
7878
type: 'notified',
7979
x: x2,
8080
y: y2
@@ -149,7 +149,7 @@ Array2DTracer.prototype = $.extend(true, Object.create(Tracer.prototype), {
149149
type: type
150150
};
151151
$.extend(step, coord);
152-
this.pushStep(step, type == 'select');
152+
tm.pushStep(this.capsule, step, type == 'select');
153153
},
154154
processStep: function (step, options) {
155155
var tracer = this;
@@ -198,21 +198,6 @@ Array2DTracer.prototype = $.extend(true, Object.create(Tracer.prototype), {
198198
$table.css('margin-top', top);
199199
$table.css('margin-left', left);
200200
},
201-
prevStep: function () {
202-
this.clear();
203-
$('#tab_trace .wrapper').empty();
204-
var finalIndex = this.traceIndex - 1;
205-
if (finalIndex < 0) {
206-
this.traceIndex = -1;
207-
return;
208-
}
209-
for (var i = 0; i < finalIndex; i++) {
210-
this.step(i, {
211-
virtual: true
212-
});
213-
}
214-
this.step(finalIndex);
215-
},
216201
mousedown: function (e) {
217202
Tracer.prototype.mousedown.call(this, e);
218203

js/module/directed_graph.js

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@ DirectedGraphTracer.prototype = $.extend(true, Object.create(Tracer.prototype),
4545
sigma.plugins.dragNodes(this.s, this.s.renderers[0]);
4646
this.graph = this.capsule.graph = this.s.graph;
4747
},
48-
resize: function () {
49-
Tracer.prototype.resize.call(this);
50-
51-
this.s.renderers[0].resize();
52-
this.refresh();
53-
},
54-
clear: function () {
55-
Tracer.prototype.clear.call(this);
56-
57-
this.clearGraphColor();
58-
},
5948
_setTreeData: function (G, root) {
6049
var tracer = this;
6150

@@ -144,10 +133,10 @@ DirectedGraphTracer.prototype = $.extend(true, Object.create(Tracer.prototype),
144133
return false;
145134
},
146135
_visit: function (target, source) {
147-
this.pushStep({type: 'visit', target: target, source: source}, true);
136+
tm.pushStep(this.capsule, {type: 'visit', target: target, source: source});
148137
},
149138
_leave: function (target, source) {
150-
this.pushStep({type: 'leave', target: target, source: source}, true);
139+
tm.pushStep(this.capsule, {type: 'leave', target: target, source: source});
151140
},
152141
processStep: function (step, options) {
153142
switch (step.type) {
@@ -165,28 +154,24 @@ DirectedGraphTracer.prototype = $.extend(true, Object.create(Tracer.prototype),
165154
}
166155
var source = step.source;
167156
if (source === undefined) source = '';
168-
this.printTrace(visit ? source + ' -> ' + step.target : source + ' <- ' + step.target);
169157
break;
170158
}
171159
},
160+
resize: function () {
161+
Tracer.prototype.resize.call(this);
162+
163+
this.s.renderers[0].resize();
164+
this.refresh();
165+
},
172166
refresh: function () {
173167
Tracer.prototype.refresh.call(this);
174168

175169
this.s.refresh();
176170
},
177-
prevStep: function () {
178-
this.clear();
179-
$('#tab_trace .wrapper').empty();
180-
var finalIndex = this.traceIndex - 1;
181-
if (finalIndex < 0) {
182-
this.traceIndex = -1;
183-
this.refresh();
184-
return;
185-
}
186-
for (var i = 0; i < finalIndex; i++) {
187-
this.step(i, {virtual: true});
188-
}
189-
this.step(finalIndex);
171+
clear: function () {
172+
Tracer.prototype.clear.call(this);
173+
174+
this.clearGraphColor();
190175
},
191176
color: {
192177
visited: '#f00',

js/module/log_tracer.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function LogTracer(module) {
2+
return Tracer.call(this, module || LogTracer);
3+
}
4+
5+
LogTracer.prototype = $.extend(true, Object.create(Tracer.prototype), {
6+
constructor: LogTracer,
7+
_print: function (msg, delay) {
8+
tm.pushStep(this.capsule, {type: 'print', msg: msg}, delay);
9+
},
10+
processStep: function (step, options) {
11+
switch (step.type) {
12+
case 'print':
13+
this.printTrace(step.msg);
14+
break;
15+
}
16+
},
17+
refresh: function () {
18+
this.scrollToEnd(Math.min(50, this.interval));
19+
},
20+
printTrace: function (message) {
21+
$('#tab_trace .wrapper').append($('<span>').append(message + '<br/>'));
22+
},
23+
scrollToEnd: function (duration) {
24+
$('#tab_trace').animate({scrollTop: $('#tab_trace')[0].scrollHeight}, duration);
25+
}
26+
});

js/module/tracer.js

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,39 @@
1-
var timer = null;
2-
var stepLimit = 1e6;
3-
41
var Tracer = function (module) {
52
this.module = module || Tracer;
6-
this.traces = [];
7-
this.traceIndex = -1;
8-
this.stepCnt = 0;
93
this.capsule = tm.allocate(this);
104

115
$.extend(this, this.capsule);
126
return this.new;
137
};
148

159
Tracer.prototype = {
16-
resize: function () {
17-
},
18-
clear: function () {
19-
},
20-
reset: function () {
21-
this.traces = [];
22-
this.stepCnt = 0;
23-
if (timer) clearTimeout(timer);
24-
$('#tab_trace .wrapper').empty();
25-
this.clear();
26-
},
2710
_setData: function (arguments) {
2811
var data = JSON.stringify(arguments);
2912
if (!this.new && this.lastData == data) return true;
3013
this.capsule.lastData = data;
3114
return false;
3215
},
33-
pushStep: function (step, delay) {
34-
if (this.stepCnt++ > stepLimit) throw "Tracer's stack overflow";
35-
var len = this.traces.length;
36-
var last = [];
37-
if (len == 0) {
38-
this.traces.push(last);
39-
} else {
40-
last = this.traces[len - 1];
41-
}
42-
last.push(step);
43-
if (delay) this.traces.push([]);
44-
},
4516
_sleep: function (duration) {
46-
this.pushStep({type: 'sleep', duration: duration}, true);
47-
},
48-
_print: function (msg, delay) {
49-
this.pushStep({type: 'print', msg: msg}, delay);
17+
tm.pushStep(this.capsule, {type: 'sleep', duration: duration});
5018
},
5119
_clear: function () {
52-
this.pushStep({type: 'clear'}, true);
53-
},
54-
visualize: function () {
55-
$('#btn_trace').click();
56-
this.traceIndex = -1;
57-
this.resumeStep();
20+
tm.pushStep(this.capsule, {type: 'clear'});
5821
},
59-
pauseStep: function () {
60-
if (this.traceIndex < 0) return;
61-
tm.pause = true;
62-
if (timer) clearTimeout(timer);
63-
$('#btn_pause').addClass('active');
22+
_next: function () {
23+
tm.newStep();
6424
},
65-
resumeStep: function () {
66-
tm.pause = false;
67-
this.step(this.traceIndex + 1);
68-
$('#btn_pause').removeClass('active');
69-
},
70-
step: function (i, options) {
71-
var tracer = this;
72-
73-
if (isNaN(i) || i >= this.traces.length || i < 0) return;
74-
options = options || {};
75-
76-
this.traceIndex = i;
77-
var trace = this.traces[i];
78-
var sleepDuration = 0;
79-
trace.forEach(function (step) {
80-
switch (step.type) {
81-
case 'sleep':
82-
sleepDuration = step.duration;
83-
break;
84-
case 'print':
85-
tracer.printTrace(step.msg);
86-
break;
87-
case 'clear':
88-
tracer.clear();
89-
tracer.printTrace('clear traces');
90-
break;
91-
default:
92-
tracer.module.prototype.processStep.call(tracer, step, options);
93-
}
94-
});
95-
if (!options.virtual) {
96-
this.refresh();
97-
this.scrollToEnd(Math.min(50, tm.interval));
25+
processStep: function (step, options) {
26+
switch (step.type) {
27+
case 'clear':
28+
this.clear();
29+
break;
9830
}
99-
if (tm.pause) return;
100-
timer = setTimeout(function () {
101-
tracer.step(i + 1, options);
102-
}, sleepDuration || tm.interval);
10331
},
104-
refresh: function () {
32+
resize: function () {
10533
},
106-
prevStep: function () {
107-
this.step(this.traceIndex - 1);
34+
refresh: function () {
10835
},
109-
nextStep: function () {
110-
this.step(this.traceIndex + 1);
36+
clear: function () {
11137
},
11238
mousedown: function (e) {
11339
},
@@ -116,11 +42,5 @@ Tracer.prototype = {
11642
mouseup: function (e) {
11743
},
11844
mousewheel: function (e) {
119-
},
120-
printTrace: function (message) {
121-
$('#tab_trace .wrapper').append($('<span>').append(message + '<br/>'));
122-
},
123-
scrollToEnd: function (duration) {
124-
$('#tab_trace').animate({scrollTop: $('#tab_trace')[0].scrollHeight}, duration);
12545
}
12646
};

0 commit comments

Comments
 (0)