Skip to content

Commit

Permalink
Use the module pattern in display.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fhd committed Apr 17, 2012
1 parent 8739ed7 commit cf2b318
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions public/js/sorting/display.js
@@ -1,59 +1,60 @@
var array = [], var sorting = {};
sorting = {
init: function(stack) {
if (typeof currentAlgorithmFile != "undefined") {
prettyPrint(); // Prettify


(function(sorting) {
var array = [], i;

function draw(context, width, height) {
context.clearRect(0, 0, width, height);
context.fillStyle = "rgb(0, 0, 0)";
context.font = "12px sans";
context.textBaseline = "top";
$.each(array, function(index, value) {
var barWidth = 5;
context.fillRect(index * barWidth * 2 + barWidth,
((array.length + 1) - value) * barWidth,
barWidth, barWidth * value);
});
};

for (i = 0; i < 30; i++)
array[i] = i + 1;

Array.prototype.shuffle = function() {
var s = [];
while (this.length)
s.push(this.splice(Math.random() * this.length, 1));
while (s.length)
this.push(s.pop());
return this;
};

sorting.init = function(stack) {
if (typeof currentAlgorithmFile != "undefined") {
prettyPrint(); // Prettify

array.shuffle();
$("#shuffle").click(function() {
array.shuffle(); array.shuffle();
$("#shuffle").click(function() { });
array.shuffle();
});


$("#sort").click(function() { $("#sort").click(function() {
var controls = $("#algorithms, #shuffle, #sort"), var controls = $("#algorithms, #shuffle, #sort"),
worker = new Worker("js/sorting/worker.js"); worker = new Worker("js/sorting/worker.js");
controls.attr("disabled", "true"); controls.attr("disabled", "true");
worker.onmessage = function(event) { worker.onmessage = function(event) {
var data = event.data; var data = event.data;
array = data.array; array = data.array;
if (data.finished) if (data.finished)
controls.removeAttr("disabled"); controls.removeAttr("disabled");
}; };
worker.postMessage({ worker.postMessage({
file: currentAlgorithmFile, file: currentAlgorithmFile,
functionName: currentAlgorithmFunction, functionName: currentAlgorithmFunction,
"array": array "array": array
});
}); });
});


setInterval(utils.createDrawFunction($("#canvas")[0], draw), setInterval(utils.createDrawFunction($("#canvas")[0], draw), 10);
10);
}
} }
}, };
i; })(sorting);

function draw(context, width, height) {
context.clearRect(0, 0, width, height);
context.fillStyle = "rgb(0, 0, 0)";
context.font = "12px sans";
context.textBaseline = "top";
$.each(array, function(index, value) {
var barWidth = 5;
context.fillRect(index * barWidth * 2 + barWidth,
((array.length + 1) - value) * barWidth,
barWidth, barWidth * value);
});
};

for (i = 0; i < 30; i++)
array[i] = i + 1;

Array.prototype.shuffle = function() {
var s = [];
while (this.length)
s.push(this.splice(Math.random() * this.length, 1));
while (s.length)
this.push(s.pop());
return this;
};

0 comments on commit cf2b318

Please sign in to comment.