From bff79159e1d78438ed84ed0a2a34bcf25b10fb42 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Fri, 17 Aug 2012 20:40:26 -0300 Subject: [PATCH] First clean up of the algorithm. --- mindplot/src/main/javascript/DragConnector.js | 62 ++++++++++++------- mindplot/src/main/javascript/DragTopic.js | 52 ++++++++-------- .../src/main/javascript/model/NodeModel.js | 54 +++++----------- 3 files changed, 83 insertions(+), 85 deletions(-) diff --git a/mindplot/src/main/javascript/DragConnector.js b/mindplot/src/main/javascript/DragConnector.js index 6342ef60..189d27a3 100644 --- a/mindplot/src/main/javascript/DragConnector.js +++ b/mindplot/src/main/javascript/DragConnector.js @@ -17,7 +17,7 @@ */ mindplot.DragConnector = new Class({ - initialize:function(designerModel, workspace) { + initialize:function (designerModel, workspace) { $assert(designerModel, 'designerModel can not be null'); $assert(workspace, 'workspace can not be null'); @@ -26,7 +26,7 @@ mindplot.DragConnector = new Class({ this._workspace = workspace; }, - checkConnection : function(dragTopic) { + checkConnection:function (dragTopic) { var topics = this._designerModel.getTopics(); // Must be disconnected from their current connection ?. @@ -47,8 +47,7 @@ mindplot.DragConnector = new Class({ if ($defined(mainTopicToMainTopicConnection)) { // I have to change the current connection to a main topic. dragTopic.disconnect(this._workspace); - } else - if (Math.abs(dragXPosition - currentXPosition) > mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) { + } else if (Math.abs(dragXPosition - currentXPosition) > mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) { dragTopic.disconnect(this._workspace); } } @@ -65,31 +64,50 @@ mindplot.DragConnector = new Class({ } }, - _lookUpForMainTopicToMainTopicConnection : function(dragTopic) { + _lookUpForMainTopicToMainTopicConnection:function (dragTopic) { var topics = this._designerModel.getTopics(); - var result = null; + + // Filter all the nodes that are outside the boundary ... 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... + var result = null; + var distance = null; for (var i = 0; i < topics.length; i++) { - var targetTopic = topics[i]; var position = dragTopic.getPosition(); - if (targetTopic.getType() != mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE && targetTopic != draggedNode) { - 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 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; + break; } } } diff --git a/mindplot/src/main/javascript/DragTopic.js b/mindplot/src/main/javascript/DragTopic.js index d9e67958..d495b22b 100644 --- a/mindplot/src/main/javascript/DragTopic.js +++ b/mindplot/src/main/javascript/DragTopic.js @@ -17,7 +17,7 @@ */ mindplot.DragTopic = new Class({ - initialize:function(dragShape, draggedNode, layoutManger) { + initialize:function (dragShape, draggedNode, layoutManger) { $assert(dragShape, 'Rect can not be null.'); $assert(draggedNode, 'draggedNode can not be null.'); $assert(layoutManger, 'layoutManger can not be null.'); @@ -31,13 +31,13 @@ mindplot.DragTopic = new Class({ this._isFreeLayoutEnabled = false; }, - setOrder : function(order) { + setOrder:function (order) { this._order = order; }, - setPosition : function(x, y) { + setPosition:function (x, y) { // Update drag shadow position .... - var position = {x:x,y:y}; + var position = {x:x, y:y}; if (this.isFreeLayoutOn() && this.isConnected()) { var _layoutManager = this._layoutManager; 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); if (this.isFreeLayoutOn() != isFreeEnabled) { var dragPivot = this._getDragPivot(); @@ -75,27 +75,27 @@ mindplot.DragTopic = new Class({ } }, - setVisibility:function(value) { + setVisibility:function (value) { var dragPivot = this._getDragPivot(); dragPivot.setVisibility(value); }, - isVisible:function() { + isVisible:function () { var dragPivot = this._getDragPivot(); return dragPivot.isVisible(); }, - getInnerShape : function() { + getInnerShape:function () { return this._elem2d; }, - disconnect : function(workspace) { + disconnect:function (workspace) { // Clear connection line ... var dragPivot = this._getDragPivot(); dragPivot.disconnect(workspace); }, - canBeConnectedTo : function(targetTopic) { + canBeConnectedTo:function (targetTopic) { $assert(targetTopic, 'parent can not be null'); var result = true; @@ -110,7 +110,7 @@ mindplot.DragTopic = new Class({ var targetTopicModel = targetTopic.getModel(); var childTopicModel = draggedNode.getModel(); - result = targetTopicModel.canBeConnected(childTopicModel, topicPosition, 18, targetTopic.getSize()); + result = targetTopicModel.canBeConnected(childTopicModel, topicPosition, targetTopic.getSize()); } } else { result = false; @@ -118,7 +118,7 @@ mindplot.DragTopic = new Class({ return result; }, - connectTo : function(parent) { + connectTo:function (parent) { $assert(parent, 'Parent connection node can not be null.'); // Where it should be connected ? @@ -133,11 +133,11 @@ mindplot.DragTopic = new Class({ this.setOrder(predict.order); }, - getDraggedTopic : function() { + getDraggedTopic:function () { return this._draggedNode; }, - removeFromWorkspace : function(workspace) { + removeFromWorkspace:function (workspace) { // Remove drag shadow. workspace.removeChild(this._elem2d); this._isInWorkspace = false; @@ -148,30 +148,30 @@ mindplot.DragTopic = new Class({ }, - isInWorkspace: function() { + isInWorkspace:function () { return this._isInWorkspace; }, - addToWorkspace : function(workspace) { + addToWorkspace:function (workspace) { workspace.appendChild(this._elem2d); var dragPivot = this._getDragPivot(); dragPivot.addToWorkspace(workspace); this._isInWorkspace = true; }, - _getDragPivot : function() { + _getDragPivot:function () { return mindplot.DragTopic.__getDragPivot(); }, - getPosition:function() { + getPosition:function () { return this._position; }, - isDragTopic : function() { + isDragTopic:function () { return true; }, - applyChanges : function(workspace) { + applyChanges:function (workspace) { $assert(workspace, 'workspace can not be null'); @@ -197,16 +197,16 @@ mindplot.DragTopic = new Class({ } }, - getConnectedToTopic : function() { + getConnectedToTopic:function () { var dragPivot = this._getDragPivot(); return dragPivot.getTargetTopic(); }, - isConnected : function() { + isConnected:function () { return this.getConnectedToTopic() != null; }, - isFreeLayoutOn: function() { + isFreeLayoutOn:function () { // return this._isFreeLayoutEnabled; // Disable free layout ... 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"); var pivot = mindplot.DragTopic.__getDragPivot(); workspace.appendChild(pivot); }; -mindplot.DragTopic.__getDragPivot = function() { +mindplot.DragTopic.__getDragPivot = function () { var result = mindplot.DragTopic._dragPivot; if (!$defined(result)) { result = new mindplot.DragPivot(); diff --git a/mindplot/src/main/javascript/model/NodeModel.js b/mindplot/src/main/javascript/model/NodeModel.js index 531aadde..24dc61fd 100644 --- a/mindplot/src/main/javascript/model/NodeModel.js +++ b/mindplot/src/main/javascript/model/NodeModel.js @@ -142,50 +142,30 @@ mindplot.model.NodeModel = new Class({ 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(sourcePosition, 'childPosition can not be null.'); - $assert(targetTopicHeight, 'childrenWidth 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; - if (sourceModel.getType() == mindplot.model.INodeModel.MAIN_TOPIC_TYPE) { - // Finally, check current node position ... - var yDistance = Math.abs(sourcePosition.y - targetPosition.y); - var gap = 35 + targetTopicHeight / 2; - if (targetModel.getChildren().length > 0) { - gap += Math.abs(targetPosition.y - targetModel.getChildren()[0].getPosition().y); - } + // Only can be connected if the node is in the left or right. + var targetModel = this; + var targetPosition = targetModel.getPosition(); - if (yDistance <= gap) { - // Circular connection ? - if (!sourceModel._isChildNode(this)) { - var toleranceDistance = (targetTopicSize.width / 2) + targetTopicHeight; - - 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"; + // Finally, check current node position ... + var yDistance = Math.abs(sourcePosition.y - targetPosition.y); + var gap = 35 + targetTopicSize.height / 2; + if (targetModel.getChildren().length > 0) { + gap += Math.abs(targetPosition.y - targetModel.getChildren()[0].getPosition().y); } + + if (yDistance <= gap) { + // Circular connection ? + var xDistance = (sourcePosition.x - targetPosition.x) * Math.sign(targetPosition.x); + result = xDistance > targetTopicSize.width; + } + return result; },