First clean up of the algorithm.

This commit is contained in:
Paulo Gustavo Veiga 2012-08-17 20:40:26 -03:00
parent c432bef6b1
commit bff79159e1
3 changed files with 83 additions and 85 deletions

View File

@ -17,7 +17,7 @@
*/ */
mindplot.DragConnector = new Class({ mindplot.DragConnector = new Class({
initialize:function(designerModel, workspace) { initialize:function (designerModel, workspace) {
$assert(designerModel, 'designerModel can not be null'); $assert(designerModel, 'designerModel can not be null');
$assert(workspace, 'workspace can not be null'); $assert(workspace, 'workspace can not be null');
@ -26,7 +26,7 @@ mindplot.DragConnector = new Class({
this._workspace = workspace; this._workspace = workspace;
}, },
checkConnection : function(dragTopic) { checkConnection:function (dragTopic) {
var topics = this._designerModel.getTopics(); var topics = this._designerModel.getTopics();
// Must be disconnected from their current connection ?. // Must be disconnected from their current connection ?.
@ -47,8 +47,7 @@ mindplot.DragConnector = new Class({
if ($defined(mainTopicToMainTopicConnection)) { if ($defined(mainTopicToMainTopicConnection)) {
// I have to change the current connection to a main topic. // I have to change the current connection to a main topic.
dragTopic.disconnect(this._workspace); dragTopic.disconnect(this._workspace);
} else } else if (Math.abs(dragXPosition - currentXPosition) > mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) {
if (Math.abs(dragXPosition - currentXPosition) > mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) {
dragTopic.disconnect(this._workspace); dragTopic.disconnect(this._workspace);
} }
} }
@ -65,31 +64,50 @@ mindplot.DragConnector = new Class({
} }
}, },
_lookUpForMainTopicToMainTopicConnection : function(dragTopic) { _lookUpForMainTopicToMainTopicConnection:function (dragTopic) {
var topics = this._designerModel.getTopics(); var topics = this._designerModel.getTopics();
var result = null;
// Filter all the nodes that are outside the boundary ...
var draggedNode = dragTopic.getDraggedTopic(); var draggedNode = dragTopic.getDraggedTopic();
var distance = null; var sourcePosition = dragTopic.getPosition();
topics = topics.filter(function (topic) {
var pos = topic.getPosition();
return (sourcePosition.x - pos.x) * Math.sign(pos.x) > 0 && draggedNode != topic;
});
// Topics must be ordered based on the distance vertical distance to the drag topic ...
topics = topics.sort(function (a, b) {
var aPos = a.getPosition();
var ad = (sourcePosition.x - aPos.x) * Math.sign(aPos.x);
var bPos = b.getPosition();
var bd = (sourcePosition.x - bPos.x) * Math.sign(bPos.x);
return ad > bd;
});
// Check MainTopic->MainTopic connection... // Check MainTopic->MainTopic connection...
var result = null;
var distance = null;
for (var i = 0; i < topics.length; i++) { for (var i = 0; i < topics.length; i++) {
var targetTopic = topics[i];
var position = dragTopic.getPosition(); var position = dragTopic.getPosition();
if (targetTopic.getType() != mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE && targetTopic != draggedNode) { var targetTopic = topics[i];
var canBeConnected = dragTopic.canBeConnectedTo(targetTopic);
if (canBeConnected) {
var targetPosition = targetTopic.getPosition();
var fix = position.y > targetPosition.y;
var gap = 0;
if (targetTopic.getChildren().length > 0) {
gap = Math.abs(targetPosition.y - targetTopic.getChildren()[0].getPosition().y)
}
var yDistance = Math.abs(position.y - fix * gap - targetPosition.y);
if (distance == null || yDistance < distance) {
result = targetTopic;
distance = yDistance;
}
var canBeConnected = dragTopic.canBeConnectedTo(targetTopic);
if (canBeConnected) {
var targetPosition = targetTopic.getPosition();
var fix = position.y > targetPosition.y;
var gap = 0;
if (targetTopic.getChildren().length > 0) {
gap = Math.abs(targetPosition.y - targetTopic.getChildren()[0].getPosition().y)
}
var yDistance = Math.abs(position.y - fix * gap - targetPosition.y);
if (distance == null || yDistance < distance) {
result = targetTopic;
distance = yDistance;
break;
} }
} }
} }

View File

@ -17,7 +17,7 @@
*/ */
mindplot.DragTopic = new Class({ mindplot.DragTopic = new Class({
initialize:function(dragShape, draggedNode, layoutManger) { initialize:function (dragShape, draggedNode, layoutManger) {
$assert(dragShape, 'Rect can not be null.'); $assert(dragShape, 'Rect can not be null.');
$assert(draggedNode, 'draggedNode can not be null.'); $assert(draggedNode, 'draggedNode can not be null.');
$assert(layoutManger, 'layoutManger can not be null.'); $assert(layoutManger, 'layoutManger can not be null.');
@ -31,13 +31,13 @@ mindplot.DragTopic = new Class({
this._isFreeLayoutEnabled = false; this._isFreeLayoutEnabled = false;
}, },
setOrder : function(order) { setOrder:function (order) {
this._order = order; this._order = order;
}, },
setPosition : function(x, y) { setPosition:function (x, y) {
// Update drag shadow position .... // Update drag shadow position ....
var position = {x:x,y:y}; var position = {x:x, y:y};
if (this.isFreeLayoutOn() && this.isConnected()) { if (this.isFreeLayoutOn() && this.isConnected()) {
var _layoutManager = this._layoutManager; var _layoutManager = this._layoutManager;
var par = this.getConnectedToTopic(); var par = this.getConnectedToTopic();
@ -66,7 +66,7 @@ mindplot.DragTopic = new Class({
} }
}, },
updateFreeLayout: function(event) { updateFreeLayout:function (event) {
var isFreeEnabled = (event.meta && Browser.Platform.mac) || (event.control && !Browser.Platform.mac); var isFreeEnabled = (event.meta && Browser.Platform.mac) || (event.control && !Browser.Platform.mac);
if (this.isFreeLayoutOn() != isFreeEnabled) { if (this.isFreeLayoutOn() != isFreeEnabled) {
var dragPivot = this._getDragPivot(); var dragPivot = this._getDragPivot();
@ -75,27 +75,27 @@ mindplot.DragTopic = new Class({
} }
}, },
setVisibility:function(value) { setVisibility:function (value) {
var dragPivot = this._getDragPivot(); var dragPivot = this._getDragPivot();
dragPivot.setVisibility(value); dragPivot.setVisibility(value);
}, },
isVisible:function() { isVisible:function () {
var dragPivot = this._getDragPivot(); var dragPivot = this._getDragPivot();
return dragPivot.isVisible(); return dragPivot.isVisible();
}, },
getInnerShape : function() { getInnerShape:function () {
return this._elem2d; return this._elem2d;
}, },
disconnect : function(workspace) { disconnect:function (workspace) {
// Clear connection line ... // Clear connection line ...
var dragPivot = this._getDragPivot(); var dragPivot = this._getDragPivot();
dragPivot.disconnect(workspace); dragPivot.disconnect(workspace);
}, },
canBeConnectedTo : function(targetTopic) { canBeConnectedTo:function (targetTopic) {
$assert(targetTopic, 'parent can not be null'); $assert(targetTopic, 'parent can not be null');
var result = true; var result = true;
@ -110,7 +110,7 @@ mindplot.DragTopic = new Class({
var targetTopicModel = targetTopic.getModel(); var targetTopicModel = targetTopic.getModel();
var childTopicModel = draggedNode.getModel(); var childTopicModel = draggedNode.getModel();
result = targetTopicModel.canBeConnected(childTopicModel, topicPosition, 18, targetTopic.getSize()); result = targetTopicModel.canBeConnected(childTopicModel, topicPosition, targetTopic.getSize());
} }
} else { } else {
result = false; result = false;
@ -118,7 +118,7 @@ mindplot.DragTopic = new Class({
return result; return result;
}, },
connectTo : function(parent) { connectTo:function (parent) {
$assert(parent, 'Parent connection node can not be null.'); $assert(parent, 'Parent connection node can not be null.');
// Where it should be connected ? // Where it should be connected ?
@ -133,11 +133,11 @@ mindplot.DragTopic = new Class({
this.setOrder(predict.order); this.setOrder(predict.order);
}, },
getDraggedTopic : function() { getDraggedTopic:function () {
return this._draggedNode; return this._draggedNode;
}, },
removeFromWorkspace : function(workspace) { removeFromWorkspace:function (workspace) {
// Remove drag shadow. // Remove drag shadow.
workspace.removeChild(this._elem2d); workspace.removeChild(this._elem2d);
this._isInWorkspace = false; this._isInWorkspace = false;
@ -148,30 +148,30 @@ mindplot.DragTopic = new Class({
}, },
isInWorkspace: function() { isInWorkspace:function () {
return this._isInWorkspace; return this._isInWorkspace;
}, },
addToWorkspace : function(workspace) { addToWorkspace:function (workspace) {
workspace.appendChild(this._elem2d); workspace.appendChild(this._elem2d);
var dragPivot = this._getDragPivot(); var dragPivot = this._getDragPivot();
dragPivot.addToWorkspace(workspace); dragPivot.addToWorkspace(workspace);
this._isInWorkspace = true; this._isInWorkspace = true;
}, },
_getDragPivot : function() { _getDragPivot:function () {
return mindplot.DragTopic.__getDragPivot(); return mindplot.DragTopic.__getDragPivot();
}, },
getPosition:function() { getPosition:function () {
return this._position; return this._position;
}, },
isDragTopic : function() { isDragTopic:function () {
return true; return true;
}, },
applyChanges : function(workspace) { applyChanges:function (workspace) {
$assert(workspace, 'workspace can not be null'); $assert(workspace, 'workspace can not be null');
@ -197,16 +197,16 @@ mindplot.DragTopic = new Class({
} }
}, },
getConnectedToTopic : function() { getConnectedToTopic:function () {
var dragPivot = this._getDragPivot(); var dragPivot = this._getDragPivot();
return dragPivot.getTargetTopic(); return dragPivot.getTargetTopic();
}, },
isConnected : function() { isConnected:function () {
return this.getConnectedToTopic() != null; return this.getConnectedToTopic() != null;
}, },
isFreeLayoutOn: function() { isFreeLayoutOn:function () {
// return this._isFreeLayoutEnabled; // return this._isFreeLayoutEnabled;
// Disable free layout ... // Disable free layout ...
return false; return false;
@ -214,16 +214,16 @@ mindplot.DragTopic = new Class({
}); });
mindplot.DragTopic.PIVOT_SIZE = {width:50,height:6}; mindplot.DragTopic.PIVOT_SIZE = {width:50, height:6};
mindplot.DragTopic.init = function(workspace) { mindplot.DragTopic.init = function (workspace) {
$assert(workspace, "workspace can not be null"); $assert(workspace, "workspace can not be null");
var pivot = mindplot.DragTopic.__getDragPivot(); var pivot = mindplot.DragTopic.__getDragPivot();
workspace.appendChild(pivot); workspace.appendChild(pivot);
}; };
mindplot.DragTopic.__getDragPivot = function() { mindplot.DragTopic.__getDragPivot = function () {
var result = mindplot.DragTopic._dragPivot; var result = mindplot.DragTopic._dragPivot;
if (!$defined(result)) { if (!$defined(result)) {
result = new mindplot.DragPivot(); result = new mindplot.DragPivot();

View File

@ -142,50 +142,30 @@ mindplot.model.NodeModel = new Class({
this._parent = parent; this._parent = parent;
}, },
canBeConnected:function (sourceModel, sourcePosition, targetTopicHeight, targetTopicSize) { canBeConnected:function (sourceModel, sourcePosition, targetTopicSize) {
$assert(sourceModel != this, 'The same node can not be parent and child if itself.'); $assert(sourceModel != this, 'The same node can not be parent and child if itself.');
$assert(sourcePosition, 'childPosition can not be null.'); $assert(sourcePosition, 'childPosition can not be null.');
$assert(targetTopicHeight, 'childrenWidth can not be null.');
$assert(targetTopicSize, 'targetTopicSize can not be null.'); $assert(targetTopicSize, 'targetTopicSize can not be null.');
// Only can be connected if the node is in the left or rigth.
var targetModel = this;
var mindmap = targetModel.getMindmap();
var targetPosition = targetModel.getPosition();
var result = false; var result = false;
if (sourceModel.getType() == mindplot.model.INodeModel.MAIN_TOPIC_TYPE) { // Only can be connected if the node is in the left or right.
// Finally, check current node position ... var targetModel = this;
var yDistance = Math.abs(sourcePosition.y - targetPosition.y); var targetPosition = targetModel.getPosition();
var gap = 35 + targetTopicHeight / 2;
if (targetModel.getChildren().length > 0) {
gap += Math.abs(targetPosition.y - targetModel.getChildren()[0].getPosition().y);
}
if (yDistance <= gap) { // Finally, check current node position ...
// Circular connection ? var yDistance = Math.abs(sourcePosition.y - targetPosition.y);
if (!sourceModel._isChildNode(this)) { var gap = 35 + targetTopicSize.height / 2;
var toleranceDistance = (targetTopicSize.width / 2) + targetTopicHeight; if (targetModel.getChildren().length > 0) {
gap += Math.abs(targetPosition.y - targetModel.getChildren()[0].getPosition().y);
var xDistance = sourcePosition.x - targetPosition.x;
var isTargetAtRightFromCentral = targetPosition.x >= 0;
if (isTargetAtRightFromCentral) {
if (xDistance >= -targetTopicSize.width / 2 && xDistance <= mindplot.model.INodeModel.MAIN_TOPIC_TO_MAIN_TOPIC_DISTANCE / 2 + (targetTopicSize.width / 2)) {
result = true;
}
} else {
if (xDistance <= targetTopicSize.width / 2 && Math.abs(xDistance) <= mindplot.model.INodeModel.MAIN_TOPIC_TO_MAIN_TOPIC_DISTANCE / 2 + (targetTopicSize.width / 2)) {
result = true;
}
}
}
}
} else {
throw "No implemented yet";
} }
if (yDistance <= gap) {
// Circular connection ?
var xDistance = (sourcePosition.x - targetPosition.x) * Math.sign(targetPosition.x);
result = xDistance > targetTopicSize.width;
}
return result; return result;
}, },