Minor performance improvement...

This commit is contained in:
Paulo Gustavo Veiga 2012-07-07 13:18:27 -03:00
parent f3da916965
commit 80940b8529
7 changed files with 83 additions and 81 deletions

View File

@ -139,9 +139,7 @@ mindplot.ConnectionLine = new Class({
else { else {
x = -mindplot.Topic.CONNECTOR_WIDTH; x = -mindplot.Topic.CONNECTOR_WIDTH;
} }
console.log("conector:" + x + ", " + y);
connector.setPosition(x, y); connector.setPosition(x, y);
} }
}, },

View File

@ -682,7 +682,7 @@ mindplot.Designer = new Class({
}); });
var relIds = topics.map(function (rel) { var relIds = relation.map(function (rel) {
return rel.getId(); return rel.getId();
}); });

View File

@ -16,7 +16,7 @@
* limitations under the License. * limitations under the License.
*/ */
mindplot.layout.Node = new Class({ mindplot.layout.Node = new Class({
initialize:function(id, size, position, sorter) { initialize:function (id, size, position, sorter) {
$assert(typeof id === 'number' && isFinite(id), "id can not be null"); $assert(typeof id === 'number' && isFinite(id), "id can not be null");
$assert(size, "size can not be null"); $assert(size, "size can not be null");
$assert(position, "position can not be null"); $assert(position, "position can not be null");
@ -31,90 +31,90 @@ mindplot.layout.Node = new Class({
this.setShrunken(false); this.setShrunken(false);
}, },
getId:function() { getId:function () {
return this._id; return this._id;
}, },
setFree: function(value) { setFree:function (value) {
this._setProperty('free', value); this._setProperty('free', value);
}, },
isFree: function() { isFree:function () {
return this._getProperty('free'); return this._getProperty('free');
}, },
hasFreeChanged: function() { hasFreeChanged:function () {
return this._isPropertyChanged('free'); return this._isPropertyChanged('free');
}, },
hasFreeDisplacementChanged: function() { hasFreeDisplacementChanged:function () {
return this._isPropertyChanged('freeDisplacement'); return this._isPropertyChanged('freeDisplacement');
}, },
setShrunken: function(value) { setShrunken:function (value) {
this._setProperty('shrink', value); this._setProperty('shrink', value);
}, },
areChildrenShrunken: function() { areChildrenShrunken:function () {
return this._getProperty('shrink'); return this._getProperty('shrink');
}, },
setOrder: function(order) { setOrder:function (order) {
$assert(typeof order === 'number' && isFinite(order), "Order can not be null. Value:" + order); $assert(typeof order === 'number' && isFinite(order), "Order can not be null. Value:" + order);
this._setProperty('order', order); this._setProperty('order', order);
}, },
resetPositionState : function() { resetPositionState:function () {
var prop = this._properties['position']; var prop = this._properties['position'];
if (prop) { if (prop) {
prop.hasChanged = false; prop.hasChanged = false;
} }
}, },
resetOrderState : function() { resetOrderState:function () {
var prop = this._properties['order']; var prop = this._properties['order'];
if (prop) { if (prop) {
prop.hasChanged = false; prop.hasChanged = false;
} }
}, },
resetFreeState : function() { resetFreeState:function () {
var prop = this._properties['freeDisplacement']; var prop = this._properties['freeDisplacement'];
if (prop) { if (prop) {
prop.hasChanged = false; prop.hasChanged = false;
} }
}, },
getOrder: function() { getOrder:function () {
return this._getProperty('order'); return this._getProperty('order');
}, },
hasOrderChanged: function() { hasOrderChanged:function () {
return this._isPropertyChanged('order'); return this._isPropertyChanged('order');
}, },
hasPositionChanged: function() { hasPositionChanged:function () {
return this._isPropertyChanged('position'); return this._isPropertyChanged('position');
}, },
hasSizeChanged: function() { hasSizeChanged:function () {
return this._isPropertyChanged('size'); return this._isPropertyChanged('size');
}, },
getPosition: function() { getPosition:function () {
return this._getProperty('position'); return this._getProperty('position');
}, },
setSize : function(size) { setSize:function (size) {
$assert($defined(size), "Size can not be null"); $assert($defined(size), "Size can not be null");
this._setProperty('size', Object.clone(size)); this._setProperty('size', Object.clone(size));
}, },
getSize: function() { getSize:function () {
return this._getProperty('size'); return this._getProperty('size');
}, },
setFreeDisplacement: function(displacement) { setFreeDisplacement:function (displacement) {
$assert($defined(displacement), "Position can not be null"); $assert($defined(displacement), "Position can not be null");
$assert($defined(displacement.x), "x can not be null"); $assert($defined(displacement.x), "x can not be null");
$assert($defined(displacement.y), "y can not be null"); $assert($defined(displacement.y), "y can not be null");
@ -124,30 +124,33 @@ mindplot.layout.Node = new Class({
this._setProperty('freeDisplacement', Object.clone(newDisplacement)); this._setProperty('freeDisplacement', Object.clone(newDisplacement));
}, },
resetFreeDisplacement: function() { resetFreeDisplacement:function () {
this._setProperty('freeDisplacement', {x:0, y:0}); this._setProperty('freeDisplacement', {x:0, y:0});
}, },
getFreeDisplacement: function() { getFreeDisplacement:function () {
var freeDisplacement = this._getProperty('freeDisplacement'); var freeDisplacement = this._getProperty('freeDisplacement');
return (freeDisplacement || {x:0, y:0}); return (freeDisplacement || {x:0, y:0});
}, },
setPosition : function(position) { setPosition:function (position) {
$assert($defined(position), "Position can not be null"); $assert($defined(position), "Position can not be null");
$assert($defined(position.x), "x can not be null"); $assert($defined(position.x), "x can not be null");
$assert($defined(position.y), "y can not be null"); $assert($defined(position.y), "y can not be null");
this._setProperty('position', Object.clone(position)); // This is a performance improvement to avoid movements that really could be avoided.
var currentPos = this.getPosition();
if (currentPos == null || Math.abs(currentPos.x - position.x) > 2 || Math.abs(currentPos.y - position.y) > 2)
this._setProperty('position', position);
}, },
_setProperty: function(key, value) { _setProperty:function (key, value) {
var prop = this._properties[key]; var prop = this._properties[key];
if (!prop) { if (!prop) {
prop = { prop = {
hasChanged:false, hasChanged:false,
value: null, value:null,
oldValue : null oldValue:null
}; };
} }
@ -160,21 +163,21 @@ mindplot.layout.Node = new Class({
this._properties[key] = prop; this._properties[key] = prop;
}, },
_getProperty: function(key) { _getProperty:function (key) {
var prop = this._properties[key]; var prop = this._properties[key];
return $defined(prop) ? prop.value : null; return $defined(prop) ? prop.value : null;
}, },
_isPropertyChanged: function(key) { _isPropertyChanged:function (key) {
var prop = this._properties[key]; var prop = this._properties[key];
return prop ? prop.hasChanged : false; return prop ? prop.hasChanged : false;
}, },
getSorter: function() { getSorter:function () {
return this._sorter; return this._sorter;
}, },
toString: function() { toString:function () {
return "[id:" + this.getId() + ", order:" + this.getOrder() + ", position: {" + this.getPosition().x + "," + this.getPosition().y + "}, size: {" + this.getSize().width + "," + this.getSize().height + "}, shrink:" + this.areChildrenShrunken() + "]"; return "[id:" + this.getId() + ", order:" + this.getOrder() + ", position: {" + this.getPosition().x + "," + this.getPosition().y + "}, size: {" + this.getSize().width + "," + this.getSize().height + "}, shrink:" + this.areChildrenShrunken() + "]";
} }

View File

@ -16,25 +16,25 @@
* limitations under the License. * limitations under the License.
*/ */
mindplot.layout.RootedTreeSet = new Class({ mindplot.layout.RootedTreeSet = new Class({
initialize:function() { initialize:function () {
this._rootNodes = []; this._rootNodes = [];
}, },
setRoot:function(root) { setRoot:function (root) {
$assert(root, 'root can not be null'); $assert(root, 'root can not be null');
this._rootNodes.push(this._decodate(root)); this._rootNodes.push(this._decodate(root));
}, },
getTreeRoots:function() { getTreeRoots:function () {
return this._rootNodes; return this._rootNodes;
}, },
_decodate:function(node) { _decodate:function (node) {
node._children = []; node._children = [];
return node; return node;
}, },
add: function(node) { add:function (node) {
$assert(node, 'node can not be null'); $assert(node, 'node can not be null');
$assert(!this.find(node.getId(), false), 'node already exits with this id. Id:' + node.getId()); $assert(!this.find(node.getId(), false), 'node already exits with this id. Id:' + node.getId());
$assert(!node._children, 'node already added'); $assert(!node._children, 'node already added');
@ -42,13 +42,13 @@ mindplot.layout.RootedTreeSet = new Class({
}, },
remove: function(nodeId) { remove:function (nodeId) {
$assert($defined(nodeId), 'nodeId can not be null'); $assert($defined(nodeId), 'nodeId can not be null');
var node = this.find(nodeId); var node = this.find(nodeId);
this._rootNodes.erase(node); this._rootNodes.erase(node);
}, },
connect: function(parentId, childId) { connect:function (parentId, childId) {
$assert($defined(parentId), 'parent can not be null'); $assert($defined(parentId), 'parent can not be null');
$assert($defined(childId), 'child can not be null'); $assert($defined(childId), 'child can not be null');
@ -61,7 +61,7 @@ mindplot.layout.RootedTreeSet = new Class({
this._rootNodes.erase(child); this._rootNodes.erase(child);
}, },
disconnect: function(nodeId) { disconnect:function (nodeId) {
$assert($defined(nodeId), 'nodeId can not be null'); $assert($defined(nodeId), 'nodeId can not be null');
var node = this.find(nodeId); var node = this.find(nodeId);
$assert(node._parent, "Node is not connected"); $assert(node._parent, "Node is not connected");
@ -71,7 +71,7 @@ mindplot.layout.RootedTreeSet = new Class({
node._parent = null; node._parent = null;
}, },
find:function(id, validate) { find:function (id, validate) {
$assert($defined(id), 'id can not be null'); $assert($defined(id), 'id can not be null');
var graphs = this._rootNodes; var graphs = this._rootNodes;
@ -89,7 +89,7 @@ mindplot.layout.RootedTreeSet = new Class({
}, },
_find:function(id, parent) { _find:function (id, parent) {
if (parent.getId() == id) { if (parent.getId() == id) {
return parent; return parent;
@ -107,12 +107,12 @@ mindplot.layout.RootedTreeSet = new Class({
return result; return result;
}, },
getChildren: function(node) { getChildren:function (node) {
$assert(node, 'node cannot be null'); $assert(node, 'node cannot be null');
return node._children; return node._children;
}, },
getRootNode: function(node) { getRootNode:function (node) {
$assert(node, "node cannot be null"); $assert(node, "node cannot be null");
var parent = this.getParent(node); var parent = this.getParent(node);
if ($defined(parent)) { if ($defined(parent)) {
@ -122,12 +122,12 @@ mindplot.layout.RootedTreeSet = new Class({
return node; return node;
}, },
getAncestors: function(node) { getAncestors:function (node) {
$assert(node, 'node cannot be null'); $assert(node, 'node cannot be null');
return this._getAncestors(this.getParent(node), []); return this._getAncestors(this.getParent(node), []);
}, },
_getAncestors: function(node, ancestors) { _getAncestors:function (node, ancestors) {
var result = ancestors; var result = ancestors;
if (node) { if (node) {
result.push(node); result.push(node);
@ -136,24 +136,24 @@ mindplot.layout.RootedTreeSet = new Class({
return result; return result;
}, },
getSiblings: function(node) { getSiblings:function (node) {
$assert(node, 'node cannot be null'); $assert(node, 'node cannot be null');
if (!$defined(node._parent)) { if (!$defined(node._parent)) {
return []; return [];
} }
var siblings = node._parent._children.filter(function(child) { var siblings = node._parent._children.filter(function (child) {
return child != node; return child != node;
}); });
return siblings; return siblings;
}, },
hasSinglePathToSingleLeaf: function(node) { hasSinglePathToSingleLeaf:function (node) {
$assert(node, 'node cannot be null'); $assert(node, 'node cannot be null');
return this._hasSinglePathToSingleLeaf(node); return this._hasSinglePathToSingleLeaf(node);
}, },
_hasSinglePathToSingleLeaf: function(node) { _hasSinglePathToSingleLeaf:function (node) {
var children = this.getChildren(node); var children = this.getChildren(node);
if (children.length == 1) { if (children.length == 1) {
return this._hasSinglePathToSingleLeaf(children[0]); return this._hasSinglePathToSingleLeaf(children[0]);
@ -162,21 +162,21 @@ mindplot.layout.RootedTreeSet = new Class({
return children.length == 0; return children.length == 0;
}, },
isStartOfSubBranch: function(node) { isStartOfSubBranch:function (node) {
return this.getSiblings(node).length > 0 && this.getChildren(node).length == 1; return this.getSiblings(node).length > 0 && this.getChildren(node).length == 1;
}, },
isLeaf: function(node) { isLeaf:function (node) {
$assert(node, 'node cannot be null'); $assert(node, 'node cannot be null');
return this.getChildren(node).length == 0; return this.getChildren(node).length == 0;
}, },
getParent:function(node) { getParent:function (node) {
$assert(node, 'node cannot be null'); $assert(node, 'node cannot be null');
return node._parent; return node._parent;
}, },
dump: function() { dump:function () {
var branches = this._rootNodes; var branches = this._rootNodes;
var result = ""; var result = "";
for (var i = 0; i < branches.length; i++) { for (var i = 0; i < branches.length; i++) {
@ -186,7 +186,7 @@ mindplot.layout.RootedTreeSet = new Class({
return result; return result;
}, },
_dump:function(node, indent) { _dump:function (node, indent) {
var result = indent + node + "\n"; var result = indent + node + "\n";
var children = this.getChildren(node); var children = this.getChildren(node);
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
@ -197,7 +197,7 @@ mindplot.layout.RootedTreeSet = new Class({
return result; return result;
}, },
plot: function(canvas) { plot:function (canvas) {
var branches = this._rootNodes; var branches = this._rootNodes;
for (var i = 0; i < branches.length; i++) { for (var i = 0; i < branches.length; i++) {
var branch = branches[i]; var branch = branches[i];
@ -205,7 +205,7 @@ mindplot.layout.RootedTreeSet = new Class({
} }
}, },
_plot: function(canvas, node, root) { _plot:function (canvas, node, root) {
var children = this.getChildren(node); var children = this.getChildren(node);
var cx = node.getPosition().x + canvas.width / 2 - node.getSize().width / 2; var cx = node.getPosition().x + canvas.width / 2 - node.getSize().width / 2;
var cy = node.getPosition().y + canvas.height / 2 - node.getSize().height / 2; var cy = node.getPosition().y + canvas.height / 2 - node.getSize().height / 2;
@ -216,13 +216,13 @@ mindplot.layout.RootedTreeSet = new Class({
var fillColor = this._rootNodes.contains(node) ? "#000" : (node.isFree() ? "#abc" : "#c00"); var fillColor = this._rootNodes.contains(node) ? "#000" : (node.isFree() ? "#abc" : "#c00");
rect.attr('fill', fillColor); rect.attr('fill', fillColor);
var rectPosition = {x: rect.attr("x") - canvas.width/2 + rect.attr("width")/2, y:rect.attr("y") - canvas.height/2 + rect.attr("height")/2}; var rectPosition = {x:rect.attr("x") - canvas.width / 2 + rect.attr("width") / 2, y:rect.attr("y") - canvas.height / 2 + rect.attr("height") / 2};
var rectSize = {width: rect.attr("width"), height:rect.attr("height")}; var rectSize = {width:rect.attr("width"), height:rect.attr("height")};
rect.click(function() { rect.click(function () {
console.log("[id:" + node.getId() + ", order:" + node.getOrder() + ", position:(" + rectPosition.x + "," + rectPosition.y + "), size:" + rectSize.width + "x" + rectSize.height + ", freeDisplacement:(" + node.getFreeDisplacement().x + "," + node.getFreeDisplacement().y +")]"); console.log("[id:" + node.getId() + ", order:" + node.getOrder() + ", position:(" + rectPosition.x + "," + rectPosition.y + "), size:" + rectSize.width + "x" + rectSize.height + ", freeDisplacement:(" + node.getFreeDisplacement().x + "," + node.getFreeDisplacement().y + ")]");
}); });
text.click(function() { text.click(function () {
console.log("[id:" + node.getId() + ", order:" + node.getOrder() + ", position:(" + rectPosition.x + "," + rectPosition.y + "), size:" + rectSize.width + "x" + rectSize.height + ", freeDisplacement:(" + node.getFreeDisplacement().x + "," + node.getFreeDisplacement().y +")]"); console.log("[id:" + node.getId() + ", order:" + node.getOrder() + ", position:(" + rectPosition.x + "," + rectPosition.y + "), size:" + rectSize.width + "x" + rectSize.height + ", freeDisplacement:(" + node.getFreeDisplacement().x + "," + node.getFreeDisplacement().y + ")]");
}); });
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
@ -231,7 +231,7 @@ mindplot.layout.RootedTreeSet = new Class({
} }
}, },
updateBranchPosition : function(node, position) { updateBranchPosition:function (node, position) {
var oldPos = node.getPosition(); var oldPos = node.getPosition();
node.setPosition(position); node.setPosition(position);
@ -240,48 +240,48 @@ mindplot.layout.RootedTreeSet = new Class({
var yOffset = oldPos.y - position.y; var yOffset = oldPos.y - position.y;
var children = this.getChildren(node); var children = this.getChildren(node);
children.forEach(function(child) { children.forEach(function (child) {
this.shiftBranchPosition(child, xOffset, yOffset); this.shiftBranchPosition(child, xOffset, yOffset);
}.bind(this)); }.bind(this));
}, },
shiftBranchPosition: function(node, xOffset, yOffset) { shiftBranchPosition:function (node, xOffset, yOffset) {
var position = node.getPosition(); var position = node.getPosition();
node.setPosition({x:position.x + xOffset, y:position.y + yOffset}); node.setPosition({x:position.x + xOffset, y:position.y + yOffset});
var children = this.getChildren(node); var children = this.getChildren(node);
children.forEach(function(child) { children.forEach(function (child) {
this.shiftBranchPosition(child, xOffset, yOffset); this.shiftBranchPosition(child, xOffset, yOffset);
}.bind(this)); }.bind(this));
}, },
getSiblingsInVerticalDirection: function(node, yOffset) { getSiblingsInVerticalDirection:function (node, yOffset) {
// siblings with lower or higher order, depending on the direction of the offset and on the same side as their parent // siblings with lower or higher order, depending on the direction of the offset and on the same side as their parent
var parent = this.getParent(node); var parent = this.getParent(node);
var siblings = this.getSiblings(node).filter(function(sibling) { var siblings = this.getSiblings(node).filter(function (sibling) {
var sameSide = node.getPosition().x > parent.getPosition().x ? sibling.getPosition().x > parent.getPosition().x : sibling.getPosition().x < parent.getPosition().x; var sameSide = node.getPosition().x > parent.getPosition().x ? sibling.getPosition().x > parent.getPosition().x : sibling.getPosition().x < parent.getPosition().x;
var orderOK = yOffset < 0 ? sibling.getOrder() < node.getOrder() : sibling.getOrder() > node.getOrder(); var orderOK = yOffset < 0 ? sibling.getOrder() < node.getOrder() : sibling.getOrder() > node.getOrder();
return orderOK && sameSide; return orderOK && sameSide;
}); });
if (yOffset < 0 ) { if (yOffset < 0) {
siblings.reverse(); siblings.reverse();
} }
return siblings; return siblings;
}, },
getBranchesInVerticalDirection: function(node, yOffset) { getBranchesInVerticalDirection:function (node, yOffset) {
// direct descendants of the root that do not contain the node and are on the same side // direct descendants of the root that do not contain the node and are on the same side
// and on the direction of the offset // and on the direction of the offset
var rootNode = this.getRootNode(node); var rootNode = this.getRootNode(node);
var branches = this.getChildren(rootNode).filter(function(child) { var branches = this.getChildren(rootNode).filter(function (child) {
return this._find(node.getId(), child); return this._find(node.getId(), child);
}, this); }, this);
var branch = branches[0]; var branch = branches[0];
var rootDescendants = this.getSiblings(branch).filter(function(sibling) { var rootDescendants = this.getSiblings(branch).filter(function (sibling) {
var sameSide = node.getPosition().x > rootNode.getPosition().x ? sibling.getPosition().x > rootNode.getPosition().x : sibling.getPosition().x < rootNode.getPosition().x; var sameSide = node.getPosition().x > rootNode.getPosition().x ? sibling.getPosition().x > rootNode.getPosition().x : sibling.getPosition().x < rootNode.getPosition().x;
var sameDirection = yOffset < 0 ? sibling.getOrder() < branch.getOrder() : sibling.getOrder() > branch.getOrder(); var sameDirection = yOffset < 0 ? sibling.getOrder() < branch.getOrder() : sibling.getOrder() > branch.getOrder();
return sameSide && sameDirection; return sameSide && sameDirection;

View File

@ -111,14 +111,15 @@ mindplot.model.Mindmap = new Class({
}, },
findNodeById:function (id) { findNodeById:function (id) {
var result; var result = null;
for (var i = 0; i < this._branches; i++) { for (var i = 0; i < this._branches.length; i++) {
var branch = this._branches[i]; var branch = this._branches[i];
result = branch.findNodeById(id) result = branch.findNodeById(id);
if (result) { if (result) {
break; break;
} }
} }
return result;
} }
} }
); );

View File

@ -189,7 +189,7 @@ mindplot.model.NodeModel = new Class({
findNodeById : function(id) { findNodeById : function(id) {
var result = null; var result = null;
if (this.getId() == id) { if (this.getId() == id) {
return this; result = this;
} else { } else {
var children = this.getChildren(); var children = this.getChildren();
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {

View File

@ -411,7 +411,7 @@ mindplot.persistence.XMLSerializer_Pela = new Class({
return null; return null;
} }
// Is the connections points valid ?. If it's not, do not load the relationship ... // Is the connections points valid ?. If it's not, do not load the relationship ...
if (mindmap.findNodeById(srcId) == null || mindmap.findNodeById(destId)) { if (mindmap.findNodeById(srcId) == null || mindmap.findNodeById(destId)==null) {
return null; return null;
} }