From 04fda82ba025d23ee932268ab5c746580e761e50 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sat, 18 Aug 2012 23:20:23 -0300 Subject: [PATCH] Keep simplifing connection algorithm. --- mindplot/src/main/javascript/DragConnector.js | 104 ++++++++---------- mindplot/src/main/javascript/DragTopic.js | 23 ---- .../javascript/widget/ColorPalettePanel.js | 1 - 3 files changed, 47 insertions(+), 81 deletions(-) diff --git a/mindplot/src/main/javascript/DragConnector.js b/mindplot/src/main/javascript/DragConnector.js index 189d27a3..412fa411 100644 --- a/mindplot/src/main/javascript/DragConnector.js +++ b/mindplot/src/main/javascript/DragConnector.js @@ -30,52 +30,49 @@ mindplot.DragConnector = new Class({ var topics = this._designerModel.getTopics(); // Must be disconnected from their current connection ?. - var mainTopicToMainTopicConnection = this._lookUpForMainTopicToMainTopicConnection(dragTopic); + var candidates = this._searchConnectionCandidates(dragTopic); var currentConnection = dragTopic.getConnectedToTopic(); - if ($defined(currentConnection)) { - // MainTopic->MainTopicConnection. - if (currentConnection.getType() == mindplot.model.INodeModel.MAIN_TOPIC_TYPE) { - if (mainTopicToMainTopicConnection != currentConnection) { - dragTopic.disconnect(this._workspace); - } - } - else if (currentConnection.getType() == mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE) { - // Distance if greater that the allowed. - var dragXPosition = dragTopic.getPosition().x; - var currentXPosition = currentConnection.getPosition().x; - 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) { - dragTopic.disconnect(this._workspace); - } - } + + if (currentConnection && (candidates.length == 0 || candidates[0] != currentConnection)) { + dragTopic.disconnect(this._workspace); } // Finally, connect nodes ... - if (!dragTopic.isConnected()) { - var centralTopic = topics[0]; - if ($defined(mainTopicToMainTopicConnection)) { - dragTopic.connectTo(mainTopicToMainTopicConnection); - } else if (Math.abs(dragTopic.getPosition().x - centralTopic.getPosition().x) <= mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) { - dragTopic.connectTo(centralTopic); - } + if (!dragTopic.isConnected() && candidates.length > 0) { + dragTopic.connectTo(candidates[0]); } }, - _lookUpForMainTopicToMainTopicConnection:function (dragTopic) { + _searchConnectionCandidates:function (dragTopic) { var topics = this._designerModel.getTopics(); - // Filter all the nodes that are outside the boundary ... + // Perform a initial filter to discard topics: + // - Exclude dragged topic + // - Exclude dragTopic pivot + // - Nodes that are collapsed var draggedNode = dragTopic.getDraggedTopic(); - 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; + return draggedNode != topic && topic != dragTopic._draggedNode && !topic.areChildrenShrunken() && !topic.isCollapsed(); }); - // Topics must be ordered based on the distance vertical distance to the drag topic ... + // Filter all the nodes that are outside the vertical boundary: + // * The node is to out of the x scope + // * The x distance greater the vertical tolerated distance + var sourcePosition = dragTopic.getPosition(); + topics = topics.filter(function (topic) { + var tpos = topic.getPosition(); + var distance = (sourcePosition.x - tpos.x) * Math.sign(sourcePosition.x); + + // Center topic has different alignment than the rest of the nodes. That's why i need to divide it by two... + var width = topic.getType() == mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE ? topic.getSize().width / 2 : topic.getSize().width; + return (distance > width) && ((distance - width) < mindplot.DragConnector.MAX_VERTICAL_CONNECTION_TOLERANCE); + }); + + // Assign a priority based on the distance: + // - Alignment with the targetNode + // - Vertical distance + // topics = topics.sort(function (a, b) { var aPos = a.getPosition(); var ad = (sourcePosition.x - aPos.x) * Math.sign(aPos.x); @@ -83,36 +80,29 @@ mindplot.DragConnector = new Class({ var bPos = b.getPosition(); var bd = (sourcePosition.x - bPos.x) * Math.sign(bPos.x); - return ad > bd; + var av = this._isVerticallyAligned(a.getSize(), a.getPosition(), sourcePosition); + var bv = this._isVerticallyAligned(b.getSize(), b.getPosition(), sourcePosition); + + return ((bv ? 1000 : 1) + bd) - ((av ? 1000 : 1) + ad); + + }.bind(this)); + + console.log("---- out ----"); + topics.each(function (e) { + console.log(e.getText()); }); + console.log("---- out ----"); - // Check MainTopic->MainTopic connection... - var result = null; - var distance = null; - for (var i = 0; i < topics.length; i++) { - var position = dragTopic.getPosition(); - var targetTopic = topics[i]; + return topics; + }, - 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) - } + _isVerticallyAligned:function (targetSize, targetPosition, sourcePosition) { + + return (sourcePosition.y - targetPosition.y) < targetSize.height; - var yDistance = Math.abs(position.y - fix * gap - targetPosition.y); - if (distance == null || yDistance < distance) { - result = targetTopic; - distance = yDistance; - break; - } - } - } - return result; } + }); -mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE = 400; +mindplot.DragConnector.MAX_VERTICAL_CONNECTION_TOLERANCE = 80; diff --git a/mindplot/src/main/javascript/DragTopic.js b/mindplot/src/main/javascript/DragTopic.js index d495b22b..28c00e9b 100644 --- a/mindplot/src/main/javascript/DragTopic.js +++ b/mindplot/src/main/javascript/DragTopic.js @@ -95,29 +95,6 @@ mindplot.DragTopic = new Class({ dragPivot.disconnect(workspace); }, - canBeConnectedTo:function (targetTopic) { - $assert(targetTopic, 'parent can not be null'); - - var result = true; - if (!targetTopic.areChildrenShrunken() && !targetTopic.isCollapsed()) { - // Dragged node can not be connected to himself. - if (targetTopic == this._draggedNode) { - result = false; - } else { - var draggedNode = this.getDraggedTopic(); - var topicPosition = this.getPosition(); - - var targetTopicModel = targetTopic.getModel(); - var childTopicModel = draggedNode.getModel(); - - result = targetTopicModel.canBeConnected(childTopicModel, topicPosition, targetTopic.getSize()); - } - } else { - result = false; - } - return result; - }, - connectTo:function (parent) { $assert(parent, 'Parent connection node can not be null.'); diff --git a/mindplot/src/main/javascript/widget/ColorPalettePanel.js b/mindplot/src/main/javascript/widget/ColorPalettePanel.js index b1a29924..e4d4a554 100644 --- a/mindplot/src/main/javascript/widget/ColorPalettePanel.js +++ b/mindplot/src/main/javascript/widget/ColorPalettePanel.js @@ -39,7 +39,6 @@ mindplot.widget.ColorPalettePanel = new Class({ method:'get', async:false, onRequest:function () { - console.log("loading ..."); }, onSuccess:function (responseText) { result = responseText;