Skip to content

Commit a9b0177

Browse files
committed
put all functions of tracer into a object (to prevent duplication)
1 parent 30daf17 commit a9b0177

File tree

4 files changed

+404
-422
lines changed

4 files changed

+404
-422
lines changed

js/module/array1d.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ function Array1DTracer(module) {
55
Array1DTracer.prototype = Object.create(Array2DTracer.prototype);
66
Array1DTracer.prototype.constructor = Array1DTracer;
77

8-
var Array1D = {
9-
random: function(N, min, max) {
10-
return Array2D.random(1, N, min, max)[0];
11-
},
12-
randomSorted: function(N, min, max) {
13-
return Array2D.randomSorted(1, N, min, max)[0];
14-
}
15-
};
16-
178
// Override
189
Array1DTracer.prototype._setData = function(D) {
1910
return Array2DTracer.prototype._setData.call(this, [D]);
@@ -68,4 +59,13 @@ Array1DTracer.prototype._deselectSet = function(indexes) {
6859
});
6960
});
7061
Array2DTracer.prototype._deselectSet.call(this, coords);
62+
};
63+
64+
var Array1D = {
65+
random: function(N, min, max) {
66+
return Array2D.random(1, N, min, max)[0];
67+
},
68+
randomSorted: function(N, min, max) {
69+
return Array2D.randomSorted(1, N, min, max)[0];
70+
}
7171
};

js/module/array2d.js

+70-75
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var $table = null;
22

33
function Array2DTracer(module) {
44
if (Tracer.call(this, module || Array2DTracer)) {
5-
initTable();
5+
Array2D.initTable();
66
return true;
77
}
88
return false;
@@ -12,55 +12,30 @@ Array2DTracer.prototype = Object.create(Tracer.prototype);
1212
Array2DTracer.prototype.constructor = Array2DTracer;
1313

1414
// Override
15-
Array2DTracer.prototype.resize = function() {
15+
Array2DTracer.prototype.resize = function () {
1616
Tracer.prototype.resize.call(this);
1717

1818
this.refresh();
1919
};
2020

2121
// Override
22-
Array2DTracer.prototype.clear = function() {
22+
Array2DTracer.prototype.clear = function () {
2323
Tracer.prototype.clear.call(this);
2424

25-
clearTableColor();
26-
};
27-
28-
var Array2D = {
29-
random: function(N, M, min, max) {
30-
if (!N) N = 10;
31-
if (!M) M = 10;
32-
if (min === undefined) min = 1;
33-
if (max === undefined) max = 9;
34-
var D = [];
35-
for (var i = 0; i < N; i++) {
36-
D.push([]);
37-
for (var j = 0; j < M; j++) {
38-
D[i].push((Math.random() * (max - min + 1) | 0) + min);
39-
}
40-
}
41-
return D;
42-
},
43-
44-
randomSorted: function(N, M, min, max) {
45-
return this.random(N, M, min, max).map(function(arr) {
46-
return arr.sort(function(a, b) {
47-
return a - b;
48-
});
49-
});
50-
}
25+
Array2D.clearTableColor();
5126
};
5227

5328
// Override
54-
Array2DTracer.prototype._setData = function(D) {
29+
Array2DTracer.prototype._setData = function (D) {
5530
this.D = D;
5631
this.viewX = this.viewY = 0;
5732
this.paddingH = 6;
5833
this.paddingV = 3;
5934
this.fontSize = 16;
6035

6136
if (Tracer.prototype._setData.call(this, arguments)) {
62-
$('.mtbl-row').each(function(i) {
63-
$(this).children().each(function(j) {
37+
$('.mtbl-row').each(function (i) {
38+
$(this).children().each(function (j) {
6439
$(this).text(D[i][j]);
6540
});
6641
});
@@ -83,7 +58,7 @@ Array2DTracer.prototype._setData = function(D) {
8358
return false;
8459
};
8560

86-
Array2DTracer.prototype._notify = function(x1, y1, x2, y2) {
61+
Array2DTracer.prototype._notify = function (x1, y1, x2, y2) {
8762
var second = x2 !== undefined && y2 !== undefined;
8863
this.pushStep({
8964
type: 'notifying',
@@ -109,39 +84,39 @@ Array2DTracer.prototype._notify = function(x1, y1, x2, y2) {
10984
}, false);
11085
};
11186

112-
Array2DTracer.prototype._select = function(sx, sy, ex, ey) {
87+
Array2DTracer.prototype._select = function (sx, sy, ex, ey) {
11388
this.pushSelectingStep('select', null, arguments);
11489
};
11590

116-
Array2DTracer.prototype._selectRow = function(x, sy, ey) {
91+
Array2DTracer.prototype._selectRow = function (x, sy, ey) {
11792
this.pushSelectingStep('select', 'row', arguments);
11893
};
11994

120-
Array2DTracer.prototype._selectCol = function(y, sx, ex) {
95+
Array2DTracer.prototype._selectCol = function (y, sx, ex) {
12196
this.pushSelectingStep('select', 'col', arguments);
12297
};
12398

124-
Array2DTracer.prototype._selectSet = function(coords) {
99+
Array2DTracer.prototype._selectSet = function (coords) {
125100
this.pushSelectingStep('select', 'set', arguments);
126101
};
127102

128-
Array2DTracer.prototype._deselect = function(sx, sy, ex, ey) {
103+
Array2DTracer.prototype._deselect = function (sx, sy, ex, ey) {
129104
this.pushSelectingStep('deselect', null, arguments);
130105
};
131106

132-
Array2DTracer.prototype._deselectRow = function(x, sy, ey) {
107+
Array2DTracer.prototype._deselectRow = function (x, sy, ey) {
133108
this.pushSelectingStep('deselect', 'row', arguments);
134109
};
135110

136-
Array2DTracer.prototype._deselectCol = function(y, sx, ex) {
111+
Array2DTracer.prototype._deselectCol = function (y, sx, ex) {
137112
this.pushSelectingStep('deselect', 'col', arguments);
138113
};
139114

140-
Array2DTracer.prototype._deselectSet = function(coords) {
115+
Array2DTracer.prototype._deselectSet = function (coords) {
141116
this.pushSelectingStep('deselect', 'set', arguments);
142117
};
143118

144-
Array2DTracer.prototype.pushSelectingStep = function() {
119+
Array2DTracer.prototype.pushSelectingStep = function () {
145120
var args = Array.prototype.slice.call(arguments);
146121
var type = args.shift();
147122
var mode = args.shift();
@@ -189,21 +164,21 @@ Array2DTracer.prototype.pushSelectingStep = function() {
189164
this.pushStep(step, type == 'select');
190165
};
191166

192-
Array2DTracer.prototype.processStep = function(step, options) {
167+
Array2DTracer.prototype.processStep = function (step, options) {
193168
switch (step.type) {
194169
case 'notifying':
195170
var $row = $table.find('.mtbl-row').eq(step.x);
196171
$row.find('.mtbl-cell').eq(step.y).text(step.value);
197172
case 'notified':
198173
case 'select':
199174
case 'deselect':
200-
var colorClass = step.type == 'select' || step.type == 'deselect' ? tableColorClass.selected : tableColorClass.notifying;
175+
var colorClass = step.type == 'select' || step.type == 'deselect' ? Array2D.tableColorClass.selected : Array2D.tableColorClass.notifying;
201176
var addClass = step.type == 'select' || step.type == 'notifying';
202177
if (step.coords) {
203-
step.coords.forEach(function(coord) {
178+
step.coords.forEach(function (coord) {
204179
var x = coord.x;
205180
var y = coord.y;
206-
paintColor(x, y, x, y, colorClass, addClass);
181+
Array2D.paintColor(x, y, x, y, colorClass, addClass);
207182
});
208183
} else {
209184
var sx = step.sx;
@@ -214,21 +189,21 @@ Array2DTracer.prototype.processStep = function(step, options) {
214189
if (sy === undefined) sy = step.y;
215190
if (ex === undefined) ex = step.x;
216191
if (ey === undefined) ey = step.y;
217-
paintColor(sx, sy, ex, ey, colorClass, addClass);
192+
Array2D.paintColor(sx, sy, ex, ey, colorClass, addClass);
218193
}
219194
break;
220195
}
221196
};
222197

223-
Array2DTracer.prototype.getCellCss = function() {
198+
Array2DTracer.prototype.getCellCss = function () {
224199
return {
225200
padding: this.paddingV.toFixed(1) + 'px ' + this.paddingH.toFixed(1) + 'px',
226201
'font-size': this.fontSize.toFixed(1) + 'px'
227202
};
228203
};
229204

230205
// Override
231-
Array2DTracer.prototype.refresh = function() {
206+
Array2DTracer.prototype.refresh = function () {
232207
Tracer.prototype.refresh.call(this);
233208

234209
var $parent = $table.parent();
@@ -239,7 +214,7 @@ Array2DTracer.prototype.refresh = function() {
239214
};
240215

241216
// Override
242-
Array2DTracer.prototype.prevStep = function() {
217+
Array2DTracer.prototype.prevStep = function () {
243218
this.clear();
244219
$('#tab_trace .wrapper').empty();
245220
var finalIndex = this.traceIndex - 1;
@@ -256,7 +231,7 @@ Array2DTracer.prototype.prevStep = function() {
256231
};
257232

258233
// Override
259-
Array2DTracer.prototype.mousedown = function(e) {
234+
Array2DTracer.prototype.mousedown = function (e) {
260235
Tracer.prototype.mousedown.call(this, e);
261236

262237
this.dragX = e.pageX;
@@ -265,7 +240,7 @@ Array2DTracer.prototype.mousedown = function(e) {
265240
};
266241

267242
// Override
268-
Array2DTracer.prototype.mousemove = function(e) {
243+
Array2DTracer.prototype.mousemove = function (e) {
269244
Tracer.prototype.mousemove.call(this, e);
270245

271246
if (this.dragging) {
@@ -278,14 +253,14 @@ Array2DTracer.prototype.mousemove = function(e) {
278253
};
279254

280255
// Override
281-
Array2DTracer.prototype.mouseup = function(e) {
256+
Array2DTracer.prototype.mouseup = function (e) {
282257
Tracer.prototype.mouseup.call(this, e);
283258

284259
this.dragging = false;
285260
};
286261

287262
// Override
288-
Array2DTracer.prototype.mousewheel = function(e) {
263+
Array2DTracer.prototype.mousewheel = function (e) {
289264
Tracer.prototype.mousewheel.call(this, e);
290265

291266
e.preventDefault();
@@ -303,27 +278,47 @@ Array2DTracer.prototype.mousewheel = function(e) {
303278
this.refresh();
304279
};
305280

306-
var initTable = function() {
307-
$table = $('<div class="mtbl-table">');
308-
$module_container.append($table);
309-
};
310-
311-
var paintColor = function(sx, sy, ex, ey, colorClass, addClass) {
312-
for (var i = sx; i <= ex; i++) {
313-
var $row = $table.find('.mtbl-row').eq(i);
314-
for (var j = sy; j <= ey; j++) {
315-
var $cell = $row.find('.mtbl-cell').eq(j);
316-
if (addClass) $cell.addClass(colorClass);
317-
else $cell.removeClass(colorClass);
281+
var Array2D = {
282+
random: function (N, M, min, max) {
283+
if (!N) N = 10;
284+
if (!M) M = 10;
285+
if (min === undefined) min = 1;
286+
if (max === undefined) max = 9;
287+
var D = [];
288+
for (var i = 0; i < N; i++) {
289+
D.push([]);
290+
for (var j = 0; j < M; j++) {
291+
D[i].push((Math.random() * (max - min + 1) | 0) + min);
292+
}
293+
}
294+
return D;
295+
},
296+
randomSorted: function (N, M, min, max) {
297+
return this.random(N, M, min, max).map(function (arr) {
298+
return arr.sort(function (a, b) {
299+
return a - b;
300+
});
301+
});
302+
},
303+
initTable: function () {
304+
$table = $('<div class="mtbl-table">');
305+
$module_container.append($table);
306+
},
307+
paintColor: function (sx, sy, ex, ey, colorClass, addClass) {
308+
for (var i = sx; i <= ex; i++) {
309+
var $row = $table.find('.mtbl-row').eq(i);
310+
for (var j = sy; j <= ey; j++) {
311+
var $cell = $row.find('.mtbl-cell').eq(j);
312+
if (addClass) $cell.addClass(colorClass);
313+
else $cell.removeClass(colorClass);
314+
}
318315
}
316+
},
317+
clearTableColor: function () {
318+
$table.find('.mtbl-cell').removeClass(Object.keys(Array2D.tableColorClass).join(' '));
319+
},
320+
tableColorClass: {
321+
selected: 'selected',
322+
notifying: 'notifying'
319323
}
320-
};
321-
322-
var clearTableColor = function() {
323-
$table.find('.mtbl-cell').removeClass(Object.keys(tableColorClass).join(' '));
324-
};
325-
326-
var tableColorClass = {
327-
selected: 'selected',
328-
notifying: 'notifying'
329324
};

0 commit comments

Comments
 (0)