Don't use typeof for Node and Edge 'data' field.

`data` field is always set for Node and Edge objects,
no need to check for existence - value check is enough
for `data` fields.
This commit is contained in:
anatoly techtonik 2013-02-05 22:11:12 +03:00
parent 1150320e1c
commit 6b52b72c6b
2 changed files with 8 additions and 8 deletions

View File

@ -237,7 +237,7 @@ Layout.ForceDirected = function(graph, stiffness, repulsion, damping) {
Layout.ForceDirected.prototype.point = function(node) { Layout.ForceDirected.prototype.point = function(node) {
if (typeof(this.nodePoints[node.id]) === 'undefined') { if (typeof(this.nodePoints[node.id]) === 'undefined') {
var mass = typeof(node.data.mass) !== 'undefined' ? node.data.mass : 1.0; 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);
} }
@ -246,7 +246,7 @@ Layout.ForceDirected.prototype.point = function(node) {
Layout.ForceDirected.prototype.spring = function(edge) { Layout.ForceDirected.prototype.spring = function(edge) {
if (typeof(this.edgeSprings[edge.id]) === 'undefined') { if (typeof(this.edgeSprings[edge.id]) === 'undefined') {
var length = typeof(edge.data.length) !== 'undefined' ? edge.data.length : 1.0; var length = (edge.data.length !== undefined) ? edge.data.length : 1.0;
var existingSpring = false; var existingSpring = false;

View File

@ -112,7 +112,7 @@ jQuery.fn.springy = function(params) {
}); });
Node.prototype.getWidth = function() { Node.prototype.getWidth = function() {
var text = typeof(this.data.label) !== 'undefined' ? this.data.label : this.id; var text = (this.data.label !== undefined) ? this.data.label : this.id;
if (this._width && this._width[text]) if (this._width && this._width[text])
return this._width[text]; return this._width[text];
@ -174,18 +174,18 @@ jQuery.fn.springy = function(params) {
intersection = s2; intersection = s2;
} }
var stroke = typeof(edge.data.color) !== 'undefined' ? edge.data.color : '#000000'; var stroke = (edge.data.color !== undefined) ? edge.data.color : '#000000';
var arrowWidth; var arrowWidth;
var arrowLength; var arrowLength;
var weight = typeof(edge.data.weight) !== 'undefined' ? edge.data.weight : 1.0; var weight = (edge.data.weight !== undefined) ? edge.data.weight : 1.0;
ctx.lineWidth = Math.max(weight * 2, 0.1); ctx.lineWidth = Math.max(weight * 2, 0.1);
arrowWidth = 1 + ctx.lineWidth; arrowWidth = 1 + ctx.lineWidth;
arrowLength = 8; arrowLength = 8;
var directional = typeof(edge.data.directional) !== 'undefined' ? edge.data.directional : true; var directional = (edge.data.directional !== undefined) ? edge.data.directional : true;
// line // line
var lineEnd; var lineEnd;
@ -218,7 +218,7 @@ jQuery.fn.springy = function(params) {
} }
// label // label
if (typeof(edge.data.label) !== 'undefined') { if (edge.data.label !== undefined) {
text = edge.data.label text = edge.data.label
ctx.save(); ctx.save();
ctx.textAlign = "center"; ctx.textAlign = "center";
@ -256,7 +256,7 @@ jQuery.fn.springy = function(params) {
ctx.font = "16px Verdana, sans-serif"; ctx.font = "16px Verdana, sans-serif";
ctx.fillStyle = "#000000"; ctx.fillStyle = "#000000";
ctx.font = "16px Verdana, sans-serif"; ctx.font = "16px Verdana, sans-serif";
var text = typeof(node.data.label) !== 'undefined' ? node.data.label : node.id; var text = (node.data.label !== undefined) ? node.data.label : node.id;
ctx.fillText(text, s.x - boxWidth/2 + 5, s.y - 8); ctx.fillText(text, s.x - boxWidth/2 + 5, s.y - 8);
ctx.restore(); ctx.restore();