321 lines
10 KiB
JavaScript
Raw Normal View History

/*
2011-07-27 14:53:32 -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.Relationship = new Class({
Extends:mindplot.ConnectionLine,
2012-07-07 11:52:23 -03:00
Static:{
getStrokeColor:function () {
return '#9b74e6';
},
type: "Relationship"
},
initialize:function (sourceNode, targetNode, model) {
2012-07-07 11:52:23 -03:00
$assert(sourceNode, "sourceNode can not be null");
$assert(targetNode, "targetNode can not be null");
this.parent(sourceNode, targetNode, model.getLineType());
this.setModel(model);
var strokeColor = mindplot.Relationship.getStrokeColor();
2011-07-27 14:53:32 -03:00
this._line2d.setIsSrcControlPointCustom(false);
this._line2d.setIsDestControlPointCustom(false);
this._line2d.setCursor('pointer');
this._line2d.setStroke(1, 'solid', strokeColor);
this._line2d.setDashed(4, 2);
2011-07-27 14:53:32 -03:00
this._focusShape = this._createLine(this.getLineType(), mindplot.ConnectionLine.SIMPLE_CURVED);
this._focusShape.setStroke(2, "solid", "#3f96ff");
2011-07-27 14:53:32 -03:00
var ctrlPoints = this._line2d.getControlPoints();
this._focusShape.setSrcControlPoint(ctrlPoints[0]);
this._focusShape.setDestControlPoint(ctrlPoints[1]);
this._focusShape.setVisibility(false);
this._onFocus = false;
this._isInWorkspace = false;
this._controlPointsController = new mindplot.ControlPoint();
this._startArrow = new web2d.Arrow();
this._startArrow.setStrokeColor(strokeColor);
this._startArrow.setStrokeWidth(2);
this.setShowStartArrow(true);
// Share style is disable ...
if (this._showEndArrow) {
this._endArrow = new web2d.Arrow();
this._endArrow.setStrokeColor(strokeColor);
this._endArrow.setStrokeWidth(2);
}
2011-07-27 14:53:32 -03:00
// Position the line ...
if ($defined(model.getSrcCtrlPoint())) {
var srcPoint = model.getSrcCtrlPoint().clone();
this.setSrcControlPoint(srcPoint);
}
if ($defined(model.getDestCtrlPoint())) {
var destPoint = model.getDestCtrlPoint().clone();
this.setDestControlPoint(destPoint);
}
2011-07-27 14:53:32 -03:00
},
setStroke:function (color, style, opacity) {
2011-07-30 05:22:57 -03:00
this.parent(color, style, opacity);
2011-07-27 14:53:32 -03:00
this._startArrow.setStrokeColor(color);
},
redraw:function () {
2011-07-27 14:53:32 -03:00
var line2d = this._line2d;
var sourceTopic = this._sourceTopic;
var sourcePosition = sourceTopic.getPosition();
var targetTopic = this._targetTopic;
var targetPosition = targetTopic.getPosition();
var sPos, tPos;
2011-07-27 14:53:32 -03:00
this._line2d.setStroke(2);
var ctrlPoints = this._line2d.getControlPoints();
if (!this._line2d.isDestControlPointCustom() && !this._line2d.isSrcControlPointCustom()) {
2012-05-27 18:15:46 -03:00
var defaultPoints = mindplot.util.Shape.calculateDefaultControlPoints(sourcePosition, targetPosition);
2011-07-27 14:53:32 -03:00
ctrlPoints[0].x = defaultPoints[0].x;
ctrlPoints[0].y = defaultPoints[0].y;
ctrlPoints[1].x = defaultPoints[1].x;
ctrlPoints[1].y = defaultPoints[1].y;
}
var spoint = new core.Point();
spoint.x = parseInt(ctrlPoints[0].x) + parseInt(sourcePosition.x);
spoint.y = parseInt(ctrlPoints[0].y) + parseInt(sourcePosition.y);
2011-10-09 18:14:28 -03:00
2011-07-27 14:53:32 -03:00
var tpoint = new core.Point();
tpoint.x = parseInt(ctrlPoints[1].x) + parseInt(targetPosition.x);
tpoint.y = parseInt(ctrlPoints[1].y) + parseInt(targetPosition.y);
2012-05-27 18:15:46 -03:00
sPos = mindplot.util.Shape.calculateRelationShipPointCoordinates(sourceTopic, spoint);
tPos = mindplot.util.Shape.calculateRelationShipPointCoordinates(targetTopic, tpoint);
2011-07-27 14:53:32 -03:00
line2d.setFrom(sPos.x, sPos.y);
line2d.setTo(tPos.x, tPos.y);
line2d.moveToFront();
//Positionate Arrows
this._positionArrows();
2011-07-27 14:53:32 -03:00
// Add connector ...
this._positionateConnector(targetTopic);
if (this.isOnFocus()) {
2011-08-29 20:10:05 -03:00
this._refreshShape();
2011-07-27 14:53:32 -03:00
}
this._focusShape.moveToBack();
this._controlPointsController.redraw();
},
_positionArrows:function () {
2011-07-27 14:53:32 -03:00
var tpos = this._line2d.getTo();
var spos = this._line2d.getFrom();
2011-10-09 17:59:16 -03:00
this._startArrow.setFrom(spos.x, spos.y);
2011-07-27 14:53:32 -03:00
this._startArrow.moveToBack();
if (this._endArrow) {
this._endArrow.setFrom(tpos.x, tpos.y);
this._endArrow.moveToBack();
}
2011-07-27 14:53:32 -03:00
if (this._line2d.getType() == "CurvedLine") {
var controlPoints = this._line2d.getControlPoints();
this._startArrow.setControlPoint(controlPoints[0]);
if (this._endArrow) {
this._endArrow.setControlPoint(controlPoints[1]);
}
2011-07-27 14:53:32 -03:00
} else {
this._startArrow.setControlPoint(this._line2d.getTo());
if (this._endArrow) {
this._endArrow.setControlPoint(this._line2d.getFrom());
}
2011-07-27 14:53:32 -03:00
}
2011-10-09 17:59:16 -03:00
if (this._showEndArrow) {
this._endArrow.setVisibility(this.isVisible());
}
2011-10-09 17:59:16 -03:00
this._startArrow.setVisibility(this.isVisible() && this._showStartArrow);
2011-07-27 14:53:32 -03:00
},
addToWorkspace:function (workspace) {
2011-07-27 14:53:32 -03:00
workspace.appendChild(this._focusShape);
workspace.appendChild(this._controlPointsController);
this._controlPointControllerListener = this._initializeControlPointController.bind(this);
2011-08-21 12:42:00 -03:00
this._line2d.addEvent('click', this._controlPointControllerListener);
2011-07-27 14:53:32 -03:00
this._isInWorkspace = true;
workspace.appendChild(this._startArrow);
if (this._endArrow)
workspace.appendChild(this._endArrow);
2011-07-27 14:53:32 -03:00
2011-07-30 05:22:57 -03:00
this.parent(workspace);
this._positionArrows();
2012-07-07 11:52:23 -03:00
this.redraw();
2011-07-27 14:53:32 -03:00
},
_initializeControlPointController:function () {
this.setOnFocus(true);
2011-07-27 14:53:32 -03:00
},
removeFromWorkspace:function (workspace) {
2011-07-27 14:53:32 -03:00
workspace.removeChild(this._focusShape);
workspace.removeChild(this._controlPointsController);
2011-08-21 12:42:00 -03:00
this._line2d.removeEvent('click', this._controlPointControllerListener);
2011-07-27 14:53:32 -03:00
this._isInWorkspace = false;
workspace.removeChild(this._startArrow);
if (this._endArrow)
workspace.removeChild(this._endArrow);
2011-07-27 14:53:32 -03:00
2011-07-30 05:22:57 -03:00
this.parent(workspace);
2011-07-27 14:53:32 -03:00
},
getType:function () {
return mindplot.Relationship.type;
2011-07-27 14:53:32 -03:00
},
setOnFocus:function (focus) {
2011-07-27 14:53:32 -03:00
// Change focus shape
2011-08-29 20:10:05 -03:00
if (this.isOnFocus() != focus) {
if (focus) {
this._refreshShape();
this._controlPointsController.setLine(this);
}
this._focusShape.setVisibility(focus);
this._controlPointsController.setVisibility(focus);
this._onFocus = focus;
console.log("foucus:....");
2011-10-09 18:14:28 -03:00
2011-07-27 14:53:32 -03:00
}
},
_refreshShape:function () {
2011-07-27 14:53:32 -03:00
var sPos = this._line2d.getFrom();
var tPos = this._line2d.getTo();
var ctrlPoints = this._line2d.getControlPoints();
this._focusShape.setFrom(sPos.x, sPos.y);
this._focusShape.setTo(tPos.x, tPos.y);
var shapeCtrlPoints = this._focusShape.getControlPoints();
shapeCtrlPoints[0].x = ctrlPoints[0].x;
shapeCtrlPoints[0].y = ctrlPoints[0].y;
shapeCtrlPoints[1].x = ctrlPoints[1].x;
shapeCtrlPoints[1].y = ctrlPoints[1].y;
this._focusShape.updateLine();
},
addEvent:function (type, listener) {
2011-07-27 14:53:32 -03:00
// Translate to web 2d events ...
if (type == 'onfocus') {
type = 'mousedown';
}
var line = this._line2d;
2011-08-21 12:42:00 -03:00
line.addEvent(type, listener);
2011-07-27 14:53:32 -03:00
},
isOnFocus:function () {
2011-07-27 14:53:32 -03:00
return this._onFocus;
},
isInWorkspace:function () {
2011-07-27 14:53:32 -03:00
return this._isInWorkspace;
},
setVisibility:function (value) {
2011-07-30 05:22:57 -03:00
this.parent(value);
if (this._showEndArrow)
this._endArrow.setVisibility(this._showEndArrow);
2011-07-27 14:53:32 -03:00
this._startArrow.setVisibility(this._showStartArrow && value);
},
setOpacity:function (opacity) {
2011-07-30 05:22:57 -03:00
this.parent(opacity);
2011-07-27 14:53:32 -03:00
if (this._showEndArrow)
this._endArrow.setOpacity(opacity);
if (this._showStartArrow)
this._startArrow.setOpacity(opacity);
},
setShowEndArrow:function (visible) {
2011-07-27 14:53:32 -03:00
this._showEndArrow = visible;
if (this._isInWorkspace)
this.redraw();
},
setShowStartArrow:function (visible) {
2011-07-27 14:53:32 -03:00
this._showStartArrow = visible;
if (this._isInWorkspace)
this.redraw();
},
setFrom:function (x, y) {
$assert($defined(x), "x must be defined");
$assert($defined(y), "y must be defined");
2011-10-09 17:59:16 -03:00
2011-07-27 14:53:32 -03:00
this._line2d.setFrom(x, y);
this._startArrow.setFrom(x, y);
},
setTo:function (x, y) {
$assert($defined(x), "x must be defined");
$assert($defined(y), "y must be defined");
2011-10-09 17:59:16 -03:00
2011-07-27 14:53:32 -03:00
this._line2d.setTo(x, y);
if (this._endArrow)
this._endArrow.setFrom(x, y);
2011-07-27 14:53:32 -03:00
},
setSrcControlPoint:function (control) {
2011-07-27 14:53:32 -03:00
this._line2d.setSrcControlPoint(control);
this._startArrow.setControlPoint(control);
},
setDestControlPoint:function (control) {
2011-07-27 14:53:32 -03:00
this._line2d.setDestControlPoint(control);
if (this._showEndArrow)
this._endArrow.setControlPoint(control);
2011-07-27 14:53:32 -03:00
},
getControlPoints:function () {
2011-07-27 14:53:32 -03:00
return this._line2d.getControlPoints();
},
isSrcControlPointCustom:function () {
2011-07-27 14:53:32 -03:00
return this._line2d.isSrcControlPointCustom();
},
isDestControlPointCustom:function () {
2011-07-27 14:53:32 -03:00
return this._line2d.isDestControlPointCustom();
},
setIsSrcControlPointCustom:function (isCustom) {
2011-07-27 14:53:32 -03:00
this._line2d.setIsSrcControlPointCustom(isCustom);
},
setIsDestControlPointCustom:function (isCustom) {
2011-07-27 14:53:32 -03:00
this._line2d.setIsDestControlPointCustom(isCustom);
},
2012-07-07 11:52:23 -03:00
getId:function () {
return this._model.getId();
}
});