119 lines
5.0 KiB
JavaScript
Raw Normal View History

/*
2011-07-27 08:25:10 -03:00
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mindplot.DragConnector = new Class({
2012-08-17 20:40:26 -03:00
initialize:function (designerModel, workspace) {
$assert(designerModel, 'designerModel can not be null');
$assert(workspace, 'workspace can not be null');
// this._layoutManager = layoutManager;
this._designerModel = designerModel;
this._workspace = workspace;
2011-07-27 08:25:10 -03:00
},
2009-06-07 18:59:43 +00:00
2012-08-17 20:40:26 -03:00
checkConnection:function (dragTopic) {
2012-01-12 13:44:35 -03:00
var topics = this._designerModel.getTopics();
2009-06-07 18:59:43 +00:00
2011-07-27 08:25:10 -03:00
// Must be disconnected from their current connection ?.
var mainTopicToMainTopicConnection = this._lookUpForMainTopicToMainTopicConnection(dragTopic);
var currentConnection = dragTopic.getConnectedToTopic();
if ($defined(currentConnection)) {
2011-07-27 08:25:10 -03:00
// MainTopic->MainTopicConnection.
2011-09-08 10:03:42 -03:00
if (currentConnection.getType() == mindplot.model.INodeModel.MAIN_TOPIC_TYPE) {
2011-07-27 08:25:10 -03:00
if (mainTopicToMainTopicConnection != currentConnection) {
dragTopic.disconnect(this._workspace);
}
}
2011-09-08 10:03:42 -03:00
else if (currentConnection.getType() == mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE) {
2011-07-27 08:25:10 -03:00
// Distance if greater that the allowed.
var dragXPosition = dragTopic.getPosition().x;
var currentXPosition = currentConnection.getPosition().x;
2009-06-07 18:59:43 +00:00
if ($defined(mainTopicToMainTopicConnection)) {
2011-07-27 08:25:10 -03:00
// I have to change the current connection to a main topic.
dragTopic.disconnect(this._workspace);
2012-08-17 20:40:26 -03:00
} else if (Math.abs(dragXPosition - currentXPosition) > mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) {
2011-07-27 08:25:10 -03:00
dragTopic.disconnect(this._workspace);
}
2009-06-07 18:59:43 +00:00
}
}
2011-07-27 08:25:10 -03:00
// Finally, connect nodes ...
if (!dragTopic.isConnected()) {
var centralTopic = topics[0];
if ($defined(mainTopicToMainTopicConnection)) {
2011-07-27 08:25:10 -03:00
dragTopic.connectTo(mainTopicToMainTopicConnection);
} else if (Math.abs(dragTopic.getPosition().x - centralTopic.getPosition().x) <= mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) {
dragTopic.connectTo(centralTopic);
2009-06-07 18:59:43 +00:00
}
}
2011-07-27 08:25:10 -03:00
},
2009-06-07 18:59:43 +00:00
2012-08-17 20:40:26 -03:00
_lookUpForMainTopicToMainTopicConnection:function (dragTopic) {
2012-01-12 13:44:35 -03:00
var topics = this._designerModel.getTopics();
2012-08-17 20:40:26 -03:00
// Filter all the nodes that are outside the boundary ...
2011-07-27 08:25:10 -03:00
var draggedNode = dragTopic.getDraggedTopic();
2012-08-17 20:40:26 -03:00
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;
});
2009-06-07 18:59:43 +00:00
2011-07-27 08:25:10 -03:00
// Check MainTopic->MainTopic connection...
2012-08-17 20:40:26 -03:00
var result = null;
var distance = null;
2011-07-27 08:25:10 -03:00
for (var i = 0; i < topics.length; i++) {
var position = dragTopic.getPosition();
2012-08-17 20:40:26 -03:00
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)
}
2009-06-07 18:59:43 +00:00
2012-08-17 20:40:26 -03:00
var yDistance = Math.abs(position.y - fix * gap - targetPosition.y);
if (distance == null || yDistance < distance) {
result = targetTopic;
distance = yDistance;
break;
2011-06-14 16:59:13 +01:00
}
2009-06-07 18:59:43 +00:00
}
}
2011-07-27 08:25:10 -03:00
return result;
2009-06-07 18:59:43 +00:00
}
2011-07-27 08:25:10 -03:00
});
mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE = 400;