forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtracer.js
More file actions
161 lines (134 loc) · 3.96 KB
/
tracer.js
File metadata and controls
161 lines (134 loc) · 3.96 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
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
var timer = null;
var lastModule = null, lastData = null;
var stepLimit = 1e6;
var Tracer = function (module) {
this.module = module || Tracer;
this.traces = [];
this.pause = false;
this.traceOptions = null;
this.traceIndex = -1;
this.stepCnt = 0;
var moduleChanged = lastModule != module;
if (moduleChanged) $module_container.empty();
return moduleChanged;
};
Tracer.prototype.resize = function () {
};
Tracer.prototype.clear = function () {
};
Tracer.prototype.reset = function () {
this.traces = [];
this.stepCnt = 0;
if (timer) clearTimeout(timer);
$('#tab_trace .wrapper').empty();
this.clear();
};
Tracer.prototype._setData = function (arguments) {
var data = JSON.stringify(arguments);
if (lastModule == this.module && lastData == data) return true;
lastData = data;
return false;
};
Tracer.prototype.pushStep = function (step, delay) {
if (this.stepCnt++ > stepLimit) throw "Tracer's stack overflow";
var len = this.traces.length;
var last = [];
if (len == 0) {
this.traces.push(last);
} else {
last = this.traces[len - 1];
}
last.push(step);
if (delay) this.traces.push([]);
};
Tracer.prototype._sleep = function (duration) {
this.pushStep({type: 'sleep', duration: duration}, true);
};
Tracer.prototype._print = function (msg, delay) {
this.pushStep({type: 'print', msg: msg}, delay);
};
Tracer.prototype._pace = function (interval) {
this.pushStep({type: 'pace', interval: interval}, false);
};
Tracer.prototype._clear = function () {
this.pushStep({type: 'clear'}, true);
};
Tracer.prototype.visualize = function (options) {
options = options || {};
options.interval = options.interval || 500;
$('#btn_trace').click();
this.traceOptions = options;
this.traceIndex = -1;
this.resumeStep();
};
Tracer.prototype.isPause = function () {
return this.pause;
};
Tracer.prototype.pauseStep = function () {
if (this.traceIndex < 0) return;
this.pause = true;
if (timer) clearTimeout(timer);
$('#btn_pause').addClass('active');
};
Tracer.prototype.resumeStep = function () {
this.pause = false;
this.step(this.traceIndex + 1);
$('#btn_pause').removeClass('active');
};
Tracer.prototype.step = function (i, options) {
if (isNaN(i) || i >= this.traces.length || i < 0) return;
options = options || {};
this.traceIndex = i;
var trace = this.traces[i];
var tracer = this;
var sleepDuration = 0;
trace.forEach(function (step) {
switch (step.type) {
case 'sleep':
sleepDuration = step.duration;
break;
case 'print':
printTrace(step.msg);
break;
case 'pace':
tracer.traceOptions.interval = step.interval;
break;
case 'clear':
tracer.clear();
printTrace('clear traces');
break;
default:
tracer.module.prototype.processStep.call(tracer, step, options);
}
});
if (!options.virtual) {
this.refresh();
scrollToEnd(Math.min(50, this.traceOptions.interval));
}
if (this.pause) return;
timer = setTimeout(function () {
tracer.step(i + 1, options);
}, sleepDuration || this.traceOptions.interval);
};
Tracer.prototype.refresh = function () {
};
Tracer.prototype.prevStep = function () {
this.step(this.traceIndex - 1);
};
Tracer.prototype.nextStep = function () {
this.step(this.traceIndex + 1);
};
Tracer.prototype.mousedown = function (e) {
};
Tracer.prototype.mousemove = function (e) {
};
Tracer.prototype.mouseup = function (e) {
};
Tracer.prototype.mousewheel = function (e) {
};
var printTrace = function (message) {
$('#tab_trace .wrapper').append($('<span>').append(message + '<br/>'));
};
var scrollToEnd = function (duration) {
$('#tab_trace').animate({scrollTop: $('#tab_trace')[0].scrollHeight}, duration);
};