wisemapping-open-source/mindplot/src/main/javascript/RelationshipPivot.js

113 lines
3.8 KiB
JavaScript
Raw Normal View History

2011-10-09 22:59:16 +02: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.RelationshipPivot = new Class({
2012-07-07 06:33:34 +02:00
initialize:function (workspace, designer) {
2011-10-09 22:59:16 +02:00
$assert(workspace, "workspace can not be null");
$assert(designer, "designer can not be null");
this._workspace = workspace;
this._designer = designer;
this._mouseMoveEvent = this._mouseMove.bind(this);
this._onClickEvent = this._cleanOnMouseClick.bind(this);
this._onTopicClick = this._connectOnFocus.bind(this);
},
2012-07-07 06:33:34 +02:00
start:function (sourceTopic, targetPos) {
2011-10-09 22:59:16 +02:00
$assert(sourceTopic, "sourceTopic can not be null");
$assert(targetPos, "targetPos can not be null");
this.dispose();
this._sourceTopic = sourceTopic;
if (sourceTopic != null) {
this._workspace.enableWorkspaceEvents(false);
var sourcePos = sourceTopic.getPosition();
this._pivot = new web2d.CurvedLine();
this._pivot.setStyle(web2d.CurvedLine.SIMPLE_LINE);
this._pivot.setDashed(2, 2);
this._pivot.setFrom(sourcePos.x, sourcePos.y);
this._pivot.setTo(targetPos.x, targetPos.y);
this._workspace.appendChild(this._pivot);
this._workspace.addEvent('mousemove', this._mouseMoveEvent);
this._workspace.addEvent('click', this._onClickEvent);
// Register focus events on all topics ...
var model = this._designer.getModel();
var topics = model.getTopics();
topics.each(function (topic) {
2011-10-09 22:59:16 +02:00
topic.addEvent('ontfocus', this._onTopicClick);
}.bind(this));
}
},
2012-07-07 06:33:34 +02:00
dispose:function () {
2011-10-09 22:59:16 +02:00
var workspace = this._workspace;
if (this._isActive()) {
workspace.removeEvent('mousemove', this._mouseMoveEvent);
workspace.removeEvent('click', this._onClickEvent);
var model = this._designer.getModel();
var topics = model.getTopics();
topics.each(function (topic) {
2011-10-09 22:59:16 +02:00
topic.removeEvent('ontfocus', this._onTopicClick);
}.bind(this));
workspace.removeChild(this._pivot);
workspace.enableWorkspaceEvents(true);
this._sourceTopic = null;
this._pivot = null;
}
},
2012-07-07 06:33:34 +02:00
_mouseMove:function (event) {
2011-10-09 22:59:16 +02:00
var screen = this._workspace.getScreenManager();
var pos = screen.getWorkspaceMousePosition(event);
this._pivot.setTo(pos.x - 1, pos.y - 1);
event.stopPropagation();
return false;
},
2012-07-07 06:33:34 +02:00
_cleanOnMouseClick:function (event) {
2011-10-09 22:59:16 +02:00
// The user clicks on a desktop on in other element that is not a node.
this.dispose();
event.stopPropagation();
},
2012-07-07 06:33:34 +02:00
_connectOnFocus:function (targetTopic) {
2011-10-09 22:59:16 +02:00
var sourceTopic = this._sourceTopic;
2012-07-07 06:33:34 +02:00
var mindmap = this._designer.getMindmap();
var relModel = mindmap.createRelationship(targetTopic.getId(), sourceTopic.getId());
this._designer._actionDispatcher.addRelationship(relModel);
2011-10-09 22:59:16 +02:00
this.dispose();
},
2012-07-07 06:33:34 +02:00
_isActive:function () {
2011-10-09 22:59:16 +02:00
return this._pivot != null;
}
});