Compare commits

...

1 Commits

Author SHA1 Message Date
Dennis Hotson
7ee84bf6e0 Fixed a few issues reported by jshint 2013-03-12 10:43:45 +00:00
2 changed files with 146 additions and 133 deletions

View File

@ -25,8 +25,10 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*jshint globalstrict:true, browser:true */
// Enable strict mode for EC5 compatible browsers
"use strict";
'use strict';
var Graph = function() {
this.nodeSet = {};
@ -51,7 +53,6 @@ var Node = function(id, data) {
var Edge = function(id, source, target, data) {
this.id = id;
/** @type {Node} */
this.source = source;
this.target = target;
this.data = (data !== undefined) ? data : {};
@ -118,12 +119,12 @@ Graph.prototype.addEdges = function() {
for (var i = 0; i < arguments.length; i++) {
var e = arguments[i];
var node1 = this.nodeSet[e[0]];
if (node1 == undefined) {
throw new TypeError("invalid node name: " + e[0]);
if (node1 === undefined) {
throw new TypeError('invalid node name: ' + e[0]);
}
var node2 = this.nodeSet[e[1]];
if (node2 == undefined) {
throw new TypeError("invalid node name: " + e[1]);
if (node2 === undefined) {
throw new TypeError('invalid node name: ' + e[1]);
}
var attr = e[2];
@ -145,8 +146,7 @@ Graph.prototype.newEdge = function(source, target, data) {
// find the edges from node1 to node2
Graph.prototype.getEdges = function(node1, node2) {
if (node1.id in this.adjacency
&& node2.id in this.adjacency[node1.id]) {
if (node1.id in this.adjacency && node2.id in this.adjacency[node1.id]) {
return this.adjacency[node1.id][node2.id];
}
@ -225,11 +225,11 @@ Graph.prototype.merge = function(data) {
var from = nodes[e.from];
var to = nodes[e.to];
var id = (e.directed)
? (id = e.type + "-" + from.id + "-" + to.id)
: (from.id < to.id) // normalise id for non-directed edges
? e.type + "-" + from.id + "-" + to.id
: e.type + "-" + to.id + "-" + from.id;
var id = (e.directed) ?
(id = e.type + '-' + from.id + '-' + to.id) :
(from.id < to.id) ? // normalise id for non-directed edges
(e.type + '-' + from.id + '-' + to.id) :
(e.type + '-' + to.id + '-' + from.id);
var edge = this.addEdge(new Edge(id, from, to, e.data));
edge.data.type = e.type;
@ -280,7 +280,8 @@ Layout.ForceDirected = function(graph, stiffness, repulsion, damping) {
Layout.ForceDirected.prototype.point = function(node) {
if (!(node.id in this.nodePoints)) {
var mass = (node.data.mass !== undefined) ? node.data.mass : 1.0;
this.nodePoints[node.id] = new Layout.ForceDirected.Point(Vector.random(), mass);
this.nodePoints[node.id] =
new Layout.ForceDirected.Point(Vector.random(), mass);
}
return this.nodePoints[node.id];
@ -300,7 +301,9 @@ Layout.ForceDirected.prototype.spring = function(edge) {
}, this);
if (existingSpring !== false) {
return new Layout.ForceDirected.Spring(existingSpring.point1, existingSpring.point2, 0.0, 0.0);
return new Layout.ForceDirected.Spring(
existingSpring.point1,
existingSpring.point2, 0.0, 0.0);
}
var to = this.graph.getEdges(edge.target, edge.source);
@ -311,7 +314,9 @@ Layout.ForceDirected.prototype.spring = function(edge) {
}, this);
if (existingSpring !== false) {
return new Layout.ForceDirected.Spring(existingSpring.point2, existingSpring.point1, 0.0, 0.0);
return new Layout.ForceDirected.Spring(
existingSpring.point2,
existingSpring.point1, 0.0, 0.0);
}
this.edgeSprings[edge.id] = new Layout.ForceDirected.Spring(
@ -354,12 +359,20 @@ Layout.ForceDirected.prototype.applyCoulombsLaw = function() {
if (point1 !== point2)
{
var d = point1.p.subtract(point2.p);
var distance = d.magnitude() + 0.1; // avoid massive forces at small distances (and divide by zero)
// avoid massive forces at small distances (and divide by zero)
var distance = d.magnitude() + 0.1;
var direction = d.normalise();
// apply force to each end point
point1.applyForce(direction.multiply(this.repulsion).divide(distance * distance * 0.5));
point2.applyForce(direction.multiply(this.repulsion).divide(distance * distance * -0.5));
point1.applyForce(
direction
.multiply(this.repulsion)
.divide(distance * distance * 0.5));
point2.applyForce(
direction
.multiply(this.repulsion)
.divide(distance * distance * -0.5));
}
});
});
@ -367,13 +380,18 @@ Layout.ForceDirected.prototype.applyCoulombsLaw = function() {
Layout.ForceDirected.prototype.applyHookesLaw = function() {
this.eachSpring(function(spring) {
var d = spring.point2.p.subtract(spring.point1.p); // the direction of the spring
var displacement = spring.length - d.magnitude();
var direction = d.normalise();
var dir = spring.point2.p.subtract(spring.point1.p);
var displacement = spring.length - dir.magnitude();
var direction = dir.normalise();
// apply force to each end point
spring.point1.applyForce(direction.multiply(spring.k * displacement * -0.5));
spring.point2.applyForce(direction.multiply(spring.k * displacement * 0.5));
spring.point1.applyForce(
direction
.multiply(spring.k * displacement * -0.5));
spring.point2.applyForce(
direction
.multiply(spring.k * displacement * 0.5));
});
};
@ -387,8 +405,6 @@ Layout.ForceDirected.prototype.attractToCentre = function() {
Layout.ForceDirected.prototype.updateVelocity = function(timestep) {
this.eachNode(function(node, point) {
// Is this, along with updatePosition below, the only places that your
// integration code exist?
point.v = point.v.add(point.a.multiply(timestep)).multiply(this.damping);
point.a = new Vector(0, 0);
});
@ -396,8 +412,6 @@ Layout.ForceDirected.prototype.updateVelocity = function(timestep) {
Layout.ForceDirected.prototype.updatePosition = function(timestep) {
this.eachNode(function(node, point) {
// Same question as above; along with updateVelocity, is this all of
// your integration code?
point.p = point.p.add(point.v.multiply(timestep));
});
};
@ -413,16 +427,19 @@ Layout.ForceDirected.prototype.totalEnergy = function(timestep) {
return energy;
};
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; // stolen from coffeescript, thanks jashkenas! ;-)
// stolen from coffeescript, thanks jashkenas! ;-)
var __bind = function(fn, me) { return function() {
return fn.apply(me, arguments);
}; };
Layout.requestAnimationFrame = __bind(window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element) {
(function(callback, element) {
window.setTimeout(callback, 10);
}, window);
}), window);
// start simulation
@ -491,7 +508,10 @@ Layout.ForceDirected.prototype.getBoundingBox = function() {
var padding = topright.subtract(bottomleft).multiply(0.07); // ~5% padding
return {bottomleft: bottomleft.subtract(padding), topright: topright.add(padding)};
return {
bottomleft: bottomleft.subtract(padding),
topright: topright.add(padding)
};
};
@ -518,7 +538,9 @@ Vector.prototype.multiply = function(n) {
};
Vector.prototype.divide = function(n) {
return new Vector((this.x / n) || 0, (this.y / n) || 0); // Avoid divide by zero errors..
return new Vector(
(this.x / n) || 0,
(this.y / n) || 0); // Avoid divide by zero errors..
};
Vector.prototype.magnitude = function() {
@ -553,15 +575,6 @@ Layout.ForceDirected.Spring = function(point1, point2, length, k) {
this.k = k; // spring constant (See Hooke's law) .. how stiff the spring is
};
// Layout.ForceDirected.Spring.prototype.distanceToPoint = function(point)
// {
// // hardcore vector arithmetic.. ohh yeah!
// // .. see http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment/865080#865080
// var n = this.point2.p.subtract(this.point1.p).normalise().normal();
// var ac = point.p.subtract(this.point1.p);
// return Math.abs(ac.x * n.x + ac.y * n.y);
// };
// Renderer handles the layout rendering loop
function Renderer(layout, clear, drawEdge, drawNode) {
this.layout = layout;
@ -596,13 +609,13 @@ Renderer.prototype.start = function() {
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if ( this == null ) {
throw new TypeError( " this is null or not defined" );
if (this === null) {
throw new TypeError(' this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0; // Hack to convert O.length to a UInt32
if ( {}.toString.call(callback) != "[object Function]" ) {
throw new TypeError( callback + " is not a function" );
if ({}.toString.call(callback) != '[object Function]') {
throw new TypeError(callback + ' is not a function');
}
if (thisArg) {
T = thisArg;

View File

@ -34,7 +34,7 @@ jQuery.fn.springy = function(params) {
var nodeSelected = params.nodeSelected || null;
var canvas = this[0];
var ctx = canvas.getContext("2d");
var ctx = canvas.getContext('2d');
var layout = this.layout = new Layout.ForceDirected(graph, stiffness, repulsion, damping);
@ -117,7 +117,7 @@ jQuery.fn.springy = function(params) {
return this._width[text];
ctx.save();
ctx.font = "16px Verdana, sans-serif";
ctx.font = '16px Verdana, sans-serif';
var width = ctx.measureText(text).width + 10;
ctx.restore();
@ -219,12 +219,12 @@ jQuery.fn.springy = function(params) {
// label
if (edge.data.label !== undefined) {
text = edge.data.label
text = edge.data.label;
ctx.save();
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.font = "10px Helvetica, sans-serif";
ctx.fillStyle = "#5BA6EC";
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.font = '10px Helvetica, sans-serif';
ctx.fillStyle = '#5BA6EC';
ctx.fillText(text, (x1 + x2) / 2, (y1 + y2) / 2);
ctx.restore();
}
@ -243,19 +243,19 @@ jQuery.fn.springy = function(params) {
// fill background
if (selected !== null && nearest.node !== null && selected.node.id === node.id) {
ctx.fillStyle = "#FFFFE0";
ctx.fillStyle = '#FFFFE0';
} else if (nearest !== null && nearest.node !== null && nearest.node.id === node.id) {
ctx.fillStyle = "#EEEEEE";
ctx.fillStyle = '#EEEEEE';
} else {
ctx.fillStyle = "#FFFFFF";
ctx.fillStyle = '#FFFFFF';
}
ctx.fillRect(s.x - boxWidth / 2, s.y - 10, boxWidth, 20);
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.font = "16px Verdana, sans-serif";
ctx.fillStyle = "#000000";
ctx.font = "16px Verdana, sans-serif";
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.font = '16px Verdana, sans-serif';
ctx.fillStyle = '#000000';
ctx.font = '16px Verdana, sans-serif';
var text = (node.data.label !== undefined) ? node.data.label : node.id;
ctx.fillText(text, s.x - boxWidth / 2 + 5, s.y - 8);
@ -300,6 +300,6 @@ jQuery.fn.springy = function(params) {
}
return this;
}
};
})();