109 lines
4.2 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 ?.
2012-08-18 23:20:23 -03:00
var candidates = this._searchConnectionCandidates(dragTopic);
2011-07-27 08:25:10 -03:00
var currentConnection = dragTopic.getConnectedToTopic();
2012-08-18 23:20:23 -03:00
if (currentConnection && (candidates.length == 0 || candidates[0] != currentConnection)) {
dragTopic.disconnect(this._workspace);
2009-06-07 18:59:43 +00:00
}
2011-07-27 08:25:10 -03:00
// Finally, connect nodes ...
2012-08-18 23:20:23 -03:00
if (!dragTopic.isConnected() && candidates.length > 0) {
dragTopic.connectTo(candidates[0]);
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-18 23:20:23 -03:00
_searchConnectionCandidates:function (dragTopic) {
2012-01-12 13:44:35 -03:00
var topics = this._designerModel.getTopics();
2012-08-17 20:40:26 -03:00
2012-08-18 23:20:23 -03:00
// Perform a initial filter to discard topics:
// - Exclude dragged topic
// - Exclude dragTopic pivot
// - Nodes that are collapsed
2011-07-27 08:25:10 -03:00
var draggedNode = dragTopic.getDraggedTopic();
2012-08-18 23:20:23 -03:00
topics = topics.filter(function (topic) {
return draggedNode != topic && topic != dragTopic._draggedNode && !topic.areChildrenShrunken() && !topic.isCollapsed();
});
// 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
2012-08-17 20:40:26 -03:00
var sourcePosition = dragTopic.getPosition();
topics = topics.filter(function (topic) {
2012-08-18 23:20:23 -03:00
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);
2012-08-17 20:40:26 -03:00
});
2012-08-18 23:20:23 -03:00
// Assign a priority based on the distance:
// - Alignment with the targetNode
// - Vertical distance
//
2012-08-17 20:40:26 -03:00
topics = topics.sort(function (a, b) {
var aPos = a.getPosition();
2012-08-18 23:37:32 -03:00
var ad = (aPos.x - sourcePosition.x) * Math.sign(aPos.x);
2012-08-17 20:40:26 -03:00
var bPos = b.getPosition();
2012-08-18 23:37:32 -03:00
var bd = ( bPos.x - sourcePosition.x) * Math.sign(bPos.x);
2012-08-17 20:40:26 -03:00
2012-08-18 23:20:23 -03:00
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));
2012-08-18 23:37:32 -03:00
// console.log("---- out ----");
// topics.each(function (e) {
// console.log(e.getText());
// });
// console.log("---- out ----");
2012-08-17 20:40:26 -03:00
2009-06-07 18:59:43 +00:00
2012-08-18 23:20:23 -03:00
return topics;
},
_isVerticallyAligned:function (targetSize, targetPosition, sourcePosition) {
return (sourcePosition.y - targetPosition.y) < targetSize.height;
2009-06-07 18:59:43 +00:00
}
2012-08-18 23:20:23 -03:00
2011-07-27 08:25:10 -03:00
});
2012-08-18 23:20:23 -03:00
mindplot.DragConnector.MAX_VERTICAL_CONNECTION_TOLERANCE = 80;